Ejemplo n.º 1
0
        public void SetValueForProperty_works()
        {
            var obj = new CloneClass();
            var date = DateTime.Now;

            obj.SetValueForProperty("TheInt", 1);
            obj.SetValueForProperty("TheString", "some string");
            obj.SetValueForProperty("TheDateTime", date);
            obj.SetValueForProperty("TheBool", true);
            obj.SetValueForProperty("TheDecimal", 1.1M);
            obj.SetValueForProperty("TheType", typeof(CloneClass));

            obj.TheInt.Should().Be(1);
            obj.TheString.Should().Be("some string");
            obj.TheDateTime.Should().Be(date);
            obj.TheBool.Should().Be(true);
            obj.TheDecimal.Should().Be(1.1M);
            obj.TheType.Should().Be(typeof(CloneClass));
        }
Ejemplo n.º 2
0
        private void AssertCloneProperties(CloneClass clone, CloneClass source)
        {
            ReferenceEquals(clone, source).Should().BeFalse();

            clone.TheInt.Should().Be(source.TheInt);

            clone.TheString.Should().Be(source.TheString);
            if (clone.TheString != null || source.TheString != null)
                ReferenceEquals(clone.TheString, source.TheString).Should().BeFalse();

            clone.TheDateTime.Should().Be(source.TheDateTime);
            clone.TheDecimal.Should().Be(source.TheDecimal);
            clone.TheBool.Should().Be(source.TheBool);

            if (clone.TheIntsArray.HasValues() && source.TheIntsArray.HasValues())
                clone.TheIntsArray.ContainsAll(source.TheIntsArray).Should().BeTrue();
            if (clone.TheIntsList.HasValues() && source.TheIntsList.HasValues())
                clone.TheIntsList.ContainsAll(source.TheIntsList).Should().BeTrue();

            if (clone.TheCloneClassList != null && source.TheCloneClassList != null)
                for (int i = 0; i < source.TheCloneClassList.Count; i++)
                    AssertCloneProperties(clone.TheCloneClassList[i], source.TheCloneClassList[i]);
        }
Ejemplo n.º 3
0
        public void SetValueForProperty_Returns_WhenTryingToSetPropertyThatDoesntExist()
        {
            var obj = new CloneClass();

            obj.SetValueForProperty("NonExistantProperty", "boom");
        }
Ejemplo n.º 4
0
        public void GetValueForProperty_works()
        {
            var obj = new CloneClass(1, "string1", DateTime.Now, true, 1.1M, typeof(CloneClass));

            obj.GetValueForProperty("TheInt").Should().Be(obj.TheInt);
            obj.GetValueForProperty("TheString").Should().Be(obj.TheString);
            obj.GetValueForProperty("TheDateTime").Should().Be(obj.TheDateTime);
            obj.GetValueForProperty("TheBool").Should().Be(obj.TheBool);
            obj.GetValueForProperty("TheDecimal").Should().Be(obj.TheDecimal);
            obj.GetValueForProperty("TheType").Should().Be(obj.TheType);
        }
Ejemplo n.º 5
0
        public void GetValueForProperty_returns_defaultValue_when_property_doesnt_exist()
        {
            var obj = new CloneClass();

            obj.GetValueForProperty("NonExistantProperty").Should().BeNull();
            obj.GetValueForProperty("NonExistantProperty", "doesnt exist").Should().Be("doesnt exist");
        }
Ejemplo n.º 6
0
        public void Clone_ShouldDeepCopyNonEnumerableProperties()
        {
            var source = new CloneClass(1, "name1", Dates.MondayDate, true, 1.1M, typeof(string))
            {
                TheStruct = new StructObj(2, "name2", Dates.TuesdayDate, false, 2.2M, typeof(decimal)),
                Child = new CloneClass(3, "name3", Dates.WednesdayDate, false, 3.3M, typeof(bool))
                {
                    TheStruct = new StructObj(4, "name4", Dates.ThursdayDate, true, 4.4M, typeof(CloneClass))
                }
            };

            var clone = source.Clone();

            AssertCloneProperties(clone, source);
            AssertStructProperties(clone.TheStruct, source.TheStruct);

            clone.Child.Should().NotBeNull();
            AssertCloneProperties(clone.Child, source.Child);
            AssertStructProperties(clone.Child.TheStruct, source.Child.TheStruct);

            clone.Should().NotBe(source);//ref check
        }
Ejemplo n.º 7
0
        public void Clone_ShouldDeepCopyEnumerableProperties()
        {
            var source = new CloneClass
            {
                TheIntsArray = new[] { 1, 2, 3, 4, 5 },
                TheIntsList = new List<int> { 2, 3, 4, 5, 6 },
                TheCloneClassList = new List<CloneClass>
                {
                    new CloneClass(5, "name5", Dates.FridayDate, true, 5.5M, typeof(StructObj)),
                    new CloneClass(6, "name6", Dates.SaturdayDate, false, 5.5M, typeof(DateTime))
                },
                Child = new CloneClass
                {
                    TheIntsArray = new[] { 5, 4, 3, 2, 1 },
                    TheIntsList = new List<int> { 6, 5, 4, 3, 2 },
                    TheStruct = new StructObj(4, "name4", Dates.ThursdayDate, true, 4.4M, typeof(CloneClass))
                }
            };

            var clone = source.Clone();

            AssertCloneProperties(clone, source);
            AssertStructProperties(clone.TheStruct, source.TheStruct);

            clone.Child.Should().NotBeNull();
            AssertCloneProperties(clone.Child, source.Child);
            AssertStructProperties(clone.Child.TheStruct, source.Child.TheStruct);

            clone.Should().NotBe(source);//ref check
        }