|
| 1 | +package parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// TestParseArchive runs the parser against all .txt files in the archive. |
| 11 | +// Only files from 2013-07-11 onwards are tested because older files use a |
| 12 | +// different format that the parser doesn't support. |
| 13 | +func TestParseArchive(t *testing.T) { |
| 14 | + archiveDir := filepath.Join("..", "archive", "txt") |
| 15 | + |
| 16 | + // These files are UTF-16 encoded which the parser doesn't support. |
| 17 | + skipFiles := map[string]bool{ |
| 18 | + "2016-08-26": true, |
| 19 | + "2016-09-30": true, |
| 20 | + "2022-11-18": true, |
| 21 | + } |
| 22 | + |
| 23 | + entries, err := os.ReadDir(archiveDir) |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("Failed to read archive directory: %v", err) |
| 26 | + } |
| 27 | + |
| 28 | + if len(entries) == 0 { |
| 29 | + t.Fatal("No files found in archive directory.") |
| 30 | + } |
| 31 | + |
| 32 | + for _, entry := range entries { |
| 33 | + if entry.IsDir() || filepath.Ext(entry.Name()) != ".txt" { |
| 34 | + continue |
| 35 | + } |
| 36 | + |
| 37 | + name := entry.Name() |
| 38 | + t.Run(name, func(t *testing.T) { |
| 39 | + date := strings.TrimSuffix(name, ".txt") |
| 40 | + if date < "2013-07-11" { |
| 41 | + t.Skip("File predates supported format (2013-07-11).") |
| 42 | + } |
| 43 | + if skipFiles[date] { |
| 44 | + t.Skip("File is UTF-16 encoded which the parser doesn't support.") |
| 45 | + } |
| 46 | + path := filepath.Join(archiveDir, name) |
| 47 | + |
| 48 | + f, err := os.Open(path) |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("Failed to open file: %v", err) |
| 51 | + } |
| 52 | + defer f.Close() |
| 53 | + |
| 54 | + rules, err := Parse(f) |
| 55 | + if err != nil { |
| 56 | + t.Errorf("Failed to parse: %v", err) |
| 57 | + } |
| 58 | + |
| 59 | + if rules.EffectiveDate.IsZero() { |
| 60 | + t.Error("EffectiveDate is zero.") |
| 61 | + } |
| 62 | + |
| 63 | + if len(rules.Rules) == 0 { |
| 64 | + t.Error("No rules parsed.") |
| 65 | + } |
| 66 | + |
| 67 | + if len(rules.Glossary) == 0 { |
| 68 | + t.Error("No glossary items parsed.") |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
0 commit comments