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

Side by Side Diff: tests/language/list_test.dart

Issue 10890009: Rewrite language/list_test to use Expect.throws, and to allow a TypeError exception in checked mode. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 3 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 // Dart test program for testing arrays. 4 // Dart test program for testing arrays.
5 5
6 class ListTest { 6 class ListTest {
7 static void TestIterator() { 7 static void TestIterator() {
8 List<int> a = new List<int>(10); 8 List<int> a = new List<int>(10);
9 int count = 0; 9 int count = 0;
10 10
(...skipping 30 matching lines...) Expand all
41 } 41 }
42 42
43 static void testMain() { 43 static void testMain() {
44 int len = 10; 44 int len = 10;
45 List a = new List(len); 45 List a = new List(len);
46 Expect.equals(true, a is List); 46 Expect.equals(true, a is List);
47 Expect.equals(len, a.length); 47 Expect.equals(len, a.length);
48 a.forEach(f(element) { Expect.equals(null, element); }); 48 a.forEach(f(element) { Expect.equals(null, element); });
49 a[1] = 1; 49 a[1] = 1;
50 Expect.equals(1, a[1]); 50 Expect.equals(1, a[1]);
51 bool exception_caught = false; 51 Expect.throws(() => a[len], (e) => e is IndexOutOfRangeException);
52 try {
53 var x = a[len];
54 } catch (IndexOutOfRangeException e) {
55 exception_caught = true;
56 }
57 Expect.equals(true, exception_caught);
58 52
59 exception_caught = false; 53 Expect.throws(() {
60 try {
61 List a = new List(4); 54 List a = new List(4);
62 a.setRange(1, 1, a, null); 55 a.setRange(1, 1, a, null);
63 } catch (Exception e) { 56 }, (e) => true);
ngeoffray 2012/08/28 14:17:07 Do you need to add this closure?
Bill Hesse 2012/08/28 14:31:58 No, I don't. Thanks for pointing that out.
64 exception_caught = true;
65 }
66 Expect.equals(true, exception_caught);
67 57
68 exception_caught = false; 58 Expect.throws(() {
69 try {
70 List a = new List(4); 59 List a = new List(4);
71 a.setRange(10, 1, a, 1); 60 a.setRange(10, 1, a, 1);
72 } catch (IndexOutOfRangeException e) { 61 }, (e) => e is IndexOutOfRangeException);
73 exception_caught = true;
74 }
75 Expect.equals(true, exception_caught);
76 62
77 exception_caught = false; 63 a = new List(4);
78 try { 64 List b = new List(4);
79 List a = new List(4); 65 b.setRange(0, 4, a, 0);
80 List b = new List(4);
81 b.setRange(0, 4, a, 0);
82 } catch (var e) {
83 exception_caught = true;
84 }
85 Expect.equals(false, exception_caught);
86 66
87 List<int> unsorted = [4, 3, 9, 12, -4, 9]; 67 List<int> unsorted = [4, 3, 9, 12, -4, 9];
88 int compare(a, b) { 68 int compare(a, b) {
89 if (a < b) return -1; 69 if (a < b) return -1;
90 if (a > b) return 1; 70 if (a > b) return 1;
91 return 0; 71 return 0;
92 } 72 }
93 unsorted.sort(compare); 73 unsorted.sort(compare);
94 Expect.equals(6, unsorted.length); 74 Expect.equals(6, unsorted.length);
95 Expect.equals(-4, unsorted[0]); 75 Expect.equals(-4, unsorted[0]);
96 Expect.equals(12, unsorted[unsorted.length - 1]); 76 Expect.equals(12, unsorted[unsorted.length - 1]);
97 int compare2(a, b) { 77 int compare2(a, b) {
98 if (a < b) return 1; 78 if (a < b) return 1;
99 if (a > b) return -1; 79 if (a > b) return -1;
100 return 0; 80 return 0;
101 } 81 }
102 unsorted.sort(compare2); 82 unsorted.sort(compare2);
103 Expect.equals(12, unsorted[0]); 83 Expect.equals(12, unsorted[0]);
104 Expect.equals(-4, unsorted[unsorted.length - 1]); 84 Expect.equals(-4, unsorted[unsorted.length - 1]);
105 Set<int> t = new Set<int>.from(unsorted); 85 Set<int> t = new Set<int>.from(unsorted);
106 Expect.equals(true, t.contains(9)); 86 Expect.equals(true, t.contains(9));
107 Expect.equals(true, t.contains(-4)); 87 Expect.equals(true, t.contains(-4));
108 Expect.equals(false, t.contains(-3)); 88 Expect.equals(false, t.contains(-3));
109 Expect.equals(6, unsorted.length); 89 Expect.equals(6, unsorted.length);
110 Expect.equals(5, t.length); 90 Expect.equals(5, t.length);
111 TestIterator(); 91 TestIterator();
112 int element = unsorted[2]; 92 int element = unsorted[2];
113 Expect.equals(9, element); 93 Expect.equals(9, element);
114 bool exceptionCaught = false;
115 try {
116 element = unsorted[2.1];
117 } catch (IllegalArgumentException e) {
118 exceptionCaught = true;
119 } catch (TypeError e) {
120 // For type checked mode.
121 exceptionCaught = true;
122 }
123 Expect.equals(true, exceptionCaught);
124 94
125 exceptionCaught = false; 95 Expect.throws(() => unsorted[2.1],
126 try { 96 (e) => e is IllegalArgumentException || e is TypeError);
127 var a = new List(-1);
128 } catch (Exception e) { // Must agree which exception to throw.
129 exceptionCaught = true;
130 }
131 Expect.equals(true, exceptionCaught);
132 97
133 exceptionCaught = false; 98 Expect.throws(() => new List(-1), (e) => true);
134 try { 99 Expect.throws(() => new List(99999999999999999999999), (e) => true);
ngeoffray 2012/08/28 14:17:07 ditto
135 var a = new List(99999999999999999999999); // Non-Smi.
136 } catch (Exception e) { // Must agree which exception to throw.
137 exceptionCaught = true;
138 }
139 Expect.equals(true, exceptionCaught);
140 100
141 exceptionCaught = false;
142 List list = new List(); 101 List list = new List();
143 try { 102 // We cannot write just 'list.removeLast' due to issue 3769.
144 list.removeLast(); 103 Expect.throws(() => list.removeLast(),
145 } catch (IndexOutOfRangeException e) { 104 (e) => e is IndexOutOfRangeException);
146 exceptionCaught = true;
147 }
148 Expect.equals(0, list.length); 105 Expect.equals(0, list.length);
149 Expect.equals(true, exceptionCaught);
150 } 106 }
151 } 107 }
152 108
153 main() { 109 main() {
154 ListTest.testMain(); 110 ListTest.testMain();
155 } 111 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698