Sha256: 9356ffc2ad9695abb67fc4fd73a317bf8a80508f07beef4576a08e6173fa6309

Contents?: true

Size: 1.68 KB

Versions: 86

Compression:

Stored size: 1.68 KB

Contents

package main

import (
	"log"
	"sort"
	"strconv"
	"strings"
	"text/template"

	"../../../gen"
)

func main() {
	t, err := template.New("").Parse(tmpl)
	if err != nil {
		log.Fatal(err)
	}
	var j js
	if err := gen.Gen("nucleotide-count", &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
	}
}

// OneCase represents each test case
type OneCase struct {
	Description string
	Property    string
	Input       struct {
		Strand string
	}
	Expected map[string]interface{}
}

// ErrorExpected returns true if an error should be raised
func (c OneCase) ErrorExpected() bool {
	_, exists := c.Expected["error"]
	return exists
}

// SortedMapString collects key:values for a map in sorted order
func (c OneCase) SortedMapString() string {
	strs := make([]string, 0, len(c.Expected))
	for k, v := range c.Expected {
		switch t := v.(type) {
		case float64:
			strs = append(strs, `'`+k+`': `+strconv.FormatFloat(t, 'f', -1, 64))
		default:
			log.Fatalf("unexpected type %T for %v", t, v)
		}

	}
	sort.Strings(strs)
	return strings.Join(strs, ",")
}

// template applied to above data structure generates the Go test cases
var tmpl = `package dna

{{.Header}}

{{range .J.Cases}}// {{.Description}}
var testCases = []struct {
	description   string
	strand        string
	expected      Histogram
	errorExpected bool
}{
{{range .Cases}}{
	description:	{{printf "%q"  .Description}},
	strand:		{{printf "%#v"  .Input.Strand}},
	{{if .ErrorExpected}}errorExpected:	true,
	{{else}}expected:       Histogram{ {{.SortedMapString}} },
	{{- end}}
},
{{end}}{{end}}
}
`

Version data entries

86 entries across 86 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.158 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.157 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.156 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.155 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.154 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.153 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.152 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.151 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.150 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.149 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.148 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.147 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.146 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.145 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.144 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.143 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.142 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.141 tracks/go/exercises/nucleotide-count/.meta/gen.go
trackler-2.2.1.140 tracks/go/exercises/nucleotide-count/.meta/gen.go