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