Exemplo n.º 1
0
        public void BackUntil()
        {
            var parse =
                FSBuilder
                .Skip <StringObject>(6)
                .Take(1, "Raw1")
                .Back("hello")
                .TakeRest("Raw2")
                .Seal();

            var obj = new StringObject();

            parse("123helloworld", obj);

            Assert.AreEqual("l", obj.Raw1);
            Assert.AreEqual("world", obj.Raw2);
        }
Exemplo n.º 2
0
        public void Errors()
        {
            var left  = FSBuilder.Take <UnsignedObject>("|", "A").Else((s, o) => { });
            var right = FSBuilder.Take <UnsignedObject>("|", "B").Else((s, o) => { });

            try
            {
                left.Append(right);
                Assert.Fail("Shouldn't be legal to append two else directives");
            }
            catch (Exception) {  }

            left  = FSBuilder.Skip <UnsignedObject>(1).TakeRest("A");
            right = FSBuilder.Skip <UnsignedObject>(1).TakeRest("B");

            try
            {
                left.Append(right);
                Assert.Fail("Shouldn't be legal to append two take rest directives");
            }
            catch (Exception) { }

            try
            {
                FSBuilder.Take <UnsignedObject>(5, "HelloWorld");
                Assert.Fail("Property does not exist");
            }
            catch (Exception) { }

            // These *shouldn't* throw exceptions
            FSBuilder.Take <UnsignedObject>("|", "A").Seal()("345212", new UnsignedObject());
            FSBuilder.Take <UnsignedObject>(4, "A").Seal()("1", new UnsignedObject());
            FSBuilder.Take <UnsignedObject>(1, "A").Take("|", "B").Seal()("asdf", new UnsignedObject());

            try
            {
                FSBuilder.Skip <UnsignedObject>(1).Back(-4);
                Assert.Fail("Back shouldn't accept negatives");
            }
            catch (Exception) { }

            try
            {
                FSBuilder.Skip <UnsignedObject>(-4);
                Assert.Fail("Skip shouldn't accept negatives");
            }
            catch (Exception) { }

            try
            {
                FSBuilder.Take <UnsignedObject>(-4, "A");
                Assert.Fail("Take shouldn't accept negatives");
            }
            catch (Exception) { }

            try
            {
                FSBuilder.Take <UnsignedObject>(4, "Bad");
                Assert.Fail("Bad should not be deserializable");
            }
            catch (Exception) { }

            try
            {
                FSBuilder.Take <UnsignedObject>(4, typeof(string).GetMember("Length")[0]);
                Assert.Fail("Length is not on UnsignedObject");
            }
            catch (Exception) { }

            try
            {
                FSBuilder.Take <UnsignedObject>(4, typeof(UnsignedObject).GetMember("ToString")[0]);
                Assert.Fail("ToString is not a field or property");
            }
            catch (Exception) { }

            try
            {
                FSBuilder.Take <UnsignedObject>(4, "Hidden");
                Assert.Fail("Hidden is not settable");
            }
            catch (Exception) { }

            try
            {
                FSBuilder.Take <UnsignedObject>(4, "Static");
                Assert.Fail("Statis is not an instance property");
            }
            catch (Exception) { }

            try
            {
                FSBuilder.Take <UnsignedObject>(4, "StaticField");
                Assert.Fail("StaticField is not an instance field");
            }
            catch (Exception) { }

            try
            {
                FSBuilder.Take <UnsignedObject>(4, "A", format: "yyyy-mm-dd");
                Assert.Fail("A is not a DateTime or TimeSpan");
            }
            catch (Exception) { }

            try
            {
                FSBuilder.Take <UnsignedObject>(",", "DT", format: "asdf");
                Assert.Fail("DateTime format string is invalid");
            }
            catch (Exception) { }

            try
            {
                FSBuilder.Take <UnsignedObject>(",", "TS", format: "asdf");
                Assert.Fail("TimeSpan format string is invalid");
            }
            catch (Exception) { }

            try
            {
                FSBuilder.Take <UnsignedObject>(",", "GD", format: "sadf");
                Assert.Fail("Guid format string is invalid");
            }
            catch (Exception) { }

            try
            {
                FSBuilder.Take <UnsignedObject>("", "TS");
                Assert.Fail("An empty until is invalid");
            }
            catch (Exception) { }
        }