|
| 1 | +package main |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestIssue275Tuples(t *testing.T) { |
| 6 | + mainFilename, err := generate(t, issue275Tuples) |
| 7 | + if err != nil { |
| 8 | + t.Fatalf("generate failed: %v", err) |
| 9 | + } |
| 10 | + goExec(t, mainFilename, false) // exec go run |
| 11 | + goExec(t, mainFilename, true) // exec go test |
| 12 | +} |
| 13 | + |
| 14 | +var issue275Tuples = `package main |
| 15 | +
|
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "os" |
| 19 | +) |
| 20 | +
|
| 21 | +//go:generate msgp |
| 22 | +
|
| 23 | +//msgp:tuple Test1 |
| 24 | +type Test1 struct { |
| 25 | + Foo string |
| 26 | +} |
| 27 | +
|
| 28 | +//msgp:tuple Test2 |
| 29 | +type Test2 struct { |
| 30 | + Foo string |
| 31 | + Bar string |
| 32 | +} |
| 33 | +
|
| 34 | +//msgp:tuple Test3 |
| 35 | +type Test3 struct { |
| 36 | + Foo string |
| 37 | + Bar string |
| 38 | + Baz string |
| 39 | +} |
| 40 | +
|
| 41 | +//msgp:vartuple Test |
| 42 | +type Test struct { |
| 43 | + Foo string |
| 44 | + Bar string |
| 45 | +} |
| 46 | +
|
| 47 | +func main() { |
| 48 | + t1 := Test1{Foo: "Foo1"} |
| 49 | + d1, err := t1.MarshalMsg(nil) |
| 50 | + if err != nil { |
| 51 | + fmt.Println("Test1 MarshalMsg failed:", err) |
| 52 | + os.Exit(1) |
| 53 | + } |
| 54 | +
|
| 55 | + t2 := Test2{Foo: "Foo2", Bar: "Bar2"} |
| 56 | + d2, err := t2.MarshalMsg(nil) |
| 57 | + if err != nil { |
| 58 | + fmt.Println("Test2 MarshalMsg failed:", err) |
| 59 | + os.Exit(1) |
| 60 | + } |
| 61 | +
|
| 62 | + t3 := Test3{Foo: "Foo3", Bar: "Bar3", Baz: "Baz3"} |
| 63 | + d3, err := t3.MarshalMsg(nil) |
| 64 | + if err != nil { |
| 65 | + fmt.Println("Test3 MarshalMsg failed:", err) |
| 66 | + os.Exit(1) |
| 67 | + } |
| 68 | +
|
| 69 | + var msg Test |
| 70 | +
|
| 71 | + msg = Test{} |
| 72 | + if _, err := msg.UnmarshalMsg(d1); err != nil { |
| 73 | + fmt.Println("Test UnmarshalMsg from Test1 failed:", err) |
| 74 | + os.Exit(1) |
| 75 | + } |
| 76 | + if msg.Foo != "Foo1" { |
| 77 | + fmt.Println("Test UnmarshalMsg from Test1 bad msg:", msg) |
| 78 | + os.Exit(1) |
| 79 | + } |
| 80 | +
|
| 81 | + msg = Test{} |
| 82 | + if _, err := msg.UnmarshalMsg(d2); err != nil { |
| 83 | + fmt.Println("Test UnmarshalMsg from Test2 failed:", err) |
| 84 | + os.Exit(1) |
| 85 | + } |
| 86 | + if msg.Foo != "Foo2" || msg.Bar != "Bar2" { |
| 87 | + fmt.Println("Test UnmarshalMsg from Test2 bad msg:", msg) |
| 88 | + os.Exit(1) |
| 89 | + } |
| 90 | +
|
| 91 | + msg = Test{} |
| 92 | + if _, err := msg.UnmarshalMsg(d3); err != nil { |
| 93 | + fmt.Println("Test UnmarshalMsg from Test3 failed:", err) |
| 94 | + os.Exit(1) |
| 95 | + } |
| 96 | + if msg.Foo != "Foo3" || msg.Bar != "Bar3" { |
| 97 | + fmt.Println("Test UnmarshalMsg from Test3 bad msg:", msg) |
| 98 | + os.Exit(1) |
| 99 | + } |
| 100 | +
|
| 101 | + var msg1 Test1 |
| 102 | + if _, err := msg1.UnmarshalMsg(d2); err != nil { |
| 103 | + if err.Error() != "msgp: wanted array of size 1; got 2" { |
| 104 | + fmt.Println("Test1 UnmarshalMsg from Test2 failed:", err) |
| 105 | + os.Exit(1) |
| 106 | + } |
| 107 | + } |
| 108 | +
|
| 109 | + var msg2 Test2 |
| 110 | + if _, err := msg2.UnmarshalMsg(d2); err != nil { |
| 111 | + fmt.Println("Test2 UnmarshalMsg from Test2 failed:", err) |
| 112 | + os.Exit(1) |
| 113 | + } |
| 114 | + if msg2.Foo != "Foo2" || msg2.Bar != "Bar2" { |
| 115 | + fmt.Println("Test3 UnmarshalMsg from Test2 bad msg:", msg) |
| 116 | + os.Exit(1) |
| 117 | + } |
| 118 | +
|
| 119 | + var msg3 Test3 |
| 120 | + if _, err := msg3.UnmarshalMsg(d2); err != nil { |
| 121 | + if err.Error() != "msgp: wanted array of size 3; got 2" { |
| 122 | + fmt.Println("Test3 UnmarshalMsg from Test2 failed:", err) |
| 123 | + os.Exit(1) |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | +` |
0 commit comments