diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-07-23 13:01:33 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-07-23 13:01:33 +0200 |
| commit | e240d4c03cc30b583a0811553580fe2a15cd1097 (patch) | |
| tree | 616b80f2aecc1ca99200c15f7336a07d430700dc /internal/liturgy/parse_test.go | |
| parent | 343a675a5a779cfce5a3dec9a3ad4a6ffb22de75 (diff) | |
| download | lectio-e240d4c03cc30b583a0811553580fe2a15cd1097.tar.gz lectio-e240d4c03cc30b583a0811553580fe2a15cd1097.zip | |
liturgy: Section type + HTML parse (fixtures)
Ports parse_sections, html_to_lines, extract_reference from ewangelia.py.
Parse(html) prefers the tabnowy0all lectionary tab (falls back to
tabstary0all) and errors loudly if the tab is present-but-empty or
absent. ExtractCitation(heading) pulls the parenthetical citation.
Section gains a PartID field (Addendum A): set from the heading prefix
(1. czytanie -> pierwsze_czytanie, a second same-day 1. czytanie ->
drugie_czytanie, Psalm -> psalm, Aklamacja -> aklamacja, Ewangelia ->
ewangelia; anything else -> "").
Adds testdata/2026-07-22.html (split-feast day, two 1. czytanie
sections) and testdata/2026-06-22.html (normal day), copied from
~/.cache/daily-reading/.
Diffstat (limited to 'internal/liturgy/parse_test.go')
| -rw-r--r-- | internal/liturgy/parse_test.go | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/internal/liturgy/parse_test.go b/internal/liturgy/parse_test.go new file mode 100644 index 0000000..13732f9 --- /dev/null +++ b/internal/liturgy/parse_test.go @@ -0,0 +1,123 @@ +package liturgy + +import ( + "os" + "strings" + "testing" +) + +func TestParse(t *testing.T) { + html, _ := os.ReadFile("testdata/2026-07-22.html") + secs, err := Parse(string(html)) + if err != nil { + t.Fatal(err) + } + var gospel *Section + var firstCzytanie *Section + var secondCzytanie *Section + for i := range secs { + if strings.HasPrefix(secs[i].Heading, "Ewangelia") { + gospel = &secs[i] + } + if strings.HasPrefix(secs[i].Heading, "1. czytanie") { + if firstCzytanie == nil { + firstCzytanie = &secs[i] + } else if secondCzytanie == nil { + secondCzytanie = &secs[i] + } + } + } + if gospel == nil { + t.Fatal("no gospel section") + } + if gospel.Citation != "J 20, 1. 11-18" { + t.Errorf("gospel citation = %q", gospel.Citation) + } + if len(gospel.Paragraphs) == 0 { + t.Error("gospel has no paragraphs") + } + if gospel.PartID != "ewangelia" { + t.Errorf("gospel PartID = %q, want %q", gospel.PartID, "ewangelia") + } + + if firstCzytanie == nil { + t.Fatal("no '1. czytanie' section") + } + if firstCzytanie.PartID != "pierwsze_czytanie" { + t.Errorf("first '1. czytanie' PartID = %q, want %q", firstCzytanie.PartID, "pierwsze_czytanie") + } + // 2026-07-22 is a split-feast day with two "1. czytanie" sections in the + // same tab; the second one must not collide with the first. + if secondCzytanie == nil { + t.Fatal("expected a second '1. czytanie' section (split feast fixture)") + } + if secondCzytanie.PartID != "drugie_czytanie" { + t.Errorf("second '1. czytanie' PartID = %q, want %q", secondCzytanie.PartID, "drugie_czytanie") + } +} + +func TestParseLayoutChange(t *testing.T) { + if _, err := Parse("<html><body>redesigned</body></html>"); err == nil { + t.Error("expected error on missing reading tab") + } +} + +func TestParseNormalDay(t *testing.T) { + html, err := os.ReadFile("testdata/2026-06-22.html") + if err != nil { + t.Fatal(err) + } + secs, err := Parse(string(html)) + if err != nil { + t.Fatal(err) + } + if len(secs) != 4 { + t.Fatalf("len(secs) = %d, want 4", len(secs)) + } + want := map[string]string{ + "1. czytanie": "pierwsze_czytanie", + "Psalm": "psalm", + "Aklamacja": "aklamacja", + "Ewangelia": "ewangelia", + } + for _, s := range secs { + for prefix, partID := range want { + if strings.HasPrefix(s.Heading, prefix) { + if s.PartID != partID { + t.Errorf("heading %q: PartID = %q, want %q", s.Heading, s.PartID, partID) + } + } + } + if s.Subtitle == "" { + t.Errorf("heading %q: empty subtitle", s.Heading) + } + if len(s.Paragraphs) == 0 { + t.Errorf("heading %q: no paragraphs", s.Heading) + } + } +} + +func TestExtractCitation(t *testing.T) { + cases := []struct { + heading string + want string + }{ + {"Ewangelia (Mt 7, 1-5)", "Mt 7, 1-5"}, + {"1. czytanie (Pnp 8, 6-7)", "Pnp 8, 6-7"}, + {"Psalm (Ps 63 (62), 2. 3-4. 5-6. 8-9 (R.: por. 2ab))", "Ps 63 (62), 2. 3-4. 5-6. 8-9 (R.: por. 2ab)"}, + } + for _, c := range cases { + got, err := ExtractCitation(c.heading) + if err != nil { + t.Errorf("ExtractCitation(%q) error: %v", c.heading, err) + continue + } + if got != c.want { + t.Errorf("ExtractCitation(%q) = %q, want %q", c.heading, got, c.want) + } + } + + if _, err := ExtractCitation("no parens here"); err == nil { + t.Error("expected error for heading with no parenthetical") + } +} |
