Neale Pickett
·
2021-10-14
mothballs_test.go
1package main
2
3import (
4 "archive/zip"
5 "fmt"
6 "io/ioutil"
7 "testing"
8
9 "github.com/spf13/afero"
10)
11
12type testFileContents struct {
13 Name, Body string
14}
15
16var testFiles = []testFileContents{
17 {"puzzles.txt", "1\n3\n2\n"},
18 {"answers.txt", "1 answer123\n1 answer456\n2 wat\n"},
19 {"1/puzzle.json", `{"name": "moo"}`},
20 {"2/puzzle.json", `{}`},
21 {"2/moo.txt", `moo`},
22 {"3/puzzle.json", `{}`},
23 {"3/moo.txt", `moo`},
24}
25
26func (m *Mothballs) createMothballWithFiles(cat string, contents []testFileContents) {
27 f, _ := m.Create(fmt.Sprintf("%s.mb", cat))
28 defer f.Close()
29
30 w := zip.NewWriter(f)
31 defer w.Close()
32
33 for _, file := range testFiles {
34 of, _ := w.Create(file.Name)
35 of.Write([]byte(file.Body))
36 }
37 for _, file := range contents {
38 of, _ := w.Create(file.Name)
39 of.Write([]byte(file.Body))
40 }
41}
42
43func (m *Mothballs) createMothball(cat string) {
44 m.createMothballWithFiles(
45 cat,
46 []testFileContents{
47 {"1/moo.txt", "moo"},
48 },
49 )
50}
51
52func NewTestMothballs() *Mothballs {
53 m := NewMothballs(new(afero.MemMapFs))
54 m.createMothball("pategory")
55 m.refresh()
56 return m
57}
58
59func TestMothballs(t *testing.T) {
60 m := NewTestMothballs()
61 if _, ok := m.categories["pategory"]; !ok {
62 t.Error("Didn't create a new category")
63 }
64
65 inv := m.Inventory()
66 if len(inv) != 1 {
67 t.Error("Wrong inventory size:", inv)
68 }
69 for _, cat := range inv {
70 switch cat.Name {
71 case "pategory":
72 if len(cat.Puzzles) != 3 {
73 t.Error("Puzzles list wrong length")
74 }
75 if cat.Puzzles[1] != 2 {
76 t.Error("Puzzles list not sorted")
77 }
78 }
79 for _, points := range cat.Puzzles {
80 f, _, err := m.Open(cat.Name, points, "puzzle.json")
81 if err != nil {
82 t.Error(cat.Name, err)
83 continue
84 }
85 f.Close()
86 }
87 }
88
89 if f, _, err := m.Open("nealegory", 1, "puzzle.json"); err == nil {
90 f.Close()
91 t.Error("You can't open a puzzle in a nealegory, that doesn't even rhyme!")
92 }
93
94 if f, _, err := m.Open("pategory", 1, "bozo"); err == nil {
95 f.Close()
96 t.Error("This file shouldn't exist")
97 }
98
99 if ok, _ := m.CheckAnswer("pategory", 1, "answer"); ok {
100 t.Error("Wrong answer marked right")
101 }
102 if _, err := m.CheckAnswer("pategory", 1, "answer123"); err != nil {
103 t.Error("Right answer marked wrong", err)
104 }
105 if _, err := m.CheckAnswer("pategory", 1, "answer456"); err != nil {
106 t.Error("Right answer marked wrong", err)
107 }
108 if ok, err := m.CheckAnswer("nealegory", 1, "moo"); ok {
109 t.Error("Checking answer in non-existent category should fail")
110 } else if err.Error() != "no such category: nealegory" {
111 t.Error("Wrong error message")
112 }
113
114 goofyText := "bozonics"
115 //time.Sleep(1 * time.Second) // I don't love this, but we need the mtime to increase, and it's only accurate to 1s
116 m.createMothballWithFiles(
117 "pategory",
118 []testFileContents{
119 {"1/moo.txt", goofyText},
120 },
121 )
122 m.refresh()
123 if f, _, err := m.Open("pategory", 1, "moo.txt"); err != nil {
124 t.Error("pategory/1/moo.txt", err)
125 } else if contents, err := ioutil.ReadAll(f); err != nil {
126 t.Error("read all pategory/1/moo.txt", err)
127 } else if string(contents) != goofyText {
128 t.Error("read all replacement pategory/1/moo.txt contents wrong, got", string(contents))
129 }
130
131 m.createMothball("test2")
132 m.Fs.Remove("pategory.mb")
133 m.refresh()
134 inv = m.Inventory()
135 if len(inv) != 1 {
136 t.Error("Deleted mothball is still around", inv)
137 }
138
139}