textmachine/backend/internal/lang/structure_newlang_test.go

57 lines
2.2 KiB
Go

package lang
import (
"os"
"path/filepath"
"testing"
)
// TestALanguageAbsentFromTheRepositoryCutsFromDataAlone is the project's default review question, asked as a
// test: "will a pair that is NOT in this repository work without editing Go?"
//
// Korean is the witness because it answers BOTH halves. It is a CJK-script language, so with no data file it
// falls to the embedded default and gets that default's marker 第 — which never appears in a Korean book, so
// nothing is cut. Drop a ko/structure.txt naming 제 and 장, change no Go, and the same text cuts. The data
// file is the only difference between the two halves.
func TestALanguageAbsentFromTheRepositoryCutsFromDataAlone(t *testing.T) {
root := t.TempDir()
// Half one: no file. The language resolves to the embedded CJK default, whose marker is not Korean.
bare, err := LoadSourceStructure(root, "ko")
if err != nil {
t.Fatal(err)
}
if bare == nil {
t.Fatal("a CJK-script language must still resolve to the embedded default")
}
if bare.Units['장'] {
t.Fatal("the embedded default must not already know the Korean section unit — the test would prove nothing")
}
// Half two: the language ships its own grammar. No Go changes, no new package, no registry entry.
if err := os.MkdirAll(filepath.Join(root, "ko"), 0o755); err != nil {
t.Fatal(err)
}
data := "# Korean chapter grammar\nmarker\t제\nunits\t장\n"
if err := os.WriteFile(filepath.Join(root, "ko", structureFile), []byte(data), 0o644); err != nil {
t.Fatal(err)
}
st, err := LoadSourceStructure(root, "ko")
if err != nil {
t.Fatal(err)
}
if len(st.Markers) != 1 || st.Markers[0] != "제" {
t.Fatalf("markers = %q, want [제]", st.Markers)
}
if !st.Units['장'] {
t.Fatal("the declared section unit did not load")
}
if !st.HeaderNumeralRE().MatchString("제1장 시작") {
t.Fatal("the compiled header pattern does not match the language's own header")
}
// The fingerprint must come from the FILE, not from the default: it is what re-cuts the book when the
// grammar is edited.
if st.Fingerprint() == bare.Fingerprint() {
t.Fatal("a book cut by its own grammar must not share the default's cut-tag component")
}
}