Skip to content

Commit

Permalink
Fix type determination (#36)
Browse files Browse the repository at this point in the history
* Fix type determination

* Fix test

* TestGoValue

* Fix linter
  • Loading branch information
hedhyw authored Jun 13, 2022
1 parent 106ca02 commit 7a77800
Show file tree
Hide file tree
Showing 10 changed files with 502 additions and 27 deletions.
13 changes: 10 additions & 3 deletions internal/docplugin/goplugin/goplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/docplugin/goplugin/goplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestGoPluginProcess(t *testing.T) {
assert.Equal(t, "Title", pd["GoName"])
assert.Equal(t, "\"<Title>\"", 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"])
}
})
Expand Down
27 changes: 27 additions & 0 deletions internal/docplugin/goplugin/gotype.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package goplugin
import (
"strconv"
"strings"

"github.com/hedhyw/gherkingen/v2/internal/docplugin/goplugin/goaliaser"
)

// goType definition.
Expand Down Expand Up @@ -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
}
80 changes: 80 additions & 0 deletions internal/docplugin/goplugin/gotype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"strings"
"testing"

"github.com/hedhyw/gherkingen/v2/internal/docplugin/goplugin/goaliaser"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -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)
}
})
}
}
8 changes: 8 additions & 0 deletions internal/generator/examples/bool.feature
Original file line number Diff line number Diff line change
@@ -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 |
Loading

0 comments on commit 7a77800

Please sign in to comment.