-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
application-settings-tests.ts
188 lines (149 loc) · 6.27 KB
/
application-settings-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var appSettings = require("@nativescript/core/application-settings");
import * as TKUnit from "../tk-unit";
var stringKey: string = "stringKey";
var boolKey: string = "boolKey";
var numberKey: string = "numberKey";
var noStringKey: string = "noStringKey";
var noBoolKey: string = "noBoolKey";
var noNumberKey: string = "noNumberKey";
export var testBoolean = function () {
appSettings.setBoolean(boolKey, false);
var boolValueBefore = appSettings.getBoolean(boolKey);
TKUnit.assert(false === boolValueBefore, "Cannot set boolean to false, currently it is: " + appSettings.getBoolean(boolKey));
appSettings.setBoolean("boolKey", true);
var boolValue = appSettings.getBoolean("boolKey", false);
TKUnit.assert(true === boolValue, "Cannot set boolean to true");
TKUnit.assert(true === appSettings.getBoolean(boolKey), "Cannot set boolean to true (no default)");
};
export var testString = function () {
appSettings.setString("stringKey", "String value");
var stringValue = appSettings.getString("stringKey");
TKUnit.assert("String value" === stringValue, "Cannot set string value");
};
export var testNumber = function () {
appSettings.setNumber("numberKey", 54.321);
var value = parseFloat(appSettings.getNumber("numberKey").toFixed(3));
TKUnit.assert(54.321 === value, "Cannot set number value 54.321 != " + value);
};
export var testDefaults = function () {
var defaultValue = appSettings.getString("noStringKey", "No string value");
// will return "No string value" if there is no value for "noStringKey"
TKUnit.assert("No string value" === defaultValue, "Bad default string value");
TKUnit.assert(true === appSettings.getBoolean(noBoolKey, true), "Bad default boolean value");
TKUnit.assert(123.45 === appSettings.getNumber(noNumberKey, 123.45), "Bad default number value");
};
export var testDefaultsWithNoDefaultValueProvided = function () {
var defaultValue = appSettings.getString("noStringKey");
// will return undefined if there is no value for "noStringKey"
TKUnit.assertEqual(defaultValue, undefined, "Default string value is not undefined");
TKUnit.assertEqual(appSettings.getBoolean(noBoolKey), undefined, "Default boolean value is not undefined");
TKUnit.assertEqual(appSettings.getNumber(noNumberKey), undefined, "Default number value is not undefined");
};
export var testHasKey = function () {
var hasKey = appSettings.hasKey("noBoolKey");
// will return false if there is no value for "noBoolKey"
TKUnit.assert(!hasKey, "There is a key: " + noBoolKey);
TKUnit.assert(!appSettings.hasKey(noStringKey), "There is a key: " + noStringKey);
TKUnit.assert(!appSettings.hasKey(noNumberKey), "There is a key: " + noNumberKey);
TKUnit.assert(appSettings.hasKey(boolKey), "There is no key: " + boolKey);
TKUnit.assert(appSettings.hasKey(stringKey), "There is no key: " + stringKey);
TKUnit.assert(appSettings.hasKey(numberKey), "There is no key: " + numberKey);
};
export var testRemove = function () {
appSettings.remove("boolKey");
TKUnit.assert(!appSettings.hasKey(boolKey), "Failed to remove key: " + boolKey);
appSettings.remove(stringKey);
TKUnit.assert(!appSettings.hasKey(stringKey), "Failed to remove key: " + stringKey);
appSettings.remove(numberKey);
TKUnit.assert(!appSettings.hasKey(numberKey), "Failed to remove key: " + numberKey);
};
export var testClear = function () {
appSettings.clear();
TKUnit.assert(!appSettings.hasKey(boolKey), "Failed to remove key: " + boolKey);
TKUnit.assert(!appSettings.hasKey(stringKey), "Failed to remove key: " + stringKey);
TKUnit.assert(!appSettings.hasKey(numberKey), "Failed to remove key: " + numberKey);
};
export var testFlush = function () {
appSettings.setString(stringKey, "String value");
var flushed = appSettings.flush();
// will return boolean indicating whether flush to disk was successful
TKUnit.assert(flushed, "Flush failed: " + flushed);
TKUnit.assert(appSettings.hasKey(stringKey), "There is no key: " + stringKey);
};
export var testAllKeys = function () {
appSettings.setString(stringKey, "String value");
appSettings.setBoolean(boolKey, true);
appSettings.setNumber(numberKey, 22);
var allKeys = appSettings.getAllKeys();
TKUnit.assert(allKeys.indexOf(stringKey) !== -1, `${stringKey} is missing from .allKeys()`);
TKUnit.assert(allKeys.indexOf(boolKey) !== -1, `${boolKey} is missing from .allKeys()`);
TKUnit.assert(allKeys.indexOf(numberKey) !== -1, `${numberKey} is missing from .allKeys()`);
};
export var testInvalidKey = function () {
try {
appSettings.hasKey(undefined);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
appSettings.hasKey(null);
TKUnit.assert(false, "There is a key null");
}
catch (e) {
// we should receive an exception here
}
try {
appSettings.hasKey(123);
TKUnit.assert(false, "There is a key number");
}
catch (e) {
// we should receive an exception here
}
appSettings.hasKey("string");
};
export var testInvalidValue = function () {
try {
appSettings.setBoolean(boolKey, "str");
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
appSettings.setBoolean(boolKey, 123);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
appSettings.setString(boolKey, true);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
appSettings.setString(boolKey, 123);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
appSettings.setNumber(boolKey, true);
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
try {
appSettings.setNumber(boolKey, "123");
TKUnit.assert(false, "There is a key undefined");
}
catch (e) {
// we should receive an exception here
}
};