moth/cmd/mothd/mothballs_test.go

108 lines
2.3 KiB
Go
Raw Permalink Normal View History

2020-08-17 17:43:57 -06:00
package main
import (
2020-08-17 20:33:23 -06:00
"archive/zip"
"fmt"
2020-08-17 17:43:57 -06:00
"testing"
2020-08-17 20:33:23 -06:00
"github.com/spf13/afero"
2020-08-17 17:43:57 -06:00
)
2020-08-17 20:33:23 -06:00
var testFiles = []struct {
Name, Body string
}{
2020-08-28 14:15:19 -06:00
{"puzzles.txt", "1\n3\n2\n"},
2020-08-21 17:02:38 -06:00
{"answers.txt", "1 answer123\n1 answer456\n2 wat\n"},
{"1/puzzle.json", `{"name": "moo"}`},
{"1/moo.txt", `moo`},
{"2/puzzle.json", `{}`},
{"2/moo.txt", `moo`},
{"3/puzzle.json", `{}`},
{"3/moo.txt", `moo`},
2020-08-17 20:33:23 -06:00
}
func (m *Mothballs) createMothball(cat string) {
f, _ := m.Create(fmt.Sprintf("%s.mb", cat))
defer f.Close()
w := zip.NewWriter(f)
defer w.Close()
for _, file := range testFiles {
of, _ := w.Create(file.Name)
of.Write([]byte(file.Body))
}
}
2020-08-19 18:01:21 -06:00
func NewTestMothballs() *Mothballs {
2020-08-17 20:33:23 -06:00
m := NewMothballs(new(afero.MemMapFs))
2020-08-19 18:01:21 -06:00
m.createMothball("pategory")
2020-08-18 17:04:23 -06:00
m.refresh()
2020-08-19 18:01:21 -06:00
return m
}
func TestMothballs(t *testing.T) {
m := NewTestMothballs()
if _, ok := m.categories["pategory"]; !ok {
2020-08-17 20:33:23 -06:00
t.Error("Didn't create a new category")
}
inv := m.Inventory()
if len(inv) != 1 {
t.Error("Wrong inventory size:", inv)
}
for _, cat := range inv {
2020-08-28 14:15:19 -06:00
switch cat.Name {
case "pategory":
if len(cat.Puzzles) != 3 {
t.Error("Puzzles list wrong length")
}
if cat.Puzzles[1] != 2 {
t.Error("Puzzles list not sorted")
}
}
2020-08-17 20:33:23 -06:00
for _, points := range cat.Puzzles {
f, _, err := m.Open(cat.Name, points, "puzzle.json")
if err != nil {
t.Error(cat.Name, err)
continue
}
f.Close()
}
}
2020-08-21 17:02:38 -06:00
if f, _, err := m.Open("nealegory", 1, "puzzle.json"); err == nil {
f.Close()
t.Error("You can't open a puzzle in a nealegory, that doesn't even rhyme!")
}
if f, _, err := m.Open("pategory", 1, "bozo"); err == nil {
f.Close()
t.Error("This file shouldn't exist")
}
2020-09-08 17:49:02 -06:00
if ok, _ := m.CheckAnswer("pategory", 1, "answer"); ok {
2020-08-19 18:01:21 -06:00
t.Error("Wrong answer marked right")
}
2020-09-08 17:49:02 -06:00
if _, err := m.CheckAnswer("pategory", 1, "answer123"); err != nil {
2020-08-19 18:01:21 -06:00
t.Error("Right answer marked wrong", err)
}
2020-09-08 17:49:02 -06:00
if _, err := m.CheckAnswer("pategory", 1, "answer456"); err != nil {
2020-08-19 18:01:21 -06:00
t.Error("Right answer marked wrong", err)
}
2020-09-08 17:49:02 -06:00
if ok, err := m.CheckAnswer("nealegory", 1, "moo"); ok {
2020-08-21 17:02:38 -06:00
t.Error("Checking answer in non-existent category should fail")
} else if err.Error() != "No such category: nealegory" {
t.Error("Wrong error message")
}
2020-08-19 18:01:21 -06:00
2020-08-17 20:33:23 -06:00
m.createMothball("test2")
2020-08-19 18:01:21 -06:00
m.Fs.Remove("pategory.mb")
2020-08-18 17:04:23 -06:00
m.refresh()
2020-08-17 20:33:23 -06:00
inv = m.Inventory()
if len(inv) != 1 {
t.Error("Deleted mothball is still around", inv)
}
2020-08-19 18:01:21 -06:00
2020-08-17 17:43:57 -06:00
}