Expect misc.timezone, car and useragent as tables, and a territory's zone inside it

This commit is contained in:
2026-09-18 23:09:00 +02:00
parent 66d11ce1c7
commit 97c4e17671
3 changed files with 68 additions and 57 deletions
+23 -18
View File
@@ -95,24 +95,29 @@ func TestShippedMiscCategories(t *testing.T) {
func TestShippedMiscReferenceData(t *testing.T) { func TestShippedMiscReferenceData(t *testing.T) {
f := newGenerator(t, "data/misc", WithSeed(1)) f := newGenerator(t, "data/misc", WithSeed(1))
re := map[string]*regexp.Regexp{ re := map[string]*regexp.Regexp{
"currency": regexp.MustCompile(`^[A-Z]{3}$`), "currency": regexp.MustCompile(`^[A-Z]{3}$`),
"currency.name": regexp.MustCompile(`\p{L}`), "currency.name": regexp.MustCompile(`\p{L}`),
"currency.symbol": regexp.MustCompile(`^\S+$`), "currency.symbol": regexp.MustCompile(`^\S+$`),
"territory": regexp.MustCompile(`\p{L}`), "territory": regexp.MustCompile(`\p{L}`),
"territory.alpha2": regexp.MustCompile(`^[A-Z]{2}$`), "territory.alpha2": regexp.MustCompile(`^[A-Z]{2}$`),
"territory.alpha3": regexp.MustCompile(`^[A-Z]{3}$`), "territory.alpha3": regexp.MustCompile(`^[A-Z]{3}$`),
"territory.country": regexp.MustCompile(`^[A-Z]{2}$`), "territory.country": regexp.MustCompile(`^[A-Z]{2}$`),
"language": regexp.MustCompile(`\p{L}`), "language": regexp.MustCompile(`\p{L}`),
"language.code": regexp.MustCompile(`^[a-z]{2}$`), "language.code": regexp.MustCompile(`^[a-z]{2}$`),
"language.code3": regexp.MustCompile(`^[a-z]{3}$`), "language.code3": regexp.MustCompile(`^[a-z]{3}$`),
"timezone": regexp.MustCompile(`^([A-Za-z_]+(/[A-Za-z_]+)+|UTC)$`), "timezone": regexp.MustCompile(`^[A-Za-z]+(/[A-Za-z0-9_+-]+)+$`),
"mimetype": regexp.MustCompile(`^[a-z]+/[a-z0-9.+-]+$`), "timezone.offset": regexp.MustCompile(`^[+-](0\d|1[0-4]):[0-5]\d$`),
"mimetype.ext": regexp.MustCompile(`^\.[a-z0-9_-]+$`), "timezone.territory": regexp.MustCompile(`^[A-Z]{2}$`),
"httpstatus": regexp.MustCompile(`^[1-5]\d{2} \S.*$`), "mimetype": regexp.MustCompile(`^[a-z]+/[a-z0-9.+-]+$`),
"httpstatus.code": regexp.MustCompile(`^[1-5]\d{2}$`), "mimetype.ext": regexp.MustCompile(`^\.[a-z0-9_-]+$`),
"useragent": regexp.MustCompile(`^Mozilla/5\.0 .+`), "httpstatus": regexp.MustCompile(`^[1-5]\d{2} \S.*$`),
"car": regexp.MustCompile(`^\S.* \S`), "httpstatus.code": regexp.MustCompile(`^[1-5]\d{2}$`),
"car.maker": regexp.MustCompile(`^\S`), "useragent": regexp.MustCompile(`^Mozilla/5\.0 .+`),
"useragent.browser": regexp.MustCompile(`^(Chrome|Edge|Firefox|Opera|Safari|Samsung Internet)$`),
"useragent.device": regexp.MustCompile(`^(desktop|mobile)$`),
"useragent.os": regexp.MustCompile(`^(Android|ChromeOS|Linux|Windows|iOS|macOS)$`),
"car": regexp.MustCompile(`^\S.* \S`),
"car.make": regexp.MustCompile(`^\S`),
} }
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
for p, rx := range re { for p, rx := range re {
+1 -1
View File
@@ -154,7 +154,7 @@ func TestListedPathsAllRender(t *testing.T) {
} }
} }
} }
for _, p := range []string{"misc.car.maker", "misc.territory.alpha2", "misc.territory.numeric", "misc.currency.symbol", "misc.httpstatus.code", "misc.mimetype.ext"} { for _, p := range []string{"misc.car.make", "misc.territory.alpha2", "misc.territory.numeric", "misc.currency.symbol", "misc.httpstatus.code", "misc.mimetype.ext"} {
if !slices.Contains(paths, p) { if !slices.Contains(paths, p) {
t.Errorf("List() omits %q, which the README advertises and Fake renders", p) t.Errorf("List() omits %q, which the README advertises and Fake renders", p)
} }
+44 -38
View File
@@ -644,17 +644,17 @@ func TestTableRowsAreAlternatives(t *testing.T) {
func TestTableOverridesByLayering(t *testing.T) { func TestTableOverridesByLayering(t *testing.T) {
mine := writeFiles(t, map[string]string{ mine := writeFiles(t, map[string]string{
"misc/territory.json": `{"format":"{name}","rows":"territory.tsv","key":"alpha2"}`, "misc/language.json": `{"format":"{name}","rows":"language.tsv","key":"code"}`,
"misc/territory.tsv": "alpha2\tname\nXX\tNowhere\nYY\tElsewhere\n", "misc/language.tsv": "code\tname\nxx\tNowhere\nyy\tElsewhere\n",
}) })
f, err := New(WithDataPath(mine), WithSeed(1)) f, err := New(WithDataPath(mine), WithSeed(1))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if v := fake(t, f, "misc.territory[XX]"); v != "Nowhere" { if v := fake(t, f, "misc.language[xx]"); v != "Nowhere" {
t.Fatalf("misc.territory[XX] = %q, want the layered table", v) t.Fatalf("misc.language[xx] = %q, want the layered table", v)
} }
if paths := f.List(); slices.Contains(paths, "misc.territory.alpha3") { if paths := f.List(); slices.Contains(paths, "misc.language.code3") {
t.Fatal("List() still offers the shipped table's columns under an overridden category") t.Fatal("List() still offers the shipped table's columns under an overridden category")
} }
} }
@@ -662,39 +662,45 @@ func TestTableOverridesByLayering(t *testing.T) {
func TestShippedTables(t *testing.T) { func TestShippedTables(t *testing.T) {
f := newGenerator(t, "data/misc", WithSeed(1)) f := newGenerator(t, "data/misc", WithSeed(1))
for path, want := range map[string]string{ for path, want := range map[string]string{
"territory[SE]": "Sweden", "territory[SE]": "Sweden",
"territory[Sweden].alpha3": "SWE", "territory[Sweden].alpha3": "SWE",
"territory[SE].numeric": "752", "territory[SE].numeric": "752",
"territory[SE].tld": ".se", "territory[SE].tld": ".se",
"territory[SE].calling-code": "46", "territory[SE].calling-code": "46",
"territory[SE].capital": "Stockholm", "territory[SE].capital": "Stockholm",
"territory[SE].currency": "SEK", "territory[SE].currency": "SEK",
"territory[SE].flag": "🇸🇪", "territory[SE].flag": "🇸🇪",
"territory[SE].country": "SE", "territory[SE].country": "SE",
"territory[GL]": "Greenland", "territory[GL]": "Greenland",
"territory[GL].country": "DK", "territory[GL].country": "DK",
"territory[Åland Islands].country": "FI", "territory[Åland Islands].country": "FI",
"currency[SEK].name": "Swedish Krona", "currency[SEK].name": "Swedish Krona",
"currency[SEK].symbol": "kr", "currency[SEK].symbol": "kr",
"currency[SEK].numeric": "752", "currency[SEK].numeric": "752",
"currency[SEK].decimals": "2", "currency[SEK].decimals": "2",
"currency[Euro].code": "EUR", "currency[Euro].code": "EUR",
"language[sv]": "Swedish", "language[sv]": "Swedish",
"language[sv].code3": "swe", "language[sv].code3": "swe",
"language[Swedish].code": "sv", "language[Swedish].code": "sv",
"language[nl]": "Dutch", "language[nl]": "Dutch",
"httpstatus[200]": "200 OK", "httpstatus[200]": "200 OK",
"httpstatus[404]": "404 Not Found", "httpstatus[404]": "404 Not Found",
"httpstatus[404].reason": "Not Found", "httpstatus[404].reason": "Not Found",
"httpstatus[451].reason": "Unavailable For Legal Reasons", "httpstatus[451].reason": "Unavailable For Legal Reasons",
"httpstatus[500].reason": "Internal Server Error", "httpstatus[500].reason": "Internal Server Error",
"mimetype[application/json].ext": ".json", "mimetype[application/json].ext": ".json",
"mimetype[text/markdown].ext": ".md", "mimetype[text/markdown].ext": ".md",
"mimetype[.jpg]": "image/jpeg", "mimetype[.jpg]": "image/jpeg",
"mimetype[.mov]": "video/quicktime", "mimetype[.mov]": "video/quicktime",
"mimetype[.mp3]": "audio/mpeg", "mimetype[.mp3]": "audio/mpeg",
"mimetype[.mp4]": "video/mp4", "mimetype[.mp4]": "video/mp4",
"mimetype[.ogg]": "audio/ogg", "mimetype[.ogg]": "audio/ogg",
"timezone[Europe/Stockholm]": "Europe/Stockholm",
"timezone[Europe/Stockholm].offset": "+01:00",
"timezone[Asia/Kathmandu].offset": "+05:45",
"timezone[America/New_York].offset": "-05:00",
"territory[SE].timezone": "Europe/Stockholm",
"territory[NP].timezone.offset": "+05:45",
} { } {
if got := fake(t, f, path); got != want { if got := fake(t, f, path); got != want {
t.Errorf("Fake(%q) = %q, want %q", path, got, want) t.Errorf("Fake(%q) = %q, want %q", path, got, want)