diff --git a/exercises/anagram/.gitignore b/exercises/anagram/.gitignore new file mode 100644 index 000000000..e1e55f0ad --- /dev/null +++ b/exercises/anagram/.gitignore @@ -0,0 +1 @@ +.meta/gen diff --git a/exercises/anagram/.meta/gen.go b/exercises/anagram/.meta/gen.go new file mode 100644 index 000000000..603eca671 --- /dev/null +++ b/exercises/anagram/.meta/gen.go @@ -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}} +} +` diff --git a/exercises/anagram/anagram_test.go b/exercises/anagram/anagram_test.go index d083051f5..025e616ec 100644 --- a/exercises/anagram/anagram_test.go +++ b/exercises/anagram/anagram_test.go @@ -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 diff --git a/exercises/anagram/cases_test.go b/exercises/anagram/cases_test.go new file mode 100644 index 000000000..ce6bcc5fa --- /dev/null +++ b/exercises/anagram/cases_test.go @@ -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{}, + }, +}