From cbcf8f56a7ff13f65d47649d4a9fbec2114e499b Mon Sep 17 00:00:00 2001 From: Eugene Blikh Date: Mon, 1 Dec 2025 22:02:28 +0300 Subject: [PATCH] any: unify generation for Any type Refactored code generation logic to provide a unified approach for handling the Any type across the codebase. Also added logic to generate multiple EncodeDecode tests. --- any_gen.go | 50 ++----- any_gen_test.go | 260 ++++++++++++++++++++++++++++--------- cmd/generator/generator.go | 204 +++++++++++++++++++---------- 3 files changed, 352 insertions(+), 162 deletions(-) diff --git a/any_gen.go b/any_gen.go index 2d268ff..d4a858d 100644 --- a/any_gen.go +++ b/any_gen.go @@ -1,3 +1,5 @@ +// Code generated by github.com/tarantool/go-option; DO NOT EDIT. + package option import ( @@ -6,24 +8,16 @@ import ( ) // Any represents an optional value of type any. -// It can either hold a valid Any (any type like string or int, float). -// (IsSome == true) or be empty (IsZero == true). -type Any struct { //nolint:recvcheck +// It can either hold a valid any (IsSome == true) or be empty (IsZero == true). +type Any struct { value any exists bool } var _ commonInterface[any] = (*Any)(nil) -// SomeAny creates an optional Any with the given value. -// The returned Any (any type) will have IsSome() == true and IsZero() == false. -// -// Example: -// -// o := SomeAny(7777.7777777) -// if o.IsSome() { -// v := o.Unwrap() // v == true -// } +// SomeAny creates an optional Any with the given any value. +// The returned Any will have IsSome() == true and IsZero() == false. func SomeAny(value any) Any { return Any{ value: value, @@ -31,15 +25,8 @@ func SomeAny(value any) Any { } } -// NoneAny creates an empty optional any value. +// NoneAny creates an empty optional Any value. // The returned Any will have IsSome() == false and IsZero() == true. -// -// Example: -// -// o := NoneAny() -// if o.IsZero() { -// fmt.Println("value is absent") -// } func NoneAny() Any { return Any{ exists: false, @@ -69,7 +56,7 @@ func (o Any) IsNil() bool { // Get returns the stored value and a boolean flag indicating its presence. // If the value is present, returns (value, true). -// If the value is absent, returns (zero value of byte, false). +// If the value is absent, returns (zero value of any, false). // // Recommended usage: // @@ -88,14 +75,14 @@ func (o Any) Get() (any, bool) { // Panics with: "optional value is not set" if no value is set. func (o Any) MustGet() any { if !o.exists { - panic("optional value is not set!") + panic("optional value is not set") } return o.value } // Unwrap returns the stored value regardless of presence. -// If no value is set, returns the zero value for byte. +// If no value is set, returns the zero value for any. // // Warning: Does not check presence. Use IsSome() before calling if you need // to distinguish between absent value and explicit zero value. @@ -105,11 +92,6 @@ func (o Any) Unwrap() any { // UnwrapOr returns the stored value if present. // Otherwise, returns the provided default value. -// -// Example: -// -// o := NoneAny() -// v := o.UnwrapOr(someDefaultByte) func (o Any) UnwrapOr(defaultValue any) any { if o.exists { return o.value @@ -121,11 +103,6 @@ func (o Any) UnwrapOr(defaultValue any) any { // UnwrapOrElse returns the stored value if present. // Otherwise, calls the provided function and returns its result. // Useful when the default value requires computation or side effects. -// -// Example: -// -// o := NoneAny() -// v := o.UnwrapOrElse(func() any { return computeDefault() }) func (o Any) UnwrapOrElse(defaultValue func() any) any { if o.exists { return o.value @@ -135,7 +112,7 @@ func (o Any) UnwrapOrElse(defaultValue func() any) any { } // EncodeMsgpack encodes the Any value using MessagePack format. -// - If the value is present, it is encoded as byte. +// - If the value is present, it is encoded as any. // - If the value is absent (None), it is encoded as nil. // // Returns an error if encoding fails. @@ -149,8 +126,8 @@ func (o Any) EncodeMsgpack(encoder *msgpack.Encoder) error { // DecodeMsgpack decodes a Any value from MessagePack format. // Supports two input types: -// - nil: interpreted as no value (NoneByte) -// - byte: interpreted as a present value (SomeByte) +// - nil: interpreted as no value (NoneAny) +// - any: interpreted as a present value (SomeAny) // // Returns an error if the input type is unsupported or decoding fails. // @@ -173,7 +150,6 @@ func (o *Any) DecodeMsgpack(decoder *msgpack.Decoder) error { if err != nil { return newDecodeError("Any", err) } - o.exists = true return err diff --git a/any_gen_test.go b/any_gen_test.go index 9262e9f..6533f37 100644 --- a/any_gen_test.go +++ b/any_gen_test.go @@ -1,7 +1,10 @@ +// Code generated by github.com/tarantool/go-option; DO NOT EDIT. + package option_test import ( "bytes" + "fmt" "testing" "github.com/stretchr/testify/assert" @@ -17,7 +20,7 @@ func TestAny_IsSome(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someAny := option.SomeAny("aaaaaa+++++") + someAny := option.SomeAny("hello") assert.True(t, someAny.IsSome()) }) @@ -35,7 +38,7 @@ func TestAny_IsZero(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someAny := option.SomeAny(12232.777777777) + someAny := option.SomeAny("hello") assert.False(t, someAny.IsZero()) }) @@ -53,7 +56,7 @@ func TestAny_IsNil(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someAny := option.SomeAny(777.111111) + someAny := option.SomeAny("hello") assert.False(t, someAny.IsNil()) }) @@ -71,10 +74,10 @@ func TestAny_Get(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someAny := option.SomeAny("lllllllll") + someAny := option.SomeAny("hello") val, ok := someAny.Get() require.True(t, ok) - assert.EqualValues(t, "lllllllll", val) + assert.EqualValues(t, "hello", val) }) t.Run("none", func(t *testing.T) { @@ -92,15 +95,14 @@ func TestAny_MustGet(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someAny := option.SomeAny(1111.1000000) - assert.InEpsilon(t, 1111.1000000, someAny.MustGet(), 0.01) + someAny := option.SomeAny("hello") + assert.EqualValues(t, "hello", someAny.MustGet()) }) t.Run("none", func(t *testing.T) { t.Parallel() emptyAny := option.NoneAny() - assert.Panics(t, func() { emptyAny.MustGet() }) @@ -113,18 +115,14 @@ func TestAny_Unwrap(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someAny := option.SomeAny( - "HH77771111111111111111111111111111111111111111111111111111111111111111111111") - assert.EqualValues(t, - "HH77771111111111111111111111111111111111111111111111111111111111111111111111", - someAny.Unwrap()) + someAny := option.SomeAny("hello") + assert.EqualValues(t, "hello", someAny.Unwrap()) }) t.Run("none", func(t *testing.T) { t.Parallel() emptyAny := option.NoneAny() - assert.NotPanics(t, func() { emptyAny.Unwrap() }) @@ -137,15 +135,15 @@ func TestAny_UnwrapOr(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someAny := option.SomeAny("(((09,111") - assert.EqualValues(t, "(((09,111", someAny.UnwrapOr(111111)) + someAny := option.SomeAny("hello") + assert.EqualValues(t, "hello", someAny.UnwrapOr("bye")) }) t.Run("none", func(t *testing.T) { t.Parallel() emptyAny := option.NoneAny() - assert.InEpsilon(t, 11111.8880, emptyAny.UnwrapOr(11111.8880), 0.01) + assert.EqualValues(t, "bye", emptyAny.UnwrapOr("bye")) }) } @@ -155,9 +153,9 @@ func TestAny_UnwrapOrElse(t *testing.T) { t.Run("some", func(t *testing.T) { t.Parallel() - someAny := option.SomeAny(34534534) - assert.EqualValues(t, 34534534, someAny.UnwrapOrElse(func() any { - return "EXAMPLE!!!" + someAny := option.SomeAny("hello") + assert.EqualValues(t, "hello", someAny.UnwrapOrElse(func() any { + return "bye" })) }) @@ -165,8 +163,8 @@ func TestAny_UnwrapOrElse(t *testing.T) { t.Parallel() emptyAny := option.NoneAny() - assert.EqualValues(t, 145, emptyAny.UnwrapOrElse(func() any { - return 145 + assert.EqualValues(t, "bye", emptyAny.UnwrapOrElse(func() any { + return "bye" })) }) } @@ -174,42 +172,81 @@ func TestAny_UnwrapOrElse(t *testing.T) { func TestAny_EncodeDecodeMsgpack(t *testing.T) { t.Parallel() - testCases := []struct { - name string - value any - expected any - }{ - {"string", "test string", "test string"}, - {"int", 42, int64(42)}, - {"float", 3.14, 3.14}, - {"bool", true, true}, - {"slice", []int{1, 2, 3}, []any{int8(1), int8(2), int8(3)}}, - {"map", map[string]int{"a": 1}, map[string]any{"a": int8(1)}}, - } + t.Run("some", func(t *testing.T) { + t.Parallel() - for _, testCase := range testCases { - t.Run("some_"+testCase.name, func(t *testing.T) { - t.Parallel() + var buf bytes.Buffer - var buf bytes.Buffer + enc := msgpack.NewEncoder(&buf) + dec := msgpack.NewDecoder(&buf) - enc := msgpack.NewEncoder(&buf) - dec := msgpack.NewDecoder(&buf) + someAny := option.SomeAny("hello") + err := someAny.EncodeMsgpack(enc) + require.NoError(t, err) - // Encode. - someAny := option.SomeAny(testCase.value) - err := someAny.EncodeMsgpack(enc) - require.NoError(t, err) + var unmarshaled option.Any + err = unmarshaled.DecodeMsgpack(dec) + require.NoError(t, err) + assert.True(t, unmarshaled.IsSome()) + assert.EqualValues(t, "hello", unmarshaled.Unwrap()) + }) - // Decode. - var unmarshaled option.Any + t.Run("some_1", func(t *testing.T) { + t.Parallel() - err = unmarshaled.DecodeMsgpack(dec) - require.NoError(t, err) - assert.True(t, unmarshaled.IsSome()) - assert.Equal(t, testCase.expected, unmarshaled.Unwrap()) - }) - } + var buf bytes.Buffer + + enc := msgpack.NewEncoder(&buf) + dec := msgpack.NewDecoder(&buf) + + someAny := option.SomeAny(123) + err := someAny.EncodeMsgpack(enc) + require.NoError(t, err) + + var unmarshaled option.Any + err = unmarshaled.DecodeMsgpack(dec) + require.NoError(t, err) + assert.True(t, unmarshaled.IsSome()) + assert.EqualValues(t, 123, unmarshaled.Unwrap()) + }) + + t.Run("some_2", func(t *testing.T) { + t.Parallel() + + var buf bytes.Buffer + + enc := msgpack.NewEncoder(&buf) + dec := msgpack.NewDecoder(&buf) + + someAny := option.SomeAny(true) + err := someAny.EncodeMsgpack(enc) + require.NoError(t, err) + + var unmarshaled option.Any + err = unmarshaled.DecodeMsgpack(dec) + require.NoError(t, err) + assert.True(t, unmarshaled.IsSome()) + assert.EqualValues(t, true, unmarshaled.Unwrap()) + }) + + t.Run("some_3", func(t *testing.T) { + t.Parallel() + + var buf bytes.Buffer + + enc := msgpack.NewEncoder(&buf) + dec := msgpack.NewDecoder(&buf) + + someAny := option.SomeAny(123.456) + err := someAny.EncodeMsgpack(enc) + require.NoError(t, err) + + var unmarshaled option.Any + err = unmarshaled.DecodeMsgpack(dec) + require.NoError(t, err) + assert.True(t, unmarshaled.IsSome()) + assert.EqualValues(t, 123.456, unmarshaled.Unwrap()) + }) t.Run("none", func(t *testing.T) { t.Parallel() @@ -219,20 +256,125 @@ func TestAny_EncodeDecodeMsgpack(t *testing.T) { enc := msgpack.NewEncoder(&buf) dec := msgpack.NewDecoder(&buf) - // Encode nil. emptyAny := option.NoneAny() - err := emptyAny.EncodeMsgpack(enc) require.NoError(t, err) - // Decode. var unmarshaled option.Any - err = unmarshaled.DecodeMsgpack(dec) - require.NoError(t, err) - // Verify it's none. + require.NoError(t, err) assert.False(t, unmarshaled.IsSome()) - assert.Nil(t, unmarshaled.Unwrap()) }) } + +func ExampleSomeAny() { + opt := option.SomeAny("hello") + if opt.IsSome() { + fmt.Println(opt.Unwrap()) + } + // Output: hello +} + +func ExampleNoneAny() { + opt := option.NoneAny() + if opt.IsZero() { + fmt.Println("value is absent") + } + // Output: value is absent +} + +func ExampleAny_IsSome() { + some := option.SomeAny("hello") + none := option.NoneAny() + fmt.Println(some.IsSome()) + fmt.Println(none.IsSome()) + // Output: + // true + // false +} + +func ExampleAny_IsZero() { + some := option.SomeAny("hello") + none := option.NoneAny() + fmt.Println(some.IsZero()) + fmt.Println(none.IsZero()) + // Output: + // false + // true +} + +func ExampleAny_IsNil() { + some := option.SomeAny("hello") + none := option.NoneAny() + fmt.Println(some.IsNil() == some.IsZero()) + fmt.Println(none.IsNil() == none.IsZero()) + // Output: + // true + // true +} + +func ExampleAny_Get() { + some := option.SomeAny("hello") + none := option.NoneAny() + val, ok := some.Get() + fmt.Println(val, ok) + val, ok = none.Get() + fmt.Println(val, ok) + // Output: + // hello true + // false +} + +func ExampleAny_MustGet() { + some := option.SomeAny("hello") + fmt.Println(some.MustGet()) + // Output: hello +} + +func ExampleAny_MustGet_panic() { + none := option.NoneAny() + eof := false + defer func() { + if !eof { + fmt.Println("panic!", recover()) + } + }() + fmt.Println(none.MustGet()) + eof = true + // Output: panic! optional value is not set +} + +func ExampleAny_Unwrap() { + some := option.SomeAny("hello") + none := option.NoneAny() + fmt.Println(some.Unwrap()) + fmt.Println(none.Unwrap()) + // Output: + // hello + // +} + +func ExampleAny_UnwrapOr() { + some := option.SomeAny("hello") + none := option.NoneAny() + fmt.Println(some.UnwrapOr("bye")) + fmt.Println(none.UnwrapOr("bye")) + // Output: + // hello + // bye +} + +func ExampleAny_UnwrapOrElse() { + some := option.SomeAny("hello") + none := option.NoneAny() + fmt.Println(some.UnwrapOrElse(func() any { + return "bye" + })) + fmt.Println(none.UnwrapOrElse(func() any { + return "bye" + })) + // Output: + // hello + // bye +} diff --git a/cmd/generator/generator.go b/cmd/generator/generator.go index 1f287a1..b2c9762 100644 --- a/cmd/generator/generator.go +++ b/cmd/generator/generator.go @@ -1,4 +1,3 @@ -//nolint:exhaustruct package main import ( @@ -31,8 +30,9 @@ type generatorDef struct { EncoderFunc string CheckerFunc string - TestingValue string - TestingValueOutput string + TestingValues []string + TestingValueOutputs []string + ExampleValueOutputs []string UnexpectedTestingValue string UnexpectedTestingValueOutput string ZeroTestingValueOutput string @@ -41,6 +41,10 @@ type generatorDef struct { func structToMap(def generatorDef) map[string]any { caser := cases.Title(language.English) + // Using first value for test. + testingValue := def.TestingValues[0] + testingValueOutput := def.TestingValueOutputs[0] + out := map[string]any{ "Name": caser.String(def.Name), "Type": def.Name, @@ -48,21 +52,22 @@ func structToMap(def generatorDef) map[string]any { "EncoderFunc": def.EncoderFunc, "CheckerFunc": def.CheckerFunc, - "TestingValue": def.TestingValue, - "TestingValueOutput": def.TestingValue, + "TestingValue": testingValue, + "TestingValueOutput": testingValueOutput, "UnexpectedTestingValue": def.UnexpectedTestingValue, - "UnexpectedTestingValueOutput": def.UnexpectedTestingValue, + "UnexpectedTestingValueOutput": def.UnexpectedTestingValueOutput, "ZeroTestingValueOutput": def.ZeroTestingValueOutput, + "ExampleValueOutput": def.ExampleValueOutputs[0], + + // Adding arrays for EncodeDecodeMsgpack tests. + "TestingValues": def.TestingValues, + "TestingValueOutputs": def.TestingValueOutputs, } if def.Type != "" { out["Type"] = def.Type } - if def.TestingValueOutput != "" { - out["TestingValueOutput"] = def.TestingValueOutput - } - if def.UnexpectedTestingValueOutput != "" { out["UnexpectedTestingValueOutput"] = def.UnexpectedTestingValueOutput } @@ -84,9 +89,12 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeByte", CheckerFunc: "checkNumber", - TestingValue: "12", - UnexpectedTestingValue: "13", - ZeroTestingValueOutput: zeroOutput[byte](), + TestingValues: []string{"12"}, + TestingValueOutputs: []string{"12"}, + ExampleValueOutputs: []string{"12"}, + UnexpectedTestingValue: "13", + UnexpectedTestingValueOutput: "13", + ZeroTestingValueOutput: zeroOutput[byte](), }, { Name: "int", @@ -95,9 +103,12 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeInt", CheckerFunc: "checkNumber", - TestingValue: "12", - UnexpectedTestingValue: "13", - ZeroTestingValueOutput: zeroOutput[int](), + TestingValues: []string{"12"}, + TestingValueOutputs: []string{"12"}, + ExampleValueOutputs: []string{"12"}, + UnexpectedTestingValue: "13", + UnexpectedTestingValueOutput: "13", + ZeroTestingValueOutput: zeroOutput[int](), }, { Name: "int8", @@ -106,9 +117,12 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeInt8", CheckerFunc: "checkNumber", - TestingValue: "12", - UnexpectedTestingValue: "13", - ZeroTestingValueOutput: zeroOutput[int8](), + TestingValues: []string{"12"}, + TestingValueOutputs: []string{"12"}, + ExampleValueOutputs: []string{"12"}, + UnexpectedTestingValue: "13", + UnexpectedTestingValueOutput: "13", + ZeroTestingValueOutput: zeroOutput[int8](), }, { Name: "int16", @@ -117,9 +131,12 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeInt16", CheckerFunc: "checkNumber", - TestingValue: "12", - UnexpectedTestingValue: "13", - ZeroTestingValueOutput: zeroOutput[int16](), + TestingValues: []string{"12"}, + TestingValueOutputs: []string{"12"}, + ExampleValueOutputs: []string{"12"}, + UnexpectedTestingValue: "13", + UnexpectedTestingValueOutput: "13", + ZeroTestingValueOutput: zeroOutput[int16](), }, { Name: "int32", @@ -128,9 +145,12 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeInt32", CheckerFunc: "checkNumber", - TestingValue: "12", - UnexpectedTestingValue: "13", - ZeroTestingValueOutput: zeroOutput[int32](), + TestingValues: []string{"12"}, + TestingValueOutputs: []string{"12"}, + ExampleValueOutputs: []string{"12"}, + UnexpectedTestingValue: "13", + UnexpectedTestingValueOutput: "13", + ZeroTestingValueOutput: zeroOutput[int32](), }, { Name: "int64", @@ -139,9 +159,12 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeInt64", CheckerFunc: "checkNumber", - TestingValue: "12", - UnexpectedTestingValue: "13", - ZeroTestingValueOutput: zeroOutput[int64](), + TestingValues: []string{"12"}, + TestingValueOutputs: []string{"12"}, + ExampleValueOutputs: []string{"12"}, + UnexpectedTestingValue: "13", + UnexpectedTestingValueOutput: "13", + ZeroTestingValueOutput: zeroOutput[int64](), }, { Name: "uint", @@ -150,9 +173,12 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeUint", CheckerFunc: "checkNumber", - TestingValue: "12", - UnexpectedTestingValue: "13", - ZeroTestingValueOutput: zeroOutput[uint](), + TestingValues: []string{"12"}, + TestingValueOutputs: []string{"12"}, + ExampleValueOutputs: []string{"12"}, + UnexpectedTestingValue: "13", + UnexpectedTestingValueOutput: "13", + ZeroTestingValueOutput: zeroOutput[uint](), }, { Name: "uint8", @@ -161,9 +187,12 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeUint8", CheckerFunc: "checkNumber", - TestingValue: "12", - UnexpectedTestingValue: "13", - ZeroTestingValueOutput: zeroOutput[uint8](), + TestingValues: []string{"12"}, + TestingValueOutputs: []string{"12"}, + ExampleValueOutputs: []string{"12"}, + UnexpectedTestingValue: "13", + UnexpectedTestingValueOutput: "13", + ZeroTestingValueOutput: zeroOutput[uint8](), }, { Name: "uint16", @@ -172,9 +201,12 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeUint16", CheckerFunc: "checkNumber", - TestingValue: "12", - UnexpectedTestingValue: "13", - ZeroTestingValueOutput: zeroOutput[uint16](), + TestingValues: []string{"12"}, + TestingValueOutputs: []string{"12"}, + ExampleValueOutputs: []string{"12"}, + UnexpectedTestingValue: "13", + UnexpectedTestingValueOutput: "13", + ZeroTestingValueOutput: zeroOutput[uint16](), }, { Name: "uint32", @@ -183,9 +215,12 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeUint32", CheckerFunc: "checkNumber", - TestingValue: "12", - UnexpectedTestingValue: "13", - ZeroTestingValueOutput: zeroOutput[uint32](), + TestingValues: []string{"12"}, + TestingValueOutputs: []string{"12"}, + ExampleValueOutputs: []string{"12"}, + UnexpectedTestingValue: "13", + UnexpectedTestingValueOutput: "13", + ZeroTestingValueOutput: zeroOutput[uint32](), }, { Name: "uint64", @@ -194,9 +229,12 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeUint64", CheckerFunc: "checkNumber", - TestingValue: "12", - UnexpectedTestingValue: "13", - ZeroTestingValueOutput: zeroOutput[uint64](), + TestingValues: []string{"12"}, + TestingValueOutputs: []string{"12"}, + ExampleValueOutputs: []string{"12"}, + UnexpectedTestingValue: "13", + UnexpectedTestingValueOutput: "13", + ZeroTestingValueOutput: zeroOutput[uint64](), }, { Name: "float32", @@ -205,9 +243,12 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeFloat32", CheckerFunc: "checkFloat", - TestingValue: "12", - UnexpectedTestingValue: "13", - ZeroTestingValueOutput: zeroOutput[float32](), + TestingValues: []string{"12"}, + TestingValueOutputs: []string{"12"}, + ExampleValueOutputs: []string{"12"}, + UnexpectedTestingValue: "13", + UnexpectedTestingValueOutput: "13", + ZeroTestingValueOutput: zeroOutput[float32](), }, { Name: "float64", @@ -216,9 +257,12 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeFloat64", CheckerFunc: "checkFloat", - TestingValue: "12", - UnexpectedTestingValue: "13", - ZeroTestingValueOutput: zeroOutput[float64](), + TestingValues: []string{"12"}, + TestingValueOutputs: []string{"12"}, + ExampleValueOutputs: []string{"12"}, + UnexpectedTestingValue: "13", + UnexpectedTestingValueOutput: "13", + ZeroTestingValueOutput: zeroOutput[float64](), }, { Name: "string", @@ -227,8 +271,9 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeString", CheckerFunc: "checkString", - TestingValue: "\"hello\"", - TestingValueOutput: "hello", + TestingValues: []string{"\"hello\""}, + TestingValueOutputs: []string{"\"hello\""}, + ExampleValueOutputs: []string{"hello"}, UnexpectedTestingValue: "\"bye\"", UnexpectedTestingValueOutput: "bye", ZeroTestingValueOutput: zeroOutput[string](), @@ -240,8 +285,9 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeBytes", CheckerFunc: "checkBytes", - TestingValue: "[]byte{3, 14, 15}", - TestingValueOutput: "[3 14 15]", + TestingValues: []string{"[]byte{3, 14, 15}"}, + TestingValueOutputs: []string{"[]byte{3, 14, 15}"}, + ExampleValueOutputs: []string{"[3 14 15]"}, UnexpectedTestingValue: "[]byte{3, 14, 15, 9, 26}", UnexpectedTestingValueOutput: "[3 14 15 9 26]", ZeroTestingValueOutput: zeroOutput[[]byte](), @@ -253,9 +299,26 @@ var defaultTypes = []generatorDef{ EncoderFunc: "encodeBool", CheckerFunc: "checkBool", - TestingValue: "true", - UnexpectedTestingValue: "false", - ZeroTestingValueOutput: zeroOutput[bool](), + TestingValues: []string{"true"}, + TestingValueOutputs: []string{"true"}, + ExampleValueOutputs: []string{"true"}, + UnexpectedTestingValue: "false", + UnexpectedTestingValueOutput: "false", + ZeroTestingValueOutput: zeroOutput[bool](), + }, + { + Name: "any", + Type: "any", + DecodeFunc: "decodeAny", + EncoderFunc: "encodeAny", + CheckerFunc: "checkAny", + + TestingValues: []string{"\"hello\"", "123", "true", "123.456"}, + TestingValueOutputs: []string{"\"hello\"", "123", "true", "123.456"}, + ExampleValueOutputs: []string{"hello", "123", "true", "123.456"}, + UnexpectedTestingValue: "\"bye\"", + UnexpectedTestingValueOutput: "bye", + ZeroTestingValueOutput: zeroOutput[any](), }, } @@ -603,7 +666,14 @@ func Test{{.Name}}_UnwrapOrElse(t *testing.T) { func Test{{.Name}}_EncodeDecodeMsgpack(t *testing.T) { t.Parallel() + {{ range $i, $value := .TestingValues }} + + {{ if (eq $i 0) }} t.Run("some", func(t *testing.T) { + {{ else }} + t.Run("some_{{ $i }}", func(t *testing.T) { + {{ end -}} + t.Parallel() var buf bytes.Buffer @@ -611,16 +681,18 @@ func Test{{.Name}}_EncodeDecodeMsgpack(t *testing.T) { enc := msgpack.NewEncoder(&buf) dec := msgpack.NewDecoder(&buf) - some{{.Name}} := option.Some{{.Name}}({{.TestingValue}}) - err := some{{.Name}}.EncodeMsgpack(enc) + some{{$.Name}} := option.Some{{$.Name}}({{ $value }}) + err := some{{$.Name}}.EncodeMsgpack(enc) require.NoError(t, err) - var unmarshaled option.{{.Name}} + var unmarshaled option.{{$.Name}} err = unmarshaled.DecodeMsgpack(dec) require.NoError(t, err) assert.True(t, unmarshaled.IsSome()) - assert.EqualValues(t, {{.TestingValue}}, unmarshaled.Unwrap()) + {{- $output := index $.TestingValueOutputs $i }} + assert.EqualValues(t, {{ $output }}, unmarshaled.Unwrap()) }) + {{ end }} t.Run("none", func(t *testing.T) { t.Parallel() @@ -647,7 +719,7 @@ func ExampleSome{{.Name}}() { if opt.IsSome() { fmt.Println(opt.Unwrap()) } - // Output: {{.TestingValueOutput}} + // Output: {{.ExampleValueOutput}} } func ExampleNone{{.Name}}() { @@ -696,14 +768,14 @@ func Example{{.Name}}_Get() { val, ok = none.Get() fmt.Println(val, ok) // Output: - // {{.TestingValueOutput}} true + // {{.ExampleValueOutput}} true // {{.ZeroTestingValueOutput}} false } func Example{{.Name}}_MustGet() { some := option.Some{{.Name}}({{.TestingValue}}) fmt.Println(some.MustGet()) - // Output: {{.TestingValueOutput}} + // Output: {{.ExampleValueOutput}} } func Example{{.Name}}_MustGet_panic() { @@ -725,7 +797,7 @@ func Example{{.Name}}_Unwrap() { fmt.Println(some.Unwrap()) fmt.Println(none.Unwrap()) // Output: - // {{.TestingValueOutput}} + // {{.ExampleValueOutput}} // {{.ZeroTestingValueOutput}} } @@ -735,7 +807,7 @@ func Example{{.Name}}_UnwrapOr() { fmt.Println(some.UnwrapOr({{.UnexpectedTestingValue}})) fmt.Println(none.UnwrapOr({{.UnexpectedTestingValue}})) // Output: - // {{.TestingValueOutput}} + // {{.ExampleValueOutput}} // {{.UnexpectedTestingValueOutput}} } @@ -749,7 +821,7 @@ func Example{{.Name}}_UnwrapOrElse() { return {{.UnexpectedTestingValue}} })) // Output: - // {{.TestingValueOutput}} + // {{.ExampleValueOutput}} // {{.UnexpectedTestingValueOutput}} } `