| 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 | 7 |
| 8 | 8 |
| 9 Validate(tag, a, b) { | 9 Validate(tag, a, b) { |
| 10 // tag encodes which parameters are passed in with values a: 111, b: 222. | 10 // tag encodes which parameters are passed in with values a: 111, b: 222. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 class HasMethod { | 29 class HasMethod { |
| 30 | 30 |
| 31 int calls; | 31 int calls; |
| 32 | 32 |
| 33 HasMethod() : calls = 0 {} | 33 HasMethod() : calls = 0 {} |
| 34 | 34 |
| 35 foo(tag, [a = 10, b = 20]) { | 35 foo(tag, [a = 10, b = 20]) { |
| 36 calls += 1; | 36 calls += 1; |
| 37 Validate(tag, a, b); | 37 Validate(tag, a, b); |
| 38 } | 38 } |
| 39 |
| 40 foo2(tag, {a: 10, b: 20}) { |
| 41 calls += 1; |
| 42 Validate(tag, a, b); |
| 43 } |
| 39 } | 44 } |
| 40 | 45 |
| 41 class HasField { | 46 class HasField { |
| 42 | 47 |
| 43 int calls; | 48 int calls; |
| 44 var foo; | 49 var foo, foo2; |
| 45 | 50 |
| 46 HasField() { | 51 HasField() { |
| 47 calls = 0; | 52 calls = 0; |
| 48 foo = makeFoo(this); | 53 foo = makeFoo(this); |
| 54 foo2 = makeFoo2(this); |
| 49 } | 55 } |
| 50 | 56 |
| 51 makeFoo(owner) { | 57 makeFoo(owner) { |
| 52 // This function is closed-over 'owner'. | 58 // This function is closed-over 'owner'. |
| 53 return (tag, [a = 10, b = 20]) { | 59 return (tag, [a = 10, b = 20]) { |
| 54 owner.calls += 1; | 60 owner.calls += 1; |
| 55 Validate(tag, a, b); | 61 Validate(tag, a, b); |
| 56 }; | 62 }; |
| 57 } | 63 } |
| 64 |
| 65 makeFoo2(owner) { |
| 66 // This function is closed-over 'owner'. |
| 67 return (tag, {a: 10, b: 20}) { |
| 68 owner.calls += 1; |
| 69 Validate(tag, a, b); |
| 70 }; |
| 71 } |
| 58 } | 72 } |
| 59 | 73 |
| 60 | 74 |
| 61 class NamedParametersWithConversionsTest { | 75 class NamedParametersWithConversionsTest { |
| 62 | 76 |
| 63 static checkException(thunk) { | 77 static checkException(thunk) { |
| 64 bool threw = false; | 78 bool threw = false; |
| 65 try { | 79 try { |
| 66 thunk(); | 80 thunk(); |
| 67 } catch (e) { | 81 } catch (e) { |
| 68 threw = true; | 82 threw = true; |
| 69 } | 83 } |
| 70 Expect.isTrue(threw); | 84 Expect.isTrue(threw); |
| 71 } | 85 } |
| 72 | 86 |
| 73 static testMethodCallSyntax(a) { | 87 static testMethodCallSyntax(a) { |
| 74 a.foo(''); | 88 a.foo(''); |
| 75 a.foo('a', 111); | 89 a.foo('a', 111); |
| 76 a.foo('ab', 111, 222); | 90 a.foo('ab', 111, 222); |
| 77 a.foo('a', a: 111); | 91 a.foo2('a', a: 111); |
| 78 a.foo('b', b: 222); | 92 a.foo2('b', b: 222); |
| 79 a.foo('ab', a: 111, b: 222); | 93 a.foo2('ab', a: 111, b: 222); |
| 80 a.foo('ab', b: 222, a: 111); | 94 a.foo2('ab', b: 222, a: 111); |
| 81 | 95 |
| 82 Expect.equals(7, a.calls); | 96 Expect.equals(7, a.calls); |
| 83 | 97 |
| 84 checkException(() => a.foo()); // Too few arguments. | 98 checkException(() => a.foo()); // Too few arguments. |
| 85 checkException(() => a.foo('abc', 1, 2, 3)); // Too many arguments. | 99 checkException(() => a.foo('abc', 1, 2, 3)); // Too many arguments. |
| 86 checkException(() => a.foo('c', c: 1)); // Bad name. | 100 checkException(() => a.foo2('c', c: 1)); // Bad name. |
| 87 | 101 |
| 88 Expect.equals(7, a.calls); | 102 Expect.equals(7, a.calls); |
| 89 } | 103 } |
| 90 | 104 |
| 91 static testFunctionCallSyntax(a) { | 105 static testFunctionCallSyntax(a) { |
| 92 var f = a.foo; | 106 var f = a.foo; |
| 107 var f2 = a.foo2; |
| 93 f(''); | 108 f(''); |
| 94 f('a', 111); | 109 f('a', 111); |
| 95 f('ab', 111, 222); | 110 f('ab', 111, 222); |
| 96 f('a', a: 111); | 111 f2('a', a: 111); |
| 97 f('b', b: 222); | 112 f2('b', b: 222); |
| 98 f('ab', a: 111, b: 222); | 113 f2('ab', a: 111, b: 222); |
| 99 f('ab', b: 222, a: 111); | 114 f2('ab', b: 222, a: 111); |
| 100 | 115 |
| 101 Expect.equals(7, a.calls); | 116 Expect.equals(7, a.calls); |
| 102 | 117 |
| 103 checkException(() => f()); // Too few arguments. | 118 checkException(() => f()); // Too few arguments. |
| 104 checkException(() => f('abc', 1, 2, 3)); // Too many arguments. | 119 checkException(() => f('abc', 1, 2, 3)); // Too many arguments. |
| 105 checkException(() => f('c', c: 1)); // Bad name. | 120 checkException(() => f2('c', c: 1)); // Bad name. |
| 106 | 121 |
| 107 Expect.equals(7, a.calls); | 122 Expect.equals(7, a.calls); |
| 108 } | 123 } |
| 109 | 124 |
| 110 static testMain() { | 125 static testMain() { |
| 111 // 'Plain' calls where the method/field syntax matches the object. | 126 // 'Plain' calls where the method/field syntax matches the object. |
| 112 testMethodCallSyntax(new HasMethod()); | 127 testMethodCallSyntax(new HasMethod()); |
| 113 testFunctionCallSyntax(new HasField()); | 128 testFunctionCallSyntax(new HasField()); |
| 114 | 129 |
| 115 // 'Conversion' calls where method/field call syntax does not match the | 130 // 'Conversion' calls where method/field call syntax does not match the |
| 116 // object. | 131 // object. |
| 117 testMethodCallSyntax(new HasField()); | 132 testMethodCallSyntax(new HasField()); |
| 118 testFunctionCallSyntax(new HasMethod()); | 133 testFunctionCallSyntax(new HasMethod()); |
| 119 } | 134 } |
| 120 } | 135 } |
| 121 | 136 |
| 122 main() { | 137 main() { |
| 123 NamedParametersWithConversionsTest.testMain(); | 138 NamedParametersWithConversionsTest.testMain(); |
| 124 } | 139 } |
| OLD | NEW |