コード例 #1
0
        public void CanGetColorProperty()
        {
            var rubberDuck = new { Color = "yellow" };
            var typedRubberDuck = rubberDuck.As<IDuck>();

            Assert.That(typedRubberDuck.Color == "yellow");
        }
コード例 #2
0
        public void CanInvokeMethod()
        {
            var duck = new
            {
                Color = "white",
                Quack = (Func<string>)(() => "Quack!")
            };

            var typedDuck = duck.As<IDuck>();

            Assert.That("Quack!" == typedDuck.Quack());
        }
コード例 #3
0
        public void CanPassAnonymousMethodsThroughUtilityClass()
        {
            var duck = new
            {
                Quack = Anon.Methods
                (
                    Anon.Method(() => "Quack!"),
                    Anon.Method<int, string>(i => String.Format("{0} quacks!", i))
                )
            };

            var typedDuck = duck.As<IDuck>();

            Assert.That("Quack!" == typedDuck.Quack());
        }
コード例 #4
0
        public void CanInvokeOverloadedMethods()
        {
            var launchPad = new
            {
                Fly = new Delegate[]
                {
                    (Func<Direction>)(() => Direction.West),
                    (Func<Direction, Direction>)(d => d)
                }
            };

            var launchPadMcQuack = launchPad.As<IDuck>();

            Assert.That(Direction.West == launchPadMcQuack.Fly());
            Assert.That(Direction.South == launchPadMcQuack.Fly(Direction.South));
        }
コード例 #5
0
ファイル: QMethodTests.cs プロジェクト: CodeFork/Serenity
        public void ToJSONWorks()
        {
            Assert.AreEqual(Q.ToJSON(12345), "12345", "Number");

            Assert.AreEqual(Q.ToJSON("abcd\"'e"), "\"abcd\\\"'e\"", "String");

            var date = new JsDate(2013, 12, 27, 16, 19, 35, 345);
            Assert.AreEqual(Q.ToJSON(date), "\"" + Q.Externals.FormatISODateTimeUTC(date) + "\"", "Date/Time");

            Assert.AreEqual(Q.ToJSON(12345.6780), "12345.678", "Double");

            object o = new { num = 5, str = "abc", date = date };

            string json = Q.ToJSON(o);
            Assert.IsTrue(Script.TypeOf(json) == "string", "Ensure serialized object is string");

            var deserialized = jQuery.ParseJson(json);
            o.As<dynamic>().date = Q.Externals.FormatISODateTimeUTC(date);
            Assert.AreEqual(deserialized, o, "Compare original object and deserialization");
        }
コード例 #6
0
        public void CanSetColorProperty()
        {
            var lameDuck = new { Color = "whocares" };
            var typedLameDuck = lameDuck.As<IDuck>();

            typedLameDuck.Color = "blue";

            Assert.That("blue" == typedLameDuck.Color);
        }
コード例 #7
0
        public void TestAsFunction()
        {
            using (CaptureConsole) {
                var d = new DynInst();
                var t = d.As<Two>();
                t();

                var p = new {
                    Two = new Func<bool>(() => {
                        Console.WriteLine("In Func<bool> for Two!");
                        return true;
                    })
                };

                var q = p.As<Two>();
                q();

                var z = new {
                };

                // this creates a dummy function now.

                var tz = z.As<Two>();
                tz();

                Func<bool> fTwo = new Func<bool>(() => {
                    Console.WriteLine("In fTwo");
                    return true;
                });

                fTwo.As<Two>()();

                Assert.Throws<Exception>(() => {
                    Func<string> fThree = new Func<string>(() => {
                        Console.WriteLine("In fThree");
                        return "true";
                    });

                    fThree.As<Two>()();
                });
            }
        }