public void CanSetupTreeStructureWithUpdates()
        {
            string document = ComplexObject
                              .Create()
                              .WithId(SetTo.Value("ID123"))
                              .WithItems(
                item => item
                .WithId(SetTo.Value("ARandomGuid"))
                .WithTags(
                    tag1 => tag1
                    .WithKey(SetTo.Value("Importance"))
                    .WithValue(SetTo.Value("High")),
                    tag2 => tag2
                    .WithKey(SetTo.Value("Level"))
                    .WithValue(SetTo.Null),
                    tag3 => tag3
                    .WithKey(SetTo.Value("Games"))
                    .WithValue(SetTo.Null)))
                              .WithName(SetTo.Value("Test"))
                              .UpdateExistingTagAtIndex(0,
                                                        item => item
                                                        .UpdateExistingTagAtIndex(1, tag => tag.WithValue(SetTo.Value("Medium")))
                                                        .WithTags(Updated.ByRemovingAtIndex(0)));

            Assert.Equal(
                @"{""id"":""ID123"",""items"":[{""id"":""ARandomGuid"",""tags"":[{""key"":""Level"",""value"":""Medium""},{""key"":""Games"",""value"":null}]}],""name"":""Test""}",
                document);
        }
        public void SettingEmptyStringProperty_JsonContainsEmptyPropertyName()
        {
            string document = JsonBuilder
                              .CreateObject()
                              .With("", SetTo.Value(""));

            Assert.Equal(@"{"""":""""}", document);
        }
Example #3
0
        public void SetToCollectionWithOneEmptyObject_ExpectedJsonPatternReturned()
        {
            string document = JsonBuilder
                              .CreateObject()
                              .With("first", SetTo.AnArrayContaining(item => { }))
                              .And("second", SetTo.Value("test2"));

            Assert.Equal($@"{{""first"":[{{}}],""second"":""test2""}}", document);
        }
Example #4
0
        public void SetToEmptyArrayFunc_ExpectedJsonPatternReturned()
        {
            string document = JsonBuilder
                              .CreateObject()
                              .With("first", SetTo.AnEmptyArray)
                              .And("second", SetTo.Value("test2"));

            Assert.Equal($@"{{""first"":[],""second"":""test2""}}", document);
        }
Example #5
0
        public void AddingPropertiesUsingSetToFalse_ExpectedJsonPatternReturned()
        {
            string document = JsonBuilder
                              .CreateObject()
                              .With("first", SetTo.False())
                              .And("second", SetTo.Value("test2"));

            Assert.Equal($@"{{""first"":false,""second"":""test2""}}", document);
        }
        public void WithoutThenWith_PropertyAppearsInJson()
        {
            string document = JsonBuilder
                              .CreateObject()
                              .Without("first")
                              .With("first", SetTo.Value("There"));

            Assert.Equal(@"{""first"":""There""}", document);
        }
        public void AddingPropertiesWithValues_PropertiesAppearInJson()
        {
            string document = JsonBuilder
                              .CreateObject()
                              .With("first", SetTo.Value("test1"))
                              .And("second", SetTo.Value("test2"))
                              .With("third", SetTo.Value(true));

            Assert.Equal(@"{""first"":""test1"",""second"":""test2"",""third"":true}", document);
        }
Example #8
0
        public void UpdateAllowsToModifyExistingSetValue()
        {
            string document = JsonBuilder
                              .CreateObject()
                              .With("first", SetTo.Value("test1"))
                              .With("first", Updated.By(v => v + " and test2"))
                              .And("second", SetTo.Value(true));

            Assert.Equal(@"{""first"":""test1 and test2"",""second"":true}", document);
        }
