81 lines
2.3 KiB
Go
81 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
|
|
"countrygame/web"
|
|
)
|
|
|
|
// Markdown the in-game renderer (web/js/ui/markdown.js) does not support.
|
|
var unsupportedMarkdown = []*regexp.Regexp{
|
|
regexp.MustCompile("(?m)^```"), // code blocks
|
|
regexp.MustCompile(`!\[`), // images
|
|
regexp.MustCompile(`\]\(`), // links
|
|
regexp.MustCompile(`</?[a-zA-Z]+[^>]*>`), // HTML
|
|
regexp.MustCompile(`(?m)^[ \t]+[-*] `), // nested lists
|
|
}
|
|
|
|
// rulesShape counts the building blocks of a rules file so the two languages can be compared.
|
|
func rulesShape(src string) map[string]int {
|
|
counts := map[string]int{}
|
|
inTable := false
|
|
for _, line := range strings.Split(strings.ReplaceAll(src, "\r\n", "\n"), "\n") {
|
|
if strings.HasPrefix(line, "|") {
|
|
if !inTable {
|
|
counts["tables"]++
|
|
}
|
|
inTable = true
|
|
continue
|
|
}
|
|
inTable = false
|
|
switch {
|
|
case strings.HasPrefix(line, "### "):
|
|
counts["h3"]++
|
|
case strings.HasPrefix(line, "## "):
|
|
counts["h2"]++
|
|
case strings.HasPrefix(line, "# "):
|
|
counts["h1"]++
|
|
case strings.HasPrefix(line, "- "):
|
|
counts["bullets"]++
|
|
case strings.HasPrefix(line, ">"):
|
|
counts["quotes"]++
|
|
case strings.TrimSpace(line) == "---":
|
|
counts["rules"]++
|
|
}
|
|
}
|
|
return counts
|
|
}
|
|
|
|
// The rules are shown in the game by a tiny Markdown renderer, so the files may only use what it supports,
|
|
// and the English and French versions must have the same shape.
|
|
func TestRulesFiles(t *testing.T) {
|
|
files := map[string]string{}
|
|
for _, lang := range []string{"en", "fr"} {
|
|
raw, err := web.Files.ReadFile("data/rules/rules." + lang + ".md")
|
|
if err != nil {
|
|
t.Fatalf("rules.%s.md: %v", lang, err)
|
|
}
|
|
files[lang] = string(raw)
|
|
for _, bad := range unsupportedMarkdown {
|
|
if bad.MatchString(files[lang]) {
|
|
t.Errorf("rules.%s.md uses Markdown the in-game renderer does not support: %s", lang, bad)
|
|
}
|
|
}
|
|
}
|
|
|
|
en, fr := rulesShape(files["en"]), rulesShape(files["fr"])
|
|
if en["h1"] != 1 {
|
|
t.Errorf("the rules need exactly one # title, found %d", en["h1"])
|
|
}
|
|
for _, k := range []string{"h1", "h2", "h3", "tables", "bullets", "quotes", "rules"} {
|
|
if en[k] != fr[k] {
|
|
t.Errorf("rules structure differs between languages: %s en=%d fr=%d", k, en[k], fr[k])
|
|
}
|
|
}
|
|
if en["h2"] < 8 {
|
|
t.Errorf("the rules look too short: only %d sections", en["h2"])
|
|
}
|
|
}
|