public static void ThrownExceptions(Assert assert) { assert.Expect(12); // #230 assert.Throws(TryCatchWithNotCaughtTypedException, "catch me", "A.Typed exception is not Caught"); assert.Ok(IsATry, "A. exception not caught - try section called"); assert.Ok(!IsACatch, "A. exception not caught - catch section not called"); // #229 assert.Throws(TryCatchWithNotCaughtTypedExceptionAndArgument, "catch me", "[#229] B. Typed exception is not Caught; and argument"); assert.Ok(IsBTry, "[#229] B. exception not caught - try section called"); assert.Ok(!IsBCatch, "B. exception not caught - catch section not called"); // #231 assert.Throws(TryCatchWithRethrow, "catch me", "[#231] C. Rethrow"); assert.Ok(IsCTry, "C. exception caught and re-thrown - try section called"); assert.Ok(IsCCatch, "C. exception caught and re-thrown - catch section called"); assert.Throws(TryCatchWithRethrowEx, new Func<object, bool>((error) => { return error.ToString() == "catch me"; }), "D. Rethrow with parameter"); assert.Ok(IsDTry, "D. exception caught and re-thrown - try section called"); assert.Ok(IsDCatch, "D. exception caught and re-thrown - catch section called"); }
public static void TestUseCase(Assert assert) { assert.Expect(4); assert.Throws(() => { Bridge586A.SomeDataStatic = 4; }, "a.SomeDataStatic is external"); assert.Throws(() => { Bridge586A.DoSomethingStatic(); }, "a.DoSomethingStatic() is external"); assert.Throws(() => { Bridge586B.SomeDataStatic = 4; }, "b.SomeDataStatic is external"); assert.Throws(() => { Bridge586B.DoSomethingStatic(); }, "b.DoSomethingStatic() is external"); }
public static void TestUseCase(Assert assert) { assert.Expect(1); object o = Script.Undefined; assert.Throws(() => { var s = (string)o; }, "Unable to cast type 'null' to type String"); }
public static void Test(Assert assert) { assert.Expect(10); List<string> magic1 = new List<string>(); magic1.Insert(magic1.Count, "first"); magic1.Insert(magic1.Count, "second"); assert.Equal(magic1[0], "first", "magic1[0]"); assert.Equal(magic1[1], "second", "magic1[1]"); List<string> magic2 = new List<string>(); magic2.InsertRange(magic2.Count, new[] { "first", "second" }); magic2.InsertRange(magic2.Count, new[] { "third", "fourth" }); assert.Equal(magic2[0], "first", "magic1[0]"); assert.Equal(magic2[1], "second", "magic1[1]"); assert.Equal(magic2[2], "third", "magic1[2]"); assert.Equal(magic2[3], "fourth", "magic1[3]"); assert.Throws(() => { List<string> magic = new List<string>(); magic.Insert(1, "first"); }, "Insert at length + 1"); assert.Throws(() => { List<string> magic = new List<string>(); magic.Insert(-1, "first"); }, "Insert at -1"); assert.Throws(() => { List<string> magic = new List<string>(); magic.InsertRange(1, new[] { "first", "second" }); }, "InsertRange at length + 1"); assert.Throws(() => { List<string> magic = new List<string>(); magic.InsertRange(-1, new[] { "first", "second" }); }, "InsertRange at -1"); }
public static void TestUseCase(Assert assert) { assert.Expect(2); Func<string, Bridge664A> f = s => (Bridge664A)s; // if cast will be emitted then exception will be thrown because Bridge664A is not emitted assert.Equal(f("test"), "test", "Bridge664"); assert.Throws(() => { Bridge664C b = Script.Write<Bridge664C>("{ }"); var s = (Bridge664B)b; }, "Bridge664 Should throw exception"); }
public static void Test(Assert assert) { assert.Expect(26); // TEST var persons = Person.GetPersons(); var person3 = (from p in Person.GetPersons() where p.ID == 3 select p).First(); assert.DeepEqual(person3, Person.GetPersons()[2], "First() with ID = 3"); assert.DeepEqual(persons.First(x => x.ID == 3), Person.GetPersons()[2], "First() with ID = 3 by lambda"); assert.DeepEqual(persons.Where(x => x.ID == 3).First(), Person.GetPersons()[2], "First() with Where() with ID = 3 by lambda"); assert.DeepEqual(persons.First(x => x.Group == "C"), Person.GetPersons()[1], "First() with Group = 'C' by lambda"); assert.Throws(TestLinqElementOperators.ThrowExceptionOnFirst1, "First() should throw exception if no element found"); assert.Throws(TestLinqElementOperators.ThrowExceptionOnFirst2, "First() should throw exception on empty collection"); // TEST assert.DeepEqual(persons.FirstOrDefault(x => x.ID == -1), null, "FirstOrDefault() unexisting element by lambda"); assert.DeepEqual(persons.Where(x => x.ID == -1).FirstOrDefault(), null, "FirstOrDefault() with Where() unexisting element by lambda"); assert.DeepEqual(persons.FirstOrDefault(x => x.Name == "Nemo"), persons[7], "FirstOrDefault() with Name = 'Nemo' by lambda"); assert.DeepEqual(persons.Where(x => x.Name == "Nemo").FirstOrDefault(), persons[7], "FirstOrDefault() with Where() with Name = 'Nemo' by lambda"); assert.DeepEqual((new object [] { }).FirstOrDefault(), null, "FirstOrDefault() within zero-length array by lambda"); // TEST var lastPerson = (from p in Person.GetPersons() select p).Last(); assert.DeepEqual(lastPerson, Person.GetPersons()[7], "Last() person"); assert.DeepEqual(persons.Last(x => x.ID == 4), Person.GetPersons()[3], "Last() with ID = 4 by lambda"); assert.DeepEqual(persons.Last(x => x.Group == "B"), Person.GetPersons()[6], "Last() with Group = 'B' by lambda"); assert.Throws(TestLinqElementOperators.ThrowExceptionOnLast1, "Last() should throw exception if no element found"); assert.Throws(TestLinqElementOperators.ThrowExceptionOnLast2, "Last() should throw exception on empty collection"); // TEST assert.DeepEqual(persons.LastOrDefault(x => x.ID == -1), null, "LastOrDefault() unexisting element by lambda"); assert.DeepEqual(persons.Where(x => x.ID == -1).LastOrDefault(), null, "LastOrDefault() with Where() unexisting element by lambda"); assert.DeepEqual(persons.LastOrDefault(x => x.Name == "Nemo"), persons[7], "LastOrDefault() with Name = 'Nemo' by lambda"); assert.DeepEqual((new object[] { }).LastOrDefault(), null, "LastOrDefault() within zero-length array by lambda"); // TEST int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int elementAt1 = (from n in numbers where n > 5 select n).ElementAt(1); assert.DeepEqual(elementAt1, 8, "ElementAt() should return 8"); assert.Throws(TestLinqElementOperators.ThrowExceptionOnElementAt1, "ElementAt() should throw exception if no element found"); assert.Throws(TestLinqElementOperators.ThrowExceptionOnElementAt2, "ElementAt() should throw exception on empty collection"); // TEST int elementAt1OrDefault = numbers.ElementAtOrDefault(1); assert.DeepEqual(elementAt1OrDefault, 4, "ElementAtOrDefault() should return 4"); // TEST int elementAt2OrDefault = (from n in numbers where n > 5 select n).ElementAtOrDefault(2); assert.DeepEqual(elementAt2OrDefault, 6, "ElementAtOrDefault() should return 6"); // TEST int elementAt100OrDefault = (from n in numbers where n > 5 select n).ElementAtOrDefault(100); assert.DeepEqual(elementAt100OrDefault, 0, "ElementAtOrDefault() should return 0"); }
// String functions public static void Strings(Assert assert) { //In PhantomJS some correct tests failed. We will skip them in this environment. var isPhantomJs = Utilities.BrowserHelper.IsPhantomJs(); var expectedCount = isPhantomJs ? 28 : 48; assert.Expect(expectedCount); // TEST ToLower, ToLowerCase, ToLocaleLowerCase var s = "HELLO".ToLower(); assert.DeepEqual(s, "hello", "'HELLO'.ToLower()"); s = "HELLO".ToLowerCase(); assert.DeepEqual(s, "hello", "'HELLO'.ToLowerCase()"); s = "HELLO".ToLocaleLowerCase(); assert.DeepEqual(s, "hello", "'HELLO'.ToLocaleLowerCase()"); // TEST ToUpper, ToUpperCase, ToLocaleUpperCase s = "hello".ToUpper(); assert.DeepEqual(s, "HELLO", "'hello'.ToUpper()"); s = "hello".ToUpperCase(); assert.DeepEqual(s, "HELLO", "'hello'.ToUpperCase()"); s = "HELLO".ToLocaleUpperCase(); assert.DeepEqual(s, "HELLO", "'hello'.ToLocaleUpperCase()"); s = "Hello Bridge.NET"; // TEST String(string) constructor assert.DeepEqual(new String(s), s, "new String('" + s + "')"); // TEST String(char, count) constructor assert.DeepEqual(new String('-', 4), "----", "new String('-',4)"); // TEST IndexOfAny char[] anyOf = new char[] { 'x', 'b', 'i' }; string sAnyOf = "['x','b','i']"; assert.DeepEqual(s.IndexOfAny(anyOf), 8, "'" + s + "'.IndexOfAny(" + sAnyOf + ")"); assert.Throws(() => s.IndexOfAny(anyOf, 18, 8), "'" + s + "'.IndexOfAny(" + sAnyOf + ")"); assert.Throws(() => s.IndexOfAny(null), "'" + s + "'.IndexOfAny(null)"); s = string.Empty; assert.DeepEqual(s.IndexOfAny(anyOf), -1, "String.Empty.IndexOfAny(" + sAnyOf + ")"); s = null; assert.DeepEqual(s.IndexOfAny(anyOf), -1, "null.IndexOfAny(" + sAnyOf + ")"); // TEST IndexOf s = "Hello Bridge.NET"; assert.DeepEqual(s.IndexOf('e'), 1, "'" + s + "'.IndexOf('e')"); assert.DeepEqual(s.IndexOf("e."), 11, "'" + s + "'.IndexOf('e.')"); assert.DeepEqual(s.IndexOf('e', 6, 8), 11, "'" + s + "'.IndexOf('e', 6, 8)"); assert.Throws(() => s.IndexOf(null), "'" + s + "'.IndexOf(null)"); if (!isPhantomJs) { assert.DeepEqual(s.IndexOf("E", 6, 8, StringComparison.CurrentCultureIgnoreCase), 11, "'" + s + "'.IndexOf('E', 6, 8, StringComparison.CurrentCultureIgnoreCase)"); } s = string.Empty; assert.DeepEqual(s.IndexOf('e'), -1, "String.Empty.IndexOf('e')"); s = null; assert.DeepEqual(s.IndexOf('e'), -1, "null.IndexOf('e')"); // TEST Compare string s1 = "Animal"; string s2 = "animal"; assert.DeepEqual(string.Compare(s1, s2, true), 0, "String.Compare('" + s1 + "', '" + s2 + "', true)"); if (!isPhantomJs) { assert.DeepEqual(string.Compare(s1, s2, false), 1, "String.Compare('" + s1 + "', '" + s2 + "', false)"); } if (!isPhantomJs) { string[] threeIs = new string[3]; threeIs[0] = "\u0069"; threeIs[1] = "\u0131"; threeIs[2] = "\u0049"; StringComparison[] scValues = { StringComparison.CurrentCulture, StringComparison.CurrentCultureIgnoreCase, StringComparison.InvariantCulture, StringComparison.InvariantCultureIgnoreCase, StringComparison.Ordinal, StringComparison.OrdinalIgnoreCase }; int[] expected = { -1, -1, 1, -1, 0, 1, -1, -1, 1, -1, 0, 1, -1, 1, 1, 0, 0, 0 }; int expectedIndex = 0; foreach (StringComparison sc in scValues) { Test(0, 1, sc, threeIs, expected, expectedIndex++, assert); Test(0, 2, sc, threeIs, expected, expectedIndex++, assert); Test(1, 2, sc, threeIs, expected, expectedIndex++, assert); } } // TEST Contains s = "Hello Bridge.NET"; assert.DeepEqual(s.Contains("Bridge"), true, "'" + s + "'.Contains('Bridge')"); assert.DeepEqual(s.Contains(String.Empty), true, "'" + s + "'.Contains(String.Empty)"); assert.DeepEqual(String.Empty.Contains("Bridge"), false, "String.Empty.Contains('Bridge')"); assert.Throws(() => s.Contains(null), "null.Contains('Bridge')"); // TEST Concat s = string.Concat(s, "2", "3", "4"); assert.DeepEqual(s, "Hello Bridge.NET234", "string.Concat()"); s = string.Concat(null,true,3,false); assert.DeepEqual(s, "true3false", "string.Concat()"); s = string.Concat(new string[] { "1", "2", "3", "4", "5" }); assert.DeepEqual(s, "12345", "string.Concat()"); s = string.Concat(new object[] { 1, null, 2, null, 3 }); assert.DeepEqual(s, "123", "string.Concat()"); }
public static void TestToString(Assert assert) { assert.Expect(10); var v1 = new Version("2.4.1128.65537"); var v2 = new Version(100, 200, 300, (400 << 16) + 500); var v3 = new Version(100, 200, 300); var v4 = new Version(100, 200); var v5 = new Version(); assert.Equal(v1.ToString(), "2.4.1128.65537", "c1.ToString()"); assert.Equal(v2.ToString(), "100.200.300.26214900", "c2.ToString()"); assert.Equal(v3.ToString(), "100.200.300", "c3.ToString()"); assert.Equal(v4.ToString(), "100.200", "c4.ToString()"); assert.Equal(v5.ToString(), "0.0", "c5.ToString()"); assert.Equal(v1.ToString(1), "2", "c1.ToString(1)"); assert.Equal(v1.ToString(2), "2.4", "c1.ToString(2)"); assert.Equal(v1.ToString(3), "2.4.1128", "c1.ToString(3)"); assert.Equal(v1.ToString(4), "2.4.1128.65537", "c1.ToString(4)"); assert.Throws(() => { v1.ToString(5); }, "c1.ToString(5)"); }
public static void TestParse(Assert assert) { assert.Expect(6); var s1 = "105.1.1128.65547"; var v1 = new Version(s1); assert.Equal(Version.Parse(s1).ToString(), v1.ToString(), "Version.Parse(s1)"); var s2 = "105.1"; var v2 = new Version(s2); assert.Equal(Version.Parse(s2).ToString(), v2.ToString(), "Version.Parse(s2)"); assert.Throws(() => { Version.Parse("12,123.23.12"); }, "Version.Parse(\"12,123.23.12\")"); Version vp1; var b1 = Version.TryParse("12,123.23.12", out vp1); assert.Equal(b1, false, "b1"); Version vp2; var b2 = Version.TryParse("12.3.2.1", out vp2); assert.Equal(b2, true, "b2"); assert.Equal(vp2.ToString(), "12.3.2.1", "vp2.ToString()"); }
//Check static methods and constructor public static void TestStaticConstructorsAndMethods(Assert assert) { assert.Expect(13); // TEST //Check static fields initialization assert.DeepEqual(ClassA.StatitIntNotInitialized, 0, "#74 StatitInt not initialized"); assert.DeepEqual(ClassA.StatitStringNotInitialized, null, "#74 StatitString not initialized"); assert.DeepEqual(ClassA.CONST_CHAR, 81, "#74 CONST_CHAR Q"); assert.DeepEqual(ClassA.CONST_DECIMAL, 3.123456789324324324, "#74 CONST_DECIMAL 3.123456789324324324m"); // TEST //Check static constructor assert.DeepEqual(ClassA.StaticInt, -340, "StatitInt initialized"); assert.DeepEqual(ClassA.StaticString, "Defined string", "StatitString initialized"); // TEST //Check static methods var a = ClassA.StaticMethod1(678, "ASD", double.NaN); assert.DeepEqual(ClassA.StatitIntNotInitialized, 678, "StatitIntNotInitialized 678"); assert.DeepEqual(ClassA.StatitStringNotInitialized, "ASD", "ClassA.StatitStringNotInitialized ASD"); assert.DeepEqual(a.DoubleA, double.NaN, "DoubleA double.NaN"); a = ClassA.StaticMethod2((object)678, "QWE", 234); assert.DeepEqual(ClassA.StatitIntNotInitialized, 1678, "StatitIntNotInitialized 1678"); assert.DeepEqual(ClassA.StatitStringNotInitialized, "QWE", "ClassA.StatitStringNotInitialized QWE"); assert.DeepEqual(a.DoubleA, 234, "DoubleA 234"); assert.Throws(TestSet1FailureHelper.StaticMethod2Failure, "Unable to cast type String to type Bridge.Int", "Cast exception should occur"); }
//Check instance methods and constructors public static void TestInstanceConstructorsAndMethods(Assert assert) { assert.Expect(26); //Check parameterless constructor var a = new ClassA(); // TEST assert.DeepEqual(a.NumberA, 10, "NumberA 10"); assert.DeepEqual(a.StringA, "Str", "StringA Str"); assert.DeepEqual(a.BoolA, true, "BoolA true"); assert.Ok(a.DoubleA == Double.PositiveInfinity, "DoubleA Double.PositiveInfinity"); assert.DeepEqual(a.DecimalA, -1, "DecimalA Decimal.MinusOne"); assert.Ok(a.Data != null, "Data not null"); assert.DeepEqual(a.Data.Number, 700, "Data.Number 700"); // TEST //Check constructor with parameter assert.Throws(TestSet1FailureHelper.TestConstructor1Failure, "Related should not be null", "Related should not be null"); // TEST //Check constructor with parameter assert.Throws(TestSet1FailureHelper.TestConstructor2Failure, "Should pass six parameters", "Should pass six parameters"); a = new ClassA(150, "151", true, 1.53d, 1.54m, new ClassA.Aux1() { Number = 155 }); assert.DeepEqual(a.NumberA, 150, "NumberA 150"); assert.DeepEqual(a.StringA, "151", "StringA 151"); assert.DeepEqual(a.BoolA, true, "BoolA true"); assert.DeepEqual(a.DoubleA, 1.53, "DoubleA Double.PositiveInfinity"); assert.DeepEqual(a.DecimalA, 1.54, "DecimalA 154"); assert.Ok(a.Data != null, "Data not null"); assert.DeepEqual(a.Data.Number, 155, "Data.Number 155"); // TEST //Check instance methods var b = a.Method1(); assert.Ok(b != null, "b not null"); assert.DeepEqual(b.Number, 2, "b Number 2"); assert.Ok(b.Related != null, "b.Related not null"); assert.DeepEqual(b.Related.Number, 1, "b.Related Number 1"); a.Data = b; assert.DeepEqual(a.Method3(), "2 Has related 1", "Method3 2 Has related 1"); a.Data = null; assert.DeepEqual(a.Method3(), "no data", "Method3 no data"); // TEST //Check [#68] var c68 = new Class68(); assert.DeepEqual(c68.x, 0, "c68.x 0"); assert.DeepEqual(c68.y, 1, "c68.y 1"); // TEST //Check local vars do not get overridden by fields c68.Test(); assert.DeepEqual(c68.x, 0, "c68.x 0"); assert.DeepEqual(c68.y, 1, "c68.y 1"); }