Skip to content

Commit

Permalink
anagram: Add test generator
Browse files Browse the repository at this point in the history
For exercism#605

I did not have a way to wake up exercism#695 so I started a new one.

This generator mimics the output of the connect generator so the output is
a little nicer IMHO. I bumped the `testVersion`.
  • Loading branch information
Sean 'Shaleh' Perry committed Sep 22, 2017
1 parent 905c142 commit 13f85ec
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 121 deletions.
1 change: 1 addition & 0 deletions exercises/anagram/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.meta/gen
55 changes: 55 additions & 0 deletions exercises/anagram/.meta/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"log"
"text/template"

"../../../gen"
)

func main() {
t, err := template.New("").Parse(tmpl)
if err != nil {
log.Fatal(err)
}
var j js
if err := gen.Gen("anagram", &j, t); err != nil {
log.Fatal(err)
}
}

// The JSON structure we expect to be able to unmarshal into
type js struct {
Exercise string
Version string
Comments []string
Cases []OneCase
}

// The JSON structure we expect to be able to unmarshal into
type OneCase struct {
Description string
Subject string
Candidates []string
Expected []string
}

// template applied to above data structure generates the Go test cases
var tmpl = `package anagram
{{.Header}}
var testCases = []struct {
description string
subject string
candidates []string
expected []string
}{ {{range .J.Cases}}
{
description: {{printf "%q" .Description}},
subject: {{printf "%q" .Subject}},
candidates: []string { {{range $line := .Candidates}}{{printf "\n%q," $line}}{{end}}},
expected: []string { {{range $line := .Expected}}{{printf "\n%q," $line}}{{end}}},
},{{end}}
}
`
128 changes: 7 additions & 121 deletions exercises/anagram/anagram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,127 +6,7 @@ import (
"testing"
)

var testCases = []struct {
subject string
candidates []string
expected []string
description string
}{
{
subject: "diaper",
candidates: []string{
"hello",
"world",
"zombies",
"pants",
},
expected: []string{},
description: "no matches",
},
{
subject: "ant",
candidates: []string{
"tan",
"stand",
"at",
},
expected: []string{"tan"},
description: "simple anagram",
},
{
subject: "listen",
candidates: []string{
"enlists",
"google",
"inlets",
"banana",
},
expected: []string{"inlets"},
description: "another simple anagram",
},
{
subject: "master",
candidates: []string{
"stream",
"pigeon",
"maters",
},
expected: []string{"maters", "stream"},
description: "multiple anagrams",
},
{
subject: "allergy",
candidates: []string{
"gallery",
"ballerina",
"regally",
"clergy",
"largely",
"leading",
},
expected: []string{"gallery", "largely", "regally"},
description: "multiple anagrams (again)",
},
{
subject: "galea",
candidates: []string{
"eagle",
},
expected: []string{},
description: "does not confuse different duplicates",
},
{
subject: "corn",
candidates: []string{
"corn",
"dark",
"Corn",
"rank",
"CORN",
"cron",
"park",
},
expected: []string{"cron"},
description: "identical word is not anagram",
},
{
subject: "mass",
candidates: []string{
"last",
},
expected: []string{},
description: "eliminate anagrams with same checksum",
},
{
subject: "good",
candidates: []string{
"dog",
"goody",
},
expected: []string{},
description: "eliminate anagram subsets",
},
{
subject: "Orchestra",
candidates: []string{
"cashregiser",
"carthorse",
"radishes",
},
expected: []string{"carthorse"},
description: "subjects are case insensitive",
},
{
subject: "orchestra",
candidates: []string{
"cashregiser",
"Carthorse",
"radishes",
},
expected: []string{"Carthorse"},
description: "candidates are case insensitive",
},
}
const targetTestVersion = 3

func equal(a []string, b []string) bool {
if len(b) != len(a) {
Expand All @@ -138,6 +18,12 @@ func equal(a []string, b []string) bool {
return fmt.Sprintf("%v", a) == fmt.Sprintf("%v", b)
}

func TestTestVersion(t *testing.T) {
if testVersion != targetTestVersion {
t.Fatalf("Found testVersion = %v, want %v", testVersion, targetTestVersion)
}
}

func TestDetectAnagrams(t *testing.T) {
for _, tt := range testCases {
actual := Detect(tt.subject, tt.candidates)
Expand Down
164 changes: 164 additions & 0 deletions exercises/anagram/cases_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package anagram

// Source: exercism/problem-specifications
// Commit: 196fc1a anagram: Rename duplicated test case description (#671)
// Problem Specifications Version: 1.0.1

var testCases = []struct {
description string
subject string
candidates []string
expected []string
}{
{
description: "no matches",
subject: "diaper",
candidates: []string{
"hello",
"world",
"zombies",
"pants"},
expected: []string{},
},
{
description: "detects simple anagram",
subject: "ant",
candidates: []string{
"tan",
"stand",
"at"},
expected: []string{
"tan"},
},
{
description: "does not detect false positives",
subject: "galea",
candidates: []string{
"eagle"},
expected: []string{},
},
{
description: "detects two anagrams",
subject: "master",
candidates: []string{
"stream",
"pigeon",
"maters"},
expected: []string{
"stream",
"maters"},
},
{
description: "does not detect anagram subsets",
subject: "good",
candidates: []string{
"dog",
"goody"},
expected: []string{},
},
{
description: "detects anagram",
subject: "listen",
candidates: []string{
"enlists",
"google",
"inlets",
"banana"},
expected: []string{
"inlets"},
},
{
description: "detects three anagrams",
subject: "allergy",
candidates: []string{
"gallery",
"ballerina",
"regally",
"clergy",
"largely",
"leading"},
expected: []string{
"gallery",
"regally",
"largely"},
},
{
description: "does not detect identical words",
subject: "corn",
candidates: []string{
"corn",
"dark",
"Corn",
"rank",
"CORN",
"cron",
"park"},
expected: []string{
"cron"},
},
{
description: "does not detect non-anagrams with identical checksum",
subject: "mass",
candidates: []string{
"last"},
expected: []string{},
},
{
description: "detects anagrams case-insensitively",
subject: "Orchestra",
candidates: []string{
"cashregister",
"Carthorse",
"radishes"},
expected: []string{
"Carthorse"},
},
{
description: "detects anagrams using case-insensitive subject",
subject: "Orchestra",
candidates: []string{
"cashregister",
"carthorse",
"radishes"},
expected: []string{
"carthorse"},
},
{
description: "detects anagrams using case-insensitive possible matches",
subject: "orchestra",
candidates: []string{
"cashregister",
"Carthorse",
"radishes"},
expected: []string{
"Carthorse"},
},
{
description: "does not detect a word as its own anagram",
subject: "banana",
candidates: []string{
"Banana"},
expected: []string{},
},
{
description: "does not detect a anagram if the original word is repeated",
subject: "go",
candidates: []string{
"go Go GO"},
expected: []string{},
},
{
description: "anagrams must use all letters exactly once",
subject: "tapper",
candidates: []string{
"patter"},
expected: []string{},
},
{
description: "capital word is not own anagram",
subject: "BANANA",
candidates: []string{
"Banana"},
expected: []string{},
},
}

0 comments on commit 13f85ec

Please sign in to comment.