Skip to content

Commit 7a1a65a

Browse files
committed
Add type-scope tag directive tests
1 parent d87ac41 commit 7a1a65a

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

_generated/custom_tag.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ package _generated
22

33
//go:generate msgp
44
//msgp:tag mytag
5+
//msgp:tag anothertag CustomTag2
56

67
type CustomTag struct {
78
Foo string `mytag:"foo_custom_name"`
89
Bar int `mytag:"bar1234"`
910
}
11+
12+
type CustomTag2 struct {
13+
Foo string `anothertag:"foo_msgp_name" mytag:"foo_legacy_name"`
14+
Bar int `anothertag:"bar5678" mytag:"not_used"`
15+
}

_generated/custom_tag_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ func TestCustomTag(t *testing.T) {
1616
ts := CustomTag{
1717
Foo: "foostring13579",
1818
Bar: 999_999}
19-
encDecCustomTag(t, ts, "mytag")
19+
encDecCustomTag(t, ts, "mytag", false)
20+
})
21+
t.Run("Type Scope", func(t *testing.T) {
22+
ts := CustomTag2{
23+
Foo: "foostring246810",
24+
Bar: 777_777}
25+
encDecCustomTag(t, ts, "anothertag", false)
26+
encDecCustomTag(t, ts, "mytag", true)
2027
})
2128
}
2229

23-
func encDecCustomTag(t *testing.T, testStruct msgp.Encodable, tag string) {
30+
func encDecCustomTag(t *testing.T, testStruct msgp.Encodable, tag string, expectError bool) {
2431
var b bytes.Buffer
2532
msgp.Encode(&b, testStruct)
2633

@@ -46,6 +53,12 @@ func encDecCustomTag(t *testing.T, testStruct msgp.Encodable, tag string) {
4653
// Check encoded field name
4754
field := tsType.Field(i)
4855
encodedValue, ok := encoded[field.Tag.Get(tag)]
56+
if expectError {
57+
if ok {
58+
t.Error("unexpected encoded field", field.Name)
59+
}
60+
continue
61+
}
4962
if !ok {
5063
t.Error("missing encoded value for field", field.Name)
5164
continue

0 commit comments

Comments
 (0)