More transpiler testing

This commit is contained in:
Neale Pickett 2020-08-31 16:37:51 -06:00
parent 0418877db0
commit 7c22520b70
9 changed files with 125 additions and 49 deletions

View File

@ -29,13 +29,13 @@ RFC822 body
func newTestFs() afero.Fs {
fs := afero.NewMemMapFs()
afero.WriteFile(fs, "cat0/1/puzzle.moth", testMothYaml, 0644)
afero.WriteFile(fs, "cat0/1/puzzle.md", testMothYaml, 0644)
afero.WriteFile(fs, "cat0/1/moo.txt", []byte("Moo."), 0644)
afero.WriteFile(fs, "cat0/2/puzzle.moth", testMothRfc822, 0644)
afero.WriteFile(fs, "cat0/2/puzzle.md", testMothRfc822, 0644)
afero.WriteFile(fs, "cat0/3/puzzle.moth", testMothYaml, 0644)
afero.WriteFile(fs, "cat0/4/puzzle.moth", testMothYaml, 0644)
afero.WriteFile(fs, "cat0/5/puzzle.moth", testMothYaml, 0644)
afero.WriteFile(fs, "cat0/10/puzzle.moth", []byte(`---
afero.WriteFile(fs, "cat0/4/puzzle.md", testMothYaml, 0644)
afero.WriteFile(fs, "cat0/5/puzzle.md", testMothYaml, 0644)
afero.WriteFile(fs, "cat0/10/puzzle.md", []byte(`---
Answers:
- moo
Authors:
@ -43,8 +43,10 @@ Authors:
---
body
`), 0644)
afero.WriteFile(fs, "cat0/20/puzzle.moth", []byte("Answer: no\nBadField: yes\n\nbody\n"), 0644)
afero.WriteFile(fs, "cat1/93/puzzle.moth", []byte("Answer: no\n\nbody"), 0644)
afero.WriteFile(fs, "cat0/20/puzzle.md", []byte("Answer: no\nBadField: yes\n\nbody\n"), 0644)
afero.WriteFile(fs, "cat0/21/puzzle.md", []byte("Answer: broken\nSpooon\n"), 0644)
afero.WriteFile(fs, "cat0/22/puzzle.md", []byte("---\nanswers:\n - pencil\npre:\n body: Spooon\n---\nSpoon?\n"), 0644)
afero.WriteFile(fs, "cat1/93/puzzle.md", []byte("Answer: no\n\nbody"), 0644)
return fs
}
@ -58,7 +60,7 @@ func TestThings(t *testing.T) {
if err := tp.Handle("inventory"); err != nil {
t.Error(err)
}
if stdout.String() != "cat0 1 2 3 4 5 10 20\ncat1 93\n" {
if stdout.String() != "cat0 1 2 3 4 5 10 20 21 22\ncat1 93\n" {
t.Errorf("Bad inventory: %#v", stdout.String())
}

View File

@ -127,9 +127,12 @@ func (p *Puzzle) rfc822HeaderParser(r io.Reader) error {
}
func (p *Puzzle) parseMoth() error {
r, err := p.fs.Open("puzzle.moth")
r, err := p.fs.Open("puzzle.md")
if err != nil {
return err
var err2 error
if r, err2 = p.fs.Open("puzzle.moth"); err2 != nil {
return err
}
}
defer r.Close()

View File

@ -1,6 +1,7 @@
package main
import (
"strings"
"testing"
"github.com/spf13/afero"
@ -10,36 +11,71 @@ func TestPuzzle(t *testing.T) {
puzzleFs := newTestFs()
catFs := afero.NewBasePathFs(puzzleFs, "cat0")
p1, err := NewPuzzle(catFs, 1)
if err != nil {
t.Error(err)
}
t.Log(p1)
if (len(p1.Answers) == 0) || (p1.Answers[0] != "YAML answer") {
t.Error("Answers are wrong", p1.Answers)
}
if (len(p1.Pre.Authors) != 3) || (p1.Pre.Authors[1] != "Buster") {
t.Error("Authors are wrong", p1.Pre.Authors)
}
if p1.Pre.Body != "<p>YAML body</p>\n" {
t.Errorf("Body parsed wrong: %#v", p1.Pre.Body)
{
p, err := NewPuzzle(catFs, 1)
if err != nil {
t.Error(err)
}
t.Log(p)
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)
}
}
p2, err := NewPuzzle(catFs, 2)
if err != nil {
t.Error(err)
{
p, err := NewPuzzle(catFs, 2)
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)
}
}
if (len(p2.Answers) == 0) || (p2.Answers[0] != "RFC822 answer") {
t.Error("Answers are wrong", p2.Answers)
if _, err := NewPuzzle(catFs, 3); err != nil {
t.Error("Legacy `puzzle.moth` file:", err)
}
if (len(p2.Pre.Authors) != 3) || (p2.Pre.Authors[1] != "Arthur") {
t.Error("Authors are wrong", p2.Pre.Authors)
}
if p2.Pre.Body != "<p>RFC822 body</p>\n" {
t.Errorf("Body parsed wrong: %#v", p2.Pre.Body)
if _, err := NewPuzzle(catFs, 99); err == nil {
t.Error("Non-existent puzzle", err)
}
if _, err := NewPuzzle(catFs, 10); err == nil {
t.Error("Broken YAML didn't trigger an error")
t.Error("Broken YAML")
}
if _, err := NewPuzzle(catFs, 20); err == nil {
t.Error("Bad RFC822 header")
}
if _, err := NewPuzzle(catFs, 21); err == nil {
t.Error("Boken RFC822 header")
}
if _, err := NewPuzzle(catFs, 22); err == nil {
t.Error("Duplicate bodies")
} else if !strings.HasPrefix(err.Error(), "Puzzle body present") {
t.Error("Wrong error for duplicate body:", err)
}
}
func TestFsPuzzle(t *testing.T) {
catFs := afero.NewBasePathFs(afero.NewOsFs(), "testdata")
if _, err := NewPuzzle(catFs, 1); err != nil {
t.Error(err)
}
if _, err := NewPuzzle(catFs, 2); err != nil {
t.Error(err)
}
}

View File

@ -1,15 +0,0 @@
Author: neale
Answer: moo
A MOTH file
===========
This is a moth file, woo wo!
# A MOTH file
* moo
* moo
* moo
* squeak

1
cmd/transpile/testdata/2/moo.js vendored Normal file
View File

@ -0,0 +1 @@
console.log("Moo.")

1
cmd/transpile/testdata/2/moo.txt vendored Normal file
View File

@ -0,0 +1 @@
Moo.

25
cmd/transpile/testdata/2/puzzle.md vendored Normal file
View File

@ -0,0 +1,25 @@
Summary: A legacy RFC822-formatted puzzle
Author: neale
Answer: moo 0
Answer: moo 1
Answer: moo 2
Pattern: moo *
Hint: Use the source, Luke.
KSA: none
File: moo.txt
File: moo.txt moo-two.txt
File: moo.txt moo-too.txt hidden
Script: moo.js moo.js
A MOTH file
===========
This is a moth file, woo wo!
# A MOTH file
* moo
* moo
* moo
* squeak

23
cmd/transpile/testdata/3/mkpuzzle vendored Executable file
View File

@ -0,0 +1,23 @@
#! /bin/sh
case $1 in
"")
cat <<'EOT'
{
"Answers": ["answer"],
"Pre": {
"Authors": "neale",
"Body": "I am a generated puzzle."
}
}
EOT
;;
moo.txt)
echo "Moo."
;;
*)
echo "Error: no such file: $1" 1>&2
exit 1
;;
esac