Skip to content

Commit

Permalink
Example Test (#31)
Browse files Browse the repository at this point in the history
* cleanup

* test
  • Loading branch information
nikolaydubina authored Aug 27, 2024
1 parent a520af3 commit 25e02f6
Show file tree
Hide file tree
Showing 16 changed files with 200 additions and 41 deletions.
18 changes: 18 additions & 0 deletions enum_test.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,28 @@
package {{.Package}}

import (
"fmt"
"testing"
"errors"
)

func Example{{.Type}}_{{.EncodeMethod}}() {
for _, v := range []{{.Type}}{{{.Values}}} {
b, _ := v.{{.EncodeMethod}}()
fmt.Printf("%s ", string(b))
}
// Output: {{.TagsNaked}}
}

func Example{{.Type}}_{{.DecodeMethod}}() {
for _, s := range []string{{{.Tags}}} {
var v {{.Type}}
if err := (&v).{{.DecodeMethod}}([]byte(s)); err != nil {
fmt.Println(err)
}
}
}

func Test{{.Type}}_{{.EncodeMethod}}_{{.DecodeMethod}}(t *testing.T) {
for _, v := range []{{.Type}}{{{.Values}}} {
b, err := v.{{.EncodeMethod}}()
Expand Down
2 changes: 0 additions & 2 deletions internal/testdata/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package color

type Color struct{ c uint8 }

//go:generate go-enum-encoding -type=Color
var (
UndefinedColor = Color{} // json:""
Red = Color{1} // json:"red"
Expand All @@ -17,7 +16,6 @@ type V struct {

type ColorString int

//go:generate go-enum-encoding -type=ColorString -string
const (
RedS ColorString = iota // json:"red"
GreenS // json:"green"
Expand Down
1 change: 0 additions & 1 deletion internal/testdata/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package color

type Currency struct{ c uint8 }

//go:generate go-enum-encoding -type=Currency
var (
UndefinedCurrency = Currency{} // json:""
SGD = Currency{1} // json:"SGD"
Expand Down
1 change: 0 additions & 1 deletion internal/testdata/currency_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package color

type CurrencyString struct{ c uint8 }

//go:generate go-enum-encoding -type=CurrencyString -string
var (
UndefinedCurrencyS = CurrencyString{} // json:""
SGDS = CurrencyString{1} // json:"SGD"
Expand Down
1 change: 0 additions & 1 deletion internal/testdata/currency_string_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package color

type CurrencyStringCustom struct{ c uint8 }

//go:generate go-enum-encoding -type=CurrencyStringCustom -string -encode-method=MarshalTextName -decode-method=UnmarshalTextName -string-method=StringName
var (
UndefinedCurrencySC = CurrencyStringCustom{} // json:""
SGDSC = CurrencyStringCustom{1} // json:"SGD"
Expand Down
18 changes: 18 additions & 0 deletions internal/testdata/exp/color2_enum_encoding_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions internal/testdata/exp/color_enum_encoding_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions internal/testdata/exp/colorstring_enum_encoding_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions internal/testdata/exp/currency2_enum_encoding_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions internal/testdata/exp/currency_enum_encoding_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions internal/testdata/exp/currencystring_enum_encoding_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions internal/testdata/exp/currencystringcustom_enum_encoding_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions internal/testdata/multiple.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package color

type Color2 struct{ c uint8 }

//go:generate go-enum-encoding -type=Color2
var (
UndefinedColor2 = Color2{} // json:""
Red2 = Color2{1} // json:"red"
Expand All @@ -11,7 +10,6 @@ var (

type Currency2 uint8

//go:generate go-enum-encoding -type=Currency2
const (
UndefCurrency2 Currency2 = iota // json:""
SGD2 // json:"SGD"
Expand Down
1 change: 0 additions & 1 deletion internal/testdata/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package color

type Size uint8

//go:generate go-enum-encoding -type=Size
const (
UndefinedSize Size = iota // json:""
Small // json:"small"
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func process(typeName, fileName, lineNum, packageName, mode, encodeMethod, decod
With("{{.StringMethod}}", stringMethod).
WithMap("{{.Values}}", func(_ int, v [2]string) string { return v[0] }, ", ").
WithMap("{{.Tags}}", func(_ int, v [2]string) string { return `"` + v[1] + `"` }, ",").
WithMap("{{.TagsNaked}}", func(_ int, v [2]string) string { return v[1] }, " ").
WithMap("{{.seq_bytes}}", func(_ int, v [2]string) string { return `[]byte("` + v[1] + `")` }, ", ").
WithMap("{{.seq_string}}", func(_ int, v [2]string) string { return `"` + v[1] + `"` }, ", ")

Expand Down
Loading

0 comments on commit 25e02f6

Please sign in to comment.