Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Add System.Collections.ObjectModel.ObservableCollection<> test cases to ... #291

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
87 changes: 87 additions & 0 deletions Tests/SimpleTestCases/ObservableCollectionConstructor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using System.Collections.ObjectModel;

public static class Program {

public class CustomClass
{
public string Field1;
public string Field2;

public override string ToString()
{
return Field1 + "/" + Field2;
}
}
public static void Main (string[] args) {

ObservableCollection<string> slist;

slist = new ObservableCollection<string>();
Console.WriteLine(slist.Count);

var list = new System.Collections.Generic.List<string>(10);
Console.WriteLine("String ObservableCollection with 10 elements");
slist = new ObservableCollection<string>(list);
Console.WriteLine(slist.Count);


for (int i = 0; i < slist.Count; i++)
{
PrintBool(slist[i]==null);
}


Console.WriteLine("Int ObservableCollection with 10 elements");
System.Collections.Generic.List<int> intList= new System.Collections.Generic.List<int>(10);
var intObs = new ObservableCollection<int>(intList);
Console.WriteLine(intObs.Count);


for (int i = 0; i < intObs.Count; i++)
{
PrintBool(intObs[i] == 0);
}


Console.WriteLine("String ObservableCollection with initializer ");
slist = new ObservableCollection<string> { "zero", "one", "two", "three" };


for (int i = 0; i < slist.Count; i++)
{
Console.WriteLine(slist[i]);
}


Console.WriteLine("String ObservableCollection with array constructor ");

string[] sArray = { "apple","orange","plum"};

slist = new ObservableCollection<string>(sArray);
for (int i = 0; i < slist.Count; i++)
{
Console.WriteLine(slist[i]);
}

Console.WriteLine("ListWith custom Class Initializer");


ObservableCollection<CustomClass> ccList = new ObservableCollection<CustomClass>() { new CustomClass() { Field1 = "1.first", Field2 = "1.Second" }, new CustomClass() { Field1 = "2.first", Field2 = "2.Second" } };

for (int i = 0; i < ccList.Count; i++)
{
Console.WriteLine(ccList[i].ToString());
}

var cc= new CustomClass() { Field1="one", Field2 = "two" };
ccList = new ObservableCollection<CustomClass>() { cc };
PrintBool(ccList[0]==cc);

}

static void PrintBool(bool b)
{
Console.WriteLine(b ? 1 : 0);
}
}
20 changes: 20 additions & 0 deletions Tests/SimpleTestCases/ObservableCollectionContains.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.ObjectModel;

public static class Program {
public static void Main (string[] args) {

var list = new ObservableCollection<string> { "zero", "one", "two", "three" };

PrintBool(list.Contains("two"));
PrintBool(list.Contains("zero"));
PrintBool(list.Contains("two-shouldNotExits"));
PrintBool(list.Contains("three"));

}

static void PrintBool(bool b)
{
Console.WriteLine(b ? 1 : 0);
}
}
15 changes: 15 additions & 0 deletions Tests/SimpleTestCases/ObservableCollectionGetEnumerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.ObjectModel;

public static class Program {
public static void Main () {
var list1 = new ObservableCollection<string> {
"a", "b", "c"
};
var list2 = new ObservableCollection<string>(list1);
list2.Add("d");

foreach (var s in list2)
Console.WriteLine(s);
}
}
75 changes: 75 additions & 0 deletions Tests/SimpleTestCases/ObservableCollectionICollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

public static class Program
{


public static void Main(string[] args)
{


var slist = new ObservableCollection<string> { "zero", "one", "two", "three" };


for (int i = 0; i < slist.Count; i++)
{
Console.WriteLine(slist[i]);
}


var collection = (ICollection<string>)slist;
Console.WriteLine(collection.Count);
PrintBool(collection.Contains("two"));
PrintBool(collection.Contains("two-shouldNotExits"));

// The following line passes even if IsReadonly is undefined in JSIL.Bootsrap.js, because !undefined seems to be true in JavaScript
// I can not write better unit test because of https://github.com/kevingadd/JSIL/issues/91
// Manual tests (through JavaScript debugger) shows, that the property correctly returns false
PrintBool(collection.IsReadOnly);

Console.WriteLine("ICollection<T>.Add('four)");
collection.Add("four");

for (int i = 0; i < slist.Count; i++)
{
Console.WriteLine(slist[i]);
}

Console.WriteLine("ICollection<T>.Remove('one')");
collection.Remove("one");

for (int i = 0; i < slist.Count; i++)
{
Console.WriteLine(slist[i]);
}

Console.WriteLine("ICollection<T>.CopyTo");

string[] array = new string[collection.Count];
collection.CopyTo(array, 0);

for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}

Console.WriteLine("ICollection<T>.Clear");

Console.WriteLine(collection.Count);
PrintBool(collection.Count == slist.Count);

collection.Clear();

Console.WriteLine(collection.Count);
PrintBool(collection.Count == slist.Count);


}

