moth/pkg/transpile/puzzle_test.go

196 lines
4.5 KiB
Go
Raw Permalink 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"
"strings"
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.Authors) != 3) || (p.Authors[1] != "Buster") {
t.Error("Authors are wrong", p.Authors)
2020-08-31 16:37:51 -06:00
}
if p.Objective != "moo like a cow" {
t.Error("Wrong objective")
}
if p.Success.Acceptable != "say moo" {
t.Error("Wrong Success.Acceptable")
}
if p.Success.Mastery != "say mrr" {
t.Error("Wrong Success.Mastery")
}
2021-04-13 17:28:22 -06:00
if len(p.KSAs) != 2 {
t.Error("KSAs are wrong")
}
if p.KSAs[0] != "a1" {
t.Error("KSAs[0] wrong")
}
if p.Body != "<p>YAML body</p>\n" {
t.Errorf("Body parsed wrong: %#v", p.Body)
2020-08-31 16:37:51 -06:00
}
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.Authors) != 3) || (p.Authors[1] != "Arthur") {
t.Error("Authors are wrong", p.Authors)
2020-08-31 16:37:51 -06:00
}
if p.Body != "<p>RFC822 body</p>\n" {
t.Errorf("Body parsed wrong: %#v", p.Body)
2020-08-31 16:37:51 -06:00
}
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
if puzzle, err := NewFsPuzzlePoints(catFs, 4).Puzzle(); err != nil {
t.Error("Markdown test file:", err)
} else if !strings.Contains(puzzle.Body, "<table>") {
t.Error("Markdown table extension isn't making tables")
} else if !strings.Contains(puzzle.Body, "<dl>") {
t.Error("Markdown dictionary extension isn't making tables")
}
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)
}
2021-02-18 17:54:26 -07:00
if puzzle, err := NewFsPuzzlePoints(catFs, 2).Puzzle(); err != nil {
2020-08-31 16:37:51 -06:00
t.Error(err)
} else if !strings.Contains(puzzle.Body, "class=\"moo\"") {
2021-02-18 17:54:26 -07:00
t.Error("Raw HTML didn't make it through")
2020-08-31 16:37:51 -06:00
}
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(`
attachments:
- simple
- filename: complex
filesystempath: backingfile
2020-09-15 15:58:21 -06:00
`)
p, err := yamlHeaderParser(buf)
if err != nil {
t.Error(err)
return
}
att := p.Attachments
2020-09-15 15:58:21 -06:00
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")
}
}