| OLD | NEW |
| (Empty) | |
| 1 library tests; |
| 2 |
| 3 import 'package:js/js.dart' as js; |
| 4 import 'package:js/js_wrapping.dart' as jsw; |
| 5 import 'package:unittest/unittest.dart'; |
| 6 import 'package:unittest/html_config.dart'; |
| 7 |
| 8 abstract class _Person { |
| 9 String firstname; |
| 10 |
| 11 String sayHello(); |
| 12 } |
| 13 |
| 14 class PersonMP extends jsw.MagicProxy implements _Person { |
| 15 PersonMP(String firstname, String lastname) : |
| 16 super(js.context.Person, [firstname, lastname]); |
| 17 PersonMP.fromProxy(js.Proxy proxy) : super.fromProxy(proxy); |
| 18 |
| 19 // noSuchMethod is here to suppress warning "Missing inherited members" |
| 20 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 21 } |
| 22 |
| 23 class PersonTP extends jsw.TypedProxy { |
| 24 static PersonTP cast(js.Proxy proxy) => proxy == null ? null : |
| 25 new PersonTP.fromProxy(proxy); |
| 26 |
| 27 PersonTP(String firstname, String lastname) : |
| 28 super(js.context.Person, [firstname, lastname]); |
| 29 PersonTP.fromProxy(js.Proxy proxy) : super.fromProxy(proxy); |
| 30 |
| 31 set firstname(String firstname) => $unsafe.firstname = firstname; |
| 32 String get firstname => $unsafe.firstname; |
| 33 |
| 34 List<PersonTP> get children => |
| 35 jsw.JsArrayToListAdapter.castListOfSerializables($unsafe.children, |
| 36 PersonTP.cast); |
| 37 set father(PersonTP father) => $unsafe.father = father; |
| 38 PersonTP get father => PersonTP.cast($unsafe.father); |
| 39 |
| 40 String sayHello() => $unsafe.sayHello(); |
| 41 } |
| 42 |
| 43 class Color implements js.Serializable<String> { |
| 44 static final RED = new Color._("red"); |
| 45 static final BLUE = new Color._("blue"); |
| 46 |
| 47 final String _value; |
| 48 |
| 49 Color._(this._value); |
| 50 |
| 51 String toJs() => this._value; |
| 52 operator ==(Color other) => this._value == other._value; |
| 53 } |
| 54 |
| 55 main() { |
| 56 useHtmlConfiguration(); |
| 57 |
| 58 test('TypedProxy', () { |
| 59 final john = new PersonTP('John', 'Doe'); |
| 60 expect(john.firstname, equals('John')); |
| 61 john.firstname = 'Joe'; |
| 62 expect(john.firstname, equals('Joe')); |
| 63 }); |
| 64 |
| 65 test('MagicProxy', () { |
| 66 final john = new PersonMP('John', 'Doe'); |
| 67 expect(john.firstname, equals('John')); |
| 68 expect(john['firstname'], equals('John')); |
| 69 john.firstname = 'Joe'; |
| 70 expect(john.firstname, equals('Joe')); |
| 71 expect(john['firstname'], equals('Joe')); |
| 72 john['firstname'] = 'John'; |
| 73 expect(john.firstname, equals('John')); |
| 74 expect(john['firstname'], equals('John')); |
| 75 }); |
| 76 |
| 77 test('function call', () { |
| 78 final john = new PersonTP('John', 'Doe'); |
| 79 expect(john.sayHello(), equals("Hello, I'm John Doe")); |
| 80 }); |
| 81 |
| 82 test('JsDateToDateTimeAdapter', () { |
| 83 final date = new DateTime.now(); |
| 84 final jsDate = new jsw.JsDateToDateTimeAdapter(date); |
| 85 expect(jsDate.millisecondsSinceEpoch, |
| 86 equals(date.millisecondsSinceEpoch)); |
| 87 jsDate.$unsafe.setFullYear(2000); |
| 88 expect(jsDate.year, equals(2000)); |
| 89 }); |
| 90 |
| 91 group('JsArrayToListAdapter', () { |
| 92 test('iterator', () { |
| 93 final m = new jsw.JsArrayToListAdapter<String>.fromProxy( |
| 94 js.array(["e0", "e1", "e2"])); |
| 95 |
| 96 final iterator = m.iterator; |
| 97 iterator.moveNext(); |
| 98 expect(iterator.current, equals("e0")); |
| 99 iterator.moveNext(); |
| 100 expect(iterator.current, equals("e1")); |
| 101 iterator.moveNext(); |
| 102 expect(iterator.current, equals("e2")); |
| 103 }); |
| 104 |
| 105 test('get length', () { |
| 106 final m1 = new jsw.JsArrayToListAdapter<String>.fromProxy(js.array([])); |
| 107 expect(m1.length, equals(0)); |
| 108 final m2 = new jsw.JsArrayToListAdapter<String>.fromProxy( |
| 109 js.array(["a", "b"])); |
| 110 expect(m2.length, equals(2)); |
| 111 }); |
| 112 |
| 113 test('add', () { |
| 114 final m = new jsw.JsArrayToListAdapter<String>.fromProxy(js.array([])); |
| 115 expect(m.length, equals(0)); |
| 116 m.add("a"); |
| 117 expect(m.length, equals(1)); |
| 118 expect(m[0], equals("a")); |
| 119 m.add("b"); |
| 120 expect(m.length, equals(2)); |
| 121 expect(m[0], equals("a")); |
| 122 expect(m[1], equals("b")); |
| 123 }); |
| 124 |
| 125 test('clear', () { |
| 126 final m = new jsw.JsArrayToListAdapter<String>.fromProxy( |
| 127 js.array(["a", "b"])); |
| 128 expect(m.length, equals(2)); |
| 129 m.clear(); |
| 130 expect(m.length, equals(0)); |
| 131 }); |
| 132 |
| 133 test('remove', () { |
| 134 final m = new jsw.JsArrayToListAdapter<String>.fromProxy( |
| 135 js.array(["a", "b"])); |
| 136 expect(m.length, equals(2)); |
| 137 m.remove("a"); |
| 138 expect(m.length, equals(1)); |
| 139 expect(m[0], equals("b")); |
| 140 }); |
| 141 |
| 142 test('operator []', () { |
| 143 final m = new jsw.JsArrayToListAdapter<String>.fromProxy( |
| 144 js.array(["a", "b"])); |
| 145 expect(() => m[-1], throwsA(isRangeError)); |
| 146 expect(() => m[2], throwsA(isRangeError)); |
| 147 expect(m[0], equals("a")); |
| 148 expect(m[1], equals("b")); |
| 149 }); |
| 150 |
| 151 test('operator []=', () { |
| 152 final m = new jsw.JsArrayToListAdapter<String>.fromProxy( |
| 153 js.array(["a", "b"])); |
| 154 expect(() => m[-1] = "c", throwsA(isRangeError)); |
| 155 expect(() => m[2] = "c", throwsA(isRangeError)); |
| 156 m[0] = "d"; |
| 157 m[1] = "e"; |
| 158 expect(m[0], equals("d")); |
| 159 expect(m[1], equals("e")); |
| 160 }); |
| 161 |
| 162 test('set length', () { |
| 163 final m = new jsw.JsArrayToListAdapter<String>.fromProxy( |
| 164 js.array(["a", "b"])); |
| 165 m.length = 10; |
| 166 expect(m.length, equals(10)); |
| 167 expect(m[5], equals(null)); |
| 168 m.length = 1; |
| 169 expect(m.length, equals(1)); |
| 170 expect(m[0], equals("a")); |
| 171 }); |
| 172 |
| 173 test('sort', () { |
| 174 final m = new jsw.JsArrayToListAdapter<String>.fromProxy( |
| 175 js.array(["c", "a", "b"])); |
| 176 m.sort(); |
| 177 expect(m.length, equals(3)); |
| 178 expect(m[0], equals("a")); |
| 179 expect(m[1], equals("b")); |
| 180 expect(m[2], equals("c")); |
| 181 }); |
| 182 |
| 183 test('insert', () { |
| 184 final m = new jsw.JsArrayToListAdapter<String>.fromProxy( |
| 185 js.array(["a", "b", "c"])); |
| 186 m.insert(1, "d"); |
| 187 expect(m.length, equals(4)); |
| 188 expect(m[0], equals("a")); |
| 189 expect(m[1], equals("d")); |
| 190 expect(m[2], equals("b")); |
| 191 expect(m[3], equals("c")); |
| 192 }); |
| 193 |
| 194 test('removeAt', () { |
| 195 final m = new jsw.JsArrayToListAdapter<String>.fromProxy( |
| 196 js.array(["a", "b", "c"])); |
| 197 expect(m.removeAt(1), equals("b")); |
| 198 expect(m.length, equals(2)); |
| 199 expect(m[0], equals("a")); |
| 200 expect(m[1], equals("c")); |
| 201 expect(() => m.removeAt(2), throwsA(isRangeError)); |
| 202 }); |
| 203 |
| 204 test('removeLast', () { |
| 205 final m = new jsw.JsArrayToListAdapter<String>.fromProxy( |
| 206 js.array(["a", "b", "c", null])); |
| 207 expect(m.removeLast(), isNull); |
| 208 expect(m.removeLast(), equals("c")); |
| 209 expect(m.removeLast(), equals("b")); |
| 210 expect(m.removeLast(), equals("a")); |
| 211 expect(m.length, equals(0)); |
| 212 }); |
| 213 |
| 214 test('sublist', () { |
| 215 final m = new jsw.JsArrayToListAdapter<String>.fromProxy( |
| 216 js.array(["a", "b", "c", null])); |
| 217 final sl1 = m.sublist(2); |
| 218 expect(sl1.length, equals(2)); |
| 219 expect(sl1[0], equals("c")); |
| 220 expect(sl1[1], isNull); |
| 221 final sl2 = m.sublist(1, 3); |
| 222 expect(sl2.length, equals(2)); |
| 223 expect(sl2[0], equals("b")); |
| 224 expect(sl2[1], equals("c")); |
| 225 }); |
| 226 |
| 227 test('setRange', () { |
| 228 final m = new jsw.JsArrayToListAdapter<String>.fromProxy( |
| 229 js.array(["a", "b", "c", null])); |
| 230 m.setRange(1, 2, [null, null]); |
| 231 expect(m.length, equals(4)); |
| 232 expect(m[0], equals("a")); |
| 233 expect(m[1], isNull); |
| 234 expect(m[2], isNull); |
| 235 expect(m[3], isNull); |
| 236 m.setRange(3, 1, [null, "c", null], 1); |
| 237 expect(m[0], equals("a")); |
| 238 expect(m[1], isNull); |
| 239 expect(m[2], isNull); |
| 240 expect(m[3], equals("c")); |
| 241 }); |
| 242 |
| 243 test('removeRange', () { |
| 244 final m = new jsw.JsArrayToListAdapter<String>.fromProxy( |
| 245 js.array(["a", "b", "c", null])); |
| 246 m.removeRange(1, 3); |
| 247 expect(m.length, equals(2)); |
| 248 expect(m[0], equals("a")); |
| 249 expect(m[1], isNull); |
| 250 }); |
| 251 |
| 252 test('bidirectionnal serialization of Proxy', () { |
| 253 js.context.myArray = js.array([]); |
| 254 final myList = new jsw.JsArrayToListAdapter<PersonTP>.fromProxy( |
| 255 js.context.myArray, new jsw.TranslatorForSerializable<PersonTP>( |
| 256 (p) => new PersonTP.fromProxy(p))); |
| 257 |
| 258 myList.add(new PersonTP('John', 'Doe')); |
| 259 myList.add(null); |
| 260 expect(myList[0].firstname, 'John'); |
| 261 expect(myList[1], isNull); |
| 262 }); |
| 263 |
| 264 test('family', () { |
| 265 final father = new PersonTP("John", "Doe"); |
| 266 final child1 = new PersonTP("Lewis", "Doe") |
| 267 ..father = father; |
| 268 final child2 = new PersonTP("Andy", "Doe") |
| 269 ..father = father; |
| 270 father.children.addAll([child1, child2]); |
| 271 expect(father.children.length, 2); |
| 272 expect(father.children.map((p) => p.firstname).join(","), "Lewis,Andy"); |
| 273 expect(child1.father.firstname, "John"); |
| 274 }); |
| 275 |
| 276 test('bidirectionnal serialization of Serializable', () { |
| 277 js.context.myArray = js.array([]); |
| 278 final myList = new jsw.JsArrayToListAdapter<Color>.fromProxy( |
| 279 js.context.myArray, |
| 280 new jsw.Translator<Color>( |
| 281 (e) => e != null ? new Color._(e) : null, |
| 282 (e) => e != null ? e.toJs() : null |
| 283 ) |
| 284 ); |
| 285 |
| 286 myList.add(Color.BLUE); |
| 287 myList.add(null); |
| 288 expect(myList[0], Color.BLUE); |
| 289 expect(myList[1], isNull); |
| 290 }); |
| 291 }); |
| 292 |
| 293 group('JsObjectToMapAdapter', () { |
| 294 test('get keys', () { |
| 295 final m = new jsw.JsObjectToMapAdapter<int>.fromProxy( |
| 296 js.map({"a": 1, "b": 2})); |
| 297 final keys = m.keys; |
| 298 expect(keys.length, equals(2)); |
| 299 expect(keys, contains("a")); |
| 300 expect(keys, contains("b")); |
| 301 }); |
| 302 |
| 303 test('containsKey', () { |
| 304 final m = new jsw.JsObjectToMapAdapter.fromProxy( |
| 305 js.map({"a": 1, "b": "c"})); |
| 306 expect(m.containsKey("a"), equals(true)); |
| 307 expect(m.containsKey("d"), equals(false)); |
| 308 }); |
| 309 |
| 310 test('operator []', () { |
| 311 final m = new jsw.JsObjectToMapAdapter.fromProxy( |
| 312 js.map({"a": 1, "b": "c"})); |
| 313 expect(m["a"], equals(1)); |
| 314 expect(m["b"], equals("c")); |
| 315 }); |
| 316 |
| 317 test('operator []=', () { |
| 318 final m = new jsw.JsObjectToMapAdapter.fromProxy(js.map({})); |
| 319 m["a"] = 1; |
| 320 expect(m["a"], equals(1)); |
| 321 expect(m.length, equals(1)); |
| 322 m["b"] = "c"; |
| 323 expect(m["b"], equals("c")); |
| 324 expect(m.length, equals(2)); |
| 325 }); |
| 326 |
| 327 test('remove', () { |
| 328 final m = new jsw.JsObjectToMapAdapter.fromProxy( |
| 329 js.map({"a": 1, "b": "c"})); |
| 330 expect(m.remove("a"), equals(1)); |
| 331 expect(m["b"], equals("c")); |
| 332 expect(m.length, equals(1)); |
| 333 }); |
| 334 |
| 335 test('bidirectionnal serialization of Proxy', () { |
| 336 final myMap = new jsw.JsObjectToMapAdapter<PersonTP>.fromProxy( |
| 337 js.map({}), new jsw.TranslatorForSerializable<PersonTP>((p) => |
| 338 new PersonTP.fromProxy(p))); |
| 339 myMap["a"] = new PersonTP('John', 'Doe'); |
| 340 expect(myMap["a"].firstname, 'John'); |
| 341 }); |
| 342 |
| 343 }); |
| 344 } |
| OLD | NEW |