Sha256: 3ce233cab19fac91a6c88acf6318340d066352315140d9871755dd67c109b5a4

Contents?: true

Size: 1.56 KB

Versions: 10

Compression:

Stored size: 1.56 KB

Contents

// +build ignore

package main

import (
	"log"
	"text/template"

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

func main() {
	t := template.New("").Funcs(template.FuncMap{
		"areacode":  areacode,
		"expectErr": expectErr,
		"formatted": formatted,
	})
	t, err := t.Parse(tmpl)
	if err != nil {
		log.Fatal(err)
	}
	var j js
	if err := gen.Gen("phone-number", &j, t); err != nil {
		log.Fatal(err)
	}
}

func expectErr(expected string) bool {
	return len(expected) == 0
}

func areacode(expected string) string {
	if expectErr(expected) {
		return ""
	}
	return expected[0:3]
}

func formatted(expected string) string {
	if expectErr(expected) {
		return ""
	}
	return "(" + areacode(expected) + ") " + expected[3:6] + "-" + expected[6:10]
}

// The JSON structure we expect to be able to unmarshal into
type js struct {
	Cases []struct {
		Description string
		Cases       []struct {
			Description string
			Phrase      string
			Expected    string
		}
	}
}

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

{{.Header}}

{{range .J.Cases}}// {{.Description}}
var numberTests = []struct {
	description string
	input       string
	expectErr   bool
	number      string
	areaCode    string
	formatted   string
}{
	{{range .Cases}}{
		description: {{printf "%q" .Description}},
		input: {{printf "%q" .Phrase}},
		{{if expectErr .Expected}} expectErr: {{expectErr .Expected | printf "%v" }},
		{{else}} number: {{printf "%q" .Expected}},
		areaCode: {{areacode .Expected  | printf "%q" }},
		formatted: {{formatted .Expected  | printf "%q" }},{{- end}}
},
{{end}}{{end}}
}
`

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
trackler-2.1.0.18 tracks/go/exercises/phone-number/.meta/gen.go
trackler-2.1.0.17 tracks/go/exercises/phone-number/.meta/gen.go
trackler-2.1.0.16 tracks/go/exercises/phone-number/.meta/gen.go
trackler-2.1.0.15 tracks/go/exercises/phone-number/.meta/gen.go
trackler-2.1.0.14 tracks/go/exercises/phone-number/.meta/gen.go
trackler-2.1.0.13 tracks/go/exercises/phone-number/.meta/gen.go
trackler-2.1.0.12 tracks/go/exercises/phone-number/.meta/gen.go
trackler-2.1.0.11 tracks/go/exercises/phone-number/.meta/gen.go
trackler-2.1.0.10 tracks/go/exercises/phone-number/.meta/gen.go
trackler-2.1.0.9 tracks/go/exercises/phone-number/.meta/gen.go