Skip to content

Commit

Permalink
forth: update generator
Browse files Browse the repository at this point in the history
For issue exercism#604.

- Update generator:
  Use .Header in template.
  Align template with latest canonical-data.json.

- Update test program:
  Reference testGroups containing the generated test cases.
  Annotate the FAIL output with test group name & test case description.
  Add PASS output for successful cases.
  • Loading branch information
leenipper committed Apr 8, 2017
1 parent d20228f commit 5f3dd6c
Show file tree
Hide file tree
Showing 3 changed files with 313 additions and 333 deletions.
98 changes: 35 additions & 63 deletions exercises/forth/.meta/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,78 +22,50 @@ func main() {

// The JSON structure we expect to be able to unmarshal into
type js struct {
ParsingAndNumbers section `json:"parsing and numbers"`
Addition section
Subtraction section
Multiplication section
Division section
CombinedArithmetic section `json:"combined arithmetic"`
Dup section
Drop section
Swap section
Over section
UserDefinedWords section `json:"user-defined words"`
Groups []Group `json:"cases"`
}

// A grouped section of test cases.
type section struct {
Cases []struct {
Description string
Input []string
Expected []int
}
// A group of test cases.
type Group struct {
Name string `json:"description"`
Cases []OneCase
}

// One test case.
type OneCase struct {
Description string
Input []string
Expected []int
}

// template applied to above data structure generates the Go test cases
var tmpl = `package forth
// Source: {{.Ori}}
{{if .Commit}}// Commit: {{.Commit}}
{{end}}
{{/* template for repeated caseSection */}}
{{define "caseSection"}} = []testCase{
{{range $y, $c := .}}{
{{printf "%q" $c.Description}},
{{printf "%#v" $c.Input}},
{{printf "%#v" $c.Expected}},
},
{{end}}}{{end}}
var parsingGroup{{template "caseSection" .J.ParsingAndNumbers.Cases}}
var additionGroup{{template "caseSection" .J.Addition.Cases}}
var subtractionGroup{{template "caseSection" .J.Subtraction.Cases}}
var multiplicationGroup{{template "caseSection" .J.Multiplication.Cases}}
var divisionGroup{{template "caseSection" .J.Division.Cases}}
{{.Header}}
var arithmeticGroup{{template "caseSection" .J.CombinedArithmetic.Cases}}
var dupGroup{{template "caseSection" .J.Dup.Cases}}
var dropGroup{{template "caseSection" .J.Drop.Cases}}
var swapGroup{{template "caseSection" .J.Swap.Cases}}
var overGroup{{template "caseSection" .J.Over.Cases}}
type testGroup struct {
group string
tests []testCase
}
var userdefinedGroup{{template "caseSection" .J.UserDefinedWords.Cases}}
type testCase struct {
description string
input []string
expected []int // nil slice indicates error expected.
}
var testSections = []testcaseSection{
{"parsing", parsingGroup},
{"addition(+)", additionGroup},
{"subtraction(-)", subtractionGroup},
{"multiplication(*)", multiplicationGroup},
{"division(/)", divisionGroup},
{"arithmetic", arithmeticGroup},
{"dup", dupGroup},
{"drop", dropGroup},
{"swap", swapGroup},
{"over", overGroup},
{"user-defined", userdefinedGroup},
var testGroups = []testGroup{
{{range .J.Groups}}{
group: {{printf "%q" .Name}},
tests: []testCase{
{{range .Cases}}{
{{printf "%q" .Description}},
{{printf "%#v" .Input}},
{{printf "%#v" .Expected}},
},
{{end}}
},
},
{{end}}
}
`
Loading

0 comments on commit 5f3dd6c

Please sign in to comment.