From e5c8a3542d1f590859d631f8fc29119727ae2339 Mon Sep 17 00:00:00 2001 From: ferhat elmas Date: Tue, 31 Oct 2017 16:56:12 +0100 Subject: [PATCH] isogram: add test generator --- exercises/isogram/.meta/gen.go | 55 +++++++++++++++++++++++++++++ exercises/isogram/cases_test.go | 57 +++++++++++++++++++++++++++++++ exercises/isogram/isogram_test.go | 24 +++---------- 3 files changed, 116 insertions(+), 20 deletions(-) create mode 100644 exercises/isogram/.meta/gen.go create mode 100644 exercises/isogram/cases_test.go diff --git a/exercises/isogram/.meta/gen.go b/exercises/isogram/.meta/gen.go new file mode 100644 index 000000000..1a0cfc2e4 --- /dev/null +++ b/exercises/isogram/.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("isogram", &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 + Cases []struct { + Description string + Cases []oneCase + } +} + +// Test cases +type oneCase struct { + Description string + Property string + Input string + Expected bool +} + +// Template to generate test cases. +var tmpl = `package isogram + +{{.Header}} + +var testCases = []struct { + description string + input string + expected bool +}{ {{range .J.Cases}} {{range .Cases}} +{ + description: {{printf "%q" .Description}}, + input: {{printf "%q" .Input}}, + expected: {{printf "%t" .Expected}}, +},{{end}}{{end}} +} +` diff --git a/exercises/isogram/cases_test.go b/exercises/isogram/cases_test.go new file mode 100644 index 000000000..1cc1edbf5 --- /dev/null +++ b/exercises/isogram/cases_test.go @@ -0,0 +1,57 @@ +package isogram + +// Source: exercism/problem-specifications +// Commit: f9e0ebb isogram: Update canonical-data.json as in #919 (#920) +// Problem Specifications Version: 1.2.0 + +var testCases = []struct { + description string + input string + expected bool +}{ + { + description: "empty string", + input: "", + expected: true, + }, + { + description: "isogram with only lower case characters", + input: "isogram", + expected: true, + }, + { + description: "word with one duplicated character", + input: "eleven", + expected: false, + }, + { + description: "longest reported english isogram", + input: "subdermatoglyphic", + expected: true, + }, + { + description: "word with duplicated character in mixed case", + input: "Alphabet", + expected: false, + }, + { + description: "hypothetical isogrammic word with hyphen", + input: "thumbscrew-japingly", + expected: true, + }, + { + description: "isogram with duplicated hyphen", + input: "six-year-old", + expected: true, + }, + { + description: "made-up name that is an isogram", + input: "Emily Jung Schwartzkopf", + expected: true, + }, + { + description: "duplicated character in the middle", + input: "accentor", + expected: false, + }, +} diff --git a/exercises/isogram/isogram_test.go b/exercises/isogram/isogram_test.go index 095fb190b..1ff7c4c60 100644 --- a/exercises/isogram/isogram_test.go +++ b/exercises/isogram/isogram_test.go @@ -2,29 +2,13 @@ package isogram import "testing" -var testCases = []struct { - word string - expected bool -}{ - {"duplicates", true}, - {"eleven", false}, - {"subdermatoglyphic", true}, - {"Alphabet", false}, - {"thumbscrew-japingly", true}, - {"Hjelmqvist-Gryb-Zock-Pfund-Wax", true}, - {"Heizölrückstoßabdämpfung", true}, - {"the quick brown fox", false}, - {"Emily Jung Schwartzkopf", true}, - {"éléphant", false}, -} - func TestIsIsogram(t *testing.T) { for _, c := range testCases { - if IsIsogram(c.word) != c.expected { - t.Fatalf("FAIL: Word %q, expected %v, got %v", c.word, c.expected, !c.expected) + if IsIsogram(c.input) != c.expected { + t.Fatalf("FAIL: %s\nWord %q, expected %t, got %t", c.description, c.input, c.expected, !c.expected) } - t.Logf("PASS: Word %q", c.word) + t.Logf("PASS: Word %q", c.input) } } @@ -34,7 +18,7 @@ func BenchmarkIsIsogram(b *testing.B) { b.StartTimer() for i := 0; i < b.N; i++ { - IsIsogram(c.word) + IsIsogram(c.input) } b.StopTimer()