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

isogram: add test generator #935

Merged
merged 1 commit into from
Nov 6, 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
55 changes: 55 additions & 0 deletions exercises/isogram/.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("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}}
}
`
57 changes: 57 additions & 0 deletions exercises/isogram/cases_test.go
Original file line number Diff line number Diff line change
@@ -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,
},
}
24 changes: 4 additions & 20 deletions exercises/isogram/isogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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()
Expand Down