summaryrefslogtreecommitdiffhomepage
path: root/gleam/test/ocda_lib_test.gleam
blob: c0e10e5dc16bef98bc7880e21b88c23e6f1b6bb8 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import gleam/dict
import gleam/list
import simplifile
import gleam/string
import gleeunit
import gleeunit/should
import ocda/dump
import ocda/load
import ocda/load_ssp
import ocda/model

pub fn main() {
  gleeunit.main()
}

const minimal = "% SiSU Document Abstraction v2.0
% Source: example.sst

@source {
  language: en
  languages: en
  digest: ABC
}

@meta {
  title.main: An Example
}

@body {
[1] heading :A the_title
.part: frontmatter
.sha256: DEAD
| An Example

[2] para
.part: body
.parent: 1
| First line
|
| Third line, after a blank one

}
"

pub fn version_test() {
  let doc = load_ssp.parse(minimal)
  doc.format_version |> should.equal("2.0")
  model.version_major(doc) |> should.equal("2")
  doc.source_filename |> should.equal("example.sst")
}

pub fn preamble_test() {
  let doc = load_ssp.parse(minimal)
  model.meta(doc, "title.main") |> should.equal("An Example")
}

pub fn objects_test() {
  let doc = load_ssp.parse(minimal)
  let body = model.section(doc, "body")
  body |> list.length |> should.equal(2)
}

/// The blank line in the middle of an object's text is kept, and so is the
/// count: three lines, not two.
pub fn blank_text_line_test() {
  let doc = load_ssp.parse(minimal)
  case model.section(doc, "body") {
    [_, second] ->
      second.text
      |> should.equal("First line\n\nThird line, after a blank one")
    _ -> should.fail()
  }
}

/// A heading's identifier comes from its declaration line; an object whose
/// identifier is simply its ocn has it derived, because the text form does
/// not write what it can work out.
pub fn derived_identifier_test() {
  let doc = load_ssp.parse(minimal)
  case model.section(doc, "body") {
    [first, second] -> {
      first.identifier |> should.equal("the_title")
      second.identifier |> should.equal("2")
    }
    _ -> should.fail()
  }
}

pub fn source_of_test() {
  load.source_of("a/b.ssp") |> should.equal(load.Ssp)
  load.source_of("a/b.ocda.db") |> should.equal(load.OcdaDb)
  load.source_of("a/b.sst") |> should.equal(load.Unknown)
}

/// The dump omits a field at its default, so a minimal document gives a
/// short dump and every line in it is a fact.
pub fn dump_test() {
  let text = dump.document(load_ssp.parse(minimal))
  text |> string.contains(contain: "version 2.0") |> should.be_true
  text |> string.contains(contain: "meta title.main An Example") |> should.be_true
  text |> string.contains(contain: "object body 0") |> should.be_true
  text |> string.contains(contain: "level A") |> should.be_true
  // a false boolean is not written at all
  text |> string.contains(contain: "bullet") |> should.be_false
}


/// A property is read, which the fixture above had to be corrected to show:
/// a property line is `.name: value` at the start of the line, and an
/// indented one is a line of no known shape. The fixture indented them, so
/// every property in it was being counted as malformed and the tests around
/// it passed for the wrong reason.
pub fn property_test() {
  let doc = load_ssp.parse(minimal)
  case model.section(doc, "body") {
    [first, second] -> {
      first.part |> should.equal("frontmatter")
      first.sha256 |> should.equal("DEAD")
      second.parent_ocn |> should.equal(1)
    }
    _ -> should.fail()
  }
  // and nothing in the fixture is malformed any more
  dict_size(doc.unknown.line) |> should.equal(0)
}

@external(erlang, "maps", "size")
fn dict_size(d: dict.Dict(a, b)) -> Int

/// What a path is, including the two archive forms.
pub fn pod_source_test() {
  load.source_of("a/b.sisupod") |> should.equal(load.Pod)
  load.source_of("a/b.zip") |> should.equal(load.Pod)
}

/// A zstd-wrapped archive is refused, and the refusal says why rather than
/// claiming the file is malformed. This is the one place the D and Gleam
/// libraries differ in what they accept, and it is a packaging limit: the
/// only zstd on the BEAM is a NIF that fetches zstd in its own build.
pub fn zstd_refusal_test() {
  let path = "test/fixture-zstd-frame.sisupod"
  let assert Ok(_) = simplifile.write_bits(path, <<0x28, 0xB5, 0x2F, 0xFD, 0>>)
  let assert Error(message) = load.load(path, "", False)
  message |> string.contains(contain: "zstd") |> should.be_true
  message |> string.contains(contain: "zstd -d") |> should.be_true
  let _ = simplifile.delete(path)
  Nil
}