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.1.0.49 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.48 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.47 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.46 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.45 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.44 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.43 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.42 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.41 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.40 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.39 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.38 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.37 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.36 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.34 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.33 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.32 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.31 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.30 tracks/go/exercises/change/.meta/gen.go
trackler-2.1.0.29 tracks/go/exercises/change/.meta/gen.go