OLD | NEW |
1 library map_test; | 1 library map_test; |
2 | 2 |
3 import 'package:unittest/unittest.dart' | 3 import 'package:unittest/unittest.dart' |
4 show test, expect, predicate, same, throwsA, | 4 show test, expect, predicate, same, throwsA, |
5 throwsArgumentError, throwsUnsupportedError; | 5 throwsArgumentError, throwsUnsupportedError; |
6 | 6 |
7 import '../out/protos/map_api.pb.dart' as pb; | 7 import '../out/protos/map_api.pb.dart' as pb; |
8 import '../out/protos/map_api2.pb.dart' as pb2; | 8 import '../out/protos/map_api2.pb.dart' as pb2; |
9 | 9 |
10 throwsError(Type expectedType, String expectedMessage) => | 10 throwsError(Type expectedType, String expectedMessage) => |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 expect(rec["num"], 42); | 47 expect(rec["num"], 42); |
48 rec.nums.add(123); | 48 rec.nums.add(123); |
49 expect(rec["nums"], [123]); | 49 expect(rec["nums"], [123]); |
50 rec.str = "hello"; | 50 rec.str = "hello"; |
51 expect(rec["str"], "hello"); | 51 expect(rec["str"], "hello"); |
52 var msg = new pb.NonMap(); | 52 var msg = new pb.NonMap(); |
53 rec.msg = msg; | 53 rec.msg = msg; |
54 expect(rec["msg"], same(msg)); | 54 expect(rec["msg"], same(msg)); |
55 }); | 55 }); |
56 | 56 |
57 test('operator [] handles dotted paths', () { | |
58 var rec = new pb.Rec(); | |
59 rec.msg = new pb.NonMap(); | |
60 rec.msg.str = "hello"; | |
61 expect(rec["msg.str"], "hello"); | |
62 | |
63 rec.msg.child = new pb.NonMap(); | |
64 rec.msg.child.str = "child"; | |
65 expect(rec["msg.child.str"], "child"); | |
66 }); | |
67 | |
68 test('operator []= throws exception for invalid key', () { | 57 test('operator []= throws exception for invalid key', () { |
69 var rec = new pb.Rec(); | 58 var rec = new pb.Rec(); |
70 expect(() { rec["unknown"] = 123; }, | 59 expect(() { rec["unknown"] = 123; }, |
71 throwsError(ArgumentError, "field 'unknown' not found in Rec")); | 60 throwsError(ArgumentError, "field 'unknown' not found in Rec")); |
72 }); | 61 }); |
73 | 62 |
74 test('operator []= throws exception for repeated field', () { | 63 test('operator []= throws exception for repeated field', () { |
75 // Copying the values would be confusing. | 64 // Copying the values would be confusing. |
76 var rec = new pb.Rec(); | 65 var rec = new pb.Rec(); |
77 expect(() { rec["nums"] = [1, 2]; }, throwsArgumentError); | 66 expect(() { rec["nums"] = [1, 2]; }, throwsArgumentError); |
78 }); | 67 }); |
79 | 68 |
80 test('operator []= throws exception for invalid value type', () { | 69 test('operator []= throws exception for invalid value type', () { |
81 var rec = new pb.Rec(); | 70 var rec = new pb.Rec(); |
82 expect(() { rec["num"] = "hello"; }, throwsArgumentError); | 71 expect(() { rec["num"] = "hello"; }, throwsArgumentError); |
83 expect(() { rec["str"] = 123; }, throwsArgumentError); | 72 expect(() { rec["str"] = 123; }, throwsArgumentError); |
84 }); | 73 }); |
85 | 74 |
86 test('operator []= sets the field', () { | 75 test('operator []= sets the field', () { |
87 var rec = new pb.Rec(); | 76 var rec = new pb.Rec(); |
88 rec["num"] = 123; | 77 rec["num"] = 123; |
89 expect(rec.num, 123); | 78 expect(rec.num, 123); |
90 rec["str"] = "hello"; | 79 rec["str"] = "hello"; |
91 expect(rec.str, "hello"); | 80 expect(rec.str, "hello"); |
92 }); | 81 }); |
93 | 82 |
94 test('operator []= handles dotted paths', () { | |
95 var rec = new pb.Rec(); | |
96 rec.msg = new pb.NonMap(); | |
97 rec["msg.str"] = "hello"; | |
98 expect(rec.msg.str, "hello"); | |
99 | |
100 rec.msg.child = new pb.NonMap(); | |
101 rec["msg.child.str"] = "child"; | |
102 expect(rec.msg.child.str, "child"); | |
103 }); | |
104 | |
105 test('operator []= throws exception for invalid dotted path', () { | |
106 var rec = new pb.Rec(); | |
107 rec.msg = new pb.NonMap(); | |
108 expect(() { rec["msg.unknown"] = "hello"; }, | |
109 throwsError(ArgumentError, "field 'unknown' not found in NonMap")); | |
110 expect(() { rec["msg.unknown.child"] = "hello"; }, | |
111 throwsError(ArgumentError, "field 'unknown' not found in NonMap")); | |
112 }); | |
113 | |
114 test('keys returns each field name (even when unset)', () { | 83 test('keys returns each field name (even when unset)', () { |
115 var rec = new pb.Rec(); | 84 var rec = new pb.Rec(); |
116 expect(new Set.from(rec.keys), new Set.from(["msg", "num", "nums", "str"])); | 85 expect(new Set.from(rec.keys), new Set.from(["msg", "num", "nums", "str"])); |
117 }); | 86 }); |
118 | 87 |
119 test('containsKey returns true for fields that exist (even when unset)', () { | 88 test('containsKey returns true for fields that exist (even when unset)', () { |
120 var rec = new pb.Rec(); | 89 var rec = new pb.Rec(); |
121 expect(rec.containsKey("unknown"), false); | 90 expect(rec.containsKey("unknown"), false); |
122 expect(rec.containsKey("str"), true); | 91 expect(rec.containsKey("str"), true); |
123 expect(rec.containsKey("num"), true); | 92 expect(rec.containsKey("num"), true); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 expect(rec["str"], "hello"); | 128 expect(rec["str"], "hello"); |
160 expect(rec["num"], 123); | 129 expect(rec["num"], 123); |
161 }); | 130 }); |
162 | 131 |
163 test("addAll doesn't work for repeated fields", () { | 132 test("addAll doesn't work for repeated fields", () { |
164 // It would be confusing to copy the values. | 133 // It would be confusing to copy the values. |
165 var rec = new pb.Rec(); | 134 var rec = new pb.Rec(); |
166 expect(() { rec.addAll({"nums": [1,2,3]}); }, throwsArgumentError); | 135 expect(() { rec.addAll({"nums": [1,2,3]}); }, throwsArgumentError); |
167 }); | 136 }); |
168 } | 137 } |
OLD | NEW |