Пример #1
0
        private FREObject RunObjectTests(FREContext ctx, uint argc, FREObject[] argv)
        {
            Trace("***********Start Object test***********");
            var person = argv[0];

            if (person == FREObject.Zero)
            {
                return(FREObject.Zero);
            }

            var newPerson = new FREObject().Init("com.tuarua.Person");

            Trace("New Person.ToString()", newPerson.toString());

            Trace("New Person has property name:",
                  newPerson.hasOwnProperty("name"), newPerson.hasOwnProperty("name") ? "✅" : "❌");
            Trace("New Person is of type CLASS:",
                  newPerson.Type(), newPerson.Type().Equals(FreObjectTypeSharp.Class) ? "✅" : "❌");

            Trace("New Person className is com.tuarua::Person",
                  newPerson.ClassName() == "com.tuarua::Person" ? "✅" : "❌");

            dynamic sharpPerson = new FreObjectSharp("com.tuarua.Person", "Ben McBobster", 80);

            Trace("sharpPerson.RawValue.toString()",
                  ((FREObject)sharpPerson.RawValue()).toString()); //case sensitive, calls as3 toString NOT c# ToString()

            var oldAge = person.GetProp("age").AsInt();

            Trace("Get property as Int :", oldAge, oldAge.Equals(21) ? "✅" : "❌");
            var newAge = oldAge + 10;

            person.SetProp("age", newAge);
            Trace("Set property to Int :", person.GetProp("age").AsInt(),
                  person.GetProp("age").AsInt().Equals(31) ? "✅" : "❌");

            var addition = person.Call("add", 100, 33);

            Trace("Call add :", 131, addition.AsInt().Equals(133) ? "✅" : "❌");

            try {
                var dictionary = person.AsDictionary();
                if (dictionary == null)
                {
                    return(person);
                }
                var city = (Dictionary <string, object>)dictionary["city"];
                if (city == null)
                {
                    return(person);
                }
                var name = city["name"];
                Trace("Get property from Dict :", name, name.Equals("Portland") ? "✅" : "❌");

                var sharpPersonType = sharpPerson.Type();
                Trace("Dynamic Person is of type CLASS:",
                      sharpPersonType, sharpPersonType.Equals(FreObjectTypeSharp.Class) ? "✅" : "❌");
                Trace("Dynamic Person has property name:",
                      sharpPerson.hasOwnProperty("name"), sharpPerson.hasOwnProperty("name") ? "✅" : "❌");

                dynamic            sharpCity     = new FreObjectSharp("com.tuarua.City");
                FreObjectTypeSharp sharpCityType = sharpCity.Type();

                Trace("Dynamic City is of type CLASS:",
                      sharpCityType, sharpCityType.Equals(FreObjectTypeSharp.Class) ? "✅" : "❌");

                sharpCity.name = "San Francisco";

                int    sharpAge      = sharpPerson.age;
                string sharpName     = sharpPerson.name;
                string sharpOptional = sharpPerson.opt;
                double sharpHeight   = sharpPerson.height;
                bool   sharpIsMan    = sharpPerson.isMan;

                Trace("Dynamic age as int:", sharpAge, sharpAge.Equals(80) ? "✅" : "❌");
                Trace("Dynamic name as string:", sharpName, sharpName.Equals("Ben McBobster") ? "✅" : "❌");
                Trace("Dynamic Optional string:", sharpOptional == null,
                      (sharpOptional == null).Equals(true) ? "✅" : "❌");
                Trace("Dynamic height as double:", sharpHeight, sharpHeight.Equals(1.8) ? "✅" : "❌");
                Trace("Dynamic isMan as bool:", sharpIsMan, sharpIsMan.Equals(false) ? "✅" : "❌");

                sharpPerson.age    = 999;
                sharpPerson.height = 1.88;
                sharpPerson.isMan  = true;
                sharpPerson.city   = sharpCity;

                Trace("Dynamic age as int:", (int)sharpPerson.age,
                      ((int)sharpPerson.age).Equals(999) ? "✅" : "❌");
                Trace("Dynamic height as double:", (double)sharpPerson.height,
                      ((double)sharpPerson.height).Equals(1.88) ? "✅" : "❌");
                Trace("Dynamic isMan as bool:", (bool)sharpPerson.isMan,
                      ((bool)sharpPerson.isMan).Equals(true) ? "✅" : "❌");

                sharpPerson.age    = 111.ToFREObject();
                sharpPerson.height = 2;

                Trace("Dynamic age as int:", (int)sharpPerson.age,
                      ((int)sharpPerson.age).Equals(111) ? "✅" : "❌");
                Trace("Dynamic height as double:", (double)sharpPerson.height,
                      ((double)sharpPerson.height).Equals(2) ? "✅" : "❌");
            }
            catch (Exception e) {
                Trace(e.GetType());
                Trace(e.Message);
                Trace(e.Source);
                Trace(e.StackTrace);
            }

            return(sharpPerson.RawValue());
        }