Skip to content

Commit 8715a39

Browse files
spahnkeoliverbock
authored andcommitted
Support default parameters in method invocations
If required parameters of a method were omitted they were filled with null values to match the method signature. This approach did not allow to define default values on method parameters as they were overridden by the null values. Instead we now check first if omitted trailing parameters have default values and use these instead.
1 parent 831c985 commit 8715a39

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

Source/Noesis.Javascript/JavascriptInterop.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,12 @@ JavascriptInterop::Invoker(const v8::FunctionCallbackInfo<Value>& iArgs)
741741
}
742742
}
743743
}
744+
for (int p = suppliedArguments->Length; p < arguments->Length; p++) // set default values if there are optional parameters
745+
{
746+
System::Reflection::ParameterInfo^ parameter = parametersInfo[p];
747+
if (parameter->IsOptional && parameter->HasDefaultValue)
748+
arguments[p] = parameter->DefaultValue;
749+
}
744750

745751
// skip if a conversion failed
746752
if (failed > 0)

Tests/Noesis.Javascript.Tests/ConvertFromJavascriptTests.cs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,30 @@ private class TypedPropertiesClass
1616
public Decimal decimalValue { get; set; }
1717
public float? nullableFloat { get; set; }
1818

19-
public int MethodWithoutParameters()
19+
public int methodWithoutParameters()
2020
{
2121
return 1;
2222
}
2323

24-
public int MethodWithOneParameter(int i)
24+
public int methodWithOneParameter(int i)
2525
{
2626
return i + 1;
2727
}
2828

29-
public string MethodWithMultipleMixedParameters(int i, string s, bool b)
29+
public string methodWithMultipleMixedParameters(int i, string s, bool b)
3030
{
3131
return String.Format("i: {0}, s: {1}, b: {2}", i, s, b);
3232
}
3333

34-
public string MethodWithDefaultParameter(string s = "")
34+
public string methodWithDefaultParameter(string s = "")
3535
{
3636
return s;
3737
}
38+
39+
public string methodWithRequiredAndDefaultParameters(int i, string s = "", bool b = true)
40+
{
41+
return String.Format("i: {0}, s: {1}, b: {2}", i, s, b);
42+
}
3843
}
3944

4045
[TestInitialize]
@@ -191,7 +196,7 @@ public void MethodCallWithoutParameters()
191196
{
192197
var obj = new TypedPropertiesClass();
193198
_context.SetParameter("obj", obj);
194-
var result = _context.Run("obj.MethodWithoutParameters()");
199+
var result = _context.Run("obj.methodWithoutParameters()");
195200

196201
result.Should().Be(1);
197202
}
@@ -201,7 +206,7 @@ public void MethodCallWithParameter()
201206
{
202207
var obj = new TypedPropertiesClass();
203208
_context.SetParameter("obj", obj);
204-
var result = _context.Run("obj.MethodWithOneParameter(1)");
209+
var result = _context.Run("obj.methodWithOneParameter(1)");
205210

206211
result.Should().Be(2);
207212
}
@@ -211,18 +216,17 @@ public void MethodCallWithMixedParameter()
211216
{
212217
var obj = new TypedPropertiesClass();
213218
_context.SetParameter("obj", obj);
214-
var result = _context.Run("obj.MethodWithMultipleMixedParameters(1, 'test', false)");
219+
var result = _context.Run("obj.methodWithMultipleMixedParameters(1, 'test', false)");
215220

216221
result.Should().Be("i: 1, s: test, b: False");
217222
}
218223

219224
[TestMethod]
220-
[Ignore]
221225
public void MethodCallWithDefaultParameter_PassingNoActualParameter()
222226
{
223227
var obj = new TypedPropertiesClass();
224228
_context.SetParameter("obj", obj);
225-
var result = _context.Run("obj.MethodWithDefaultParameter()");
229+
var result = _context.Run("obj.methodWithDefaultParameter()");
226230

227231
result.Should().Be("");
228232
}
@@ -232,7 +236,7 @@ public void MethodCallWithDefaultParameter_PassingActualParameter()
232236
{
233237
var obj = new TypedPropertiesClass();
234238
_context.SetParameter("obj", obj);
235-
var result = _context.Run("obj.MethodWithDefaultParameter('foo')");
239+
var result = _context.Run("obj.methodWithDefaultParameter('foo')");
236240

237241
result.Should().Be("foo");
238242
}
@@ -242,9 +246,29 @@ public void MethodCallWithDefaultParameter_PassingExcplicitNullAsActualParameter
242246
{
243247
var obj = new TypedPropertiesClass();
244248
_context.SetParameter("obj", obj);
245-
var result = _context.Run("obj.MethodWithDefaultParameter(null)");
249+
var result = _context.Run("obj.methodWithDefaultParameter(null)");
246250

247251
result.Should().Be(null);
248252
}
253+
254+
[TestMethod]
255+
public void MethodCallWithRequiredAndDefaultParameters_PassingNoOptionalActualParameter()
256+
{
257+
var obj = new TypedPropertiesClass();
258+
_context.SetParameter("obj", obj);
259+
var result = _context.Run("obj.methodWithRequiredAndDefaultParameters(1)");
260+
261+
result.Should().Be("i: 1, s: , b: True");
262+
}
263+
264+
[TestMethod]
265+
public void MethodCallWithRequiredAndDefaultParameters_PassingAllActualParameter()
266+
{
267+
var obj = new TypedPropertiesClass();
268+
_context.SetParameter("obj", obj);
269+
var result = _context.Run("obj.methodWithRequiredAndDefaultParameters(1, 'test', false)");
270+
271+
result.Should().Be("i: 1, s: test, b: False");
272+
}
249273
}
250274
}

0 commit comments

Comments
 (0)