Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[browser][wasm][tests] Add tests for javascript Array #38806

Merged
merged 1 commit into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
<Compile Include="System\Runtime\InteropServices\JavaScript\JavaScriptTests.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\DataViewTests.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\TypedArrayTests.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\ArrayTests.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
Copy link
Contributor

@MaximLipnin MaximLipnin Jul 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this line is going to be redundant: #38793


using System.Collections.Generic;
using System.Runtime.InteropServices.JavaScript;
using Xunit;

namespace System.Runtime.InteropServices.JavaScript.Tests
{
public static class ArrayTests
{
[Fact]
public static void ArrayLength()
{
Array d = new Array();
Assert.Equal(0, d.Length);
}

[Fact]
public static void ArrayLength1()
{
Array d = new Array(50);
Assert.Equal(50, d.Length);
}

[Fact]
public static void Array_GetSetItem()
{
var jsArray = new Array(7, 8, 9, 10, 11, 12, 13);
IList<int> iList = new int[] { 7, 8, 9, 10, 11, 12, 13 };

Assert.Equal(jsArray.Length, iList.Count);
for (int i = 0; i < iList.Count; i++)
{
Assert.Equal(jsArray[i], iList[i]);

iList[i] = 99;
jsArray[i] = 99;
Assert.Equal(99, iList[i]);
Assert.Equal(99, jsArray[i]);
}
}

[Fact]
public static void Array_GetSetItemInvalid()
{
var jsArray = new Array(7, 8, 9, 10, 11, 12, 13);
Assert.Null(jsArray[-1]);
Assert.Null(jsArray[jsArray.Length]);
Assert.Equal(0, jsArray[-1] = 0);
Assert.Equal(0, jsArray[jsArray.Length] = 0);
}

[Fact]
public static void Array_GetItemIndex()
{
var jsArray = new Array(7, 8, 9, 10, 11, 12, 13);
Assert.Equal(7, jsArray[0]);
Assert.Equal(8, jsArray[1]);
Assert.Equal(9, jsArray[2]);
Assert.Equal(10, jsArray[3]);
Assert.Equal(11, jsArray[4]);
Assert.Equal(12, jsArray[5]);
Assert.Equal(13, jsArray[6]);
}

[Fact]
public static void Array_GetSetItemIndex()
{
var jsArray = new Array(7, 8, 9, 10, 11, 12, 13);
for (int d = 0; d < jsArray.Length; d++)
{
jsArray[d] = (int)jsArray[d] + 1;
}
Assert.Equal(8, jsArray[0]);
Assert.Equal(9, jsArray[1]);
Assert.Equal(10, jsArray[2]);
Assert.Equal(11, jsArray[3]);
Assert.Equal(12, jsArray[4]);
Assert.Equal(13, jsArray[5]);
Assert.Equal(14, jsArray[6]);
}

[Fact]
public static void Array_Pop()
{
var jsArray = new Array(7, 8, 9, 10, 11, 12, 13);
Assert.Equal(13, jsArray.Pop());
Assert.Equal(12, jsArray.Pop());
Assert.Equal(11, jsArray.Pop());
Assert.Equal(10, jsArray.Pop());
Assert.Equal(9, jsArray.Pop());
Assert.Equal(8, jsArray.Pop());
Assert.Equal(7, jsArray.Pop());
Assert.Equal(0, jsArray.Length);
}

[Fact]
public static void Array_PushPop()
{
var objArray = new object[] { "test7", "test8", "test9", "test10", "test11", "test12", "test13" };
var jsArray = new Array();
for (int d = 0; d < objArray.Length; d++)
{
jsArray.Push(objArray[d]);
}
Assert.Equal("test13", jsArray.Pop());
Assert.Equal("test12", jsArray.Pop());
Assert.Equal("test11", jsArray.Pop());
Assert.Equal("test10", jsArray.Pop());
Assert.Equal("test9", jsArray.Pop());
Assert.Equal("test8", jsArray.Pop());
Assert.Equal("test7", jsArray.Pop());
Assert.Equal(0, jsArray.Length);
}

[Fact]
public static void Array_PushShift()
{
var objArray = new object[] { "test7", "test8", "test9", "test10", "test11", "test12", "test13" };
var jsArray = new Array();
for (int d = 0; d < objArray.Length; d++)
{
jsArray.Push(objArray[d]);
}
Assert.Equal("test7", jsArray.Shift());
Assert.Equal("test8", jsArray.Shift());
Assert.Equal("test9", jsArray.Shift());
Assert.Equal("test10", jsArray.Shift());
Assert.Equal("test11", jsArray.Shift());
Assert.Equal("test12", jsArray.Shift());
Assert.Equal("test13", jsArray.Shift());
Assert.Equal(0, jsArray.Length);
}

[Fact]
public static void Array_UnShiftShift()
{
var objArray = new object[] { "test7", "test8", "test9", "test10", "test11", "test12", "test13" };
var jsArray = new Array();
for (int d = 0; d < objArray.Length; d++)
{
Assert.Equal(d + 1, jsArray.UnShift(objArray[d]));
}
Assert.Equal("test13", jsArray.Shift());
Assert.Equal("test12", jsArray.Shift());
Assert.Equal("test11", jsArray.Shift());
Assert.Equal("test10", jsArray.Shift());
Assert.Equal("test9", jsArray.Shift());
Assert.Equal("test8", jsArray.Shift());
Assert.Equal("test7", jsArray.Shift());
Assert.Equal(0, jsArray.Length);
}

[Fact]
public static void Array_IndexOf()
{
var beasts = new Array("ant", "bison", "camel", "duck", "bison");
Assert.Equal(1, beasts.IndexOf("bison"));
Assert.Equal(4, beasts.IndexOf("bison", 2));
Assert.Equal(-1, beasts.IndexOf("giraffe"));
}

[Fact]
public static void Array_LastIndexOf()
{
var beasts = new Array("Dodo", "Tiger", "Penguin", "Dodo");
Assert.Equal(3, beasts.LastIndexOf("Dodo"));
Assert.Equal(1, beasts.LastIndexOf("Tiger"));
Assert.Equal(0, beasts.LastIndexOf("Dodo", 2)); // The array is searched backwards
Assert.Equal(-1, beasts.LastIndexOf("giraffe"));
}
}
}