diff --git a/exercises/practice/diamond/.meta/gen.go b/exercises/practice/diamond/.meta/gen.go new file mode 100644 index 000000000..8ca9e0eaf --- /dev/null +++ b/exercises/practice/diamond/.meta/gen.go @@ -0,0 +1,51 @@ +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("diamond", &j, t); err != nil { + log.Fatal(err) + } +} + +// The JSON structure we expect to be able to unmarshal into +type js struct { + Cases []struct { + Description string `json:"description"` + Input struct { + Letter string `json:"letter"` + } `json:"input"` + Expected []string `json:"expected"` + } `json:"cases"` +} + +// template applied to above data structure generates the Go test cases +var tmpl = `package diamond + +{{.Header}} + +var testCases = []struct { + description string + input string + expected []string + expectedError error +}{ +{{range .J.Cases}}{ + description: {{printf "%q" .Description}}, + input: {{printf "%q" .Input.Letter}}, + expectedError: nil, + expected: []string { {{range $line := .Expected}}{{printf "\n%q," $line}}{{end}} +}, +}, +{{end}} +}` diff --git a/exercises/practice/diamond/.meta/tests.toml b/exercises/practice/diamond/.meta/tests.toml index 799c63d03..4e7802ec8 100644 --- a/exercises/practice/diamond/.meta/tests.toml +++ b/exercises/practice/diamond/.meta/tests.toml @@ -1,6 +1,13 @@ -# This is an auto-generated file. Regular comments will be removed when this -# file is regenerated. Regenerating will not touch any manually added keys, -# so comments can be added in a "comment" key. +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. [202fb4cc-6a38-4883-9193-a29d5cb92076] description = "Degenerate case with a single 'A' row" diff --git a/exercises/practice/diamond/cases_test.go b/exercises/practice/diamond/cases_test.go new file mode 100644 index 000000000..cb46281be --- /dev/null +++ b/exercises/practice/diamond/cases_test.go @@ -0,0 +1,114 @@ +package diamond + +// Source: exercism/problem-specifications +// Commit: d137db1 Format using prettier (#1917) + +var testCases = []struct { + description string + input string + expected []string + expectedError error +}{ + { + description: "Degenerate case with a single 'A' row", + input: "A", + expectedError: nil, + expected: []string{ + "A", + }, + }, + { + description: "Degenerate case with no row containing 3 distinct groups of spaces", + input: "B", + expectedError: nil, + expected: []string{ + " A ", + "B B", + " A ", + }, + }, + { + description: "Smallest non-degenerate case with odd diamond side length", + input: "C", + expectedError: nil, + expected: []string{ + " A ", + " B B ", + "C C", + " B B ", + " A ", + }, + }, + { + description: "Smallest non-degenerate case with even diamond side length", + input: "D", + expectedError: nil, + expected: []string{ + " A ", + " B B ", + " C C ", + "D D", + " C C ", + " B B ", + " A ", + }, + }, + { + description: "Largest possible diamond", + input: "Z", + expectedError: nil, + expected: []string{ + " A ", + " B B ", + " C C ", + " D D ", + " E E ", + " F F ", + " G G ", + " H H ", + " I I ", + " J J ", + " K K ", + " L L ", + " M M ", + " N N ", + " O O ", + " P P ", + " Q Q ", + " R R ", + " S S ", + " T T ", + " U U ", + " V V ", + " W W ", + " X X ", + " Y Y ", + "Z Z", + " Y Y ", + " X X ", + " W W ", + " V V ", + " U U ", + " T T ", + " S S ", + " R R ", + " Q Q ", + " P P ", + " O O ", + " N N ", + " M M ", + " L L ", + " K K ", + " J J ", + " I I ", + " H H ", + " G G ", + " F F ", + " E E ", + " D D ", + " C C ", + " B B ", + " A ", + }, + }, +} diff --git a/exercises/practice/diamond/diamond_test.go b/exercises/practice/diamond/diamond_test.go index 692cb0522..7e47414c4 100644 --- a/exercises/practice/diamond/diamond_test.go +++ b/exercises/practice/diamond/diamond_test.go @@ -1,6 +1,7 @@ package diamond import ( + "fmt" "math/rand" "reflect" "strings" @@ -38,6 +39,22 @@ func checkCorrect(requirement func(byte, []string) bool, keepSeparator bool, t * } } +func TestDiamond(t *testing.T) { + for _, testCase := range testCases { + t.Run(testCase.description, func(t *testing.T) { + expected := fmt.Sprintf("%s\n", strings.Join(testCase.expected, "\n")) + got, err := Gen(testCase.input[0]) + + if err != testCase.expectedError { + t.Fatalf("Gen(%q)\nExpected:%v\nGot:%v", testCase.input, testCase.expectedError, err) + } + if got != expected { + t.Fatalf("Gen(%q)\nExpected:\n%s\n(len=%d)\nGot:\n%s\n(len=%d)", testCase.input, expected, len(expected), got, len(got)) + } + }) + } +} + func TestFirstRowContainsOneA(t *testing.T) { requirement := func(char byte, rows []string) bool { return len(rows) > 0 && strings.Count(rows[0], "A") == 1