add more test coverage (#109)

This commit is contained in:
Alex Bice authored and GitHub committed 2022-06-08 12:38:15 -07:00
1 parent e94287eeb7
commit 95d64ebbc0
6 files changed
+2513 -59

No files matched your search

+1
View File
@@ -27,6 +27,7 @@ func TestColorMustParse(t *testing.T) {
x := `avocadogreen`
assert.PanicsWithError(t, x+" is not a valid Color", func() { MustParseColor(x) })
assert.NotPanics(t, func() { MustParseColor(ColorGreen.String()) })
}
func TestColorUnmarshal(t *testing.T) {
+1 -1
View File
@@ -1,4 +1,4 @@
//go:generate ../bin/go-enum -f=$GOFILE --marshal --lower --ptr --mustparse
//go:generate ../bin/go-enum -f=$GOFILE --nocase
package example
+8 -58
View File
@@ -55,38 +55,13 @@ func ParseAllNegative(name string) (AllNegative, error) {
if x, ok := _AllNegativeValue[name]; ok {
return x, nil
}
// Case insensitive parse, do a separate lookup to prevent unnecessary cost of lowercasing a string if we don't need to.
if x, ok := _AllNegativeValue[strings.ToLower(name)]; ok {
return x, nil
}
return AllNegative(0), fmt.Errorf("%s is not a valid AllNegative", name)
}
// MustParseAllNegative converts a string to a AllNegative, and panics if is not valid.
func MustParseAllNegative(name string) AllNegative {
val, err := ParseAllNegative(name)
if err != nil {
panic(err)
}
return val
}
func (x AllNegative) Ptr() *AllNegative {
return &x
}
// MarshalText implements the text marshaller method.
func (x AllNegative) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
// UnmarshalText implements the text unmarshaller method.
func (x *AllNegative) UnmarshalText(text []byte) error {
name := string(text)
tmp, err := ParseAllNegative(name)
if err != nil {
return err
}
*x = tmp
return nil
}
const (
// StatusUnknown is a Status of type Unknown.
StatusUnknown Status = iota + -1
@@ -126,34 +101,9 @@ func ParseStatus(name string) (Status, error) {
if x, ok := _StatusValue[name]; ok {
return x, nil
}
// Case insensitive parse, do a separate lookup to prevent unnecessary cost of lowercasing a string if we don't need to.
if x, ok := _StatusValue[strings.ToLower(name)]; ok {
return x, nil
}
return Status(0), fmt.Errorf("%s is not a valid Status", name)
}
// MustParseStatus converts a string to a Status, and panics if is not valid.
func MustParseStatus(name string) Status {
val, err := ParseStatus(name)
if err != nil {
panic(err)
}
return val
}
func (x Status) Ptr() *Status {
return &x
}
// MarshalText implements the text marshaller method.
func (x Status) MarshalText() ([]byte, error) {
return []byte(x.String()), nil
}
// UnmarshalText implements the text unmarshaller method.
func (x *Status) UnmarshalText(text []byte) error {
name := string(text)
tmp, err := ParseStatus(name)
if err != nil {
return err
}
*x = tmp
return nil
}
+101
View File
@@ -0,0 +1,101 @@
package example
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestStatusString(t *testing.T) {
tests := map[string]struct {
input string
output Status
}{
"bad": {
input: `Bad`,
output: StatusBad,
},
"unknown": {
input: `Unknown`,
output: StatusUnknown,
},
"good": {
input: `Good`,
output: StatusGood,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
output, err := ParseStatus(tc.input)
assert.NoError(t, err)
assert.Equal(t, tc.output, output)
assert.Equal(t, tc.input, output.String())
})
}
t.Run("failures", func(t *testing.T) {
assert.Equal(t, "Status(99)", Status(99).String())
failedStatus, err := ParseStatus("")
assert.Error(t, err)
assert.Equal(t, Status(0), failedStatus)
t.Run("cased", func(t *testing.T) {
actual, err := ParseStatus("BAD")
assert.NoError(t, err)
assert.Equal(t, StatusBad, actual)
})
})
}
func TestNegativeString(t *testing.T) {
tests := map[string]struct {
input string
output AllNegative
}{
"unknown": {
input: `Unknown`,
output: AllNegativeUnknown,
},
"good": {
input: `Good`,
output: AllNegativeGood,
},
"bad": {
input: `Bad`,
output: AllNegativeBad,
},
"ugly": {
input: `Ugly`,
output: AllNegativeUgly,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
output, err := ParseAllNegative(tc.input)
assert.NoError(t, err)
assert.Equal(t, tc.output, output)
assert.Equal(t, tc.input, output.String())
})
}
t.Run("failures", func(t *testing.T) {
assert.Equal(t, "AllNegative(99)", AllNegative(99).String())
allN, err := ParseAllNegative("")
assert.Error(t, err)
assert.Equal(t, AllNegative(0), allN)
})
t.Run("cased", func(t *testing.T) {
actual, err := ParseAllNegative("UGLY")
assert.NoError(t, err)
assert.Equal(t, AllNegativeUgly, actual)
})
}
File diff suppressed because it is too large. Load diff
+23
View File
@@ -67,6 +67,29 @@ func TestExampleFile(t *testing.T) {
}
}
// TestExampleFileMoreOptions
func TestExampleFileMoreOptions(t *testing.T) {
g := NewGenerator().
WithMarshal().
WithSQLDriver().
WithCaseInsensitiveParse().
WithNames().
WithoutSnakeToCamel().
WithMustParse().
WithForceLower().
WithTemplates(`../example/user_template.tmpl`)
// Parse the file given in arguments
imported, err := g.GenerateFromFile(testExample)
require.Nil(t, err, "Error generating formatted code")
outputLines := strings.Split(string(imported), "\n")
cupaloy.SnapshotT(t, outputLines)
if false {
fmt.Println(string(imported))
}
}
// TestExampleFile
func TestNoPrefixExampleFile(t *testing.T) {
g := NewGenerator().