Пример #1
0
        public void ExceptionTestListIsMap()
        {
            var tree = JObject.Parse("{ 'Id' : '1234', '__queryContext' : { 'catalogLin' : [ 'a', 'b' ] } }");
            var trav = SimpleTraversal.NewTraversal("__queryContext");

            // barfs here, needs the 'List list =' part to trigger it
            FluentActions
            .Invoking(() => (JArray)trav.Get(tree))
            .Should().Throw <InvalidCastException>();
        }
Пример #2
0
        public void ExceptionTestLMapIsListErasure()
        {
            var tree = JObject.Parse("{ 'Id' : '1234', '__queryContext' : { 'catalogLin' : { 'a' : 'b' } } }");

            var trav = SimpleTraversal.NewTraversal("__queryContext");

            // this works
            var queryContext = (JObject)trav.Get(tree);

            // this does not
            FluentActions
            .Invoking(() => (JArray)queryContext["catalogLin"])
            .Should().Throw <InvalidCastException>();
        }
Пример #3
0
        private static IEnumerable <TestCaseData> CreateTestCases(string testName)
        {
            var tests = new object[][]
            {
                new object[]
                {
                    "Simple Map Test",
                    SimpleTraversal.NewTraversal("a.b"),
                    JToken.Parse("{ \"a\" : null }"),
                    JToken.Parse("{ \"a\" : { \"b\" : \"tuna\" } }"),
                    new JValue("tuna")
                },
                new object[]
                {
                    "Simple explicit array test",
                    SimpleTraversal.NewTraversal("a.[1].b"),
                    JToken.Parse("{ \"a\" : null }"),
                    JToken.Parse("{ \"a\" : [ null, { \"b\" : \"tuna\" } ] }"),
                    new JValue("tuna")
                },
                new object[]
                {
                    "Leading Array test",
                    SimpleTraversal.NewTraversal("[0].a"),
                    JToken.Parse("[ ]"),
                    JToken.Parse("[ { \"a\" : \"b\" } ]"),
                    new JValue("b")
                },
                new object[]
                {
                    "Auto expand array test",
                    SimpleTraversal.NewTraversal("a.[].b"),
                    JToken.Parse("{ \"a\" : null }"),
                    JToken.Parse("{ \"a\" : [ { \"b\" : null } ] }"),
                    null
                }
            };

            foreach (var test in tests)
            {
                yield return(new TestCaseData(test.Skip(1).ToArray())
                {
                    TestName = $"{testName}({test[0]})"
                });
            }
        }
Пример #4
0
        public void TestOverwrite()
        {
            var traversal = SimpleTraversal.NewTraversal("a.b");

            var actual      = JToken.Parse("{ \"a\" : { \"b\" : \"tuna\" } }");
            var expectedOne = JToken.Parse("{ \"a\" : { \"b\" : \"one\" } }");
            var expectedTwo = JToken.Parse("{ \"a\" : { \"b\" : \"two\" } }");

            traversal.Get(actual).Should().BeEquivalentTo(JValue.CreateString("tuna"));

            // Set twice and verify that the sets did in fact overwrite
            traversal.Set(actual, "one").Should().BeEquivalentTo(JValue.CreateString("one"));
            actual.Should().BeEquivalentTo(expectedOne);

            traversal.Set(actual, "two").Should().BeEquivalentTo(JValue.CreateString("two"));
            actual.Should().BeEquivalentTo(expectedTwo);
        }
Пример #5
0
        public void TestAutoArray()
        {
            var traversal = SimpleTraversal.NewTraversal("a.[].b");

            var expected = JToken.Parse("{ \"a\" : [ { \"b\" : \"one\" }, { \"b\" : \"two\" } ] }");

            var actual = new JObject();

            traversal.Get(actual).Should().BeNull();
            actual.Count.Should().Be(0); // get didn't add anything

            // Add two things and validate the Auto Expand array
            traversal.Set(actual, "one").Should().BeEquivalentTo(JValue.CreateString("one"));
            traversal.Set(actual, "two").Should().BeEquivalentTo(JValue.CreateString("two"));

            actual.Should().BeEquivalentTo(expected);
        }
Пример #6
0
 public static IEnumerable <TestCaseData> RemoveTestCases()
 {
     return(new TestCaseData[]
     {
         new TestCaseData(
             SimpleTraversal.NewTraversal("__queryContext"),
             JObject.Parse("{ 'Id' : '1234', '__queryContext' : { 'catalogLin' : [ 'a', 'b' ] } }"),
             JObject.Parse("{ 'Id' : '1234' }"),
             JObject.Parse("{ 'catalogLin' : [ 'a', 'b' ] }")
             )
         {
             TestName = "RemoveTests(Inception Map Test)"
         },
         new TestCaseData(
             SimpleTraversal.NewTraversal("a.list.[1]"),
             JObject.Parse("{ 'a' : { 'list' : [ 'a', 'b', 'c' ] } }"),
             JObject.Parse("{ 'a' : { 'list' : [ 'a', 'c' ] } }"),
             new JValue("b")
             )
         {
             TestName = "RemoveTests(List Test)"
         },
         new TestCaseData(
             SimpleTraversal.NewTraversal("a.list"),
             JObject.Parse("{ 'a' : { 'list' : [ 'a', 'b', 'c' ] } }"),
             JObject.Parse("{ 'a' : { } }"),
             new JArray("a", "b", "c")
             )
         {
             TestName = "RemoveTests(Map leave empty Map)"
         },
         new TestCaseData(
             SimpleTraversal.NewTraversal("a.list.[0]"),
             JObject.Parse("{ 'a' : { 'list' : [ 'a' ] } }"),
             JObject.Parse("{ 'a' : { 'list' : [ ] } }"),
             new JValue("a")
             )
         {
             TestName = "RemoveTestsMap leave empty List)"
         }
     });
 }