OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 render | 5 package render |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "fmt" | 9 "fmt" |
10 "reflect" | 10 "reflect" |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 // Output: Render test: | 196 // Output: Render test: |
197 // fmt.Printf: render.testStruct{S:"hello", V:(*map[string]int)(0x600
dd065), I:42} | 197 // fmt.Printf: render.testStruct{S:"hello", V:(*map[string]int)(0x600
dd065), I:42} |
198 // render.Render: render.testStruct{S:"hello", V:(*map[string]int){"bar"
:1, "foo":0}, I:render.customType(42)} | 198 // render.Render: render.testStruct{S:"hello", V:(*map[string]int){"bar"
:1, "foo":0}, I:render.customType(42)} |
199 } | 199 } |
200 | 200 |
201 var pointerRE = regexp.MustCompile(`\(0x[a-f0-9]+\)`) | 201 var pointerRE = regexp.MustCompile(`\(0x[a-f0-9]+\)`) |
202 | 202 |
203 func sanitizePointer(s string) string { | 203 func sanitizePointer(s string) string { |
204 return pointerRE.ReplaceAllString(s, "(0x600dd065)") | 204 return pointerRE.ReplaceAllString(s, "(0x600dd065)") |
205 } | 205 } |
| 206 |
| 207 type chanList []chan int |
| 208 |
| 209 func (c chanList) Len() int { return len(c) } |
| 210 func (c chanList) Swap(i, j int) { c[i], c[j] = c[j], c[i] } |
| 211 func (c chanList) Less(i, j int) bool { |
| 212 return reflect.ValueOf(c[i]).Pointer() < reflect.ValueOf(c[j]).Pointer() |
| 213 } |
| 214 |
| 215 func TestMapSortRendering(t *testing.T) { |
| 216 type namedMapType map[int]struct{ a int } |
| 217 type mapKey struct{ a, b int } |
| 218 |
| 219 chans := make(chanList, 5) |
| 220 for i := range chans { |
| 221 chans[i] = make(chan int) |
| 222 } |
| 223 |
| 224 tcs := []struct { |
| 225 in interface{} |
| 226 expect string |
| 227 }{ |
| 228 { |
| 229 map[uint32]struct{}{1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6
: {}, 7: {}, 8: {}}, |
| 230 "map[uint32]struct {}{1:{}, 2:{}, 3:{}, 4:{}, 5:{}, 6:{}
, 7:{}, 8:{}}", |
| 231 }, |
| 232 { |
| 233 map[int8]struct{}{1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6:
{}, 7: {}, 8: {}}, |
| 234 "map[int8]struct {}{1:{}, 2:{}, 3:{}, 4:{}, 5:{}, 6:{},
7:{}, 8:{}}", |
| 235 }, |
| 236 { |
| 237 map[uintptr]struct{}{1: {}, 2: {}, 3: {}, 4: {}, 5: {},
6: {}, 7: {}, 8: {}}, |
| 238 "map[uintptr]struct {}{1:{}, 2:{}, 3:{}, 4:{}, 5:{}, 6:{
}, 7:{}, 8:{}}", |
| 239 }, |
| 240 { |
| 241 namedMapType{10: struct{ a int }{20}}, |
| 242 "render.namedMapType{10:struct { a int }{20}}", |
| 243 }, |
| 244 { |
| 245 map[mapKey]struct{}{mapKey{3, 1}: {}, mapKey{1, 3}: {},
mapKey{1, 2}: {}, mapKey{2, 1}: {}}, |
| 246 "map[render.mapKey]struct {}{render.mapKey{a:1, b:2}:{},
render.mapKey{a:1, b:3}:{}, render.mapKey{a:2, b:1}:{}, render.mapKey{a:3, b:1}
:{}}", |
| 247 }, |
| 248 { |
| 249 map[float64]struct{}{10.5: {}, 10.15: {}, 1203: {}, 1: {
}, 2: {}}, |
| 250 "map[float64]struct {}{1:{}, 2:{}, 10.15:{}, 10.5:{}, 12
03:{}}", |
| 251 }, |
| 252 { |
| 253 map[bool]struct{}{true: {}, false: {}}, |
| 254 "map[bool]struct {}{false:{}, true:{}}", |
| 255 }, |
| 256 { |
| 257 map[interface{}]struct{}{1: {}, 2: {}, 3: {}, "foo": {}}
, |
| 258 `map[interface{}]struct {}{1:{}, 2:{}, 3:{}, "foo":{}}`, |
| 259 }, |
| 260 { |
| 261 map[complex64]struct{}{1 + 2i: {}, 2 + 1i: {}, 3 + 1i: {
}, 1 + 3i: {}}, |
| 262 "map[complex64]struct {}{(1+2i):{}, (1+3i):{}, (2+1i):{}
, (3+1i):{}}", |
| 263 }, |
| 264 { |
| 265 map[chan int]string{nil: "a", chans[0]: "b", chans[1]: "
c", chans[2]: "d", chans[3]: "e", chans[4]: "f"}, |
| 266 `map[(chan int)]string{(chan int)(PTR):"a", (chan int)(P
TR):"b", (chan int)(PTR):"c", (chan int)(PTR):"d", (chan int)(PTR):"e", (chan in
t)(PTR):"f"}`, |
| 267 }, |
| 268 } |
| 269 |
| 270 for _, tc := range tcs { |
| 271 assertRendersLike(t, reflect.TypeOf(tc.in).Name(), tc.in, tc.exp
ect) |
| 272 } |
| 273 } |
OLD | NEW |