OLD | NEW |
| (Empty) |
1 dart_library.library('expect', null, /* Imports */[ | |
2 'dart_sdk' | |
3 ], function(exports, dart_sdk) { | |
4 'use strict'; | |
5 const core = dart_sdk.core; | |
6 const dart = dart_sdk.dart; | |
7 const dartx = dart_sdk.dartx; | |
8 const expect = Object.create(null); | |
9 expect.Expect = class Expect extends core.Object { | |
10 static _truncateString(string, start, end, length) { | |
11 if (dart.notNull(end) - dart.notNull(start) > dart.notNull(length)) { | |
12 end = dart.notNull(start) + dart.notNull(length); | |
13 } else if (dart.notNull(end) - dart.notNull(start) < dart.notNull(length))
{ | |
14 let overflow = dart.notNull(length) - (dart.notNull(end) - dart.notNull(
start)); | |
15 if (overflow > 10) overflow = 10; | |
16 start = dart.notNull(start) - ((overflow + 1) / 2)[dartx.truncate](); | |
17 end = dart.notNull(end) + (overflow / 2)[dartx.truncate](); | |
18 if (dart.notNull(start) < 0) start = 0; | |
19 if (dart.notNull(end) > dart.notNull(string[dartx.length])) end = string
[dartx.length]; | |
20 } | |
21 if (start == 0 && end == string[dartx.length]) return string; | |
22 let buf = new core.StringBuffer(); | |
23 if (dart.notNull(start) > 0) buf.write("..."); | |
24 for (let i = start; dart.notNull(i) < dart.notNull(end); i = dart.notNull(
i) + 1) { | |
25 let code = string[dartx.codeUnitAt](i); | |
26 if (dart.notNull(code) < 32) { | |
27 buf.write("\\x"); | |
28 buf.write("0123456789abcdef"[dartx.get]((dart.notNull(code) / 16)[dart
x.truncate]())); | |
29 buf.write("0123456789abcdef"[dartx.get](code[dartx['%']](16))); | |
30 } else { | |
31 buf.writeCharCode(string[dartx.codeUnitAt](i)); | |
32 } | |
33 } | |
34 if (dart.notNull(end) < dart.notNull(string[dartx.length])) buf.write("...
"); | |
35 return buf.toString(); | |
36 } | |
37 static _stringDifference(expected, actual) { | |
38 if (dart.notNull(expected[dartx.length]) < 20 && dart.notNull(actual[dartx
.length]) < 20) return null; | |
39 for (let i = 0; i < dart.notNull(expected[dartx.length]) && i < dart.notNu
ll(actual[dartx.length]); i++) { | |
40 if (expected[dartx.codeUnitAt](i) != actual[dartx.codeUnitAt](i)) { | |
41 let start = i; | |
42 i++; | |
43 while (i < dart.notNull(expected[dartx.length]) && i < dart.notNull(ac
tual[dartx.length])) { | |
44 if (expected[dartx.codeUnitAt](i) == actual[dartx.codeUnitAt](i)) br
eak; | |
45 i++; | |
46 } | |
47 let end = i; | |
48 let truncExpected = expect.Expect._truncateString(expected, start, end
, 20); | |
49 let truncActual = expect.Expect._truncateString(actual, start, end, 20
); | |
50 return `at index ${start}: Expected <${truncExpected}>, ` + `Found: <$
{truncActual}>`; | |
51 } | |
52 } | |
53 return null; | |
54 } | |
55 static equals(expected, actual, reason) { | |
56 if (reason === void 0) reason = null; | |
57 if (dart.equals(expected, actual)) return; | |
58 let msg = expect.Expect._getMessage(reason); | |
59 if (typeof expected == 'string' && typeof actual == 'string') { | |
60 let stringDifference = expect.Expect._stringDifference(expected, actual)
; | |
61 if (stringDifference != null) { | |
62 expect.Expect._fail(`Expect.equals(${stringDifference}${msg}) fails.`)
; | |
63 } | |
64 } | |
65 expect.Expect._fail(`Expect.equals(expected: <${expected}>, actual: <${act
ual}>${msg}) fails.`); | |
66 } | |
67 static isTrue(actual, reason) { | |
68 if (reason === void 0) reason = null; | |
69 if (dart.notNull(expect._identical(actual, true))) return; | |
70 let msg = expect.Expect._getMessage(reason); | |
71 expect.Expect._fail(`Expect.isTrue(${actual}${msg}) fails.`); | |
72 } | |
73 static isFalse(actual, reason) { | |
74 if (reason === void 0) reason = null; | |
75 if (dart.notNull(expect._identical(actual, false))) return; | |
76 let msg = expect.Expect._getMessage(reason); | |
77 expect.Expect._fail(`Expect.isFalse(${actual}${msg}) fails.`); | |
78 } | |
79 static isNull(actual, reason) { | |
80 if (reason === void 0) reason = null; | |
81 if (null == actual) return; | |
82 let msg = expect.Expect._getMessage(reason); | |
83 expect.Expect._fail(`Expect.isNull(actual: <${actual}>${msg}) fails.`); | |
84 } | |
85 static isNotNull(actual, reason) { | |
86 if (reason === void 0) reason = null; | |
87 if (null != actual) return; | |
88 let msg = expect.Expect._getMessage(reason); | |
89 expect.Expect._fail(`Expect.isNotNull(actual: <${actual}>${msg}) fails.`); | |
90 } | |
91 static identical(expected, actual, reason) { | |
92 if (reason === void 0) reason = null; | |
93 if (dart.notNull(expect._identical(expected, actual))) return; | |
94 let msg = expect.Expect._getMessage(reason); | |
95 expect.Expect._fail(`Expect.identical(expected: <${expected}>, actual: <${
actual}>${msg}) ` + "fails."); | |
96 } | |
97 static fail(msg) { | |
98 expect.Expect._fail(`Expect.fail('${msg}')`); | |
99 } | |
100 static approxEquals(expected, actual, tolerance, reason) { | |
101 if (tolerance === void 0) tolerance = null; | |
102 if (reason === void 0) reason = null; | |
103 if (tolerance == null) { | |
104 tolerance = (dart.notNull(expected) / 10000.0)[dartx.abs](); | |
105 } | |
106 if (dart.notNull((dart.notNull(expected) - dart.notNull(actual))[dartx.abs
]()) <= dart.notNull(tolerance)) return; | |
107 let msg = expect.Expect._getMessage(reason); | |
108 expect.Expect._fail(`Expect.approxEquals(expected:<${expected}>, actual:<$
{actual}>, ` + `tolerance:<${tolerance}>${msg}) fails`); | |
109 } | |
110 static notEquals(unexpected, actual, reason) { | |
111 if (reason === void 0) reason = null; | |
112 if (!dart.equals(unexpected, actual)) return; | |
113 let msg = expect.Expect._getMessage(reason); | |
114 expect.Expect._fail(`Expect.notEquals(unexpected: <${unexpected}>, actual:
<${actual}>${msg}) ` + "fails."); | |
115 } | |
116 static listEquals(expected, actual, reason) { | |
117 if (reason === void 0) reason = null; | |
118 let msg = expect.Expect._getMessage(reason); | |
119 let n = dart.notNull(expected[dartx.length]) < dart.notNull(actual[dartx.l
ength]) ? expected[dartx.length] : actual[dartx.length]; | |
120 for (let i = 0; i < dart.notNull(n); i++) { | |
121 if (!dart.equals(expected[dartx.get](i), actual[dartx.get](i))) { | |
122 expect.Expect._fail(`Expect.listEquals(at index ${i}, ` + `expected: <
${expected[dartx.get](i)}>, actual: <${actual[dartx.get](i)}>${msg}) fails`); | |
123 } | |
124 } | |
125 if (expected[dartx.length] != actual[dartx.length]) { | |
126 expect.Expect._fail('Expect.listEquals(list length, ' + `expected: <${ex
pected[dartx.length]}>, actual: <${actual[dartx.length]}>${msg}) ` + 'fails: Nex
t element <' + `${dart.notNull(expected[dartx.length]) > dart.notNull(n) ? expec
ted[dartx.get](n) : actual[dartx.get](n)}>`); | |
127 } | |
128 } | |
129 static mapEquals(expected, actual, reason) { | |
130 if (reason === void 0) reason = null; | |
131 let msg = expect.Expect._getMessage(reason); | |
132 for (let key of expected[dartx.keys]) { | |
133 if (!dart.notNull(actual[dartx.containsKey](key))) { | |
134 expect.Expect._fail(`Expect.mapEquals(missing expected key: <${key}>${
msg}) fails`); | |
135 } | |
136 expect.Expect.equals(expected[dartx.get](key), actual[dartx.get](key)); | |
137 } | |
138 for (let key of actual[dartx.keys]) { | |
139 if (!dart.notNull(expected[dartx.containsKey](key))) { | |
140 expect.Expect._fail(`Expect.mapEquals(unexpected key: <${key}>${msg})
fails`); | |
141 } | |
142 } | |
143 } | |
144 static stringEquals(expected, actual, reason) { | |
145 if (reason === void 0) reason = null; | |
146 if (expected == actual) return; | |
147 let msg = expect.Expect._getMessage(reason); | |
148 let defaultMessage = `Expect.stringEquals(expected: <${expected}>", <${act
ual}>${msg}) fails`; | |
149 if (expected == null || actual == null) { | |
150 expect.Expect._fail(`${defaultMessage}`); | |
151 } | |
152 let left = 0; | |
153 let right = 0; | |
154 let eLen = expected[dartx.length]; | |
155 let aLen = actual[dartx.length]; | |
156 while (true) { | |
157 if (left == eLen || left == aLen || expected[dartx.get](left) != actual[
dartx.get](left)) { | |
158 break; | |
159 } | |
160 left++; | |
161 } | |
162 let eRem = dart.notNull(eLen) - left; | |
163 let aRem = dart.notNull(aLen) - left; | |
164 while (true) { | |
165 if (right == eRem || right == aRem || expected[dartx.get](dart.notNull(e
Len) - right - 1) != actual[dartx.get](dart.notNull(aLen) - right - 1)) { | |
166 break; | |
167 } | |
168 right++; | |
169 } | |
170 let leftSnippet = expected[dartx.substring](left < 10 ? 0 : left - 10, lef
t); | |
171 let rightSnippetLength = right < 10 ? right : 10; | |
172 let rightSnippet = expected[dartx.substring](dart.notNull(eLen) - right, d
art.notNull(eLen) - right + rightSnippetLength); | |
173 let eSnippet = expected[dartx.substring](left, dart.notNull(eLen) - right)
; | |
174 let aSnippet = actual[dartx.substring](left, dart.notNull(aLen) - right); | |
175 if (dart.notNull(eSnippet[dartx.length]) > 43) { | |
176 eSnippet = dart.notNull(eSnippet[dartx.substring](0, 20)) + "..." + dart
.notNull(eSnippet[dartx.substring](dart.notNull(eSnippet[dartx.length]) - 20)); | |
177 } | |
178 if (dart.notNull(aSnippet[dartx.length]) > 43) { | |
179 aSnippet = dart.notNull(aSnippet[dartx.substring](0, 20)) + "..." + dart
.notNull(aSnippet[dartx.substring](dart.notNull(aSnippet[dartx.length]) - 20)); | |
180 } | |
181 let leftLead = "..."; | |
182 let rightTail = "..."; | |
183 if (left <= 10) leftLead = ""; | |
184 if (right <= 10) rightTail = ""; | |
185 let diff = `\nDiff (${left}..${dart.notNull(eLen) - right}/${dart.notNull(
aLen) - right}):\n` + `${leftLead}${leftSnippet}[ ${eSnippet} ]${rightSnippet}${
rightTail}\n` + `${leftLead}${leftSnippet}[ ${aSnippet} ]${rightSnippet}${rightT
ail}`; | |
186 expect.Expect._fail(`${defaultMessage}${diff}`); | |
187 } | |
188 static setEquals(expected, actual, reason) { | |
189 if (reason === void 0) reason = null; | |
190 let missingSet = core.Set.from(expected); | |
191 missingSet.removeAll(actual); | |
192 let extraSet = core.Set.from(actual); | |
193 extraSet.removeAll(expected); | |
194 if (dart.notNull(extraSet.isEmpty) && dart.notNull(missingSet.isEmpty)) re
turn; | |
195 let msg = expect.Expect._getMessage(reason); | |
196 let sb = new core.StringBuffer(`Expect.setEquals(${msg}) fails`); | |
197 if (!dart.notNull(missingSet.isEmpty)) { | |
198 sb.write('\nExpected collection does not contain: '); | |
199 } | |
200 for (let val of missingSet) { | |
201 sb.write(`${val} `); | |
202 } | |
203 if (!dart.notNull(extraSet.isEmpty)) { | |
204 sb.write('\nExpected collection should not contain: '); | |
205 } | |
206 for (let val of extraSet) { | |
207 sb.write(`${val} `); | |
208 } | |
209 expect.Expect._fail(sb.toString()); | |
210 } | |
211 static throws(f, check, reason) { | |
212 if (check === void 0) check = null; | |
213 if (reason === void 0) reason = null; | |
214 let msg = reason == null ? "" : `(${reason})`; | |
215 if (!dart.is(f, expect._Nullary)) { | |
216 expect.Expect._fail(`Expect.throws${msg}: Function f not callable with z
ero arguments`); | |
217 } | |
218 try { | |
219 f(); | |
220 } catch (e) { | |
221 let s = dart.stackTrace(e); | |
222 if (check != null) { | |
223 if (!dart.notNull(dart.dcall(check, e))) { | |
224 expect.Expect._fail(`Expect.throws${msg}: Unexpected '${e}'\n${s}`); | |
225 } | |
226 } | |
227 return; | |
228 } | |
229 | |
230 expect.Expect._fail(`Expect.throws${msg} fails: Did not throw`); | |
231 } | |
232 static _getMessage(reason) { | |
233 return reason == null ? "" : `, '${reason}'`; | |
234 } | |
235 static _fail(message) { | |
236 dart.throw(new expect.ExpectException(message)); | |
237 } | |
238 }; | |
239 dart.setSignature(expect.Expect, { | |
240 statics: () => ({ | |
241 _truncateString: [core.String, [core.String, core.int, core.int, core.int]
], | |
242 _stringDifference: [core.String, [core.String, core.String]], | |
243 equals: [dart.void, [dart.dynamic, dart.dynamic], [core.String]], | |
244 isTrue: [dart.void, [dart.dynamic], [core.String]], | |
245 isFalse: [dart.void, [dart.dynamic], [core.String]], | |
246 isNull: [dart.void, [dart.dynamic], [core.String]], | |
247 isNotNull: [dart.void, [dart.dynamic], [core.String]], | |
248 identical: [dart.void, [dart.dynamic, dart.dynamic], [core.String]], | |
249 fail: [dart.void, [core.String]], | |
250 approxEquals: [dart.void, [core.num, core.num], [core.num, core.String]], | |
251 notEquals: [dart.void, [dart.dynamic, dart.dynamic], [core.String]], | |
252 listEquals: [dart.void, [core.List, core.List], [core.String]], | |
253 mapEquals: [dart.void, [core.Map, core.Map], [core.String]], | |
254 stringEquals: [dart.void, [core.String, core.String], [core.String]], | |
255 setEquals: [dart.void, [core.Iterable, core.Iterable], [core.String]], | |
256 throws: [dart.void, [dart.functionType(dart.void, [])], [expect._CheckExce
ptionFn, core.String]], | |
257 _getMessage: [core.String, [core.String]], | |
258 _fail: [dart.void, [core.String]] | |
259 }), | |
260 names: ['_truncateString', '_stringDifference', 'equals', 'isTrue', 'isFalse
', 'isNull', 'isNotNull', 'identical', 'fail', 'approxEquals', 'notEquals', 'lis
tEquals', 'mapEquals', 'stringEquals', 'setEquals', 'throws', '_getMessage', '_f
ail'] | |
261 }); | |
262 expect._identical = function(a, b) { | |
263 return core.identical(a, b); | |
264 }; | |
265 dart.fn(expect._identical, core.bool, [dart.dynamic, dart.dynamic]); | |
266 expect._CheckExceptionFn = dart.typedef('_CheckExceptionFn', () => dart.functi
onType(core.bool, [dart.dynamic])); | |
267 expect._Nullary = dart.typedef('_Nullary', () => dart.functionType(dart.dynami
c, [])); | |
268 expect.ExpectException = class ExpectException extends core.Object { | |
269 new(message) { | |
270 this.message = message; | |
271 } | |
272 toString() { | |
273 return this.message; | |
274 } | |
275 }; | |
276 expect.ExpectException[dart.implements] = () => [core.Exception]; | |
277 dart.setSignature(expect.ExpectException, { | |
278 constructors: () => ({new: [expect.ExpectException, [core.String]]}) | |
279 }); | |
280 expect.NoInline = class NoInline extends core.Object { | |
281 new() { | |
282 } | |
283 }; | |
284 dart.setSignature(expect.NoInline, { | |
285 constructors: () => ({new: [expect.NoInline, []]}) | |
286 }); | |
287 expect.TrustTypeAnnotations = class TrustTypeAnnotations extends core.Object { | |
288 new() { | |
289 } | |
290 }; | |
291 dart.setSignature(expect.TrustTypeAnnotations, { | |
292 constructors: () => ({new: [expect.TrustTypeAnnotations, []]}) | |
293 }); | |
294 expect.AssumeDynamic = class AssumeDynamic extends core.Object { | |
295 new() { | |
296 } | |
297 }; | |
298 dart.setSignature(expect.AssumeDynamic, { | |
299 constructors: () => ({new: [expect.AssumeDynamic, []]}) | |
300 }); | |
301 // Exports: | |
302 exports.expect = expect; | |
303 }); | |
OLD | NEW |