diff --git a/internal/docplugin/goplugin/goplugin.go b/internal/docplugin/goplugin/goplugin.go index 42ea213..23ec3b3 100644 --- a/internal/docplugin/goplugin/goplugin.go +++ b/internal/docplugin/goplugin/goplugin.go @@ -155,7 +155,9 @@ func (p GoPlugin) handleStruct( val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Text) val.PluginData[dataFieldGoType] = string(goTypeString) case model.TableCell: - val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Value) + if val.PluginData[dataFieldGoValue] == nil { + val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Value) + } if val.PluginData[dataFieldGoType] == nil { val.PluginData[dataFieldGoType] = string(goTypeString) } @@ -199,13 +201,18 @@ func (p GoPlugin) fillExampleHeaderTypes(examples *model.Examples) (err error) { return fmt.Errorf("invalid header cells: %w", err) } + goType := determinateGoType(values) cell := examples.TableHeader.Cells[i] - cell.PluginData[dataFieldGoType] = goTypeString + cell.PluginData[dataFieldGoType] = string(goType) cell.PluginData[dataFieldGoValue] = p.aliaser.StringValue(cell.Value) cell.PluginData[dataFieldGoName] = p.aliaser.NameAlias(cell.Value) for _, row := range examples.TableBody { - row.PluginData[dataFieldGoType] = string(determinateGoType(values)) + cell := row.Cells[i] + + goVal, goType := goValue(p.aliaser, cell.Value, goType) + cell.PluginData[dataFieldGoValue] = goVal + cell.PluginData[dataFieldGoType] = goType } } diff --git a/internal/docplugin/goplugin/goplugin_test.go b/internal/docplugin/goplugin/goplugin_test.go index e408fc2..0d06d2f 100644 --- a/internal/docplugin/goplugin/goplugin_test.go +++ b/internal/docplugin/goplugin/goplugin_test.go @@ -83,7 +83,7 @@ func TestGoPluginProcess(t *testing.T) { assert.Equal(t, "Title", pd["GoName"]) assert.Equal(t, "\"\"", pd["GoValue"]) - pd = doc.Feature.Children[0].Scenario.Examples[0].TableBody[0].PluginData + pd = doc.Feature.Children[0].Scenario.Examples[0].TableHeader.Cells[0].PluginData assert.Equal(t, "int", pd["GoType"]) } }) diff --git a/internal/docplugin/goplugin/gotype.go b/internal/docplugin/goplugin/gotype.go index 36e9edd..a4d7ef5 100644 --- a/internal/docplugin/goplugin/gotype.go +++ b/internal/docplugin/goplugin/gotype.go @@ -3,6 +3,8 @@ package goplugin import ( "strconv" "strings" + + "github.com/hedhyw/gherkingen/v2/internal/docplugin/goplugin/goaliaser" ) // goType definition. @@ -89,3 +91,28 @@ func goBool(val string) string { return strconv.FormatBool(b) } + +func goValue( + alias *goaliaser.Aliaser, + val string, + goType goType, +) (string, goType) { + switch goType { + case goTypeInt: + if val := goInt(val); val != "" { + return val, goTypeInt + } + case goTypeBool: + if val := goBool(val); val != "" { + return val, goTypeBool + } + case goTypeFloat64: + if val := goFloat64(val); val != "" { + return val, goTypeFloat64 + } + case goTypeString: + default: + } + + return alias.StringValue(val), goTypeString +} diff --git a/internal/docplugin/goplugin/gotype_test.go b/internal/docplugin/goplugin/gotype_test.go index 4f64c21..d6e7577 100644 --- a/internal/docplugin/goplugin/gotype_test.go +++ b/internal/docplugin/goplugin/gotype_test.go @@ -4,6 +4,8 @@ import ( "strings" "testing" + "github.com/hedhyw/gherkingen/v2/internal/docplugin/goplugin/goaliaser" + "github.com/stretchr/testify/assert" ) @@ -59,3 +61,81 @@ func TestDeterminateGoType(t *testing.T) { }) } } + +func TestGoValue(t *testing.T) { + t.Parallel() + + testCases := []struct { + In string + InGoType goType + Exp string + ExpGoType goType + }{{ + In: `Simple test`, + InGoType: goTypeString, + Exp: `"Simple test"`, + ExpGoType: goTypeString, + }, { + In: `Simple test`, + InGoType: goTypeInt, + Exp: `"Simple test"`, + ExpGoType: goTypeString, + }, { + In: `Simple test`, + InGoType: goTypeFloat64, + Exp: `"Simple test"`, + ExpGoType: goTypeString, + }, { + In: `Simple test`, + InGoType: goTypeBool, + Exp: `"Simple test"`, + ExpGoType: goTypeString, + }, { + In: `100`, + InGoType: goTypeInt, + Exp: `100`, + ExpGoType: goTypeInt, + }, { + In: `1 000 000`, + InGoType: goTypeInt, + Exp: `1000000`, + ExpGoType: goTypeInt, + }, { + In: `+`, + InGoType: goTypeBool, + Exp: `true`, + ExpGoType: goTypeBool, + }, { + In: `F`, + InGoType: goTypeBool, + Exp: `false`, + ExpGoType: goTypeBool, + }, { + In: `100.120`, + InGoType: goTypeFloat64, + Exp: `100.120`, + ExpGoType: goTypeFloat64, + }, { + In: `10 000.120`, + InGoType: goTypeFloat64, + Exp: `10000.120`, + ExpGoType: goTypeFloat64, + }} + + for i, tc := range testCases { + i, tc := i, tc + + t.Run(string(tc.InGoType)+"_"+tc.In, func(t *testing.T) { + t.Parallel() + + gotVal, gotType := goValue(goaliaser.New(), tc.In, tc.InGoType) + if gotVal != tc.Exp { + t.Errorf("%d:\n\tin: %s\n\texp: %s\n\tgot: %s", i, tc.In, tc.Exp, gotVal) + } + + if gotType != tc.ExpGoType { + t.Errorf("%d:\n\tin: %s\n\texp: %s\n\tgot: %s", i, tc.In, tc.ExpGoType, gotType) + } + }) + } +} diff --git a/internal/generator/examples/bool.feature b/internal/generator/examples/bool.feature new file mode 100644 index 0000000..6a28a76 --- /dev/null +++ b/internal/generator/examples/bool.feature @@ -0,0 +1,8 @@ +Feature: Type determinatiopn + Scenario: All type are determinated + When generator comleted + Then correct types are shown + Examples: + | <bool> | <int> | <string> | <flag> | <float64> | + | true | 1 | hello | - | 1.0 | + | false | 2 | world | + | 0.0 | diff --git a/internal/generator/examples/bool.feature.json b/internal/generator/examples/bool.feature.json new file mode 100644 index 0000000..46b210b --- /dev/null +++ b/internal/generator/examples/bool.feature.json @@ -0,0 +1,321 @@ +{ + "Feature": { + "Location": { + "Line": 1, + "Column": 1, + "PluginData": {} + }, + "Tags": [], + "Language": "en", + "Keyword": "Feature", + "Name": "Type determinatiopn", + "Description": "", + "Children": [ + { + "Scenario": { + "Location": { + "Line": 2, + "Column": 3, + "PluginData": {} + }, + "Tags": [], + "Keyword": "Scenario", + "Name": "All type are determinated", + "Description": "", + "Steps": [ + { + "Location": { + "Line": 3, + "Column": 5, + "PluginData": {} + }, + "Keyword": "When ", + "Text": "generator comleted", + "ID": "0194fdc2-fa2f-4cc0-81d3-ff12045b73c8", + "PluginData": { + "GoName": "When", + "GoType": "string", + "GoValue": "\"generator comleted\"" + } + }, + { + "Location": { + "Line": 4, + "Column": 5, + "PluginData": {} + }, + "Keyword": "Then ", + "Text": "correct types are shown", + "ID": "6e4ff95f-f662-45ee-a82a-bdf44a2d0b75", + "PluginData": { + "GoName": "Then", + "GoType": "string", + "GoValue": "\"correct types are shown\"" + } + } + ], + "Examples": [ + { + "Location": { + "Line": 5, + "Column": 5, + "PluginData": {} + }, + "Tags": [], + "Keyword": "Examples", + "Name": "", + "Description": "", + "ID": "f83b8e88-3bbf-457a-ab99-c5b252c7429c", + "TableHeader": { + "Location": { + "Line": 6, + "Column": 5, + "PluginData": {} + }, + "Cells": [ + { + "Location": { + "Line": 6, + "Column": 7, + "PluginData": {} + }, + "Value": "\u003cbool\u003e", + "PluginData": { + "GoName": "Bool", + "GoType": "bool", + "GoValue": "\"\u003cbool\u003e\"" + } + }, + { + "Location": { + "Line": 6, + "Column": 16, + "PluginData": {} + }, + "Value": "\u003cint\u003e", + "PluginData": { + "GoName": "Int", + "GoType": "int", + "GoValue": "\"\u003cint\u003e\"" + } + }, + { + "Location": { + "Line": 6, + "Column": 24, + "PluginData": {} + }, + "Value": "\u003cstring\u003e", + "PluginData": { + "GoName": "String", + "GoType": "string", + "GoValue": "\"\u003cstring\u003e\"" + } + }, + { + "Location": { + "Line": 6, + "Column": 35, + "PluginData": {} + }, + "Value": "\u003cflag\u003e", + "PluginData": { + "GoName": "Flag", + "GoType": "bool", + "GoValue": "\"\u003cflag\u003e\"" + } + }, + { + "Location": { + "Line": 6, + "Column": 44, + "PluginData": {} + }, + "Value": "\u003cfloat64\u003e", + "PluginData": { + "GoName": "Float64", + "GoType": "float64", + "GoValue": "\"\u003cfloat64\u003e\"" + } + } + ], + "ID": "fb180daf-48a7-4ee0-b10d-394651850fd4", + "PluginData": { + "GoValue": "\"\u003cbool\u003e_\u003cint\u003e_\u003cstring\u003e_\u003cflag\u003e_\u003cfloat64\u003e\"" + } + }, + "TableBody": [ + { + "Location": { + "Line": 7, + "Column": 5, + "PluginData": {} + }, + "Cells": [ + { + "Location": { + "Line": 7, + "Column": 7, + "PluginData": {} + }, + "Value": "true", + "PluginData": { + "GoType": "bool", + "GoValue": "true" + } + }, + { + "Location": { + "Line": 7, + "Column": 16, + "PluginData": {} + }, + "Value": "1", + "PluginData": { + "GoType": "int", + "GoValue": "1" + } + }, + { + "Location": { + "Line": 7, + "Column": 24, + "PluginData": {} + }, + "Value": "hello", + "PluginData": { + "GoType": "string", + "GoValue": "\"hello\"" + } + }, + { + "Location": { + "Line": 7, + "Column": 35, + "PluginData": {} + }, + "Value": "-", + "PluginData": { + "GoType": "bool", + "GoValue": "false" + } + }, + { + "Location": { + "Line": 7, + "Column": 44, + "PluginData": {} + }, + "Value": "1.0", + "PluginData": { + "GoType": "float64", + "GoValue": "1.0" + } + } + ], + "ID": "a178892e-e285-4ce1-9114-55780875d64e", + "PluginData": { + "GoValue": "\"true_1_hello_-_1.0\"" + } + }, + { + "Location": { + "Line": 8, + "Column": 5, + "PluginData": {} + }, + "Cells": [ + { + "Location": { + "Line": 8, + "Column": 7, + "PluginData": {} + }, + "Value": "false", + "PluginData": { + "GoType": "bool", + "GoValue": "false" + } + }, + { + "Location": { + "Line": 8, + "Column": 16, + "PluginData": {} + }, + "Value": "2", + "PluginData": { + "GoType": "int", + "GoValue": "2" + } + }, + { + "Location": { + "Line": 8, + "Column": 24, + "PluginData": {} + }, + "Value": "world", + "PluginData": { + "GoType": "string", + "GoValue": "\"world\"" + } + }, + { + "Location": { + "Line": 8, + "Column": 35, + "PluginData": {} + }, + "Value": "+", + "PluginData": { + "GoType": "bool", + "GoValue": "true" + } + }, + { + "Location": { + "Line": 8, + "Column": 44, + "PluginData": {} + }, + "Value": "0.0", + "PluginData": { + "GoType": "float64", + "GoValue": "0.0" + } + } + ], + "ID": "e2d3d0d0-de6b-48f9-b44c-e85ff044c6b1", + "PluginData": { + "GoValue": "\"false_2_world_+_0.0\"" + } + } + ], + "PluginData": { + "GoName": "Undefined", + "GoType": "string", + "GoValue": "\"Examples\"" + } + } + ], + "ID": "32f3a8ae-b79e-4856-b659-c18f0dcecc77", + "PluginData": { + "GoName": "Scenario", + "GoType": "string", + "GoValue": "\"All type are determinated\"" + } + }, + "PluginData": {} + } + ], + "PluginData": { + "GoName": "TypeDeterminatiopn", + "GoType": "string", + "GoValue": "\"Type determinatiopn\"" + } + }, + "Comments": [], + "PluginData": {}, + "PackageName": "examples_test" +} diff --git a/internal/generator/examples/bool.feature_test.go b/internal/generator/examples/bool.feature_test.go new file mode 100644 index 0000000..2a9732e --- /dev/null +++ b/internal/generator/examples/bool.feature_test.go @@ -0,0 +1,36 @@ +package examples_test + +import ( + "testing" + + "github.com/hedhyw/gherkingen/v2/pkg/bdd" +) + +func TestTypeDeterminatiopn(t *testing.T) { + f := bdd.NewFeature(t, "Type determinatiopn") + + f.Scenario("All type are determinated", func(t *testing.T, f *bdd.Feature) { + type testCase struct { + Bool bool `field:"<bool>"` + Int int `field:"<int>"` + String string `field:"<string>"` + Flag bool `field:"<flag>"` + Float64 float64 `field:"<float64>"` + } + + testCases := map[string]testCase{ + "true_1_hello_-_1.0": {true, 1, "hello", false, 1.0}, + "false_2_world_+_0.0": {false, 2, "world", true, 0.0}, + } + + f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) { + f.When("generator comleted", func() { + + }) + f.Then("correct types are shown", func() { + + }) + }) + }) + +} diff --git a/internal/generator/examples/issue_26.feature.json b/internal/generator/examples/issue_26.feature.json index b13c164..484ebb6 100644 --- a/internal/generator/examples/issue_26.feature.json +++ b/internal/generator/examples/issue_26.feature.json @@ -84,7 +84,6 @@ ], "ID": "6e4ff95f-f662-45ee-a82a-bdf44a2d0b75", "PluginData": { - "GoType": "string", "GoValue": "\"hello_world\"" } } diff --git a/internal/generator/examples/readme.feature.json b/internal/generator/examples/readme.feature.json index cbbbc8a..79da9de 100644 --- a/internal/generator/examples/readme.feature.json +++ b/internal/generator/examples/readme.feature.json @@ -110,7 +110,7 @@ "Value": "\u003cexit_status\u003e", "PluginData": { "GoName": "ExitStatus", - "GoType": "string", + "GoType": "int", "GoValue": "\"\u003cexit_status\u003e\"" } }, @@ -123,7 +123,7 @@ "Value": "\u003cprinted\u003e", "PluginData": { "GoName": "Printed", - "GoType": "string", + "GoType": "bool", "GoValue": "\"\u003cprinted\u003e\"" } } @@ -161,8 +161,8 @@ }, "Value": "0", "PluginData": { - "GoType": "string", - "GoValue": "\"0\"" + "GoType": "int", + "GoValue": "0" } }, { @@ -173,14 +173,13 @@ }, "Value": "true", "PluginData": { - "GoType": "string", - "GoValue": "\"true\"" + "GoType": "bool", + "GoValue": "true" } } ], "ID": "e2d3d0d0-de6b-48f9-b44c-e85ff044c6b1", "PluginData": { - "GoType": "bool", "GoValue": "\"--help_0_true\"" } }, @@ -211,8 +210,8 @@ }, "Value": "0", "PluginData": { - "GoType": "string", - "GoValue": "\"0\"" + "GoType": "int", + "GoValue": "0" } }, { @@ -223,14 +222,13 @@ }, "Value": "true", "PluginData": { - "GoType": "string", - "GoValue": "\"true\"" + "GoType": "bool", + "GoValue": "true" } } ], "ID": "f83b8e88-3bbf-457a-ab99-c5b252c7429c", "PluginData": { - "GoType": "bool", "GoValue": "\"-help_0_true\"" } }, @@ -261,8 +259,8 @@ }, "Value": "1", "PluginData": { - "GoType": "string", - "GoValue": "\"1\"" + "GoType": "int", + "GoValue": "1" } }, { @@ -273,14 +271,13 @@ }, "Value": "false", "PluginData": { - "GoType": "string", - "GoValue": "\"false\"" + "GoType": "bool", + "GoValue": "false" } } ], "ID": "32f3a8ae-b79e-4856-b659-c18f0dcecc77", "PluginData": { - "GoType": "bool", "GoValue": "\"-invalid_1_false\"" } } diff --git a/internal/generator/examples/readme.feature_test.go b/internal/generator/examples/readme.feature_test.go index b58a611..d137ea7 100644 --- a/internal/generator/examples/readme.feature_test.go +++ b/internal/generator/examples/readme.feature_test.go @@ -12,14 +12,14 @@ func TestApplicationCommandLineTool(t *testing.T) { f.Scenario("User wants to see usage information", func(t *testing.T, f *bdd.Feature) { type testCase struct { Flag string `field:"<flag>"` - ExitStatus string `field:"<exit_status>"` - Printed string `field:"<printed>"` + ExitStatus int `field:"<exit_status>"` + Printed bool `field:"<printed>"` } testCases := map[string]testCase{ - "--help_0_true": {"--help", "0", "true"}, - "-help_0_true": {"-help", "0", "true"}, - "-invalid_1_false": {"-invalid", "1", "false"}, + "--help_0_true": {"--help", 0, true}, + "-help_0_true": {"-help", 0, true}, + "-invalid_1_false": {"-invalid", 1, false}, } f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {