Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: mojom/generators/go/templates/unions_test.go

Issue 2045063002: [New go generator] Implement declaring unions. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojom/generators/go/templates/unions.go ('k') | mojom/generators/go/translator/mojom_file.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package templates
6
7 import (
8 "testing"
9
10 "mojom/generators/go/translator"
11 )
12
13 func TestUnionInterfaceDecl(t *testing.T) {
14 expected := `type SomeUnion interface {
15 Tag() uint32
16 Interface() interface{}
17 __Reflect(__SomeUnionReflect)
18 Encode(encoder *bindings.Encoder) error
19 }
20
21 type __SomeUnionReflect struct {
22 Alpha string
23 Beta uint32
24 }`
25
26 union := translator.UnionTemplate{
27 Name: "SomeUnion",
28 Fields: []translator.UnionFieldTemplate{
29 {Name: "Alpha", Type: "string"},
30 {Name: "Beta", Type: "uint32"},
31 },
32 }
33
34 check(t, expected, "UnionInterfaceDecl", union)
35 }
36
37 func TestUnionFieldDecl(t *testing.T) {
38 expected := `type SomeUnionAlpha struct{ Value string }
39
40 func (u *SomeUnionAlpha) Tag() uint32 { return 5 }
41 func (u *SomeUnionAlpha) Interface() interface{} { return u.Value }
42 func (u *SomeUnionAlpha) __Reflect(__SomeUnionReflect) {}`
43
44 union := translator.UnionTemplate{Name: "SomeUnion"}
45
46 field := translator.UnionFieldTemplate{
47 Name: "Alpha",
48 Type: "string",
49 Tag: 5,
50 Union: &union,
51 }
52
53 check(t, expected, "UnionFieldDecl", field)
54 }
55
56 func TestUnionFieldEncode(t *testing.T) {
57 expected := `func (u *SomeUnionSomeField) Encode(encoder *bindings.Encod er) error {
58 encoder.WriteUnionHeader(u.Tag())
59 if err := encoder.WriteUint32(u.Value); err != nil {
60 return err
61 }
62
63 encoder.FinishWritingUnionValue()
64 return nil
65 }`
66
67 union := translator.UnionTemplate{Name: "SomeUnion"}
68
69 info := mockEncodingInfo{
70 isSimple: true,
71 identifier: "u.Value",
72 writeFunction: "WriteUint32",
73 }
74
75 field := translator.UnionFieldTemplate{
76 Name: "SomeField",
77 Type: "string",
78 Tag: 5,
79 Union: &union,
80 EncodingInfo: info,
81 }
82
83 check(t, expected, "UnionFieldEncode", field)
84 }
85
86 func TestUnionFieldDecode(t *testing.T) {
87 expected := `func (u *SomeUnionSomeField) decodeInternal(decoder *bindin gs.Decoder) error {
88 value, err := decoder.ReadUint32()
89 if err != nil {
90 return err
91 }
92 u.Value = value
93
94 return nil
95 }`
96
97 union := translator.UnionTemplate{Name: "SomeUnion"}
98
99 info := mockEncodingInfo{
100 isSimple: true,
101 identifier: "u.Value",
102 writeFunction: "WriteUint32",
103 readFunction: "ReadUint32",
104 }
105
106 field := translator.UnionFieldTemplate{
107 Name: "SomeField",
108 Type: "string",
109 Tag: 5,
110 Union: &union,
111 EncodingInfo: info,
112 }
113
114 check(t, expected, "UnionFieldDecode", field)
115 }
116
117 func TestUnionDecode(t *testing.T) {
118 expected := `func DecodeSomeUnion(decoder *bindings.Decoder) (SomeUnion, error) {
119 size, tag, err := decoder.ReadUnionHeader()
120 if err != nil {
121 return nil, err
122 }
123
124 if size == 0 {
125 decoder.SkipUnionValue()
126 return nil, nil
127 }
128
129 switch tag {
130 case 0:
131 var value SomeUnionField1
132 if err := value.decodeInternal(decoder); err != nil {
133 return nil, err
134 }
135 decoder.FinishReadingUnionValue()
136 return &value, nil
137 case 1:
138 var value SomeUnionField2
139 if err := value.decodeInternal(decoder); err != nil {
140 return nil, err
141 }
142 decoder.FinishReadingUnionValue()
143 return &value, nil
144 }
145
146 decoder.SkipUnionValue()
147 return &SomeUnionUnknown{tag: tag}, nil
148 }`
149
150 field1 := translator.UnionFieldTemplate{
151 Name: "Field1",
152 Tag: 0,
153 }
154
155 field2 := translator.UnionFieldTemplate{
156 Name: "Field2",
157 Tag: 1,
158 }
159
160 union := translator.UnionTemplate{
161 Name: "SomeUnion",
162 Fields: []translator.UnionFieldTemplate{
163 field1,
164 field2,
165 },
166 }
167
168 check(t, expected, "UnionDecode", union)
169 }
OLDNEW
« no previous file with comments | « mojom/generators/go/templates/unions.go ('k') | mojom/generators/go/translator/mojom_file.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698