Skip to content

Commit

Permalink
Add type-scope tag directive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
very-amused committed May 20, 2024
1 parent d87ac41 commit 7a1a65a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 6 additions & 0 deletions _generated/custom_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ package _generated

//go:generate msgp
//msgp:tag mytag
//msgp:tag anothertag CustomTag2

type CustomTag struct {
Foo string `mytag:"foo_custom_name"`
Bar int `mytag:"bar1234"`
}

type CustomTag2 struct {
Foo string `anothertag:"foo_msgp_name" mytag:"foo_legacy_name"`
Bar int `anothertag:"bar5678" mytag:"not_used"`
}
17 changes: 15 additions & 2 deletions _generated/custom_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ func TestCustomTag(t *testing.T) {
ts := CustomTag{
Foo: "foostring13579",
Bar: 999_999}
encDecCustomTag(t, ts, "mytag")
encDecCustomTag(t, ts, "mytag", false)
})
t.Run("Type Scope", func(t *testing.T) {
ts := CustomTag2{
Foo: "foostring246810",
Bar: 777_777}
encDecCustomTag(t, ts, "anothertag", false)
encDecCustomTag(t, ts, "mytag", true)
})
}

func encDecCustomTag(t *testing.T, testStruct msgp.Encodable, tag string) {
func encDecCustomTag(t *testing.T, testStruct msgp.Encodable, tag string, expectError bool) {
var b bytes.Buffer
msgp.Encode(&b, testStruct)

Expand All @@ -46,6 +53,12 @@ func encDecCustomTag(t *testing.T, testStruct msgp.Encodable, tag string) {
// Check encoded field name
field := tsType.Field(i)
encodedValue, ok := encoded[field.Tag.Get(tag)]
if expectError {
if ok {
t.Error("unexpected encoded field", field.Name)
}
continue
}
if !ok {
t.Error("missing encoded value for field", field.Name)
continue
Expand Down

0 comments on commit 7a1a65a

Please sign in to comment.