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 | 4 |
5 library LocalStorageTest; | |
6 import 'package:unittest/unittest.dart'; | |
7 import 'package:unittest/html_config.dart'; | |
8 import 'dart:html'; | 5 import 'dart:html'; |
9 | 6 |
| 7 import 'package:expect/minitest.dart'; |
| 8 |
10 main() { | 9 main() { |
11 useHtmlConfiguration(); | 10 setUp(() { |
| 11 window.localStorage['key1'] = 'val1'; |
| 12 window.localStorage['key2'] = 'val2'; |
| 13 window.localStorage['key3'] = 'val3'; |
| 14 }); |
12 | 15 |
13 void testWithLocalStorage(String name, fn()) { | 16 tearDown(() { |
14 test(name, () { | 17 window.localStorage.clear(); |
15 window.localStorage['key1'] = 'val1'; | 18 }); |
16 window.localStorage['key2'] = 'val2'; | |
17 window.localStorage['key3'] = 'val3'; | |
18 | 19 |
19 try { | 20 test('containsValue', () { |
20 fn(); | |
21 } finally { | |
22 window.localStorage.clear(); | |
23 } | |
24 }); | |
25 } | |
26 | |
27 testWithLocalStorage('containsValue', () { | |
28 expect(window.localStorage.containsValue('does not exist'), isFalse); | 21 expect(window.localStorage.containsValue('does not exist'), isFalse); |
29 expect(window.localStorage.containsValue('key1'), isFalse); | 22 expect(window.localStorage.containsValue('key1'), isFalse); |
30 expect(window.localStorage.containsValue('val1'), isTrue); | 23 expect(window.localStorage.containsValue('val1'), isTrue); |
31 expect(window.localStorage.containsValue('val3'), isTrue); | 24 expect(window.localStorage.containsValue('val3'), isTrue); |
32 }); | 25 }); |
33 | 26 |
34 testWithLocalStorage('containsKey', () { | 27 test('containsKey', () { |
35 expect(window.localStorage.containsKey('does not exist'), isFalse); | 28 expect(window.localStorage.containsKey('does not exist'), isFalse); |
36 expect(window.localStorage.containsKey('val1'), isFalse); | 29 expect(window.localStorage.containsKey('val1'), isFalse); |
37 expect(window.localStorage.containsKey('key1'), isTrue); | 30 expect(window.localStorage.containsKey('key1'), isTrue); |
38 expect(window.localStorage.containsKey('key3'), isTrue); | 31 expect(window.localStorage.containsKey('key3'), isTrue); |
39 }); | 32 }); |
40 | 33 |
41 testWithLocalStorage('[]', () { | 34 test('[]', () { |
42 expect(window.localStorage['does not exist'], isNull); | 35 expect(window.localStorage['does not exist'], isNull); |
43 expect(window.localStorage['key1'], 'val1'); | 36 expect(window.localStorage['key1'], 'val1'); |
44 expect(window.localStorage['key3'], 'val3'); | 37 expect(window.localStorage['key3'], 'val3'); |
45 }); | 38 }); |
46 | 39 |
47 testWithLocalStorage('[]=', () { | 40 test('[]=', () { |
48 expect(window.localStorage['key4'], isNull); | 41 expect(window.localStorage['key4'], isNull); |
49 window.localStorage['key4'] = 'val4'; | 42 window.localStorage['key4'] = 'val4'; |
50 expect(window.localStorage['key4'], 'val4'); | 43 expect(window.localStorage['key4'], 'val4'); |
51 | 44 |
52 expect(window.localStorage['key3'], 'val3'); | 45 expect(window.localStorage['key3'], 'val3'); |
53 window.localStorage['key3'] = 'val3-new'; | 46 window.localStorage['key3'] = 'val3-new'; |
54 expect(window.localStorage['key3'], 'val3-new'); | 47 expect(window.localStorage['key3'], 'val3-new'); |
55 }); | 48 }); |
56 | 49 |
57 testWithLocalStorage('putIfAbsent', () { | 50 test('putIfAbsent', () { |
58 expect(window.localStorage['key4'], isNull); | 51 expect(window.localStorage['key4'], isNull); |
59 expect(window.localStorage.putIfAbsent('key4', () => 'val4'), 'val4'); | 52 expect(window.localStorage.putIfAbsent('key4', () => 'val4'), 'val4'); |
60 expect(window.localStorage['key4'], 'val4'); | 53 expect(window.localStorage['key4'], 'val4'); |
61 | 54 |
62 expect(window.localStorage['key3'], 'val3'); | 55 expect(window.localStorage['key3'], 'val3'); |
63 expect(window.localStorage.putIfAbsent('key3', | 56 expect(window.localStorage.putIfAbsent('key3', |
64 () => expect(false, isTrue, reason: 'should not be called')), 'val3'); | 57 () { |
| 58 fail('should not be called'); |
| 59 return 'unused'; |
| 60 }), 'val3'); |
65 expect(window.localStorage['key3'], 'val3'); | 61 expect(window.localStorage['key3'], 'val3'); |
66 }); | 62 }); |
67 | 63 |
68 testWithLocalStorage('remove', () { | 64 test('remove', () { |
69 expect(window.localStorage.remove('does not exist'), isNull); | 65 expect(window.localStorage.remove('does not exist'), isNull); |
70 expect(window.localStorage.remove('key3'), 'val3'); | 66 expect(window.localStorage.remove('key3'), 'val3'); |
71 expect(window.localStorage, equals({'key1': 'val1', 'key2': 'val2'})); | 67 expect(window.localStorage, equals({'key1': 'val1', 'key2': 'val2'})); |
72 }); | 68 }); |
73 | 69 |
74 testWithLocalStorage('clear', () { | 70 test('clear', () { |
75 window.localStorage.clear(); | 71 window.localStorage.clear(); |
76 expect(window.localStorage, equals({})); | 72 expect(window.localStorage, equals({})); |
77 }); | 73 }); |
78 | 74 |
79 testWithLocalStorage('forEach', () { | 75 test('forEach', () { |
80 Map<String, String> results = {}; | 76 Map<String, String> results = {}; |
81 window.localStorage.forEach((k, v) { | 77 window.localStorage.forEach((k, v) { |
82 results[k] = v; | 78 results[k] = v; |
83 }); | 79 }); |
84 expect(results, equals({'key1': 'val1', 'key2': 'val2', 'key3': 'val3'})); | 80 expect(results, equals({'key1': 'val1', 'key2': 'val2', 'key3': 'val3'})); |
85 }); | 81 }); |
86 | 82 |
87 testWithLocalStorage('getKeys', () { | 83 test('getKeys', () { |
88 expect(window.localStorage.keys.toList(), | 84 expect(window.localStorage.keys.toList(), |
89 unorderedEquals(['key1', 'key2', 'key3'])); | 85 unorderedEquals(['key1', 'key2', 'key3'])); |
90 }); | 86 }); |
91 | 87 |
92 testWithLocalStorage('getVals', () { | 88 test('getVals', () { |
93 expect(window.localStorage.values.toList(), | 89 expect(window.localStorage.values.toList(), |
94 unorderedEquals(['val1', 'val2', 'val3'])); | 90 unorderedEquals(['val1', 'val2', 'val3'])); |
95 }); | 91 }); |
96 | 92 |
97 testWithLocalStorage('length', () { | 93 test('length', () { |
98 expect(window.localStorage.length, 3); | 94 expect(window.localStorage.length, 3); |
99 window.localStorage.clear(); | 95 window.localStorage.clear(); |
100 expect(window.localStorage.length, 0); | 96 expect(window.localStorage.length, 0); |
101 }); | 97 }); |
102 | 98 |
103 testWithLocalStorage('isEmpty', () { | 99 test('isEmpty', () { |
104 expect(window.localStorage.isEmpty, isFalse); | 100 expect(window.localStorage.isEmpty, isFalse); |
105 window.localStorage.clear(); | 101 window.localStorage.clear(); |
106 expect(window.localStorage.isEmpty, isTrue); | 102 expect(window.localStorage.isEmpty, isTrue); |
107 }); | 103 }); |
108 } | 104 } |
OLD | NEW |