(** Migration from OUnit to Alcotest *)
let ( >:: ) : string -> (unit -> unit) -> unit Alcotest.test_case =
 fun name test -> Alcotest.test_case name `Quick test

let ( >::: ) : string -> unit Alcotest.test_case list -> unit Alcotest.test =
 fun test_name tests -> (test_name, tests)

let make_test :
    (module Alcotest.TESTABLE with type t = 't) -> 't Alcotest.testable =
 fun (type t) (module T : Alcotest.TESTABLE with type t = t) ->
  Alcotest.testable T.pp T.equal

(** Create a testable for SQLite data type *)
let sql_testable =
  make_test
    (module struct
      type t = Sqlite3.Data.t =
        | NONE
        | NULL
        | INT of int64
        | FLOAT of float
        | TEXT of string
        | BLOB of string
      [@@deriving show, eq]
    end)

let csv_data_type_testable =
  make_test
    (module struct
      type t = ImportDataTypes.Value.t =
        | Null
        | Error of string
        | Content of string
        | Integer of int
        | Float of float
      [@@deriving show, eq]
    end)

let csv_result = Alcotest.(list @@ array @@ csv_data_type_testable)
let data_type_testable = make_test (module ImportDataTypes.Types)
let extern_testable = make_test (module ImporterSyntax.Extern)
let table_testable = make_test (module ImportDataTypes.Table)
let int_container_testable = make_test (module ImportContainers.IntSet)

let trimed_string =
  make_test
    (module struct
      type t = string

      let equal s1 s2 = String.equal (String.trim s1) (String.trim s2)
      let pp format t = Format.fprintf format "%s" (String.trim t)
    end)

let expression_testable =
  make_test
    (module struct
      type t = ImportDataTypes.Path.t ImportExpression.T.t [@@deriving show, eq]
    end)

let dep_key_testable =
  make_test
    (module struct
      type t = ImportAnalyser.Dependency.key [@@deriving show, eq]
    end)

let chunk =
  make_test
    (module struct
      type t = ImportAnalyser.Chunk.t

      let pp formater t =
        Format.fprintf formater "%s" (Buffer.contents t.ImportAnalyser.Chunk.b)

      let equal t1 t2 =
        let to_string t = Buffer.contents t.ImportAnalyser.Chunk.b in
        String.equal (to_string t1) (to_string t2)
    end)

let syntax =
  make_test
    (module struct
      type t = ImporterSyntax.t

      let pp format t =
        Format.fprintf format "%s"
          (Otoml.Printer.to_string (ImporterSyntax.repr t))

      let equal t1 t2 = t1 = t2
    end)