static void PrintBool(bool b)
{
Console.WriteLine(b ? 1 : 0);
}
}
75 changes: 75 additions & 0 deletions Tests/SimpleTestCases/ObservableCollectionIList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

public static class Program
{


public static void Main(string[] args)
{


var slist = new ObservableCollection<string> { "zero", "one", "two", "three" };



// collection test are already implemented in ListiCollection.cs

// NOTE: JSIL is quite forgiving when implementing the interface - it does not take into account the returning parameter type
// so "object get_Item(i)" can also implement "T get_Item(int)". The listExternals implementation as a little bit
// loosey goosey. To be strict, different types of collection (ArrayList, List<T> should really have slightly
// different method signatures for get_item, set_Item and others - this is currenlty only partially covverred in JSIL.Bootsrap.js)
Console.WriteLine("----- interface ----");

var list = (IList<string>) slist;

Console.WriteLine(list.Count);
Console.WriteLine(list[0]);
Console.WriteLine(list[1]);
Console.WriteLine(list[2]);
Console.WriteLine(list[3]);

Console.WriteLine(list.IndexOf("two"));

list.Insert(1,"inserted");
Console.WriteLine(list.Count);
Console.WriteLine(list[4]);

list.RemoveAt(2);
Console.WriteLine(list.Count);
Console.WriteLine(list[3]);


list[2] = "modified";
Console.WriteLine(list[2]);

// also check item acecssor throw class (not the interface)
slist = new ObservableCollection<string> { "zero", "one", "two", "three" };

Console.WriteLine("----- class ----");

Console.WriteLine(slist.Count);
Console.WriteLine(slist[0]);
Console.WriteLine(slist[1]);
Console.WriteLine(slist[2]);
Console.WriteLine(slist[3]);

Console.WriteLine(slist.IndexOf("two"));

slist.Insert(1,"inserted");
Console.WriteLine(slist.Count);
Console.WriteLine(slist[4]);

slist.RemoveAt(2);
Console.WriteLine(slist.Count);
Console.WriteLine(slist[3]);


slist[2] = "modified";
Console.WriteLine(slist[2]);

}


}
17 changes: 17 additions & 0 deletions Tests/SimpleTestCases/ObservableCollectionIndexOf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.ObjectModel;

public static class Program {
public static void Main (string[] args) {

var list = new ObservableCollection<string> { "zero", "one", "two", "three" };

Console.WriteLine(list.IndexOf("two"));
Console.WriteLine(list.IndexOf("zero"));
Console.WriteLine(list.IndexOf("two-shouldNotExits"));
Console.WriteLine(list.IndexOf("three"));

// Other overloads not implemented yet
}

}
20 changes: 20 additions & 0 deletions Tests/SimpleTestCases/ObservableCollectionIndexOfEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.ObjectModel;

public enum Enum1 {
Value0 = 0,
Value1 = 1,
}

public static class Program {
public static void Main (string[] args) {
var list = new ObservableCollection<Enum1> { Enum1.Value0, Enum1.Value1, Enum1.Value0, (Enum1)3 };

Console.WriteLine(list.IndexOf(Enum1.Value0));
Console.WriteLine(list.IndexOf(Enum1.Value1));
Console.WriteLine(list.IndexOf((Enum1)2));

// Other overloads not implemented yet
}

}
26 changes: 26 additions & 0 deletions Tests/SimpleTestCases/ObservableCollectionInsert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.ObjectModel;

public static class Program {
public static void Main (string[] args) {


var list = new ObservableCollection<string> { "zero", "one", "two", "three" };

Console.WriteLine(list.IndexOf("two"));
Console.WriteLine(list.IndexOf("zero"));
Console.WriteLine(list.IndexOf("three"));

list.Insert(0, "newOne");
Console.WriteLine(list.IndexOf("three"));
Console.WriteLine(list.IndexOf("newOne"));

list.Insert(3, "anotherNewOne");
Console.WriteLine(list.IndexOf("three"));
Console.WriteLine(list.IndexOf("anotherNewOne"));
Console.WriteLine(list.IndexOf("one"));


}

}
Loading