Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
drshriveer committed Sep 21, 2023
1 parent 6fdec46 commit 1c23c60
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 130 deletions.
260 changes: 132 additions & 128 deletions genum/internal/enumerable_with_traits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package internal_test

import (
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/stretchr/testify/require"

"github.com/drshriveer/gtools/genum"
"github.com/drshriveer/gtools/genum/gen"
"github.com/drshriveer/gtools/genum/internal"
)

func TestGenerate_EnumerableWithTraits(t *testing.T) {
Expand All @@ -21,131 +26,130 @@ func TestGenerate_EnumerableWithTraits(t *testing.T) {
require.NoError(t, generator.Parse())
}

//
// func TestEnumerableWithTraitsGeneration(t *testing.T) {
// tests := []struct {
// description string
// enumName string
// disableTraits bool
// expectError bool
// }{
// {
// description: "EnumerableWithTraits parses successfully",
// enumName: "EnumerableWithTraits",
// expectError: false,
// },
// {
// description: "Creatures parses successfully",
// enumName: "Creatures",
// expectError: false,
// },
// {
// description: "ErrEnum1 fails due to inconsistent traits",
// enumName: "ErrEnum1",
// expectError: true,
// },
// {
// description: "ErrEnum2 fails due to no trait names",
// enumName: "ErrEnum2",
// expectError: true,
// },
// {
// description: "ErrEnum1 succeeds with traits disabled",
// enumName: "ErrEnum1",
// disableTraits: true,
// expectError: false,
// },
// {
// description: "ErrEnum2 succeeds with traits disabled",
// enumName: "ErrEnum2",
// disableTraits: true,
// expectError: false,
// },
// }
//
// for _, test := range tests {
// t.Run(test.description, func(t *testing.T) {
// generator := gen.Generate{
// InFile: "./enumerable_with_traits.go",
// OutFile: "./enumerable_with_traits.genum.go",
// Types: []string{test.enumName},
// GenJSON: true,
// GenYAML: true,
// GenText: true,
// DisableTraits: test.disableTraits,
// }
//
// err := generator.Parse()
// if test.expectError {
// require.Error(t, err)
// } else {
// require.NoError(t, err)
// }
// })
// }
// }
//
// func TestCreatures_Traits(t *testing.T) {
// tests := []struct {
// enum internal.Creatures
// numLegs int
// isMammal bool
// }{
// {enum: internal.NotCreature, numLegs: 0, isMammal: false},
// {enum: internal.Cat, numLegs: internal.CatLegs, isMammal: true},
// {enum: internal.Feline, numLegs: internal.CatLegs, isMammal: true},
// {enum: internal.Feline2, numLegs: internal.CatLegs, isMammal: true},
// {enum: internal.Dog, numLegs: internal.DogLegs, isMammal: true},
// {enum: internal.Ant, numLegs: internal.AntLegs, isMammal: false},
// {enum: internal.Spider, numLegs: internal.SpiderLegs, isMammal: false},
// {enum: internal.Human, numLegs: internal.HumanLegs, isMammal: true},
// {enum: internal.SeaAnemone, numLegs: 0, isMammal: false},
// }
//
// for _, test := range tests {
// t.Run(test.enum.String(), func(t *testing.T) {
// assert.Implements(t, (*genum.Enum)(nil), test.enum)
// assert.True(t, test.enum.IsValid())
// assert.Equal(t, test.numLegs, test.enum.NumCreatureLegs())
// assert.Equal(t, test.isMammal, test.enum.IsCreatureMammal())
// })
// }
// }
//
// func TestEnumerableWithTraits_Traits(t *testing.T) {
// tests := []struct {
// enum internal.EnumerableWithTraits
// trait string
// timeout time.Duration
// typedTrait internal.OtherType
// }{
// {
// enum: internal.E1,
// trait: "trait 1",
// timeout: 5 * time.Minute,
// typedTrait: "OtherType0",
// },
// {
// enum: internal.E2,
// trait: "trait 2",
// timeout: 1 * time.Minute,
// typedTrait: "OtherType2",
// },
// {
// enum: internal.E3,
// trait: "trait 3",
// timeout: 2 * time.Minute,
// typedTrait: "OtherType3",
// },
// }
//
// for _, test := range tests {
// t.Run(test.enum.String(), func(t *testing.T) {
// assert.Implements(t, (*genum.Enum)(nil), test.enum)
// assert.True(t, test.enum.IsValid())
// assert.Equal(t, test.trait, test.enum.Trait())
// assert.Equal(t, test.timeout, test.enum.Timeout())
// assert.Equal(t, test.typedTrait, test.enum.TypedStringTrait())
// })
// }
// }
func TestEnumerableWithTraitsGeneration(t *testing.T) {
tests := []struct {
description string
enumName string
disableTraits bool
expectError bool
}{
{
description: "EnumerableWithTraits parses successfully",
enumName: "EnumerableWithTraits",
expectError: false,
},
{
description: "Creatures parses successfully",
enumName: "Creatures",
expectError: false,
},
{
description: "ErrEnum1 fails due to inconsistent traits",
enumName: "ErrEnum1",
expectError: true,
},
{
description: "ErrEnum2 fails due to no trait names",
enumName: "ErrEnum2",
expectError: true,
},
{
description: "ErrEnum1 succeeds with traits disabled",
enumName: "ErrEnum1",
disableTraits: true,
expectError: false,
},
{
description: "ErrEnum2 succeeds with traits disabled",
enumName: "ErrEnum2",
disableTraits: true,
expectError: false,
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
generator := gen.Generate{
InFile: "./enumerable_with_traits.go",
OutFile: "./enumerable_with_traits.genum.go",
Types: []string{test.enumName},
GenJSON: true,
GenYAML: true,
GenText: true,
DisableTraits: test.disableTraits,
}

err := generator.Parse()
if test.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}

func TestCreatures_Traits(t *testing.T) {
tests := []struct {
enum internal.Creatures
numLegs int
isMammal bool
}{
{enum: internal.NotCreature, numLegs: 0, isMammal: false},
{enum: internal.Cat, numLegs: internal.CatLegs, isMammal: true},
{enum: internal.Feline, numLegs: internal.CatLegs, isMammal: true},
{enum: internal.Feline2, numLegs: internal.CatLegs, isMammal: true},
{enum: internal.Dog, numLegs: internal.DogLegs, isMammal: true},
{enum: internal.Ant, numLegs: internal.AntLegs, isMammal: false},
{enum: internal.Spider, numLegs: internal.SpiderLegs, isMammal: false},
{enum: internal.Human, numLegs: internal.HumanLegs, isMammal: true},
{enum: internal.SeaAnemone, numLegs: 0, isMammal: false},
}

for _, test := range tests {
t.Run(test.enum.String(), func(t *testing.T) {
assert.Implements(t, (*genum.Enum)(nil), test.enum)
assert.True(t, test.enum.IsValid())
assert.Equal(t, test.numLegs, test.enum.NumCreatureLegs())
assert.Equal(t, test.isMammal, test.enum.IsCreatureMammal())
})
}
}

func TestEnumerableWithTraits_Traits(t *testing.T) {
tests := []struct {
enum internal.EnumerableWithTraits
trait string
timeout time.Duration
typedTrait internal.OtherType
}{
{
enum: internal.E1,
trait: "trait 1",
timeout: 5 * time.Minute,
typedTrait: "OtherType0",
},
{
enum: internal.E2,
trait: "trait 2",
timeout: 1 * time.Minute,
typedTrait: "OtherType2",
},
{
enum: internal.E3,
trait: "trait 3",
timeout: 2 * time.Minute,
typedTrait: "OtherType3",
},
}

for _, test := range tests {
t.Run(test.enum.String(), func(t *testing.T) {
assert.Implements(t, (*genum.Enum)(nil), test.enum)
assert.True(t, test.enum.IsValid())
assert.Equal(t, test.trait, test.enum.Trait())
assert.Equal(t, test.timeout, test.enum.Timeout())
assert.Equal(t, test.typedTrait, test.enum.TypedStringTrait())
})
}
}
1 change: 1 addition & 0 deletions gerror/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/itzg/go-flagsfiller v1.12.0 h1:LSwSUGxzZqueprm0D8FBCAG0JMgwAkkh2UjtwreNgAg=
github.com/itzg/go-flagsfiller v1.12.0/go.mod h1:47WeO9fl+QyS48AdRHfarhF3rKBh0enbe90tTko03gg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ _tools-generate:
_invokeMod cmd target='all':
#!/usr/bin/env bash
if [ "{{ target }}" = "all" ]; then
xargs -L1 -t -I {} {{ cmd }} <<< "{{ MODS }}"
xargs -L1 -P 8 -t -I {} {{ cmd }} <<< "{{ MODS }}"
else
xargs -L1 -t -I {} {{ cmd }} <<< "{{ target }}"
xargs -L1 -P 8 -t -I {} {{ cmd }} <<< "{{ target }}"
fi

0 comments on commit 1c23c60

Please sign in to comment.