Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: samples/tests/samples/total/total_test_lib.dart

Issue 9537001: Generate dart:html bindings for Dartium as well as Frog. All unittests now pass (or are disabled fo… (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 totalTests() {
2 test('DateUtils', () {
3 _isDate('1/1');
4 _isDate('12/1');
5 _isDate('11/31');
6 _isDate('01/1');
7 _isDate('1/01');
8 _isDate('01/01');
9 _isDate('1-1');
10 _isDate('12-1');
11 _isDate('11-31');
12 _isDate('01-1');
13 _isDate('1-01');
14 _isDate('01-01');
15 _isDate('1/1/1970');
16 _isDate('01/1/1970');
17 _isDate('1/01/1970');
18 _isDate('1/1/70');
19 _isDate('1/01/1970');
20 _isNotDate("1/1A");
21 _isNotDate("11/222");
22 _isNotDate("11/22/");
23 _checkDate('7/7/2011', 40731.0);
24 _checkDate('7/7/11', 40731.0);
25 _checkDate('7/07/2011', 40731.0);
26 _checkDate('07/07/2011', 40731.0);
27 _checkDate('07/7/2011', 40731.0);
28 _checkDate('7-7-2011', 40731.0);
29 _checkDate('7-7-11', 40731.0);
30 _checkDate('7-07-2011', 40731.0);
31 _checkDate('07-07-2011', 40731.0);
32 _checkDate('07-7-2011', 40731.0);
33
34 // Test MM-DD-<current year>
35 double date = DateUtils.parseDate('7/7');
36 double years = (date - 40731.0) / 365.0;
37 // Assert days/365 is close to an integer -- this will be true for > 100 yea rs
38 Expect.isTrue(years - years.floor() < 0.1, "7/7");
39 });
40
41 asyncTest('HtmlUtils', 1, () {
42 Expect.equals("", HtmlUtils.quoteHtml(""));
43 Expect.equals("&lt;", HtmlUtils.quoteHtml("<"));
44 Expect.equals("&lt;&lt;&lt;&lt;", HtmlUtils.quoteHtml("<<<<"));
45 Expect.equals("Hello&lt;World", HtmlUtils.quoteHtml("Hello<World"));
46 Expect.equals("Hello&lt;&lt;World", HtmlUtils.quoteHtml("Hello<<World"));
47 Expect.equals("Hello&lt;W&lt;orld", HtmlUtils.quoteHtml("Hello<W<orld"));
48 Expect.equals("&lt;Hello&lt;World", HtmlUtils.quoteHtml("<Hello<World"));
49 Expect.equals("&lt;&lt;Hello&lt;World&lt;",
50 HtmlUtils.quoteHtml("<<Hello<World<"));
51 Expect.equals("&lt;&lt;Hello&lt;W&lt;orld&lt;",
52 HtmlUtils.quoteHtml("<<Hello<W<orld<"));
53
54 Document doc = window.document;
55 Element body = doc.body;
56 DivElement div = new Element.tag('div');
57 body.nodes.add(div);
58
59 HtmlUtils.setIntegerProperty(div, "left", 100, "px");
60 div.computedStyle.then((CSSStyleDeclaration computedStyle) {
61 String valueAsString = computedStyle.getPropertyValue("left");
62 // FIXME: Test fails, with valueAsString == "auto". However,
63 // setIntegerProperty works when tested in practice, so there is
64 // something wrong with recovering the value.
65 // Expect.equals("100", valueAsString);
66
67 div.remove();
68 callbackDone();
69 });
70 });
71
72 test('StringUtils', () {
73 _assertNumeric("0");
74 _assertNumeric(".0");
75 _assertNumeric("0.0");
76 _assertNumeric(".1");
77 _assertNumeric("1.");
78 _assertNumeric("1.0");
79 _assertNumeric("1e17");
80 _assertNumeric("1.e17");
81 _assertNumeric(".1e17");
82 _assertNumeric("1e+17");
83 _assertNumeric("1.e-17");
84 _assertNumeric(".1e+17");
85 _assertNumeric("12345.17");
86 _assertNumeric("2.3993e-37");
87 _assertNumeric("-12345.17");
88 _assertNumeric("+12345.17");
89 _assertNumeric("-12345.17e2");
90 _assertNumeric("+12345.17e2");
91 _assertNumeric("-12345.17e-2");
92 _assertNumeric("+12345.17e+2");
93 _assertNumeric("-12345.17e+2");
94 _assertNumeric("+12345.17e-2");
95 _assertNonNumeric("");
96 _assertNonNumeric("ABC");
97 _assertNonNumeric("++12345.17");
98 _assertNonNumeric("12345.17.2");
99 _assertNonNumeric("-12345.17e");
100 _assertNonNumeric("-12345.17e-");
101 _assertNonNumeric("-12345.17e+");
102 _assertNonNumeric("-12345.17e2.5");
103 _assertNonNumeric("++12345.17A");
104 _assertNonNumeric("++12345.17");
105 _assertNonNumeric("++12345.17e");
106 _assertNonNumeric("++12345.17");
107 _assertNonNumeric("--12345.17A");
108 _assertNonNumeric("--12345.17");
109 _assertNonNumeric("--12345.17e");
110 _assertNonNumeric("--12345.17");
111 _assertNonNumeric("e17");
112 _assertNonNumeric("E17");
113 _assertNonNumeric(".e17");
114 _assertNonNumeric(".E17");
115 _assertNonNumeric("-e17");
116 _assertNonNumeric("-E17");
117 _assertNonNumeric("+.e17");
118 _assertNonNumeric("+.E17");
119
120 Expect.equals("A", StringUtils.columnString(1));
121 Expect.equals("Z", StringUtils.columnString(26));
122 Expect.equals("AA", StringUtils.columnString(27));
123 Expect.equals("AZ", StringUtils.columnString(52));
124 Expect.equals("BA", StringUtils.columnString(53));
125 Expect.equals("BZ", StringUtils.columnString(78));
126 Expect.equals("ZA", StringUtils.columnString(677));
127 Expect.equals("ZZ", StringUtils.columnString(702));
128 Expect.equals("AAA", StringUtils.columnString(703));
129 Expect.equals("ZZZ", StringUtils.columnString(18278));
130 Expect.equals("AAAA", StringUtils.columnString(18279));
131
132 Expect.equals(-1, StringUtils.compare("a", "b"));
133 Expect.equals(1, StringUtils.compare("b", "a"));
134 Expect.equals(0, StringUtils.compare("a", "a"));
135 Expect.equals(-1, StringUtils.compare("ab", "abb"));
136 Expect.equals(1, StringUtils.compare("abb", "ab"));
137 // Note: StringUtils.compare sorts empty string before non-empty strings,
138 // but the comparator used to sort spreadhseet cells deals with this case
139 // internally and never calls StringUtils.compare with an empty string argum ent
140 Expect.isTrue(StringUtils.compare("", "a") < 0);
141 Expect.isTrue(StringUtils.compare("a", "") > 0);
142 });
143
144 test('Scanner', () {
145 {
146 String input =
147 "ROUND(R1C0 * ((R1C1 / 1234.0000) / (1 - POWER(1 + (R1C1 / 1234.5000)," +
148 " -12 * R1C2))), 2)";
149 Scanner scanner =
150 new Scanner.preserveWhitespace(input,
151 new CellLocation(null, new RowCol(0, 0)));
152 List<Token> tokens = scanner.scan();
153
154 List<String> expected = <String>[
155 "ROUND", "(", "R1C0", " ", "*", " ", "(", "(", "R1C1", " ", "/", " ",
156 "1234", ")", " ", "/", " ", "(", "1", " ", "-", " ", "POWER", "(", "1" ,
157 " ", "+", " ", "(", "R1C1", " ", "/", " ", "1234.5", ")", ",", " ", "-",
158 "12", " ", "*", " ", "R1C2", ")", ")", ")", ",", " ", "2", ")", ];
159
160 int index = 0;
161 tokens.forEach((Token token) {
162 Expect.equals(expected[index++], token.toString());
163 });
164 }
165
166 validate(String input) {
167 Scanner scanner =
168 new Scanner.preserveWhitespace(input, new CellLocation(null,
169 new RowCol(1, 1)));
170 StringBuffer sb = new StringBuffer();
171 while (true) {
172 Token token = scanner.nextToken();
173 if (token == null) {
174 break;
175 }
176 sb.add(token.toString());
177 }
178
179 Expect.equals(input, sb.toString());
180 }
181
182 validate("10 < 12");
183 validate("10 > 12");
184 validate("10 = 12");
185 validate("10 <> 12");
186 validate("10 <= 12");
187 validate("10 >= 12");
188 validate("FALSE");
189 validate("TRUE");
190 validate("ROUND(R1C0*((R1C1/1200)/(1-POWER(1+(R1C1/1200),-12*R1C2))),2)");
191 validate("R1C1 + R2C2");
192 validate("ThisIsAFunction999()");
193 validate("LOG10(1000)");
194 validate("R1C2 R1C2 R1C2 R1C2");
195 validate("R[1]C2 R[-1]C2 R[1]C-2\tR[-1]C-2");
196 validate("R1C[2]R1C[2]R1C[-2]R1C[-2]");
197 validate("R[1]C[2]R[-1]C[2]R[1]C[-2]R[-1]C[-2]");
198 validate("R111C222 R111C222 R111C222 R111C2");
199 validate("R[111]C222:R[-111]C222, R[111]C222:R[-111]C222");
200 validate("R111C[222]R111C[222]R111C[-222]R111C[-222]");
201 validate("(R[111]C[222],R[-111]C[222]:R[111]C[-222], R[-111]C[-222])");
202 validate("R1C0");
203 validate("R[-1]C + 1");
204 // Problem: R[-1]C-R[-1]C[2] : the C- is really C[0]- but looks like C
205 // followed by a (degenerate) number
206 validate("R[-1]C[1] - R[-1]C[2]");
207 validate("RC[-1]* R1C1 / 1200");
208 validate("R1C3 -RC[-1]");
209 });
210
211 test('Parser', () {
212 String input =
213 "ROUND(R1C0*((R1C1/1200)/(1-POWER(1+(R1C1/1200),-12*R1C2))),2)";
214 Scanner scanner = new Scanner(input,
215 new CellLocation(null, new RowCol(1, 1)));
216 Parser parser = new Parser(scanner);
217 List<Token> tokens = parser.parse();
218
219 List<String> expected = [
220 "R1C0", "R1C1", "1200", "/", "1", "1",
221 "R1C1", "1200", "/", "+", "0", "12", "-",
222 "R1C2", "*", "2" /* nargs */, "POWER", "-", "/", "*", "2",
223 "2" /* nargs */, "ROUND"];
224
225 int index = 0;
226 tokens.forEach((Token token) {
227 Expect.equals(expected[index++], token.toString());
228 });
229 });
230
231 test('Functions', () {
232 validate(String formula, double expected) {
233 try {
234 CellLocation location = new CellLocation(null, new RowCol(0, 0));
235 Value value = new StringFormula(formula, location).calculate();
236 double actual = value.asDouble(null);
237 Expect.approxEquals(expected, actual, 0.00001,
238 "Error evaluating formula: ${formula}");
239 } catch (var error) {
240 Expect.fail("Error evaluating formula: ${formula}, error=${error}");
241 }
242 }
243
244 validate("2<5", 1.0);
245 validate("2>5", 0.0);
246 validate("2=5", 0.0);
247 validate("5=5", 1.0);
248 validate("2<>5", 1.0);
249 validate("5<>5", 0.0);
250 validate("2<=1", 0.0);
251 validate("2<=2", 1.0);
252 validate("2<=3", 1.0);
253 validate("2>=1", 1.0);
254 validate("2>=2", 1.0);
255 validate("2>=3", 0.0);
256 validate("FALSE", 0.0);
257 validate("TRUE", 1.0);
258 validate("FALSE()", 0.0);
259 validate("TRUE()", 1.0);
260 validate("NOT(FALSE)", 1.0);
261 validate("NOT(TRUE)", 0.0);
262 validate("NOT(FALSE())", 1.0);
263 validate("NOT(TRUE())", 0.0);
264 validate("AND(FALSE)", 0.0);
265 validate("AND(TRUE)", 1.0);
266 validate("AND(FALSE,FALSE)", 0.0);
267 validate("AND(FALSE,TRUE)", 0.0);
268 validate("AND(TRUE,FALSE)", 0.0);
269 validate("AND(TRUE,TRUE)", 1.0);
270 validate("OR(FALSE,FALSE)", 0.0);
271 validate("OR(FALSE,TRUE)", 1.0);
272 validate("OR(TRUE,FALSE)", 1.0);
273 validate("OR(TRUE,TRUE)", 1.0);
274 validate("IF(TRUE,3,4)", 3.0);
275 validate("IF(FALSE,3,4)", 4.0);
276 validate("IF(TRUE,3)", 3.0);
277 validate("IF(FALSE,3)", 0.0);
278 validate("IF(NOT(FALSE()),3,4)", 3.0);
279 validate("IF(NOT(TRUE()),3,4)", 4.0);
280 // Unary functions
281 validate("5-2", 3.0);
282 validate("5--2", 7.0);
283 validate("ABS(3)", 3.0);
284 validate("ABS(-3)", 3.0);
285 validate("COS(1)", 0.540302306);
286 validate("DEGREES(3)", 171.887339);
287 validate("EVEN(1.5)", 2.0);
288 validate("EVEN(3)", 4.0);
289 validate("EVEN(2)", 2.0);
290 validate("EVEN(-1)", -2.0);
291 validate("EVEN(-2)", -2.0);
292 validate("EXP(0)", 1.0);
293 validate("EXP(1)", 2.71828183);
294 validate("EXP(-1)", 0.367879441);
295 validate("FACT(0)", 1.0);
296 validate("FACT(1)", 1.0);
297 validate("FACT(2)", 2.0);
298 validate("FACT(3)", 6.0);
299 validate("FACT(4)", 24.0);
300 validate("FACTDOUBLE(5)", 15.0);
301 validate("FACTDOUBLE(6)", 48.0);
302 validate("FACTDOUBLE(10)", 3840.0);
303 validate("FACTDOUBLE(0)", 1.0);
304 validate("INT(8.9)", 8.0);
305 validate("INT(-8.9)", -9.0);
306 validate("LN(1)", 0.0);
307 validate("LN(2)", 0.693147181);
308 validate("LOG10(1)", 0.0);
309 validate("LOG10(10)", 1.0);
310 validate("LOG10(20)", 1.30103);
311 validate("ODD(1.5)", 3.0);
312 validate("ODD(3)", 3.0);
313 validate("ODD(2)", 3.0);
314 validate("ODD(-1)", -1.0);
315 validate("ODD(-2)", -3.0);
316 validate("RADIANS(180)", 3.14159265);
317 validate("SIGN(-3)", -1.0);
318 validate("SIGN(0)", 0.0);
319 validate("SIGN(3)", 1.0);
320 validate("SIN(1)", 0.841470985);
321 validate("SQRT(2)", 1.41421356);
322 validate("TANH(0)", 0.0);
323 validate("TANH(LN((1 + SQRT(5))/2))", Math.sqrt(5.0) / 5.0);
324
325 // Binary functions
326 validate("COMBIN(7,3)", 35.0);
327 validate("COMBIN(7,4)", 35.0);
328 validate("LOG(1, 10)", 0.0);
329 validate("LOG(10, 10)", 1.0);
330 validate("LOG(20, 10)", 1.30103);
331 validate("MOD(3, 2)", 1.0);
332 validate("MOD(-3, 2)", 1.0);
333 validate("MOD(3, -2)", -1.0);
334 validate("MOD(-3, -2)", -1.0);
335 validate("QUOTIENT(5,2)", 2.0);
336 // validate("QUOTIENT(4.5,3.1)", 1.0);
337 validate("QUOTIENT(-10,3)", -3.0);
338 validate("ROUND(SQRT(2), 2)", 1.41);
339 validate("ROUND(SQRT(2), 1)", 1.4);
340 validate("POWER(SQRT(2), 2)", 2.0);
341 validate("TRUNC(8.567)", 8.0);
342 validate("TRUNC(8.567, 1)", 8.5);
343 validate("TRUNC(8.567, 2)", 8.56);
344 validate("TRUNC(-8.567)", -8.0);
345 validate("TRUNC(-8.567, 1)", -8.5);
346 validate("TRUNC(-8.567, 2)", -8.56);
347
348 // Date/Time functions
349 validate("HOUR(40730.6789)", 16.0);
350 validate("MINUTE(40730.6789)", 17.0);
351 validate("SECOND(40730.6789)", 36.0);
352 validate("YEAR(DATE(2011,7,4))", 2011.0);
353 validate("MONTH(DATE(2011,7,4))", 7.0);
354 validate("DAY(DATE(2011,7,4))", 4.0);
355
356 // N-ary functions
357 validate("GCD(5,2)", 1.0);
358 validate("GCD(24,36)", 12.0);
359 validate("GCD(24,36,4)", 4.0);
360 validate("GCD(7,1)", 1.0);
361 validate("GCD(5,0)", 5.0);
362 validate("LCM(5,2)", 10.0);
363 validate("LCM(24,36)", 72.0);
364 validate("LCM(24,36,5)", 360.0);
365 validate("MULTINOMIAL(2, 3, 4)", 1260.0);
366 validate("SERIESSUM(PI()/4,0,2,1,-1/FACT(2),1/FACT(4),-1/FACT(6))",
367 0.707103);
368 });
369
370 test('Mortgage', () {
371 SYLKProducer producer = new SYLKProducer();
372 String data = producer.makeExample("mortgage");
373 Spreadsheet spreadsheet = new Spreadsheet();
374 Reader reader = new SYLKReader();
375 reader.loadFromString(spreadsheet, data);
376 Expect.approxEquals(383.05,
377 spreadsheet.getDoubleValue(new RowCol(99, 2)), 0.05);
378 Expect.approxEquals(36977.28,
379 spreadsheet.getDoubleValue(new RowCol(100, 2)), 0.05);
380 Expect.approxEquals(6976.86,
381 spreadsheet.getDoubleValue(new RowCol(100, 3)), 0.05);
382 Expect.approxEquals(30000.42,
383 spreadsheet.getDoubleValue(new RowCol(100, 4)), 0.05);
384 });
385
386 test('Vlookup', () {
387 Spreadsheet spreadsheet = new Spreadsheet();
388 spreadsheet.setCellFromContentString(new RowCol(1, 1), "4.14");
389 spreadsheet.setCellFromContentString(new RowCol(2, 1), "4.19");
390 spreadsheet.setCellFromContentString(new RowCol(3, 1), "5.17");
391 spreadsheet.setCellFromContentString(new RowCol(4, 1), "5.77");
392 spreadsheet.setCellFromContentString(new RowCol(5, 1), "6.39");
393 spreadsheet.setCellFromContentString(new RowCol(1, 2), "1");
394 spreadsheet.setCellFromContentString(new RowCol(2, 2), "2");
395 spreadsheet.setCellFromContentString(new RowCol(3, 2), "3");
396 spreadsheet.setCellFromContentString(new RowCol(4, 2), "4");
397 spreadsheet.setCellFromContentString(new RowCol(5, 2), "5");
398
399 // Test exact match: 5.77 ==> 4
400 spreadsheet.setCellFromContentString(new RowCol(6, 1),
401 "=VLOOKUP(5.77,A1:B5,2,0)");
402 Expect.equals(4.0, spreadsheet.getDoubleValue(new RowCol(6, 1)));
403
404 // Test approximate match: 5.177 < 5.3 < 5.77 ==> 3
405 spreadsheet.setCellFromContentString(new RowCol(6, 1),
406 "=VLOOKUP(5.3,A1:B5,2,1)");
407 Expect.equals(3.0, spreadsheet.getDoubleValue(new RowCol(6, 1)));
408
409 // Omit 'range_lookup' flag, behavior is the same as "true"
410 spreadsheet.setCellFromContentString(new RowCol(6, 1),
411 "=VLOOKUP(5.3,A1:B5,2)");
412 Expect.equals(3.0, spreadsheet.getDoubleValue(new RowCol(6, 1)));
413 });
414
415 test('Hlookup', () {
416 Spreadsheet spreadsheet = new Spreadsheet();
417 spreadsheet.setCellFromContentString(new RowCol(1, 1), "4.14");
418 spreadsheet.setCellFromContentString(new RowCol(1, 2), "4.19");
419 spreadsheet.setCellFromContentString(new RowCol(1, 3), "5.17");
420 spreadsheet.setCellFromContentString(new RowCol(1, 4), "5.77");
421 spreadsheet.setCellFromContentString(new RowCol(1, 5), "6.39");
422 spreadsheet.setCellFromContentString(new RowCol(2, 1), "1");
423 spreadsheet.setCellFromContentString(new RowCol(2, 2), "2");
424 spreadsheet.setCellFromContentString(new RowCol(2, 3), "3");
425 spreadsheet.setCellFromContentString(new RowCol(2, 4), "4");
426 spreadsheet.setCellFromContentString(new RowCol(2, 5), "5");
427
428 // Test exact match: 5.77 ==> 4
429 spreadsheet.setCellFromContentString(new RowCol(1, 6),
430 "=HLOOKUP(5.77,A1:E2,2,0)");
431 Expect.equals(4.0, spreadsheet.getDoubleValue(new RowCol(1, 6)));
432
433 // Test approximate match: 5.177 < 5.3 < 5.77 ==> 3
434 spreadsheet.setCellFromContentString(new RowCol(1, 6),
435 "=HLOOKUP(5.3,A1:E2,2,1)");
436 Expect.equals(3.0, spreadsheet.getDoubleValue(new RowCol(1, 6)));
437
438 // Omit 'range_lookup' flag, behavior is the same as "true"
439 spreadsheet.setCellFromContentString(new RowCol(6, 1),
440 "=HLOOKUP(5.3,A1:E2,2)");
441 Expect.equals(3.0, spreadsheet.getDoubleValue(new RowCol(6, 1)));
442 });
443 }
444
445 _checkDate(String date, num value) {
446 Expect.equals(value.toString(), DateUtils.parseDate(date).toString());
447 }
448
449 _isDate(String date) {
450 Expect.isTrue(DateUtils.isDate(date), "DateUtils.isDate(${date})");
451 }
452
453 _isNotDate(String date) {
454 Expect.isFalse(DateUtils.isDate(date), "DateUtils.isDate(${date})");
455 }
456
457 _assertNumeric(String s) {
458 Expect.isTrue(StringUtils.isNumeric(s), 'StringUtils.isNumeric("{$s}")');
459 }
460
461 _assertNonNumeric(String s) {
462 Expect.isFalse(StringUtils.isNumeric(s), '!StringUtils.isNumeric("${s}")');
463 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698