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

secret-handshake: create test case generator #693

Merged
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
49 changes: 49 additions & 0 deletions exercises/secret-handshake/.meta/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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("secret-handshake", &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
Cases []struct {
Description string
Property string
Input uint
Expected []string
}
}
}

// template applied to above data structure generates the Go test cases
var tmpl = `package secret

{{.Header}}

type secretHandshakeTest struct {
code uint
h []string
}

var tests = []secretHandshakeTest {
{{range .J.Cases}} {{range .Cases}}{ {{ .Input }}, {{printf "%#v" .Expected }},
},
{{end}}{{end}}
}
`
24 changes: 24 additions & 0 deletions exercises/secret-handshake/cases_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package secret

// Source: exercism/x-common
// Commit: 69c8700 secret-handshake: remove tests for numbers > 31 (#794)
// x-common version: 1.1.0

type secretHandshakeTest struct {
code uint
h []string
}

var tests = []secretHandshakeTest{
{1, []string{"wink"}},
{2, []string{"double blink"}},
{4, []string{"close your eyes"}},
{8, []string{"jump"}},
{3, []string{"wink", "double blink"}},
{19, []string{"double blink", "wink"}},
{24, []string{"jump"}},
{16, []string{}},
{15, []string{"wink", "double blink", "close your eyes", "jump"}},
{31, []string{"jump", "close your eyes", "double blink", "wink"}},
{0, []string{}},
}
2 changes: 1 addition & 1 deletion exercises/secret-handshake/example.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package secret

const testVersion = 1
const testVersion = 2

var signals = []string{"wink", "double blink", "close your eyes", "jump"}

Expand Down
18 changes: 1 addition & 17 deletions exercises/secret-handshake/secret_handshake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,7 @@ import (
"testing"
)

const targetTestVersion = 1

var tests = []struct {
code uint
h []string
}{
{1, []string{"wink"}},
{2, []string{"double blink"}},
{4, []string{"close your eyes"}},
{8, []string{"jump"}},
{3, []string{"wink", "double blink"}},
{19, []string{"double blink", "wink"}},
{31, []string{"jump", "close your eyes", "double blink", "wink"}},
{0, nil},
{32, nil},
{33, []string{"wink"}},
}
const targetTestVersion = 2

func TestTestVersion(t *testing.T) {
if testVersion != targetTestVersion {
Expand Down