Sha256: de2107a47c06c02643e37221a2490d1482da06867bfb43a2cb3d1a35c23c4ddd

Contents?: true

Size: 1.44 KB

Versions: 68

Compression:

Stored size: 1.44 KB

Contents

package main

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

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

func main() {
	t := template.New("").Funcs(template.FuncMap{
		"fileContentData": FileContentData,
	})
	t, err := t.Parse(tmpl)
	if err != nil {
		log.Fatal(err)
	}
	var j js
	if err := gen.Gen("grep", &j, t); err != nil {
		log.Fatal(err)
	}
}

// The JSON structure we expect to be able to unmarshal into
type js struct {
	Comments []string
	Cases    []struct {
		Description string
		Cases       []struct {
			Description string
			Input       struct {
				Pattern string
				Flags   []string
				Files   []string
			}
			Expected []string
		}
	}
}

func FileContentData(comments []string) []string {
	var start int
	for i, c := range comments {
		if strings.Contains(c, "contents are listed below:") {
			start = i + 1
			break
		}
	}
	return comments[start:]
}

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

{{.Header}}

var fileContentData = []string{ {{range $line := fileContentData .J.Comments}}{{printf "\n%q," $line}}{{end}}
}

var testCases = []struct {
	description string
	pattern     string
	flags       []string
	files       []string
	expected    []string
}{
{{range .J.Cases}}
	{{range .Cases}}{
		description: {{printf "%q" .Description}},
		pattern: {{printf "%q" .Input.Pattern}},
		flags: {{printf "%#v" .Input.Flags}},
		files: {{printf "%#v" .Input.Files}},
		expected: {{printf "%#v" .Expected}},
},
{{end}}{{end}}
}
`

Version data entries

68 entries across 68 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.179 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.178 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.177 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.176 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.175 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.174 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.173 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.172 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.171 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.170 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.169 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.167 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.166 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.165 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.164 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.163 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.162 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.161 tracks/go/exercises/grep/.meta/gen.go
trackler-2.2.1.160 tracks/go/exercises/grep/.meta/gen.go