Ejemplo n.º 1
0
        public void Interpretation_ExecutMethodWithConverter()
        {
            var json = @"{ ""method-name"": ""SetVariants"", ""colors"": [""red"", ""green"", ""blue""] }";

            var obj = new MyObject();

            var interpreter = Interpretation.Create <IMyObject>(obj, typeof(MyObjectExtensions));

            interpreter.RegisterConverter((ParameterInfo parameter, string value, out object newValue) =>
            {
                if (parameter.Name == "colors")
                {
                    var colors = JsonConvert.DeserializeObject <string[]>(value);
                    newValue   = colors.Select(c => Color.FromName(c)).ToArray();
                    return(true);
                }

                newValue = null;
                return(false);
            });

            interpreter.Execute(json);

            var expected = new Color[] { Color.Red, Color.Green, Color.Blue };

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(expected[i], obj.Variants[i]);
            }
        }
Ejemplo n.º 2
0
        public void Interpretation_JsonWithoutMethodName()
        {
            var json = @"{ }";
            var obj  = new MyObject();

            var interpreter = Interpretation.Create <IMyObject>(obj, typeof(MyObjectExtensions));
            var result      = interpreter.Execute(json);
        }
Ejemplo n.º 3
0
        public void Interpretation_ExecuteMethodOnInterface()
        {
            var json = @"{ ""method-name"": ""SetName"", ""name"": ""Kees C. Bakker"" }";
            var obj  = new MyObject();

            var interpreter = Interpretation.Create <IMyObject>(obj, typeof(MyObjectExtensions));

            interpreter.Execute(json);

            Assert.AreEqual("Kees C. Bakker", obj.Name);
        }
Ejemplo n.º 4
0
        public void Interpretation_ExecuteClassMethod()
        {
            var json = @"{ ""method-name"": ""SetId"", ""id"": ""42"" }";
            var obj  = new MyObject();

            obj.Name = "Haas";
            obj.Id   = 7;

            var interpreter = Interpretation.Create <IMyObject>(obj, typeof(MyObjectExtensions));
            var result      = interpreter.Execute(json);
        }
Ejemplo n.º 5
0
        public void Interpretation_ExecutMethodWithIConvertable()
        {
            var json = @"{ ""method-name"": ""SetColor"", ""color"": ""red"" }";

            var obj = new MyObject();

            var interpreter = Interpretation.Create <IMyObject>(obj, typeof(MyObjectExtensions));

            interpreter.Execute(json);

            Assert.AreEqual(Color.Red, obj.Color);
        }
Ejemplo n.º 6
0
        public void Interpretation_ExecuteExtensionMethodWithReturnType()
        {
            var json = @"{ ""method-name"": ""Greet"" }";
            var obj  = new MyObject();

            obj.Name = "Haas";

            var interpreter = Interpretation.Create <IMyObject>(obj, typeof(MyObjectExtensions));
            var result      = interpreter.Execute(json);

            Assert.AreEqual(obj.Greet(), result);
        }
Ejemplo n.º 7
0
        public void Interpretation_ExecutMethodWithOptionalParameter()
        {
            var json = @"{ ""method-name"": ""GetColorName"" }";

            var obj = new MyObject();

            obj.SetColor(Color.FromArgb(255, 44, 0));

            var interpreter = Interpretation.Create <IMyObject>(obj, typeof(MyObjectExtensions));
            var result      = interpreter.Execute(json);

            Assert.AreEqual(obj.GetColorName(), result);
        }
Ejemplo n.º 8
0
        public void Interpretation_ExecuteArray()
        {
            var json = @"[{ ""method-name"": ""SetName"", ""name"": ""Kees C. Bakker"" },
                          { ""method-name"": ""ToString"" },
                          { ""method-name"": ""Greet"" }]";

            var obj = new MyObject();

            var interpreter = Interpretation.Create <IMyObject>(obj, typeof(MyObjectExtensions));

            interpreter.Execute(json);

            Assert.AreEqual("Kees C. Bakker", obj.Name);
        }
Ejemplo n.º 9
0
 public void Interpretation_CannotCreateInterpreterForNull()
 {
     var interpreter = Interpretation.Create <IMyObject>(null);
 }
Ejemplo n.º 10
0
 public void Interpretation_CannotCreateInterpreterWithNonExtensionType()
 {
     var obj         = new MyObject();
     var interpreter = Interpretation.Create <IMyObject>(obj, typeof(MyObject));
 }
Ejemplo n.º 11
0
 public void Interpretation_CannotCreateInterpreterOnClassType()
 {
     var obj         = new MyObject();
     var interpreter = Interpretation.Create(obj, typeof(MyObjectExtensions));
 }
Ejemplo n.º 12
0
        public void TestRemoteTimer()
        {
            var json        = "";
            var timer       = new RemoteTimer(10);
            var interpretor = Interpretation.Create <IRemoteTimer>(timer, typeof(IRemoteTimerExtensions));

            Assert.IsFalse(timer.IsRunning);
            Assert.AreEqual(10, timer.Interval);

            /* Start the timer and assert it is running */
            json = @"{ ""method-name"": ""Start"" }";
            interpretor.Execute(json);

            Assert.IsTrue(timer.IsRunning);

            Thread.Sleep(50);

            /* Pause the timer twice and assert running state */

            json = @"{ ""method-name"": ""Pause"" }";
            interpretor.Execute(json);
            Assert.IsFalse(timer.IsRunning);

            json = @"{ ""method-name"": ""Pause"" }";
            interpretor.Execute(json);
            Assert.IsTrue(timer.IsRunning);

            Thread.Sleep(50);

            /* Stop the timer, set a new interval and restart it: */
            json = @"[{ ""method-name"": ""Stop""},
                        { ""method-name"": ""SetInterval"", ""newInterval"": 20 },
                        { ""method-name"": ""Start""}]";

            interpretor.Execute(json);
            Assert.AreEqual(20, timer.Interval);

            Thread.Sleep(100);

            /* Get tick information from timer */
            json = @"{ ""method-name"": ""GetTicksTotal""}";
            var ticks = interpretor.Execute(json);

            Assert.IsNotNull(ticks);
            Assert.IsInstanceOfType(ticks, typeof(int));
            Assert.IsTrue(Convert.ToInt32(ticks) > 0);

            /* Try to execute public method on RemoteTimer */
            bool error = false;

            json = @"{ ""method-name"": ""Dispose"" }";

            try
            {
                interpretor.Execute(json);
            }
            catch { error = true; }

            Assert.IsTrue(error);

            //dispose timer because the test is ready
            timer.Dispose();
        }