Sha256: 27b78afef0ce3f019e650f93ca15fbaafdaa8347e280aa33d15d08171fde915d

Contents?: true

Size: 1.88 KB

Versions: 130

Compression:

Stored size: 1.88 KB

Contents

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("change", &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
	Comments []string
	Cases    []OneCase
}

// template applied to above data structure generates the Go test cases

type OneCase struct {
	Description string
	Property    string
	Coins       []int
	Target      int
	Expected    interface{}
}

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

func (c OneCase) IntSlice() []int {
	_, list := determineExpected(c.Expected)
	return list
}

// determineExpected examines an .Expected interface{} object and determines
// whether an error is indicated by an int value (actually -1) in the JSON,
// or whether .Expected is a slice of integer coins.
func determineExpected(expected interface{}) (valid bool, list []int) {
	_, ok := expected.(int)
	if ok {
		return false, nil
	}
	ilist, ok := expected.([]interface{})
	if !ok {
		return false, nil
	}
	list = make([]int, 0)
	for _, iv := range ilist {
		// The literals from the JSON are unmarshalled to float64 values,
		// which are converted to int for the template output.
		v, isFloat64 := iv.(float64)
		if isFloat64 {
			list = append(list, int(v))
		}
	}
	return true, list
}

var tmpl = `package change

{{.Header}}

var testCases = []struct {
	description    string
	coins          []int
	target         int
	valid          bool     // true => no error, false => error expected
	expectedChange []int    // when .valid == true, the expected change coins
}{
{{range .J.Cases}}{
	{{printf "%q"  .Description}},
	{{printf "%#v" .Coins}},
	{{printf "%d"  .Target}},
	{{printf "%v"  .Valid}},
	{{printf "%#v" .IntSlice}},
},
{{end}}}
`

Version data entries

130 entries across 130 versions & 1 rubygems

Version Path
trackler-2.2.1.86 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.85 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.84 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.83 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.82 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.81 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.80 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.79 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.78 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.77 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.76 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.75 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.74 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.73 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.72 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.71 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.70 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.69 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.68 tracks/go/exercises/change/.meta/gen.go
trackler-2.2.1.67 tracks/go/exercises/change/.meta/gen.go