Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nth-prime: add generator
Browse files Browse the repository at this point in the history
Add .meta/gen.go to generate cases_test.go.

Update test program to use generated test case array.
Output FAIL and PASS in test output.

For exercism#605.
leenipper committed Nov 18, 2017
1 parent 69bf90e commit 0df6d5d
Showing 3 changed files with 123 additions and 18 deletions.
76 changes: 76 additions & 0 deletions exercises/nth-prime/.meta/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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("nth-prime", &j, t); err != nil {
log.Fatal(err)
}
}

type OneCase struct {
Description string
Input int
Expected interface{}
}

// The JSON structure we expect to be able to unmarshal into
type js struct {
Cases []OneCase
}

func (c OneCase) HasPrimeAnswer() bool {
hasPrimeAnswer, _ := determineExpected(c.Expected)
return hasPrimeAnswer
}

func (c OneCase) PrimeAnswer() int {
_, answer := determineExpected(c.Expected)
return answer
}

// determineExpected examines an .Expected interface{} object and determines
// whether a test case has a Prime answer or expects an error,
// returning true and the answer, or false and zero.
func determineExpected(expected interface{}) (bool, int) {
value, ok := expected.(float64)
if ok {
return true, int(value)
}
return false, 0
}

// template applied to above data structure generates the Go test cases
var tmpl = `package prime
{{.Header}}
var tests = []struct {
description string
n int
p int
ok bool
}{
{{range .J.Cases}}{
"{{.Description}}",
{{.Input}},
{{- if .HasPrimeAnswer}}
{{.PrimeAnswer}},
true,
{{- else}}
0,
false,
{{- end}}
},
{{end}}}
`
43 changes: 43 additions & 0 deletions exercises/nth-prime/cases_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package prime

// Source: exercism/problem-specifications
// Commit: e82cbcd nth-prime: Use object instead of bool for invalid value (#969)
// Problem Specifications Version: 2.0.0

var tests = []struct {
description string
n int
p int
ok bool
}{
{
"first prime",
1,
2,
true,
},
{
"second prime",
2,
3,
true,
},
{
"sixth prime",
6,
13,
true,
},
{
"big prime",
10001,
104743,
true,
},
{
"there is no zeroth prime",
0,
0,
false,
},
}
22 changes: 4 additions & 18 deletions exercises/nth-prime/nth_prime_test.go
Original file line number Diff line number Diff line change
@@ -2,33 +2,19 @@ package prime

import "testing"

var tests = []struct {
n int
p int
ok bool
}{
{1, 2, true},
{2, 3, true},
{3, 5, true},
{4, 7, true},
{5, 11, true},
{6, 13, true},
{10001, 104743, true},
{0, 0, false},
}

func TestNth(t *testing.T) {
for _, test := range tests {
switch p, ok := Nth(test.n); {
case !ok:
if test.ok {
t.Fatalf("Nth(%d) returned !ok. Expecting ok.", test.n)
t.Fatalf("FAIL %s\nNth(%d) returned !ok. Expecting ok.", test.description, test.n)
}
case !test.ok:
t.Fatalf("Nth(%d) = %d, ok = %t. Expecting !ok.", test.n, p, ok)
t.Fatalf("FAIL %s\nNth(%d) = %d, ok = %t. Expecting !ok.", test.description, test.n, p, ok)
case p != test.p:
t.Fatalf("Nth(%d) = %d, want %d.", test.n, p, test.p)
t.Fatalf("FAIL %s\nNth(%d) = %d, want %d.", test.description, test.n, p, test.p)
}
t.Logf("PASS %s", test.description)
}
}

0 comments on commit 0df6d5d

Please sign in to comment.