File: test.ml

package info (click to toggle)
ocaml-metadata 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 592 kB
  • sloc: ml: 1,770; makefile: 31; sh: 5
file content (82 lines) | stat: -rw-r--r-- 2,306 bytes parent folder | download
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
let () =
  (* assert (Metadata.ID3v2.unterminate 2 "\000ab\000de\000\000" = "\000ab\000de"); *)
  (* Little endian. *)
  assert (
    Metadata.CharEncoding.Naive.convert ~source:`UTF_16LE "a\x00b\x00c\x00"
    = "abc");
  assert (
    Metadata.CharEncoding.Naive.convert ~source:`UTF_16
      "\xff\xfea\x00b\x00c\x00"
    = "abc");
  (* Big endian. *)
  assert (
    Metadata.CharEncoding.Naive.convert ~source:`UTF_16BE "\x00a\x00b\x00c"
    = "abc");
  assert (
    Metadata.CharEncoding.Naive.convert ~source:`UTF_16
      "\xfe\xff\x00a\x00b\x00c"
    = "abc")

let () =
  List.iter
    (fun version ->
      let tag =
        Metadata.ID3v2.make ~version
          Metadata.ID3v2.
            [
              {
                id = `TIT2;
                data = `Text (`UTF_8, "foobar😅");
                flags = default_flags `TIT2;
              };
              {
                id = `TALB;
                data = `Text (`UTF_8, "Let's go get them ⚡️");
                flags = [];
              };
            ]
      in
      ignore
        (Metadata.Reader.with_string
           (fun reader ->
             let tags = Metadata.ID3v2.parse reader in
             assert (List.assoc "title" tags = {|foobar😅|});
             assert (List.assoc "album" tags = {|Let's go get them ⚡️|});
             tags)
           tag))
    [3; 4]

let () =
  let tag =
    Metadata.ID3v2.make ~version:4
      Metadata.ID3v2.
        [
          {
            id = `TIT2;
            data = `Text (`UTF_8, "foobar😅");
            flags = default_flags `TIT2;
          };
          {
            id = `TALB;
            data = `Text (`UTF_8, "Let's go get them ⚡️");
            flags = [];
          };
        ]
  in
  let custom_parser_labels = ref [] in
  let custom_parser { Metadata.read; length; label; _ } =
    custom_parser_labels := label :: !custom_parser_labels;
    match label with
      | "TIT2" ->
          let s = read () in
          assert (length = String.length s)
      | _ -> ()
  in
  ignore
    (Metadata.Reader.with_string ~custom_parser
       (fun reader ->
         let tags = Metadata.ID3v2.parse reader in
         assert (tags = [("album", {|Let's go get them ⚡️|})]);
         tags)
       tag);
  assert (!custom_parser_labels = ["TALB"; "TIT2"])