From e1994c4e01788fe46f534dddb104a1c808f47f48 Mon Sep 17 00:00:00 2001 From: Ralph Amissah Date: Fri, 25 Sep 2026 21:18:06 -0400 Subject: ocda-lib in Gleam, and four agreements of 36 of 36 The model, both loaders, the dispatch and the canonical dump, in Gleam. A deliberate field-for-field mirror of the D library, including where that is not the most idiomatic Gleam: text is one string with the lines joined by newline rather than a list of lines, and the properties are typed fields rather than a list of pairs. A difference that existed in one language and not the other would show in every dump and mean nothing, so the two lose the same distinctions in the same places. The dump is what makes the agreement testable. Both languages write it byte for byte alike, so four questions are diff, and over the reference collection all four hold: D: .ssp == .ocda.db 36 of 36 Gleam: .ssp == .ocda.db 36 of 36 .ssp: D == Gleam 36 of 36 .ocda.db: D == Gleam 36 of 36 and a fifth, that the two report exactly the same ignored properties with the same counts. The Gleam .ssp loader agreed with the D one at the first attempt. The Gleam .ocda.db loader had one defect of its own, found by the sweep and worth recording because of its shape: a JSON element decoder handled strings and integers and fell through to a fallback that yielded "". table_widths is an integer array except where the columns divide evenly, when it is [33.3333,...], and one table in the collection does that. Its widths came back as three empty strings, so a value that could not be read turned into one that looked read. That is the failure the unknown counts exist to prevent, and a decoder fallback quietly reintroduces it. Shipped as an Erlang shipment rather than an escript, as the .ocda.db reader is and for the same reason: sqlite on the BEAM is a NIF and a NIF cannot load from inside an escript archive. gleam.toml and the model came in with the previous commit, which was about the dump; they belong here. (assisted by Claude-Code) --- gleam/test/ocda_lib_test.gleam | 104 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 gleam/test/ocda_lib_test.gleam (limited to 'gleam/test') diff --git a/gleam/test/ocda_lib_test.gleam b/gleam/test/ocda_lib_test.gleam new file mode 100644 index 0000000..6d4907c --- /dev/null +++ b/gleam/test/ocda_lib_test.gleam @@ -0,0 +1,104 @@ +import gleam/list +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 +} + -- cgit v1.2.3