Example #9
0
        public void UpdateAllowsToModifyExistingSetValueUsingStrongTyping()
        {
            string document = JsonBuilder
                              .CreateObject()
                              .With("first", SetTo.Value("test1"))
                              .And("second", SetTo.Value(true))
                              .And("second", Updated.By <bool>(v => !v));

            Assert.Equal(@"{""first"":""test1"",""second"":false}", document);
        }
        public void AddingPropertiesUsingSetToRandomGuid_ExpectedJsonPatternReturned()
        {
            string document = JsonBuilder
                              .CreateObject()
                              .With("first", SetTo.RandomGuid())
                              .And("second", SetTo.Value("test2"));

            var guidRegex = @"[0-9A-F]{8}-([0-9A-F]{4}-){3}[0-9A-F]{12}";

            Assert.True(Regex.IsMatch(document, $@"{{""first"":""{guidRegex}"",""second"":""test2""}}"));
        }
Example #11
0
        public void SetToCollectionWithTwoComplexObjects_ExpectedJsonPatternReturned()
        {
            string document = JsonBuilder
                              .CreateObject()
                              .With("first", SetTo.AnArrayContaining(
                                        item => { item.With("first", SetTo.Value("hasValue")); },
                                        item => { item.With("second", SetTo.Value("alsoHasAValue")); }))
                              .And("second", SetTo.Value("test2"));

            Assert.Equal(
                $@"{{""first"":[{{""first"":""hasValue""}},{{""second"":""alsoHasAValue""}}],""second"":""test2""}}",
                document);
        }
Example #12
0
        public void SetToCollectionThenUpdatedByRemoving_ExpectedJsonPatternReturned()
        {
            string document = JsonBuilder
                              .CreateObject()
                              .With("first", SetTo.AnArrayContaining(
                                        item => item.With("aProperty", SetTo.Value("AValue")),
                                        item => { }))
                              .And("second", SetTo.Value("test2"))
                              .With("first", Updated.ByRemovingAtIndex(0))
                              .With("first", Updated.AtIndex(0,
                                                             item => item.With("second", SetTo.Value("NewValue"))));

            Assert.Equal($@"{{""first"":[{{""second"":""NewValue""}}],""second"":""test2""}}", document);
        }
Example #13
0
        public void CanSetCustomPropertiesAndGenericProperties()
        {
            string document = CustomObject
                              .Create()
                              .WithId(SetTo.Value("ID123"))
                              .With("description", SetTo.Value("This is a description"))
                              .WithName(SetTo.Value("Test"))
                              .With("Age", SetTo.Value(42))
                              .WithCat(SetTo.Null);

            Assert.Equal(
                @"{""id"":""ID123"",""description"":""This is a description"",""name"":""Test"",""Age"":42,""cat"":null}",
                document);
        }
Example #14
0
        public void SetToCollectionThenInsertAtIndex0_ExpectedJsonPatternReturned()
        {
            string document = JsonBuilder
                              .CreateObject()
                              .With("first", SetTo.AnArrayContaining(
                                        item => item.With("number", SetTo.Value("one")),
                                        item => { }))
                              .And("second", SetTo.Value("test2"))
                              .With("first", Updated.WithArrayItemsInsertedAtIndex(0,
                                                                                   item => item.With("number", SetTo.Value("zero"))))
                              .With("first", Updated.AtIndex(2,
                                                             item => item.With("aNother", SetTo.Value("NewValue"))));

            Assert.Equal($@"{{""first"":[{{""number"":""zero""}},{{""number"":""one""}},{{""aNother"":""NewValue""}}],""second"":""test2""}}", document);
        }
Example #15
0
        public void AddingPropertiesUsingWithAndAnd_PropertiesAppearInJson()
        {
            string document = JsonBuilder
                              .CreateObject()
                              .With("first", SetTo.Value("test1"))
                              .And("second", SetTo.Value("test2"))
                              .With("third", SetTo.Null)
                              .And("fourth", SetTo.Null)
                              .With("fifth", SetTo.Null())
                              .And("sixth", SetTo.Null())
                              .With("seventh")
                              .And("eighth");

            Assert.Equal(
                @"{""first"":""test1"",""second"":""test2"",""third"":null,""fourth"":null,""fifth"":null,""sixth"":null,""seventh"":null,""eighth"":null}",
                document);
        }