diff --git a/cmd/transpile/main_test.go b/cmd/transpile/main_test.go index 352029b..c37d305 100644 --- a/cmd/transpile/main_test.go +++ b/cmd/transpile/main_test.go @@ -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()) } diff --git a/cmd/transpile/puzzle.go b/cmd/transpile/puzzle.go index 41b1c60..44271bc 100644 --- a/cmd/transpile/puzzle.go +++ b/cmd/transpile/puzzle.go @@ -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() diff --git a/cmd/transpile/puzzle_test.go b/cmd/transpile/puzzle_test.go index 4ef8191..bbd5435 100644 --- a/cmd/transpile/puzzle_test.go +++ b/cmd/transpile/puzzle_test.go @@ -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 != "

YAML body

\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 != "

YAML body

\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 != "

RFC822 body

\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 != "

RFC822 body

\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) } } diff --git a/cmd/transpile/rfc822.md b/cmd/transpile/rfc822.md deleted file mode 100644 index e972932..0000000 --- a/cmd/transpile/rfc822.md +++ /dev/null @@ -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 - diff --git a/cmd/transpile/yaml.md b/cmd/transpile/testdata/1/puzzle.md similarity index 100% rename from cmd/transpile/yaml.md rename to cmd/transpile/testdata/1/puzzle.md diff --git a/cmd/transpile/testdata/2/moo.js b/cmd/transpile/testdata/2/moo.js new file mode 100644 index 0000000..09de201 --- /dev/null +++ b/cmd/transpile/testdata/2/moo.js @@ -0,0 +1 @@ +console.log("Moo.") diff --git a/cmd/transpile/testdata/2/moo.txt b/cmd/transpile/testdata/2/moo.txt new file mode 100644 index 0000000..e31bd82 --- /dev/null +++ b/cmd/transpile/testdata/2/moo.txt @@ -0,0 +1 @@ +Moo. diff --git a/cmd/transpile/testdata/2/puzzle.md b/cmd/transpile/testdata/2/puzzle.md new file mode 100644 index 0000000..540b816 --- /dev/null +++ b/cmd/transpile/testdata/2/puzzle.md @@ -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 + diff --git a/cmd/transpile/testdata/3/mkpuzzle b/cmd/transpile/testdata/3/mkpuzzle new file mode 100755 index 0000000..9b73f3c --- /dev/null +++ b/cmd/transpile/testdata/3/mkpuzzle @@ -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 + \ No newline at end of file