Skip to content

Commit

Permalink
Added the test-cases from canonical-data.json to exercise diamond (#2255
Browse files Browse the repository at this point in the history
)
  • Loading branch information
eklatzer authored Jun 21, 2022
1 parent 2dfc431 commit 8d2361b
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 3 deletions.
51 changes: 51 additions & 0 deletions exercises/practice/diamond/.meta/gen.go
Original file line number Diff line number Diff line change
@@ -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}}
}`
13 changes: 10 additions & 3 deletions exercises/practice/diamond/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
114 changes: 114 additions & 0 deletions exercises/practice/diamond/cases_test.go
Original file line number Diff line number Diff line change
@@ -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 ",
},
},
}
17 changes: 17 additions & 0 deletions exercises/practice/diamond/diamond_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package diamond

import (
"fmt"
"math/rand"
"reflect"
"strings"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8d2361b

Please sign in to comment.