2020-09-08 17:49:02 -06:00
|
|
|
package transpile
|
2020-09-04 15:29:06 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2020-11-02 14:40:43 -07:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2020-09-04 15:29:06 -06:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFsCategory(t *testing.T) {
|
|
|
|
c := NewFsCategory(newTestFs(), "cat0")
|
|
|
|
|
|
|
|
if inv, err := c.Inventory(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if len(inv) != 9 {
|
|
|
|
t.Error("Inventory wrong length", inv)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p, err := c.Puzzle(1); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if len(p.Answers) != 1 {
|
|
|
|
t.Error("Wrong length for answers", p.Answers)
|
|
|
|
} else if p.Answers[0] != "YAML answer" {
|
|
|
|
t.Error("Wrong answer list", p.Answers)
|
|
|
|
} else if !c.Answer(1, p.Answers[0]) {
|
|
|
|
t.Error("Correct answer not accepted")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Answer(1, "incorrect answer") {
|
|
|
|
t.Error("Incorrect answer accepted as correct")
|
|
|
|
}
|
|
|
|
|
|
|
|
if r, err := c.Open(1, "moo.txt"); err != nil {
|
2020-09-04 18:28:23 -06:00
|
|
|
t.Log(c.Puzzle(1))
|
2020-09-04 15:29:06 -06:00
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
defer r.Close()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if _, err := io.Copy(buf, r); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if buf.String() != "Moo." {
|
|
|
|
t.Error("Opened file contents wrong")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r, err := c.Open(1, "error"); err == nil {
|
|
|
|
r.Close()
|
|
|
|
t.Error("File wasn't supposed to exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOsFsCategory(t *testing.T) {
|
|
|
|
fs := NewRecursiveBasePathFs(afero.NewOsFs(), "testdata")
|
|
|
|
static := NewFsCategory(fs, "static")
|
|
|
|
|
|
|
|
if p, err := static.Puzzle(1); err != nil {
|
|
|
|
t.Error(err)
|
2021-02-24 16:34:35 -07:00
|
|
|
} else if len(p.Authors) != 1 {
|
|
|
|
t.Error("Wrong authors list", p.Authors)
|
|
|
|
} else if p.Authors[0] != "neale" {
|
|
|
|
t.Error("Wrong authors", p.Authors)
|
2020-09-04 15:29:06 -06:00
|
|
|
}
|
|
|
|
|
2020-09-14 13:40:55 -06:00
|
|
|
if p, err := static.Puzzle(3); err != nil {
|
|
|
|
t.Error(err)
|
2021-02-24 16:34:35 -07:00
|
|
|
} else if len(p.Authors) != 1 {
|
|
|
|
t.Error("Wrong authors", p.Authors)
|
2020-09-14 13:40:55 -06:00
|
|
|
}
|
|
|
|
|
2020-09-04 15:29:06 -06:00
|
|
|
generated := NewFsCategory(fs, "generated")
|
|
|
|
|
|
|
|
if inv, err := generated.Inventory(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if len(inv) != 5 {
|
|
|
|
t.Error("Wrong inventory", inv)
|
|
|
|
}
|
|
|
|
|
|
|
|
if p, err := generated.Puzzle(1); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else if len(p.Answers) != 1 {
|
|
|
|
t.Error("Wrong answers", p.Answers)
|
|
|
|
} else if p.Answers[0] != "answer1.0" {
|
|
|
|
t.Error("Wrong answers:", p.Answers)
|
|
|
|
}
|
|
|
|
if _, err := generated.Puzzle(20); err == nil {
|
|
|
|
t.Error("Puzzle shouldn't exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
if r, err := generated.Open(1, "moo.txt"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
defer r.Close()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if _, err := io.Copy(buf, r); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if buf.String() != "Moo.\n" {
|
|
|
|
t.Errorf("Wrong body: %#v", buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r, err := generated.Open(1, "fail"); err == nil {
|
|
|
|
r.Close()
|
|
|
|
t.Error("File shouldn't exist")
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:40:43 -07:00
|
|
|
if r, err := generated.Open(1, "cow.txt"); err != nil {
|
|
|
|
if e, ok := err.(*exec.ExitError); ok {
|
|
|
|
t.Error(err, string(e.Stderr))
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
defer r.Close()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if _, err := io.Copy(buf, r); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(buf.String(), "moo.") {
|
|
|
|
t.Errorf("Wrong body: %#v", buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 15:29:06 -06:00
|
|
|
if !generated.Answer(1, "answer1.0") {
|
|
|
|
t.Error("Correct answer failed")
|
|
|
|
}
|
|
|
|
if generated.Answer(1, "wrong") {
|
|
|
|
t.Error("Incorrect answer didn't fail")
|
|
|
|
}
|
|
|
|
if generated.Answer(2, "error") {
|
|
|
|
t.Error("Error answer didn't fail")
|
|
|
|
}
|
|
|
|
}
|