Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anagram: Add test generator #869

Merged
merged 2 commits into from
Sep 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}}
}
`
122 changes: 0 additions & 122 deletions exercises/anagram/anagram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,128 +6,6 @@ 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",
},
}

func equal(a []string, b []string) bool {
if len(b) != len(a) {
return false
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{},
},
}