Ejemplo n.º 1
0
        public void TestParseWithEscapes()
        {
            var success = UpdaterParser.TryParse(@"Name=\;", out var parsedExpressions, out var valueArray);

            Assert.IsTrue(success);
            Assert.AreEqual(1, parsedExpressions.Count);
            Assert.AreEqual(2, valueArray.Length);
            Assert.AreEqual("Name", parsedExpressions[0].Variable);
            Assert.AreEqual(1, parsedExpressions[0].Index);
            Assert.AreEqual(@";", valueArray[1]);

            success = UpdaterParser.TryParse(@"Name=\\;", out parsedExpressions, out valueArray);
            Assert.IsTrue(success);
            Assert.AreEqual(1, parsedExpressions.Count);
            Assert.AreEqual(2, valueArray.Length);
            Assert.AreEqual("Name", parsedExpressions[0].Variable);
            Assert.AreEqual(1, parsedExpressions[0].Index);
            Assert.AreEqual(@"\", valueArray[1]);

            success = UpdaterParser.TryParse(@"Name=\\\;", out parsedExpressions, out valueArray);
            Assert.IsTrue(success);
            Assert.AreEqual(1, parsedExpressions.Count);
            Assert.AreEqual(2, valueArray.Length);
            Assert.AreEqual("Name", parsedExpressions[0].Variable);
            Assert.AreEqual(1, parsedExpressions[0].Index);
            Assert.AreEqual(@"\;", valueArray[1]);
        }
Ejemplo n.º 2
0
        public void TestBasicParse()
        {
            var success = UpdaterParser.TryParse("Name", out _, out _);

            Assert.IsFalse(success);
            success = UpdaterParser.TryParse("Name=roy;Age", out _, out _);
            Assert.IsFalse(success);
            success = UpdaterParser.TryParse("Name=roy;;", out _, out _);
            Assert.IsFalse(success);

            success = UpdaterParser.TryParse("Name=", out var parsedExpressions, out var valueArray);
            Assert.IsTrue(success);
            Assert.AreEqual(1, parsedExpressions.Count);
            Assert.AreEqual(2, valueArray.Length);
            Assert.AreEqual("Name", parsedExpressions[0].Variable);
            Assert.AreEqual(1, parsedExpressions[0].Index);
            Assert.AreEqual("", valueArray[1]);

            success = UpdaterParser.TryParse("Name=roy", out parsedExpressions, out valueArray);
            Assert.IsTrue(success);
            Assert.AreEqual(1, parsedExpressions.Count);
            Assert.AreEqual(2, valueArray.Length);
            Assert.AreEqual("Name", parsedExpressions[0].Variable);
            Assert.AreEqual(1, parsedExpressions[0].Index);
            Assert.AreEqual("roy", valueArray[1]);

            success = UpdaterParser.TryParse("Name=roy;", out parsedExpressions, out valueArray);
            Assert.IsTrue(success);
            Assert.AreEqual(1, parsedExpressions.Count);
            Assert.AreEqual(2, valueArray.Length);
            Assert.AreEqual("Name", parsedExpressions[0].Variable);
            Assert.AreEqual(1, parsedExpressions[0].Index);
            Assert.AreEqual("roy", valueArray[1]);

            success = UpdaterParser.TryParse("Name=roy;Age=", out parsedExpressions, out valueArray);
            Assert.IsTrue(success);
            Assert.AreEqual(2, parsedExpressions.Count);
            Assert.AreEqual(3, valueArray.Length);
            Assert.AreEqual("Name", parsedExpressions[0].Variable);
            Assert.AreEqual(1, parsedExpressions[0].Index);
            Assert.AreEqual("roy", valueArray[1]);
            Assert.AreEqual("Age", parsedExpressions[1].Variable);
            Assert.AreEqual(2, parsedExpressions[1].Index);
            Assert.AreEqual("", valueArray[2]);
        }
Ejemplo n.º 3
0
        public void TestUpdaterBuilderBasicCase()
        {
            var user = new User {
                Age = 18, Name = "Roy"
            };

            var parseSuccess = UpdaterParser.TryParse("Age=99;Name=xxx", out var parsedExpressions, out var valueArray);

            Assert.IsTrue(parseSuccess);
            var dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();

            valueArray[0] = user;
            dynamicFunction.DynamicInvoke(user, "24", "roy\\");
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(DateTime.MinValue, user.BirthDay);
            Assert.AreEqual(Guid.Empty, user.ID);
            Assert.AreEqual(0, (int)user.Location);
            Assert.AreEqual(@"roy\", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"Age=;", out parsedExpressions, out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(0, user.Age);
            Assert.AreEqual(DateTime.MinValue, user.BirthDay);
            Assert.AreEqual(Guid.Empty, user.ID);
            Assert.AreEqual(0, (int)user.Location);
            Assert.AreEqual(@"roy\", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"Age=invalid_value;", out parsedExpressions, out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(0, user.Age);
            Assert.AreEqual(DateTime.MinValue, user.BirthDay);
            Assert.AreEqual(Guid.Empty, user.ID);
            Assert.AreEqual(0, (int)user.Location);
            Assert.AreEqual(@"roy\", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"Age=24;", out parsedExpressions, out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(DateTime.MinValue, user.BirthDay);
            Assert.AreEqual(Guid.Empty, user.ID);
            Assert.AreEqual(0, (int)user.Location);
            Assert.AreEqual(@"roy\", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"BirthDay=;", out parsedExpressions, out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(DateTime.MinValue, user.BirthDay);
            Assert.AreEqual(Guid.Empty, user.ID);
            Assert.AreEqual(0, (int)user.Location);
            Assert.AreEqual(@"roy\", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"BirthDay=invalid_value;", out parsedExpressions, out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(DateTime.MinValue, user.BirthDay);
            Assert.AreEqual(Guid.Empty, user.ID);
            Assert.AreEqual(0, (int)user.Location);
            Assert.AreEqual(@"roy\", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"BirthDay=5/22/2015 12:00:00 AM;", out parsedExpressions,
                                                  out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(new DateTime(2015, 5, 22), user.BirthDay);
            Assert.AreEqual(Guid.Empty, user.ID);
            Assert.AreEqual(0, (int)user.Location);
            Assert.AreEqual(@"roy\", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"ID=;", out parsedExpressions, out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(new DateTime(2015, 5, 22), user.BirthDay);
            Assert.AreEqual(Guid.Empty, user.ID);
            Assert.AreEqual(0, (int)user.Location);
            Assert.AreEqual(@"roy\", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"ID=invalid_id;", out parsedExpressions,
                                                  out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(new DateTime(2015, 5, 22), user.BirthDay);
            Assert.AreEqual(Guid.Empty, user.ID);
            Assert.AreEqual(0, (int)user.Location);
            Assert.AreEqual(@"roy\", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"ID=db4f8676-08ae-46bd-a058-2a57864274cd;", out parsedExpressions,
                                                  out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(new DateTime(2015, 5, 22), user.BirthDay);
            Assert.AreEqual(Guid.Parse("db4f8676-08ae-46bd-a058-2a57864274cd"), user.ID);
            Assert.AreEqual(0, (int)user.Location);
            Assert.AreEqual(@"roy\", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"Location=invalid;", out parsedExpressions, out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(new DateTime(2015, 5, 22), user.BirthDay);
            Assert.AreEqual(Guid.Parse("db4f8676-08ae-46bd-a058-2a57864274cd"), user.ID);
            Assert.AreEqual(0, (int)user.Location);
            Assert.AreEqual(@"roy\", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"Location=1;", out parsedExpressions, out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(new DateTime(2015, 5, 22), user.BirthDay);
            Assert.AreEqual(Guid.Parse("db4f8676-08ae-46bd-a058-2a57864274cd"), user.ID);
            Assert.AreEqual(1, (int)user.Location);
            Assert.AreEqual(Location.NewYork, user.Location);
            Assert.AreEqual(@"roy\", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"Location=10;", out parsedExpressions, out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(new DateTime(2015, 5, 22), user.BirthDay);
            Assert.AreEqual(Guid.Parse("db4f8676-08ae-46bd-a058-2a57864274cd"), user.ID);
            Assert.AreEqual(10, (int)user.Location);
            Assert.AreEqual(@"roy\", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"Name=;", out parsedExpressions, out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(new DateTime(2015, 5, 22), user.BirthDay);
            Assert.AreEqual(Guid.Parse("db4f8676-08ae-46bd-a058-2a57864274cd"), user.ID);
            Assert.AreEqual(10, (int)user.Location);
            Assert.AreEqual("", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"Name=roy\\\;", out parsedExpressions, out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(new DateTime(2015, 5, 22), user.BirthDay);
            Assert.AreEqual(Guid.Parse("db4f8676-08ae-46bd-a058-2a57864274cd"), user.ID);
            Assert.AreEqual(10, (int)user.Location);
            Assert.AreEqual(@"roy\;", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"ParentalDay=;", out parsedExpressions, out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(new DateTime(2015, 5, 22), user.BirthDay);
            Assert.AreEqual(Guid.Parse("db4f8676-08ae-46bd-a058-2a57864274cd"), user.ID);
            Assert.AreEqual(10, (int)user.Location);
            Assert.AreEqual(@"roy\;", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"ParentalDay=invalid_value;", out parsedExpressions, out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(new DateTime(2015, 5, 22), user.BirthDay);
            Assert.AreEqual(Guid.Parse("db4f8676-08ae-46bd-a058-2a57864274cd"), user.ID);
            Assert.AreEqual(10, (int)user.Location);
            Assert.AreEqual(@"roy\;", user.Name);
            Assert.AreEqual(null, user.ParentalDay);

            parseSuccess = UpdaterParser.TryParse(@"ParentalDay=5/22/2015 12:00:00 AM;", out parsedExpressions,
                                                  out valueArray);
            Assert.IsTrue(parseSuccess);
            dynamicFunction = UpdaterBuilder.Build(parsedExpressions, typeof(User)).Compile();
            valueArray[0]   = user;
            dynamicFunction.DynamicInvoke(valueArray);
            Assert.AreEqual(24, user.Age);
            Assert.AreEqual(new DateTime(2015, 5, 22), user.BirthDay);
            Assert.AreEqual(Guid.Parse("db4f8676-08ae-46bd-a058-2a57864274cd"), user.ID);
            Assert.AreEqual(10, (int)user.Location);
            Assert.AreEqual(@"roy\;", user.Name);
            Assert.AreEqual(new DateTime(2015, 5, 22), user.ParentalDay);
        }