| 1 | package fiesta |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io/fs" |
| 6 | "os" |
| 7 | "strconv" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // TestSourcesIsSourcesJSON: Sources() is hack/seed/sources.json, key |
| 12 | // for key and field for field. The file is read from disk here, not |
| 13 | // through the embed, so the test compares the API with the record |
| 14 | // rather than with itself; a field the record has and Source lacks |
| 15 | // fails by name, as does a key either side lacks. |
| 16 | func TestSourcesIsSourcesJSON(t *testing.T) { |
| 17 | b, err := os.ReadFile("hack/seed/sources.json") |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | var record map[string]map[string]json.RawMessage |
| 22 | if err := json.Unmarshal(b, &record); err != nil { |
| 23 | t.Fatalf("hack/seed/sources.json: %v", err) |
| 24 | } |
| 25 | got := Sources() |
| 26 | if len(record) < 160 { |
| 27 | t.Fatalf("hack/seed/sources.json holds %d example states, want the seeded 160 or more; the comparison would be over too little", len(record)) |
| 28 | } |
| 29 | for _, key := range sortedNames(record) { |
| 30 | source, ok := got[key] |
| 31 | if !ok { |
| 32 | t.Errorf("Sources() has no %s, which sources.json records", key) |
| 33 | continue |
| 34 | } |
| 35 | fields := map[string]string{ |
| 36 | "kind": strconv.Quote(source.Kind), |
| 37 | "source": strconv.Quote(source.Source), |
| 38 | "locale": strconv.Quote(source.Locale), |
| 39 | "state": strconv.Itoa(source.State), |
| 40 | } |
| 41 | if source.View != "" { |
| 42 | fields["view"] = strconv.Quote(source.View) |
| 43 | } |
| 44 | for _, field := range sortedNames(record[key]) { |
| 45 | if want := string(record[key][field]); fields[field] != want { |
| 46 | t.Errorf("Sources()[%q] has %s %s, and sources.json %s", key, field, fields[field], want) |
| 47 | } |
| 48 | } |
| 49 | for field := range fields { |
| 50 | if _, ok := record[key][field]; !ok { |
| 51 | t.Errorf("Sources()[%q] has a %s, and sources.json none", key, field) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | for _, key := range sortedNames(got) { |
| 56 | if _, ok := record[key]; !ok { |
| 57 | t.Errorf("Sources() has %s, which sources.json does not record", key) |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // TestEverySourceIsAnExampleState: each key Sources() returns is an |
| 63 | // example and one of its states, as examples/examples.json lists them, |
| 64 | // so a caller can open the example a source names. |
| 65 | func TestEverySourceIsAnExampleState(t *testing.T) { |
| 66 | var manifest struct { |
| 67 | Examples []struct { |
| 68 | Name string |
| 69 | States []struct{ ID string } |
| 70 | } |
| 71 | } |
| 72 | b, err := fs.ReadFile(FS(), "examples/examples.json") |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | if err := json.Unmarshal(b, &manifest); err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | states := map[string]bool{} |
| 80 | for _, example := range manifest.Examples { |
| 81 | for _, state := range example.States { |
| 82 | states[example.Name+"/"+state.ID] = true |
| 83 | } |
| 84 | } |
| 85 | for _, key := range sortedNames(Sources()) { |
| 86 | if !states[key] { |
| 87 | t.Errorf("Sources() has %s, which is no example state in examples/examples.json", key) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // TestSourcesIsACopy: a caller that edits the map it was given does not |
| 93 | // edit the next caller's. |
| 94 | func TestSourcesIsACopy(t *testing.T) { |
| 95 | first := Sources() |
| 96 | for key := range first { |
| 97 | delete(first, key) |
| 98 | } |
| 99 | if len(Sources()) == 0 { |
| 100 | t.Error("emptying the map Sources() returned emptied the next call's") |
| 101 | } |
| 102 | } |
| 103 | |