|
| 1 | +package {{ .PackageName }} |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +{{ define "Background" }} |
| 8 | + {{- $Background := . -}} |
| 9 | + background := func(t *testing.T) interface{} { |
| 10 | + {{- range $Background.Steps }} |
| 11 | + // {{ .Keyword }}{{ .Text }} |
| 12 | + |
| 13 | + {{ end }} |
| 14 | + |
| 15 | + return nil // TODO: Feel free to modify return value(s). |
| 16 | + } |
| 17 | + |
| 18 | +{{ end }} |
| 19 | + |
| 20 | +{{ define "Scenario" }} |
| 21 | + {{- $Scenario := . -}} |
| 22 | + t.Run({{ $Scenario.PluginData.GoValue }}, func({{- /* |
| 23 | + t is usualy unused if there are no examples |
| 24 | + */ -}}{{- if and $Scenario.Examples (not $Scenario.PluginData.GoParallel) -}}_{{- else -}}t{{- end -}} *testing.T) { |
| 25 | + {{- range $Scenario.Examples }} |
| 26 | + {{- if $Scenario.PluginData.GoParallel }} |
| 27 | + t.Parallel() |
| 28 | + |
| 29 | + {{ end -}} |
| 30 | + |
| 31 | + {{- /* Define test case struct. */ -}} |
| 32 | + |
| 33 | + type testCase struct { |
| 34 | + {{- range .TableHeader.Cells }} |
| 35 | + {{ .PluginData.GoName }} {{ .PluginData.GoType }} `field:"{{.Value}}"` |
| 36 | + {{- end -}} |
| 37 | + } |
| 38 | + |
| 39 | + testCases := map[string]testCase{ |
| 40 | + {{- range .TableBody }} |
| 41 | + {{ .PluginData.GoValue }}: { |
| 42 | + {{- /* Struct fields start. */ -}} |
| 43 | + {{- range $index, $cell := .Cells -}} |
| 44 | + {{- if $index -}},{{ end }} {{- $cell.PluginData.GoValue -}} |
| 45 | + {{- end -}} |
| 46 | + {{- /* Struct fields end. */ -}} |
| 47 | + }, |
| 48 | + {{- end }} |
| 49 | + } |
| 50 | + |
| 51 | + for name, tc := range testCases { |
| 52 | + {{- if $Scenario.PluginData.GoParallel }} |
| 53 | + tc := tc |
| 54 | + {{ end -}} |
| 55 | + |
| 56 | + t.Run(name, func(t *testing.T) { |
| 57 | + {{- if $Scenario.PluginData.GoParallel }} |
| 58 | + t.Parallel() |
| 59 | + |
| 60 | + {{ end -}} |
| 61 | + _ = tc // TODO: Use and remove. |
| 62 | + {{- if $Scenario.PluginData.GoHasBackground }} |
| 63 | + _ = background(t) |
| 64 | + |
| 65 | + {{ end -}} |
| 66 | + |
| 67 | + {{- range $Scenario.Steps }} |
| 68 | + // {{ .Keyword }}{{ .Text }} |
| 69 | + |
| 70 | + {{ end }} |
| 71 | + }) |
| 72 | + } |
| 73 | + {{- else }} |
| 74 | + {{- if $Scenario.PluginData.GoParallel }} |
| 75 | + t.Parallel() |
| 76 | + |
| 77 | + {{ end -}} |
| 78 | + {{- if $Scenario.PluginData.GoHasBackground }} |
| 79 | + _ = background(t) |
| 80 | + |
| 81 | + {{ end }} |
| 82 | + {{- range $Scenario.Steps }} |
| 83 | + // {{ .Keyword }}{{ .Text }} |
| 84 | + |
| 85 | + {{ end -}} |
| 86 | + {{ end }} |
| 87 | + }) |
| 88 | +{{ end }} |
| 89 | + |
| 90 | +{{ define "Rule" }} |
| 91 | + {{ $Rule := . }} |
| 92 | + t.Run({{ $Rule.PluginData.GoValue }}, func({{- if $Rule.PluginData.GoParallel -}}t{{- else -}}_{{- end -}} *testing.T) { |
| 93 | + {{- if $Rule.PluginData.GoParallel }} |
| 94 | + t.Parallel() |
| 95 | + |
| 96 | + {{ end -}} |
| 97 | + {{- range $Rule.Children -}} |
| 98 | + |
| 99 | + {{- if .Background }} |
| 100 | + {{ template "Background" .Background }} |
| 101 | + {{- end }} |
| 102 | + |
| 103 | + {{- if .Scenario }} |
| 104 | + {{- template "Scenario" .Scenario -}} |
| 105 | + {{- end }} |
| 106 | + |
| 107 | + {{- end -}} |
| 108 | + }) |
| 109 | +{{ end }} |
| 110 | + |
| 111 | +func Test{{ .Feature.PluginData.GoName }}(t *testing.T) { |
| 112 | + {{- if .Feature.PluginData.GoParallel }} |
| 113 | + t.Parallel() |
| 114 | + |
| 115 | + {{ end -}} |
| 116 | + {{ if .Feature.PluginData.GoComment }} |
| 117 | + /* {{ .Feature.PluginData.GoComment }} */ |
| 118 | + {{ end }} |
| 119 | + |
| 120 | + {{- range .Feature.Children }} |
| 121 | + |
| 122 | + {{ if .Background }} |
| 123 | + {{ template "Background" .Background }} |
| 124 | + {{- end -}} |
| 125 | + |
| 126 | + {{ if .Scenario }} |
| 127 | + {{ template "Scenario" .Scenario }} |
| 128 | + {{- end -}} |
| 129 | + |
| 130 | + {{ if .Rule }} |
| 131 | + {{ template "Rule" .Rule }} |
| 132 | + {{- end -}} |
| 133 | + |
| 134 | + {{- end -}} |
| 135 | +} |
0 commit comments