File: test.pp.ml

package info (click to toggle)
ocaml-csv 2.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 396 kB
  • sloc: ml: 2,117; makefile: 72
file content (111 lines) | stat: -rw-r--r-- 3,359 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
IF_LWT(open Lwt  open Lwt_io, open Printf)

module C = IF_LWT(Csv_lwt, Csv)

let do_testcsv ?separator ?strip ?backslash_escape filename expected =
  TRY_WITH(
    let%lwt csv = (C.load ?separator ?strip ?backslash_escape filename) in
    if csv <> expected then (
      printf "input file: %s\n" filename;%lwt
      printf "Csv library produced:\n";%lwt
      C.print csv;%lwt
      printf "Expected:\n";%lwt
      C.print expected;%lwt
      raise(Failure "failed")
    )
    else return()
  , Csv.Failure(nrow, nfield, err) ->
    (printf "The file %S line %i, field %i, does not conform to the CSV \
      specifications: %s\n" filename nrow nfield err;%lwt
    raise(Failure "failed")))


let main () =
  do_testcsv
    "testcsv1.csv"
    [ [ "This is a test\nwith commas,,,,,\n\nand carriage returns." ] ];%lwt
  do_testcsv
    "testcsv2.csv"
    [ [ "Normal field"; "Quoted field"; "Quoted field with \"\" quotes" ] ];%lwt
  do_testcsv
    "testcsv3.csv"
    [ [ "" ];
      [ ""; "" ];
      [ ""; ""; "" ];
      [ ""; ""; ""; "" ];
      [ ""; ""; ""; ""; "" ] ];%lwt
  do_testcsv
    "testcsv4.csv"
    [];%lwt
  do_testcsv
    "testcsv5.csv"
    [ [ "This is a test\nwith commas,,,,,\n\nand carriage returns.";
        "a second field"; "a third field" ];
      [ "a fourth field on a new line" ] ];%lwt
  do_testcsv
    "testcsv6.csv"
    [ [ "This is a test\nwith commas,,,,,\n\nand carriage returns\nand \000";
        "a second field"; "a third field" ];
      [ "a fourth field on a new line" ] ];%lwt

  do_testcsv
    "testcsv7.csv"
    [ [ "Initial"; "and"; "final"; ""; "spaces"; "do not matter" ];
      [ " Quoted spaces "; "are"; " important " ] ];%lwt

  do_testcsv
    "testcsv7.csv" ~strip:false
    [ [ " Initial "; " and "; " final"; " "; "\tspaces   "; " do not matter " ];
      [ " Quoted spaces "; " are"; " important " ] ];%lwt

  do_testcsv ~separator:'\t'
    "testcsv8.csv"
    [["Foo"; "Bar"]; ["Baz"; "Boof"]; ["a"; ""; "c"]];%lwt

  do_testcsv "testcsv9.csv"
    [["A1"; "A2"; "A3"]; ["B1"; "B2"]; ["C1"]];%lwt

  do_testcsv "testcsv10.csv" ~backslash_escape:true
    [["a"; "b\"c"; "d\\d\000"]];%lwt

  do_testcsv "testcsv12.csv" ~separator:';'
    [["Foo"; "Bar"]; ["Baz"; "Boof"]; ["a"; ""; "c"]]

let () =
  IF_LWT(Lwt_main.run,)(main())

let () =
  let csv1 = [ [ "a"; "b"; "c"; ""; "" ];
               [ "f"; "g"; "h"; "i"; "" ];
               [ "" ];
               [ ] ] in
  let csv2 = Csv.trim ~top:false ~left:false ~right:true ~bottom:true csv1 in
  assert(compare csv1 csv2 <> 0);
  assert(Csv.compare csv1 csv2 = 0)

let () =
  let csv1 = [ [ "a"; "b"; "c"; ""; "" ];
               [ "f"; "g"; "h"; "i"; "" ];
               [ "" ];
               [ ] ] in
  let csv2 = [ [ "a"; "b"; "c"; "d"; "" ];
               [ "f"; "g"; "h"; "i"; "" ];
               [ "" ];
               [ ] ] in
  assert (Csv.compare csv1 csv2 < 0)

let () =
  let csv1 = [ [ "a"; "b"; "c"; ""; "" ];
               [ "f"; "g"; "h"; "i"; "" ];
               [ "" ];
               [ ] ] in
  let csv2 = [ [ "A"; "B"; "C"; ""; "" ];
               [ "F"; "G"; "H"; "I"; "" ];
               [ "" ];
               [ ] ] in
  assert (Csv.map ~f:String.capitalize_ascii csv1 = csv2)


let () =
  print_endline IF_LWT("All conformity tests succeeded (Csv_lwt).",
                       "All conformity tests succeeded (Csv).")