From a9ce6c1566ecb7a85a4d634fe31c1a5546ddd056 Mon Sep 17 00:00:00 2001 From: alphapage Date: Tue, 19 Nov 2013 02:09:25 -0800 Subject: [PATCH] Add System.Collections.ObjectModel.ObservableCollection<> test cases to help implementation --- .../ObservableCollectionConstructor.cs | 87 +++++++++++++++++++ .../ObservableCollectionContains.cs | 20 +++++ .../ObservableCollectionGetEnumerator.cs | 15 ++++ .../ObservableCollectionICollection.cs | 75 ++++++++++++++++ .../ObservableCollectionIList.cs | 75 ++++++++++++++++ .../ObservableCollectionIndexOf.cs | 17 ++++ .../ObservableCollectionIndexOfEnum.cs | 20 +++++ .../ObservableCollectionInsert.cs | 26 ++++++ .../ObservableCollectionNotifications.cs | 79 +++++++++++++++++ .../ObservableCollectionToArray.cs | 30 +++++++ 10 files changed, 444 insertions(+) create mode 100644 Tests/SimpleTestCases/ObservableCollectionConstructor.cs create mode 100644 Tests/SimpleTestCases/ObservableCollectionContains.cs create mode 100644 Tests/SimpleTestCases/ObservableCollectionGetEnumerator.cs create mode 100644 Tests/SimpleTestCases/ObservableCollectionICollection.cs create mode 100644 Tests/SimpleTestCases/ObservableCollectionIList.cs create mode 100644 Tests/SimpleTestCases/ObservableCollectionIndexOf.cs create mode 100644 Tests/SimpleTestCases/ObservableCollectionIndexOfEnum.cs create mode 100644 Tests/SimpleTestCases/ObservableCollectionInsert.cs create mode 100644 Tests/SimpleTestCases/ObservableCollectionNotifications.cs create mode 100644 Tests/SimpleTestCases/ObservableCollectionToArray.cs diff --git a/Tests/SimpleTestCases/ObservableCollectionConstructor.cs b/Tests/SimpleTestCases/ObservableCollectionConstructor.cs new file mode 100644 index 000000000..41e0f2be4 --- /dev/null +++ b/Tests/SimpleTestCases/ObservableCollectionConstructor.cs @@ -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 slist; + + slist = new ObservableCollection(); + Console.WriteLine(slist.Count); + + var list = new System.Collections.Generic.List(10); + Console.WriteLine("String ObservableCollection with 10 elements"); + slist = new ObservableCollection(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 intList= new System.Collections.Generic.List(10); + var intObs = new ObservableCollection(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 { "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(sArray); + for (int i = 0; i < slist.Count; i++) + { + Console.WriteLine(slist[i]); + } + + Console.WriteLine("ListWith custom Class Initializer"); + + + ObservableCollection ccList = new ObservableCollection() { 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() { cc }; + PrintBool(ccList[0]==cc); + + } + + static void PrintBool(bool b) + { + Console.WriteLine(b ? 1 : 0); + } +} \ No newline at end of file diff --git a/Tests/SimpleTestCases/ObservableCollectionContains.cs b/Tests/SimpleTestCases/ObservableCollectionContains.cs new file mode 100644 index 000000000..5ee9552d4 --- /dev/null +++ b/Tests/SimpleTestCases/ObservableCollectionContains.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.ObjectModel; + +public static class Program { + public static void Main (string[] args) { + + var list = new ObservableCollection { "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); + } +} \ No newline at end of file diff --git a/Tests/SimpleTestCases/ObservableCollectionGetEnumerator.cs b/Tests/SimpleTestCases/ObservableCollectionGetEnumerator.cs new file mode 100644 index 000000000..bd7122d30 --- /dev/null +++ b/Tests/SimpleTestCases/ObservableCollectionGetEnumerator.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.ObjectModel; + +public static class Program { + public static void Main () { + var list1 = new ObservableCollection { + "a", "b", "c" + }; + var list2 = new ObservableCollection(list1); + list2.Add("d"); + + foreach (var s in list2) + Console.WriteLine(s); + } +} \ No newline at end of file diff --git a/Tests/SimpleTestCases/ObservableCollectionICollection.cs b/Tests/SimpleTestCases/ObservableCollectionICollection.cs new file mode 100644 index 000000000..e18b27855 --- /dev/null +++ b/Tests/SimpleTestCases/ObservableCollectionICollection.cs @@ -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 { "zero", "one", "two", "three" }; + + + for (int i = 0; i < slist.Count; i++) + { + Console.WriteLine(slist[i]); + } + + + var collection = (ICollection)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.Add('four)"); + collection.Add("four"); + + for (int i = 0; i < slist.Count; i++) + { + Console.WriteLine(slist[i]); + } + + Console.WriteLine("ICollection.Remove('one')"); + collection.Remove("one"); + + for (int i = 0; i < slist.Count; i++) + { + Console.WriteLine(slist[i]); + } + + Console.WriteLine("ICollection.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.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); + } +} \ No newline at end of file diff --git a/Tests/SimpleTestCases/ObservableCollectionIList.cs b/Tests/SimpleTestCases/ObservableCollectionIList.cs new file mode 100644 index 000000000..788b770e6 --- /dev/null +++ b/Tests/SimpleTestCases/ObservableCollectionIList.cs @@ -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 { "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 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) 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 { "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]); + + } + + +} \ No newline at end of file diff --git a/Tests/SimpleTestCases/ObservableCollectionIndexOf.cs b/Tests/SimpleTestCases/ObservableCollectionIndexOf.cs new file mode 100644 index 000000000..0210ab3b9 --- /dev/null +++ b/Tests/SimpleTestCases/ObservableCollectionIndexOf.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.ObjectModel; + +public static class Program { + public static void Main (string[] args) { + + var list = new ObservableCollection { "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 + } + +} \ No newline at end of file diff --git a/Tests/SimpleTestCases/ObservableCollectionIndexOfEnum.cs b/Tests/SimpleTestCases/ObservableCollectionIndexOfEnum.cs new file mode 100644 index 000000000..2ea94af29 --- /dev/null +++ b/Tests/SimpleTestCases/ObservableCollectionIndexOfEnum.cs @@ -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.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 + } + +} \ No newline at end of file diff --git a/Tests/SimpleTestCases/ObservableCollectionInsert.cs b/Tests/SimpleTestCases/ObservableCollectionInsert.cs new file mode 100644 index 000000000..cd1036b0e --- /dev/null +++ b/Tests/SimpleTestCases/ObservableCollectionInsert.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.ObjectModel; + +public static class Program { + public static void Main (string[] args) { + + + var list = new ObservableCollection { "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")); + + + } + +} \ No newline at end of file diff --git a/Tests/SimpleTestCases/ObservableCollectionNotifications.cs b/Tests/SimpleTestCases/ObservableCollectionNotifications.cs new file mode 100644 index 000000000..d2c88beaf --- /dev/null +++ b/Tests/SimpleTestCases/ObservableCollectionNotifications.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.ObjectModel; + +public static class Program +{ + public static void Main(string[] args) + { + + + var list = new ObservableCollection { "zero", "one", "two", "three" }; + list.CollectionChanged += list_CollectionChanged; + + 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")); + + list.Add("five"); + + list.Move(list.IndexOf("five"), list.IndexOf("one")); + + list.Remove("five"); + + list.RemoveAt(list.IndexOf("two")); + + list.Clear(); + } + + static void list_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) + { + string oldItems = "", newItems = ""; + if (e.OldItems != null) + foreach (var item in e.OldItems) + { + oldItems += item + ","; + } + if (e.NewItems != null) + foreach (var item in e.NewItems) + { + newItems += item + ","; + } + if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) + { + Console.WriteLine("Add:" + e.OldStartingIndex + " " + oldItems + " =>" + e.NewStartingIndex + " " + newItems); + } + else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Move) + { + Console.WriteLine("Move:" + e.OldStartingIndex + " " + oldItems + " =>" + e.NewStartingIndex + " " + newItems); + } + else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) + { + Console.WriteLine("Remove:" + e.OldStartingIndex + " " + oldItems + " =>" + e.NewStartingIndex + " " + newItems); + } + else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace) + { + Console.WriteLine("Replace:" + e.OldStartingIndex + " " + oldItems + " =>" + e.NewStartingIndex + " " + newItems); + } + else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset) + { + Console.WriteLine("Reset:" + e.OldStartingIndex + " " + oldItems + " =>" + e.NewStartingIndex + " " + newItems); + } + + string str = "ObservableCollection:"; + foreach (var item in ((ObservableCollection)sender)) + { + str += item + ","; + } + Console.WriteLine(str); + } + +} \ No newline at end of file diff --git a/Tests/SimpleTestCases/ObservableCollectionToArray.cs b/Tests/SimpleTestCases/ObservableCollectionToArray.cs new file mode 100644 index 000000000..f75fa2b02 --- /dev/null +++ b/Tests/SimpleTestCases/ObservableCollectionToArray.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.ObjectModel; +using System.Linq; + +public static class Program { + public static void Main (string[] args) { + + var intList = new ObservableCollection() { 1, 2, 3, 4 }; + + Console.WriteLine(intList.Count); + + + int[] intArr = intList.ToArray(); // System.Linq.Enumerable::ToArray + + Console.WriteLine(intArr.Length); + Console.WriteLine(intArr[0]); + Console.WriteLine(intArr[1]); + Console.WriteLine(intArr[2]); + Console.WriteLine(intArr[3]); + + intArr = intList.ToArray(); // System.Collections.Generic.List`1::ToArray() + + Console.WriteLine(intArr.Length); + Console.WriteLine(intArr[0]); + Console.WriteLine(intArr[1]); + Console.WriteLine(intArr[2]); + Console.WriteLine(intArr[3]); + } + +} \ No newline at end of file