moth/pkg/transpile/puzzle_test.go

171 lines
3.8 KiB
Go
Raw Normal View History

2020-09-08 17:49:02 -06:00
package transpile
2020-08-28 17:41:17 -06:00
import (
2020-09-01 20:12:57 -06:00
"bytes"
"io"
2020-08-28 17:41:17 -06:00
"testing"
"github.com/spf13/afero"
)
func TestPuzzle(t *testing.T) {
puzzleFs := newTestFs()
2020-09-04 13:00:23 -06:00
catFs := NewRecursiveBasePathFs(puzzleFs, "cat0")
2020-08-28 17:41:17 -06:00
2020-08-31 16:37:51 -06:00
{
2020-09-11 13:03:19 -06:00
pd := NewFsPuzzlePoints(catFs, 1)
2020-09-03 20:04:43 -06:00
p, err := pd.Puzzle()
2020-08-31 16:37:51 -06:00
if err != nil {
t.Error(err)
}
2020-09-01 20:12:57 -06:00
2020-08-31 16:37:51 -06:00
if (len(p.Answers) == 0) || (p.Answers[0] != "YAML answer") {
t.Error("Answers are wrong", p.Answers)
}
if (len(p.Pre.Authors) != 3) || (p.Pre.Authors[1] != "Buster") {
t.Error("Authors are wrong", p.Pre.Authors)
}
if p.Pre.Body != "<p>YAML body</p>\n" {
t.Errorf("Body parsed wrong: %#v", p.Pre.Body)
}
2020-09-14 13:40:55 -06:00
f, err := pd.Open("moo.txt")
if err != nil {
t.Error(err)
}
defer f.Close()
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, f); err != nil {
t.Error(err)
}
if buf.String() != "Moo." {
t.Error("Attachment wrong: ", buf.String())
}
2020-08-28 17:41:17 -06:00
}
2020-08-31 16:37:51 -06:00
{
2020-09-11 13:03:19 -06:00
p, err := NewFsPuzzlePoints(catFs, 2).Puzzle()
2020-08-31 16:37:51 -06:00
if err != nil {
t.Error(err)
}
if (len(p.Answers) == 0) || (p.Answers[0] != "RFC822 answer") {
t.Error("Answers are wrong", p.Answers)
}
if (len(p.Pre.Authors) != 3) || (p.Pre.Authors[1] != "Arthur") {
t.Error("Authors are wrong", p.Pre.Authors)
}
if p.Pre.Body != "<p>RFC822 body</p>\n" {
t.Errorf("Body parsed wrong: %#v", p.Pre.Body)
}
2020-08-28 17:41:17 -06:00
}
2020-08-31 16:37:51 -06:00
2020-09-11 13:03:19 -06:00
if _, err := NewFsPuzzlePoints(catFs, 3).Puzzle(); err != nil {
2020-08-31 16:37:51 -06:00
t.Error("Legacy `puzzle.moth` file:", err)
2020-08-28 17:41:17 -06:00
}
2020-08-31 16:37:51 -06:00
2020-09-11 13:03:19 -06:00
if _, err := NewFsPuzzlePoints(catFs, 99).Puzzle(); err == nil {
2020-08-31 16:37:51 -06:00
t.Error("Non-existent puzzle", err)
2020-08-28 17:41:17 -06:00
}
2020-09-11 13:03:19 -06:00
if _, err := NewFsPuzzlePoints(catFs, 10).Puzzle(); err == nil {
2020-08-31 16:37:51 -06:00
t.Error("Broken YAML")
2020-08-28 17:41:17 -06:00
}
2020-09-11 13:03:19 -06:00
if _, err := NewFsPuzzlePoints(catFs, 20).Puzzle(); err == nil {
2020-08-31 16:37:51 -06:00
t.Error("Bad RFC822 header")
2020-08-28 17:41:17 -06:00
}
2020-09-11 13:03:19 -06:00
if _, err := NewFsPuzzlePoints(catFs, 21).Puzzle(); err == nil {
2020-08-31 16:37:51 -06:00
t.Error("Boken RFC822 header")
2020-08-28 17:41:17 -06:00
}
2020-09-04 13:00:23 -06:00
{
fs := afero.NewMemMapFs()
if err := afero.WriteFile(fs, "1/mkpuzzle", []byte("bleat"), 0755); err != nil {
t.Error(err)
}
2020-09-11 13:03:19 -06:00
p := NewFsPuzzlePoints(fs, 1)
2020-09-04 13:00:23 -06:00
if _, ok := p.(FsCommandPuzzle); !ok {
t.Error("We didn't get an FsCommandPuzzle")
}
if _, err := p.Puzzle(); err == nil {
t.Error("We didn't get an error trying to run a command from a MemMapFs")
}
}
2020-08-31 16:37:51 -06:00
}
2020-08-28 17:41:17 -06:00
2020-08-31 16:37:51 -06:00
func TestFsPuzzle(t *testing.T) {
2020-09-04 15:29:06 -06:00
catFs := NewRecursiveBasePathFs(NewRecursiveBasePathFs(afero.NewOsFs(), "testdata"), "static")
2020-08-31 16:37:51 -06:00
2020-09-11 13:03:19 -06:00
if _, err := NewFsPuzzlePoints(catFs, 1).Puzzle(); err != nil {
2020-09-01 20:12:57 -06:00
t.Error(err)
}
2020-09-11 13:03:19 -06:00
if _, err := NewFsPuzzlePoints(catFs, 2).Puzzle(); err != nil {
2020-08-31 16:37:51 -06:00
t.Error(err)
}
2020-09-11 13:03:19 -06:00
mkpuzzleDir := NewFsPuzzlePoints(catFs, 3)
2020-09-03 20:04:43 -06:00
if _, err := mkpuzzleDir.Puzzle(); err != nil {
2020-08-31 16:37:51 -06:00
t.Error(err)
2020-08-28 17:41:17 -06:00
}
2020-09-01 20:12:57 -06:00
2020-09-04 13:00:23 -06:00
if r, err := mkpuzzleDir.Open("moo.txt"); err != nil {
2020-09-01 20:12:57 -06:00
t.Error(err)
} else {
2020-09-04 13:00:23 -06:00
defer r.Close()
2020-09-01 20:12:57 -06:00
buf := new(bytes.Buffer)
2020-09-04 13:00:23 -06:00
if _, err := io.Copy(buf, r); err != nil {
2020-09-01 20:12:57 -06:00
t.Error(err)
}
if buf.String() != "Moo.\n" {
2020-09-04 13:00:23 -06:00
t.Errorf("Wrong body: %#v", buf.String())
2020-09-01 20:12:57 -06:00
}
}
2020-09-04 13:00:23 -06:00
if r, err := mkpuzzleDir.Open("error"); err == nil {
r.Close()
t.Error("Error open didn't return error")
}
if !mkpuzzleDir.Answer("moo") {
t.Error("Right answer marked wrong")
}
if mkpuzzleDir.Answer("wrong") {
t.Error("Wrong answer marked correct")
}
if mkpuzzleDir.Answer("error") {
t.Error("Error answer marked correct")
}
2020-08-28 17:41:17 -06:00
}
2020-09-15 15:58:21 -06:00
func TestAttachment(t *testing.T) {
buf := bytes.NewBufferString(`
pre:
attachments:
- simple
- filename: complex
filesystempath: backingfile
`)
p, err := yamlHeaderParser(buf)
if err != nil {
t.Error(err)
return
}
att := p.Pre.Attachments
if len(att) != 2 {
t.Error("Wrong number of attachments", att)
}
if att[0].Filename != "simple" {
t.Error("Attachment 0 wrong")
}
if att[0].Filename != att[0].FilesystemPath {
t.Error("Attachment 0 wrong")
}
if att[1].Filename != "complex" {
t.Error("Attachment 1 wrong")
}
if att[1].FilesystemPath != "backingfile" {
t.Error("Attachment 2 wrong")
}
}