| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package templates | 5 package templates |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "testing" | 8 "testing" |
| 9 ) | 9 ) |
| 10 | 10 |
| 11 func TestDecodingStructVersions(t *testing.T) { | |
| 12 expected := `var someStruct_Versions []bindings.DataHeader = []bindings.
DataHeader{ | |
| 13 bindings.DataHeader{80, 0}, | |
| 14 bindings.DataHeader{100, 1}, | |
| 15 bindings.DataHeader{120, 2}, | |
| 16 }` | |
| 17 | |
| 18 type structVersion struct { | |
| 19 NumBytes uint32 | |
| 20 Version uint32 | |
| 21 } | |
| 22 | |
| 23 s := struct { | |
| 24 PrivateName string | |
| 25 Versions []structVersion | |
| 26 }{ | |
| 27 PrivateName: "someStruct", | |
| 28 Versions: []structVersion{ | |
| 29 {80, 0}, | |
| 30 {100, 1}, | |
| 31 {120, 2}, | |
| 32 }, | |
| 33 } | |
| 34 | |
| 35 check(t, expected, "StructVersions", s) | |
| 36 } | |
| 37 | |
| 38 func TestDecodingSimpleFieldDecoding(t *testing.T) { | 11 func TestDecodingSimpleFieldDecoding(t *testing.T) { |
| 39 expected := `value, err := decoder.ReadUint8() | 12 expected := `value, err := decoder.ReadUint8() |
| 40 if err != nil { | 13 if err != nil { |
| 41 return err | 14 return err |
| 42 } | 15 } |
| 43 s.Fuint8 = value` | 16 s.Fuint8 = value` |
| 44 | 17 |
| 45 encodingInfo := mockEncodingInfo{ | 18 encodingInfo := mockEncodingInfo{ |
| 46 » » IsSimple: true, | 19 » » isSimple: true, |
| 47 » » Identifier: "s.Fuint8", | 20 » » identifier: "s.Fuint8", |
| 48 » » ReadFunction: "ReadUint8", | 21 » » readFunction: "ReadUint8", |
| 49 } | 22 } |
| 50 | 23 |
| 51 check(t, expected, "FieldDecodingTmpl", encodingInfo) | 24 check(t, expected, "FieldDecodingTmpl", encodingInfo) |
| 52 } | 25 } |
| 53 | 26 |
| 54 func TestDecodingHandleFieldDecoding(t *testing.T) { | 27 func TestDecodingHandleFieldDecoding(t *testing.T) { |
| 55 expected := `handle, err := decoder.ReadHandle() | 28 expected := `handle, err := decoder.ReadHandle() |
| 56 if err != nil { | 29 if err != nil { |
| 57 return err | 30 return err |
| 58 } | 31 } |
| 59 if handle.IsValid() { | 32 if handle.IsValid() { |
| 60 s.SomeHandle = handle | 33 s.SomeHandle = handle |
| 61 } else { | 34 } else { |
| 62 return &bindings.ValidationError{bindings.UnexpectedInvalidHandle, "unex
pected invalid handle"} | 35 return &bindings.ValidationError{bindings.UnexpectedInvalidHandle, "unex
pected invalid handle"} |
| 63 }` | 36 }` |
| 64 | 37 |
| 65 encodingInfo := mockEncodingInfo{ | 38 encodingInfo := mockEncodingInfo{ |
| 66 » » IsHandle: true, | 39 » » isHandle: true, |
| 67 » » Identifier: "s.SomeHandle", | 40 » » identifier: "s.SomeHandle", |
| 68 » » ReadFunction: "ReadHandle", | 41 » » readFunction: "ReadHandle", |
| 69 } | 42 } |
| 70 | 43 |
| 71 check(t, expected, "FieldDecodingTmpl", encodingInfo) | 44 check(t, expected, "FieldDecodingTmpl", encodingInfo) |
| 72 } | 45 } |
| 73 | 46 |
| 74 func TestDecodingNullableHandleFieldDecoding(t *testing.T) { | 47 func TestDecodingNullableHandleFieldDecoding(t *testing.T) { |
| 75 expected := `handle, err := decoder.ReadHandle() | 48 expected := `handle, err := decoder.ReadHandle() |
| 76 if err != nil { | 49 if err != nil { |
| 77 return err | 50 return err |
| 78 } | 51 } |
| 79 if handle.IsValid() { | 52 if handle.IsValid() { |
| 80 s.SomeNullableHandle = &handle | 53 s.SomeNullableHandle = &handle |
| 81 } else { | 54 } else { |
| 82 s.SomeNullableHandle = nil | 55 s.SomeNullableHandle = nil |
| 83 }` | 56 }` |
| 84 | 57 |
| 85 encodingInfo := mockEncodingInfo{ | 58 encodingInfo := mockEncodingInfo{ |
| 86 » » IsHandle: true, | 59 » » isHandle: true, |
| 87 » » Identifier: "s.SomeNullableHandle", | 60 » » identifier: "s.SomeNullableHandle", |
| 88 » » ReadFunction: "ReadHandle", | 61 » » readFunction: "ReadHandle", |
| 89 » » IsNullable: true, | 62 » » isNullable: true, |
| 90 } | 63 } |
| 91 | 64 |
| 92 check(t, expected, "FieldDecodingTmpl", encodingInfo) | 65 check(t, expected, "FieldDecodingTmpl", encodingInfo) |
| 93 } | 66 } |
| 94 | 67 |
| 95 func TestDecodingStringFieldDecoding(t *testing.T) { | 68 func TestDecodingStringFieldDecoding(t *testing.T) { |
| 96 expected := `pointer, err := decoder.ReadPointer() | 69 expected := `pointer, err := decoder.ReadPointer() |
| 97 if err != nil { | 70 if err != nil { |
| 98 return err | 71 return err |
| 99 } | 72 } |
| 100 if pointer == 0 { | 73 if pointer == 0 { |
| 101 return &bindings.ValidationError{bindings.UnexpectedNullPointer, "unexpe
cted null pointer"} | 74 return &bindings.ValidationError{bindings.UnexpectedNullPointer, "unexpe
cted null pointer"} |
| 102 } else { | 75 } else { |
| 103 value, err := decoder.ReadString() | 76 value, err := decoder.ReadString() |
| 104 if err != nil { | 77 if err != nil { |
| 105 return err | 78 return err |
| 106 } | 79 } |
| 107 s.FString = value | 80 s.FString = value |
| 108 }` | 81 }` |
| 109 | 82 |
| 110 encodingInfo := mockEncodingInfo{ | 83 encodingInfo := mockEncodingInfo{ |
| 111 » » IsSimple: true, | 84 » » isSimple: true, |
| 112 » » Identifier: "s.FString", | 85 » » identifier: "s.FString", |
| 113 » » ReadFunction: "ReadString", | 86 » » readFunction: "ReadString", |
| 114 » » IsPointer: true, | 87 » » isPointer: true, |
| 115 } | 88 } |
| 116 | 89 |
| 117 check(t, expected, "FieldDecodingTmpl", encodingInfo) | 90 check(t, expected, "FieldDecodingTmpl", encodingInfo) |
| 118 } | 91 } |
| 119 | 92 |
| 120 func TestDecodingNullableStringFieldDecoding(t *testing.T) { | 93 func TestDecodingNullableStringFieldDecoding(t *testing.T) { |
| 121 expected := `pointer, err := decoder.ReadPointer() | 94 expected := `pointer, err := decoder.ReadPointer() |
| 122 if err != nil { | 95 if err != nil { |
| 123 return err | 96 return err |
| 124 } | 97 } |
| 125 if pointer == 0 { | 98 if pointer == 0 { |
| 126 s.FString = nil | 99 s.FString = nil |
| 127 } else { | 100 } else { |
| 128 value, err := decoder.ReadString() | 101 value, err := decoder.ReadString() |
| 129 if err != nil { | 102 if err != nil { |
| 130 return err | 103 return err |
| 131 } | 104 } |
| 132 s.FString = value | 105 s.FString = value |
| 133 }` | 106 }` |
| 134 | 107 |
| 135 encodingInfo := mockEncodingInfo{ | 108 encodingInfo := mockEncodingInfo{ |
| 136 » » IsSimple: true, | 109 » » isSimple: true, |
| 137 » » Identifier: "s.FString", | 110 » » identifier: "s.FString", |
| 138 » » ReadFunction: "ReadString", | 111 » » readFunction: "ReadString", |
| 139 » » IsPointer: true, | 112 » » isPointer: true, |
| 140 » » IsNullable: true, | 113 » » isNullable: true, |
| 141 } | 114 } |
| 142 | 115 |
| 143 check(t, expected, "FieldDecodingTmpl", encodingInfo) | 116 check(t, expected, "FieldDecodingTmpl", encodingInfo) |
| 144 } | 117 } |
| 145 | 118 |
| 146 func TestDecodingStructFieldDecoding(t *testing.T) { | 119 func TestDecodingStructFieldDecoding(t *testing.T) { |
| 147 expected := `pointer, err := decoder.ReadPointer() | 120 expected := `pointer, err := decoder.ReadPointer() |
| 148 if err != nil { | 121 if err != nil { |
| 149 return err | 122 return err |
| 150 } | 123 } |
| 151 if pointer == 0 { | 124 if pointer == 0 { |
| 152 return &bindings.ValidationError{bindings.UnexpectedNullPointer, "unexpe
cted null pointer"} | 125 return &bindings.ValidationError{bindings.UnexpectedNullPointer, "unexpe
cted null pointer"} |
| 153 } else { | 126 } else { |
| 154 if err := s.FStruct.Decode(decoder); err != nil { | 127 if err := s.FStruct.Decode(decoder); err != nil { |
| 155 return err | 128 return err |
| 156 } | 129 } |
| 157 }` | 130 }` |
| 158 | 131 |
| 159 encodingInfo := mockEncodingInfo{ | 132 encodingInfo := mockEncodingInfo{ |
| 160 » » IsPointer: true, | 133 » » isPointer: true, |
| 161 » » IsStruct: true, | 134 » » isStruct: true, |
| 162 » » Identifier: "s.FStruct", | 135 » » identifier: "s.FStruct", |
| 163 } | 136 } |
| 164 | 137 |
| 165 check(t, expected, "FieldDecodingTmpl", encodingInfo) | 138 check(t, expected, "FieldDecodingTmpl", encodingInfo) |
| 166 } | 139 } |
| 167 | 140 |
| 168 func TestDecodingNullableStructFieldDecoding(t *testing.T) { | 141 func TestDecodingNullableStructFieldDecoding(t *testing.T) { |
| 169 expected := `pointer, err := decoder.ReadPointer() | 142 expected := `pointer, err := decoder.ReadPointer() |
| 170 if err != nil { | 143 if err != nil { |
| 171 return err | 144 return err |
| 172 } | 145 } |
| 173 if pointer == 0 { | 146 if pointer == 0 { |
| 174 s.FStruct = nil | 147 s.FStruct = nil |
| 175 } else { | 148 } else { |
| 176 s.FStruct = new(SomeStruct) | 149 s.FStruct = new(SomeStruct) |
| 177 if err := s.FStruct.Decode(decoder); err != nil { | 150 if err := s.FStruct.Decode(decoder); err != nil { |
| 178 return err | 151 return err |
| 179 } | 152 } |
| 180 }` | 153 }` |
| 181 | 154 |
| 182 encodingInfo := mockEncodingInfo{ | 155 encodingInfo := mockEncodingInfo{ |
| 183 » » IsNullable: true, | 156 » » isNullable: true, |
| 184 » » IsPointer: true, | 157 » » isPointer: true, |
| 185 » » IsStruct: true, | 158 » » isStruct: true, |
| 186 » » Identifier: "s.FStruct", | 159 » » identifier: "s.FStruct", |
| 187 » » GoType: "SomeStruct", | 160 » » goType: "SomeStruct", |
| 188 } | 161 } |
| 189 | 162 |
| 190 check(t, expected, "FieldDecodingTmpl", encodingInfo) | 163 check(t, expected, "FieldDecodingTmpl", encodingInfo) |
| 191 } | 164 } |
| OLD | NEW |