Skip to content

Commit

Permalink
[browser][wasm] Add Function tests. (dotnet#38323)
Browse files Browse the repository at this point in the history
* Add Function tests.

* Format code.
  • Loading branch information
kjpou1 authored Jun 24, 2020
1 parent 188a3a8 commit e0286fb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;
using Xunit;
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,65 @@ public static void CoreTypes()

var arr10 = new Uint8Array(sharedArr50);
Assert.Equal(50, arr10.Length);

var arr11 = new Uint16Array(sharedArr50);
Assert.Equal(25, arr11.Length);

var arr12 = new Uint32Array(sharedArr40);
Assert.Equal(10, arr12.Length);

var arr13 = new Int8Array(sharedArr50);
Assert.Equal(50, arr13.Length);

var arr14 = new Int16Array(sharedArr40);
Assert.Equal(20, arr14.Length);

var arr15 = new Int32Array(sharedArr40);
Assert.Equal(10, arr15.Length);

var arr16 = new Float32Array(sharedArr40);
Assert.Equal(10, arr16.Length);

var arr17 = new Float64Array(sharedArr40);
Assert.Equal(5, arr17.Length);
}

[Fact]
public static void FunctionSumCall()
public static void FunctionSum()
{
// The Difference Between call() and apply()
// The difference is:
// The call() method takes arguments separately.
// The apply() method takes arguments as an array.
var sum = new Function("a", "b", "return a + b");
Assert.Equal(8, (int)sum.Call(null, 3, 5));

Assert.Equal(13, (int)sum.Apply(null, new object[] { 6, 7 }));
}

[Fact]
public static void FunctionMath()
{
JSObject math = (JSObject)Runtime.GetGlobalObject("Math");
Assert.NotNull(math);

Function mathMax = (Function)math.GetObjectProperty("max");
Assert.NotNull(mathMax);

var maxValue = (int)mathMax.Apply(null, new object[] { 5, 6, 2, 3, 7 });
Assert.Equal(7, maxValue);

maxValue = (int)mathMax.Call(null, 5, 6, 2, 3, 7);
Assert.Equal(7, maxValue);

Function mathMin = (Function)((JSObject)Runtime.GetGlobalObject("Math")).GetObjectProperty("min");
Assert.NotNull(mathMin);

var minValue = (int)mathMin.Apply(null, new object[] { 5, 6, 2, 3, 7 });
Assert.Equal(2, minValue);

minValue = (int)mathMin.Call(null, 5, 6, 2, 3, 7);
Assert.Equal(2, minValue);
}
}
}

0 comments on commit e0286fb

Please sign in to comment.