| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 // | |
| 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. | |
| 7 | |
| 8 | |
| 9 Validate(tag, a, b) { | |
| 10 // tag encodes which parameters are passed in with values a: 111, b: 222. | |
| 11 if (tag == 'ab') { | |
| 12 Expect.equals(a, 111); | |
| 13 Expect.equals(b, 222); | |
| 14 } | |
| 15 if (tag == 'a') { | |
| 16 Expect.equals(a, 111); | |
| 17 Expect.equals(b, 20); | |
| 18 } | |
| 19 if (tag == 'b') { | |
| 20 Expect.equals(a, 10); | |
| 21 Expect.equals(b, 222); | |
| 22 } | |
| 23 if (tag == '') { | |
| 24 Expect.equals(a, 10); | |
| 25 Expect.equals(b, 20); | |
| 26 } | |
| 27 } | |
| 28 | |
| 29 class HasMethod { | |
| 30 | |
| 31 int calls; | |
| 32 | |
| 33 HasMethod() : calls = 0 {} | |
| 34 | |
| 35 foo(tag, [a = 10, b = 20]) { | |
| 36 calls += 1; | |
| 37 Validate(tag, a, b); | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 class HasField { | |
| 42 | |
| 43 int calls; | |
| 44 var foo; | |
| 45 | |
| 46 HasField() { | |
| 47 calls = 0; | |
| 48 foo = makeFoo(this); | |
| 49 } | |
| 50 | |
| 51 makeFoo(owner) { | |
| 52 // This function is closed-over 'owner'. | |
| 53 return (tag, [a = 10, b = 20]) { | |
| 54 owner.calls += 1; | |
| 55 Validate(tag, a, b); | |
| 56 }; | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 | |
| 61 class NamedParametersWithConversionsTest { | |
| 62 | |
| 63 static checkException(thunk) { | |
| 64 bool threw = false; | |
| 65 try { | |
| 66 thunk(); | |
| 67 } catch (var e) { | |
| 68 threw = true; | |
| 69 } | |
| 70 Expect.isTrue(threw); | |
| 71 } | |
| 72 | |
| 73 static testMethodCallSyntax(a) { | |
| 74 a.foo(''); | |
| 75 a.foo('a', 111); | |
| 76 a.foo('ab', 111, 222); | |
| 77 a.foo('a', a: 111); | |
| 78 a.foo('b', b: 222); | |
| 79 a.foo('ab', a: 111, b: 222); | |
| 80 a.foo('ab', b: 222, a: 111); | |
| 81 | |
| 82 Expect.equals(7, a.calls); | |
| 83 | |
| 84 checkException(() => a.foo()); // Too few arguments. | |
| 85 checkException(() => a.foo('abc', 1, 2, 3)); // Too many arguments. | |
| 86 checkException(() => a.foo('c', c: 1)); // Bad name. | |
| 87 | |
| 88 Expect.equals(7, a.calls); | |
| 89 } | |
| 90 | |
| 91 static testFunctionCallSyntax(a) { | |
| 92 var f = a.foo; | |
| 93 f(''); | |
| 94 f('a', 111); | |
| 95 f('ab', 111, 222); | |
| 96 f('a', a: 111); | |
| 97 f('b', b: 222); | |
| 98 f('ab', a: 111, b: 222); | |
| 99 f('ab', b: 222, a: 111); | |
| 100 | |
| 101 Expect.equals(7, a.calls); | |
| 102 | |
| 103 checkException(() => f()); // Too few arguments. | |
| 104 checkException(() => f('abc', 1, 2, 3)); // Too many arguments. | |
| 105 checkException(() => f('c', c: 1)); // Bad name. | |
| 106 | |
| 107 Expect.equals(7, a.calls); | |
| 108 } | |
| 109 | |
| 110 static testMain() { | |
| 111 // 'Plain' calls where the method/field syntax matches the object. | |
| 112 testMethodCallSyntax(new HasMethod()); | |
| 113 testFunctionCallSyntax(new HasField()); | |
| 114 | |
| 115 // 'Conversion' calls where method/field call syntax does not match the | |
| 116 // object. | |
| 117 testMethodCallSyntax(new HasField()); | |
| 118 testFunctionCallSyntax(new HasMethod()); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 main() { | |
| 123 NamedParametersWithConversionsTest.testMain(); | |
| 124 } | |
| OLD | NEW |