OLD | NEW |
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 // | 4 // |
5 // Test named arguments work as expected regardless of whether the function or | 5 // Test named arguments work as expected regardless of whether the function or |
6 // method is called via function call syntax or method call syntax. | 6 // method is called via function call syntax or method call syntax. |
7 // VMOptions=--optimization-counter-threshold=10 | 7 // VMOptions=--optimization-counter-threshold=10 |
8 | 8 |
9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
10 | 10 |
11 | |
12 Validate(tag, a, b) { | 11 Validate(tag, a, b) { |
13 // tag encodes which parameters are passed in with values a: 111, b: 222. | 12 // tag encodes which parameters are passed in with values a: 111, b: 222. |
14 if (tag == 'ab') { | 13 if (tag == 'ab') { |
15 Expect.equals(a, 111); | 14 Expect.equals(a, 111); |
16 Expect.equals(b, 222); | 15 Expect.equals(b, 222); |
17 } | 16 } |
18 if (tag == 'a') { | 17 if (tag == 'a') { |
19 Expect.equals(a, 111); | 18 Expect.equals(a, 111); |
20 Expect.equals(b, 20); | 19 Expect.equals(b, 20); |
21 } | 20 } |
22 if (tag == 'b') { | 21 if (tag == 'b') { |
23 Expect.equals(a, 10); | 22 Expect.equals(a, 10); |
24 Expect.equals(b, 222); | 23 Expect.equals(b, 222); |
25 } | 24 } |
26 if (tag == '') { | 25 if (tag == '') { |
27 Expect.equals(a, 10); | 26 Expect.equals(a, 10); |
28 Expect.equals(b, 20); | 27 Expect.equals(b, 20); |
29 } | 28 } |
30 } | 29 } |
31 | 30 |
32 class HasMethod { | 31 class HasMethod { |
33 | |
34 int calls; | 32 int calls; |
35 | 33 |
36 HasMethod() : calls = 0 {} | 34 HasMethod() : calls = 0 {} |
37 | 35 |
38 foo(tag, [a = 10, b = 20]) { | 36 foo(tag, [a = 10, b = 20]) { |
39 calls += 1; | 37 calls += 1; |
40 Validate(tag, a, b); | 38 Validate(tag, a, b); |
41 } | 39 } |
42 | 40 |
43 foo2(tag, {a: 10, b: 20}) { | 41 foo2(tag, {a: 10, b: 20}) { |
44 calls += 1; | 42 calls += 1; |
45 Validate(tag, a, b); | 43 Validate(tag, a, b); |
46 } | 44 } |
47 } | 45 } |
48 | 46 |
49 class HasField { | 47 class HasField { |
50 | |
51 int calls; | 48 int calls; |
52 var foo, foo2; | 49 var foo, foo2; |
53 | 50 |
54 HasField() { | 51 HasField() { |
55 calls = 0; | 52 calls = 0; |
56 foo = makeFoo(this); | 53 foo = makeFoo(this); |
57 foo2 = makeFoo2(this); | 54 foo2 = makeFoo2(this); |
58 } | 55 } |
59 | 56 |
60 makeFoo(owner) { | 57 makeFoo(owner) { |
61 // This function is closed-over 'owner'. | 58 // This function is closed-over 'owner'. |
62 return (tag, [a = 10, b = 20]) { | 59 return (tag, [a = 10, b = 20]) { |
63 owner.calls += 1; | 60 owner.calls += 1; |
64 Validate(tag, a, b); | 61 Validate(tag, a, b); |
65 }; | 62 }; |
66 } | 63 } |
67 | 64 |
68 makeFoo2(owner) { | 65 makeFoo2(owner) { |
69 // This function is closed-over 'owner'. | 66 // This function is closed-over 'owner'. |
70 return (tag, {a: 10, b: 20}) { | 67 return (tag, {a: 10, b: 20}) { |
71 owner.calls += 1; | 68 owner.calls += 1; |
72 Validate(tag, a, b); | 69 Validate(tag, a, b); |
73 }; | 70 }; |
74 } | 71 } |
75 } | 72 } |
76 | 73 |
77 | |
78 class NamedParametersWithConversionsTest { | 74 class NamedParametersWithConversionsTest { |
79 | |
80 static checkException(thunk) { | 75 static checkException(thunk) { |
81 bool threw = false; | 76 bool threw = false; |
82 try { | 77 try { |
83 thunk(); | 78 thunk(); |
84 } catch (e) { | 79 } catch (e) { |
85 threw = true; | 80 threw = true; |
86 } | 81 } |
87 Expect.isTrue(threw); | 82 Expect.isTrue(threw); |
88 } | 83 } |
89 | 84 |
90 static testMethodCallSyntax(a) { | 85 static testMethodCallSyntax(a) { |
91 a.foo(''); | 86 a.foo(''); |
92 a.foo('a', 111); | 87 a.foo('a', 111); |
93 a.foo('ab', 111, 222); | 88 a.foo('ab', 111, 222); |
94 a.foo2('a', a: 111); | 89 a.foo2('a', a: 111); |
95 a.foo2('b', b: 222); | 90 a.foo2('b', b: 222); |
96 a.foo2('ab', a: 111, b: 222); | 91 a.foo2('ab', a: 111, b: 222); |
97 a.foo2('ab', b: 222, a: 111); | 92 a.foo2('ab', b: 222, a: 111); |
98 | 93 |
99 Expect.equals(7, a.calls); | 94 Expect.equals(7, a.calls); |
100 | 95 |
101 checkException(() => a.foo()); // Too few arguments. | 96 checkException(() => a.foo()); // Too few arguments. |
102 checkException(() => a.foo('abc', 1, 2, 3)); // Too many arguments. | 97 checkException(() => a.foo('abc', 1, 2, 3)); // Too many arguments. |
103 checkException(() => a.foo2('c', c: 1)); // Bad name. | 98 checkException(() => a.foo2('c', c: 1)); // Bad name. |
104 checkException(() => a.foo2('c', a:111, c: 1)); // Bad name. | 99 checkException(() => a.foo2('c', a: 111, c: 1)); // Bad name. |
105 | 100 |
106 Expect.equals(7, a.calls); | 101 Expect.equals(7, a.calls); |
107 } | 102 } |
108 | 103 |
109 static testFunctionCallSyntax(a) { | 104 static testFunctionCallSyntax(a) { |
110 var f = a.foo; | 105 var f = a.foo; |
111 var f2 = a.foo2; | 106 var f2 = a.foo2; |
112 f(''); | 107 f(''); |
113 f('a', 111); | 108 f('a', 111); |
114 f('ab', 111, 222); | 109 f('ab', 111, 222); |
(...skipping 22 matching lines...) Expand all Loading... |
137 testMethodCallSyntax(new HasField()); | 132 testMethodCallSyntax(new HasField()); |
138 testFunctionCallSyntax(new HasMethod()); | 133 testFunctionCallSyntax(new HasMethod()); |
139 } | 134 } |
140 } | 135 } |
141 | 136 |
142 main() { | 137 main() { |
143 for (var i = 0; i < 20; i++) { | 138 for (var i = 0; i < 20; i++) { |
144 NamedParametersWithConversionsTest.testMain(); | 139 NamedParametersWithConversionsTest.testMain(); |
145 } | 140 } |
146 } | 141 } |
OLD | NEW |