-
-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perfect-numbers: create test case generator (#952)
Add .meta/gen.go to generate cases_test.go. Update test program to use generated test case array. Put FAIL and PASS in test result output. Keep TestGivesPositiveRequiredError redundant test on zero since it verifies ErrOnlyPositive. Update example solution to use int64 for input type since -1 test case in canonical-data.json. For #605.
- Loading branch information
Showing
4 changed files
with
194 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
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("perfect-numbers", &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 int64 | ||
Expected interface{} | ||
} | ||
|
||
func (c oneCase) Valid() bool { | ||
valid, _ := determineExpected(c.Expected) | ||
return valid | ||
} | ||
|
||
func (c oneCase) ExpectedClassification() string { | ||
_, e := determineExpected(c.Expected) | ||
switch e { | ||
case "perfect": | ||
return "ClassificationPerfect" | ||
case "abundant": | ||
return "ClassificationAbundant" | ||
case "deficient": | ||
return "ClassificationDeficient" | ||
} | ||
return e | ||
} | ||
|
||
// determineExpected examines an .Expected interface{} object and determines | ||
// whether a test case is valid(bool) and has a classification or expects an error, | ||
// returning valid and classification. | ||
func determineExpected(expected interface{}) (bool, string) { | ||
exp, ok := expected.(string) | ||
if ok { | ||
return ok, exp | ||
} | ||
return false, "" | ||
} | ||
|
||
// Template to generate test cases. | ||
var tmpl = `package perfect | ||
{{.Header}} | ||
var classificationTestCases = []struct { | ||
description string | ||
input int64 | ||
ok bool | ||
expected Classification | ||
}{ {{range .J.Cases}} {{range .Cases}} | ||
{ | ||
description: "{{.Description}}", | ||
input: {{.Input}}, | ||
{{if .Valid}} ok: true, | ||
expected: {{.ExpectedClassification}}, | ||
{{- else}} ok: false, | ||
{{- end}} | ||
},{{end}}{{end}} | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package perfect | ||
|
||
// Source: exercism/problem-specifications | ||
// Commit: 924bb7a perfect-numbers: fix misleading test description | ||
// Problem Specifications Version: 1.0.1 | ||
|
||
var classificationTestCases = []struct { | ||
description string | ||
input int64 | ||
ok bool | ||
expected Classification | ||
}{ | ||
{ | ||
description: "Smallest perfect number is classified correctly", | ||
input: 6, | ||
ok: true, | ||
expected: ClassificationPerfect, | ||
}, | ||
{ | ||
description: "Medium perfect number is classified correctly", | ||
input: 28, | ||
ok: true, | ||
expected: ClassificationPerfect, | ||
}, | ||
{ | ||
description: "Large perfect number is classified correctly", | ||
input: 33550336, | ||
ok: true, | ||
expected: ClassificationPerfect, | ||
}, | ||
{ | ||
description: "Smallest abundant number is classified correctly", | ||
input: 12, | ||
ok: true, | ||
expected: ClassificationAbundant, | ||
}, | ||
{ | ||
description: "Medium abundant number is classified correctly", | ||
input: 30, | ||
ok: true, | ||
expected: ClassificationAbundant, | ||
}, | ||
{ | ||
description: "Large abundant number is classified correctly", | ||
input: 33550335, | ||
ok: true, | ||
expected: ClassificationAbundant, | ||
}, | ||
{ | ||
description: "Smallest prime deficient number is classified correctly", | ||
input: 2, | ||
ok: true, | ||
expected: ClassificationDeficient, | ||
}, | ||
{ | ||
description: "Smallest non-prime deficient number is classified correctly", | ||
input: 4, | ||
ok: true, | ||
expected: ClassificationDeficient, | ||
}, | ||
{ | ||
description: "Medium deficient number is classified correctly", | ||
input: 32, | ||
ok: true, | ||
expected: ClassificationDeficient, | ||
}, | ||
{ | ||
description: "Large deficient number is classified correctly", | ||
input: 33550337, | ||
ok: true, | ||
expected: ClassificationDeficient, | ||
}, | ||
{ | ||
description: "Edge case (no factors other than itself) is classified correctly", | ||
input: 1, | ||
ok: true, | ||
expected: ClassificationDeficient, | ||
}, | ||
{ | ||
description: "Zero is rejected (not a natural number)", | ||
input: 0, | ||
ok: false, | ||
}, | ||
{ | ||
description: "Negative integer is rejected (not a natural number)", | ||
input: -1, | ||
ok: false, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters