public void TestMethod()
        {
            //TestB tb = new TestB() { IdB = 2 };
            A ta = new A()
            {
                IdA = 1
            };
            //tb.TestAReferences = new List<TestA>() {
            //    new TestA() { IdA = 3 },
            //    new TestA() { IdA = 6 }
            //};

            //tb.TestAReference.TestBReference = new TestB() { Id = 4 };
            //ta.TestBReference.TestAReference.TestBReference = null;

            ContractConfiguration <A> cc = new ContractConfiguration <A>();

            cc.Property(ta => ta.IdA).Ignore();
            cc.Field("unnamedPrivateField").HasName("privateFieldSerialized");

            var serializerSettings = new JsonSerializerSettings()
            {
                ContractResolver = new RuntimeContractResolver(cc),
                TraceWriter      = new ReportingTraceWriter()
            };

            var json = JsonConvert.SerializeObject(ta, serializerSettings);
        }
        public void Serialize_DoesNotSerializeSinceNotConfigured(string propName)
        {
            TestA ta = new TestA();
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            var json = JsonConvert.SerializeObject(ta, GetSettings(cc));

            JsonStringValidator jsAssert = new JsonStringValidator(json);

            Assert.IsFalse(jsAssert.HasProperty(propName));
        }
        public void PropertyFromExpr_AccessProperty()
        {
            var mappedName = "testok";
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Property(ta => ta.IdA).HasName(mappedName);

            var mapping = cc.PropertiesMapping.First();

            Assert.AreEqual("IdA", mapping.Key.Name);
            Assert.AreEqual(mappedName, mapping.Value.Name);
        }
        public void PropertyFromString_AccessProperty(string propName)
        {
            var mappedName = "testok";
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Property(propName).HasName(mappedName);

            var mapping = cc.PropertiesMapping.First();

            Assert.AreEqual(propName, mapping.Key.Name);
            Assert.AreEqual(mappedName, mapping.Value.Name);
        }
        public void PropertyFromString_ConfigureTwice_OverwriteConfig(string propName)
        {
            var mappedName = "testok";
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Property(propName).HasName("before");
            cc.Property(propName).HasName(mappedName);

            var mapping = cc.PropertiesMapping.Single();

            Assert.AreEqual(propName, mapping.Key.Name);
            Assert.AreEqual(mappedName, mapping.Value.Name);
        }
        public void PropertyFromExpr_ConfigureTwice_OverwriteConfig()
        {
            var mappedName = "testok";
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Property(ta => ta.IdA).HasName("before");
            cc.Property(ta => ta.IdA).HasName(mappedName);

            var mapping = cc.PropertiesMapping.Single();

            Assert.AreEqual("IdA", mapping.Key.Name);
            Assert.AreEqual(mappedName, mapping.Value.Name);
        }
        public void FieldFromString_AccessPrivateField()
        {
            var mappedName = "testok";
            var fieldName  = "privateField";
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Field(fieldName).HasName(mappedName);

            var mapping = cc.PropertiesMapping.First();

            Assert.AreEqual(fieldName, mapping.Key.Name);
            Assert.AreEqual(mappedName, mapping.Value.Name);
        }
        public void FieldFromString_ConfigureTwice_OverwriteConfig()
        {
            var mappedName = "testok";
            var fieldName  = "privateField";
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Field(fieldName).HasName("before");
            cc.Field(fieldName).HasName(mappedName);

            var mapping = cc.PropertiesMapping.Single();

            Assert.AreEqual(fieldName, mapping.Key.Name);
            Assert.AreEqual(mappedName, mapping.Value.Name);
        }
        public void Serialize_OverridePrivatePropertyName()
        {
            TestA ta = new TestA();

            ta.SetPrivateProp(1);
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Property("PrivateProp").HasName("private_prop");

            var json = JsonConvert.SerializeObject(ta, GetSettings(cc));

            JsonStringValidator jsAssert = new JsonStringValidator(json);

            Assert.IsTrue(jsAssert.HasPropertyWithValue("private_prop", 1));
        }
        public void Serialize_UnIgnorePrivateField()
        {
            TestA ta = new TestA();

            ta.SetIgnoredPrivateField(1);
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Field("ignoredPrivateField").Ignore(false).HasName("unignored_private_field");

            var json = JsonConvert.SerializeObject(ta, GetSettings(cc));

            JsonStringValidator jsAssert = new JsonStringValidator(json);

            Assert.IsTrue(jsAssert.HasPropertyWithValue("unignored_private_field", 1));
        }
        public void Serialize_OverridePropertyName()
        {
            TestA ta = new TestA()
            {
                IdA = 1
            };
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Property(ta => ta.IdA).HasName("id_a");

            var json = JsonConvert.SerializeObject(ta, GetSettings(cc));

            JsonStringValidator jsAssert = new JsonStringValidator(json);

            Assert.IsTrue(jsAssert.HasPropertyWithValue("id_a", 1));
        }
        public void Serialize_UnnamedProperty()
        {
            TestA ta = new TestA()
            {
                UnnamedProperty = 1
            };
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Property(ta => ta.UnnamedProperty).HasName("unnamed_property");

            var json = JsonConvert.SerializeObject(ta, GetSettings(cc));

            JsonStringValidator jsAssert = new JsonStringValidator(json);

            Assert.IsTrue(jsAssert.HasPropertyWithValue("unnamed_property", 1));
        }
        public void Serialize_NestedObjectNaming()
        {
            TestA ta = new TestA();

            ta.TestBReference = new TestB()
            {
                IdB = 1
            };
            ContractConfiguration <TestB> cc = new ContractConfiguration <TestB>();

            cc.Property(tb => tb.IdB).HasName("id_b");

            var json = JsonConvert.SerializeObject(ta, GetSettings(cc));

            JsonStringValidator jsAssert = new JsonStringValidator(json, jt => jt["testB"]);

            Assert.IsTrue(jsAssert.HasPropertyWithValue("id_b", 1));
        }
        public void Serialize_IgnorePrivateField()
        {
            TestA ta = new TestA();

            ta.SetPrivateField(1);
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Field("privateField").Ignore();

            var staticJson = JsonConvert.SerializeObject(ta);
            var json       = JsonConvert.SerializeObject(ta, GetSettings(cc));

            JsonStringValidator jsAssertStatic = new JsonStringValidator(staticJson);

            Assert.IsTrue(jsAssertStatic.HasProperty("privateField"));

            JsonStringValidator jsAssert = new JsonStringValidator(json);

            Assert.IsFalse(jsAssert.HasProperty("privateField"));
        }
        public void Serialize_IgnoreProperty()
        {
            TestA ta = new TestA()
            {
                IdA = 1
            };
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Property(ta => ta.IdA).Ignore();

            var staticJson = JsonConvert.SerializeObject(ta);
            var json       = JsonConvert.SerializeObject(ta, GetSettings(cc));

            JsonStringValidator jsAssertStatic = new JsonStringValidator(staticJson);

            Assert.IsTrue(jsAssertStatic.HasProperty("idA"));

            JsonStringValidator jsAssert = new JsonStringValidator(json);

            Assert.IsFalse(jsAssert.HasProperty("idA"));
        }
        public void Serialize_NestedArrayNaming()
        {
            TestB tb = new TestB()
            {
                IdB = 1
            };
            var tas = new TestA[]
            {
                new TestA()
                {
                    IdA = 1
                },
                new TestA()
                {
                    IdA = 2
                },
                new TestA()
                {
                    IdA = 3
                },
                new TestA()
                {
                    IdA = 4
                },
            };

            tb.TestAReferences.AddRange(tas);
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Property(ta => ta.IdA).HasName("id_a");

            var json = JsonConvert.SerializeObject(tb, GetSettings(cc));

            for (int i = 0; i < tas.Length; i++)
            {
                JsonStringValidator jsAssert = new JsonStringValidator(json, jt => jt["testAs"].Cast <JObject>().ElementAt(i));
                Assert.IsTrue(jsAssert.HasPropertyWithValue("id_a", i + 1));
            }
        }
 public static FlexibleContractResolver WithConfiguration(ContractConfiguration configuration)
 {
     return(new FlexibleContractResolver(configuration));
 }
        public void PropertyFromString_InvalidProperty_Throw(string propName)
        {
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Property(propName);
        }
        public void PropertyFromString_AccessMethod_Throw()
        {
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Property("TestMe");
        }
 public FlexibleContractResolver(ContractConfiguration configuration)
 {
     Configuration = configuration;
 }
        public void PropertyFromExpr_AccessMethod_Throw()
        {
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Property(ta => ta.TestMethod());
        }
        public void FieldFromString_ArgNull_Throw(string fieldName)
        {
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Field(fieldName);
        }
 public TypesResolvingConfigurationBuilder(ContractConfiguration contractConfiguration)
 {
     ContractConfiguration = contractConfiguration;
 }
        public void FieldFromString_AccessMethod_Throw()
        {
            ContractConfiguration <TestA> cc = new ContractConfiguration <TestA>();

            cc.Field("TestMe");
        }