Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // Test program for map literals. | 4 // Test program for map literals. |
| 5 | 5 |
| 6 class MapLiteralTest { | 6 class MapLiteralTest { |
| 7 | 7 |
| 8 static testMain() { | 8 static testMain() { |
| 9 var map = { "a": 1, "b": 2, "c": 3 }; | 9 var map = { "a": 1, "b": 2, "c": 3 }; |
| 10 | 10 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 m["14"] = 14; | 54 m["14"] = 14; |
| 55 m["15"] = 15; | 55 m["15"] = 15; |
| 56 m["16"] = 16; | 56 m["16"] = 16; |
| 57 Expect.equals(16, m.length); | 57 Expect.equals(16, m.length); |
| 58 m.remove("1"); | 58 m.remove("1"); |
| 59 m.remove("1"); // Remove element twice. | 59 m.remove("1"); // Remove element twice. |
| 60 m.remove("16"); | 60 m.remove("16"); |
| 61 Expect.equals(14, m.length); | 61 Expect.equals(14, m.length); |
| 62 | 62 |
| 63 // Check that last value of duplicate key wins for const maps. | 63 // Check that last value of duplicate key wins for const maps. |
| 64 final cmap = const <num>{"a": 10, "b": 100, "a": 1000}; /// static type warn ing | 64 final cmap = const <String, num>{"a": 10, "b": 100, "a": 1000}; /// static t ype warning |
|
ahe
2012/08/16 19:52:49
Several long lines
regis
2012/08/16 20:11:00
I do not want to disrupt the special meaning of th
| |
| 65 Expect.equals(2, cmap.length); | 65 Expect.equals(2, cmap.length); |
| 66 Expect.equals(1000, cmap["a"]); | 66 Expect.equals(1000, cmap["a"]); |
| 67 Expect.equals(100, cmap["b"]); | 67 Expect.equals(100, cmap["b"]); |
| 68 | 68 |
| 69 final cmap2 = const <num>{"a": 10, "a": 100, "a": 1000}; /// static type war ning | 69 final cmap2 = const <String, num>{"a": 10, "a": 100, "a": 1000}; /// static type warning |
| 70 Expect.equals(1, cmap2.length); | 70 Expect.equals(1, cmap2.length); |
| 71 Expect.equals(1000, cmap["a"]); | 71 Expect.equals(1000, cmap["a"]); |
| 72 | 72 |
| 73 // Check that last value of duplicate key wins for mutable maps. | 73 // Check that last value of duplicate key wins for mutable maps. |
| 74 var mmap = <num>{"a": 10, "b": 100, "a": 1000}; /// static type warning | 74 var mmap = <String, num>{"a": 10, "b": 100, "a": 1000}; /// static type warn ing |
| 75 | 75 |
| 76 Expect.equals(2, mmap.length); | 76 Expect.equals(2, mmap.length); |
| 77 Expect.equals(1000, mmap["a"]); | 77 Expect.equals(1000, mmap["a"]); |
| 78 Expect.equals(100, mmap["b"]); | 78 Expect.equals(100, mmap["b"]); |
| 79 | 79 |
| 80 // Check that even if a key gets eliminated (the first "a"), all values | 80 // Check that even if a key gets eliminated (the first "a"), all values |
| 81 // are still evaluated, including side effects. | 81 // are still evaluated, including side effects. |
| 82 int counter = 0; | 82 int counter = 0; |
| 83 int ctr() { counter += 10; return counter; } | 83 int ctr() { counter += 10; return counter; } |
| 84 mmap = <num>{"a": ctr(), "b": ctr(), "a": ctr()}; /// static type warning | 84 mmap = <String, num>{"a": ctr(), "b": ctr(), "a": ctr()}; /// static type wa rning |
| 85 Expect.equals(2, mmap.length); | 85 Expect.equals(2, mmap.length); |
| 86 Expect.equals(40, ctr()); | 86 Expect.equals(40, ctr()); |
| 87 Expect.equals(30, mmap["a"]); | 87 Expect.equals(30, mmap["a"]); |
| 88 Expect.equals(20, mmap["b"]); | 88 Expect.equals(20, mmap["b"]); |
| 89 | 89 |
| 90 Expect.equals(10, { "beta": 100, "alpha": 9 + 1 }["alpha"]); | 90 Expect.equals(10, { "beta": 100, "alpha": 9 + 1 }["alpha"]); |
| 91 Expect.equals(10, <Map>{ | 91 Expect.equals(10, <String, Map>{ |
| 92 "beta": {"delta": 10}, | 92 "beta": {"delta": 10}, |
| 93 "alpha": {"gamma": 10} }["alpha"]["gamma"]); | 93 "alpha": {"gamma": 10} }["alpha"]["gamma"]); |
| 94 | 94 |
| 95 // Map literals at beginning of statement. | 95 // Map literals at beginning of statement. |
| 96 <num>{"pink": 100}; | 96 <String, num>{"pink": 100}; |
| 97 const <num>{"floyd": 100}; | 97 const <String, num>{"floyd": 100}; |
| 98 } | 98 } |
| 99 } | 99 } |
| 100 | 100 |
| 101 | 101 |
| 102 main() { | 102 main() { |
| 103 MapLiteralTest.testMain(); | 103 MapLiteralTest.testMain(); |
| 104 } | 104 } |
| OLD | NEW |