示例#1
0
        private static void CtorSetTests()
        {
            QUnit.Module("CtorSetTests");

            QUnit.Test("Simple string property CtorSet initialisation", assert =>
            {
                var x = new SomethingWithStringId("abc");
                assert.Equal(x.Id, "abc");
            });

            QUnit.Test("Simple string property CtorSet initialisation on 'Plain' autoProperty", assert =>
            {
                var x = new SomethingWithPlainAutoPropertyStringId("abc");
                assert.Equal(x.Id, "abc");
            });

            QUnit.Test("CtorSet may not be called outside of the constructor (only works if CtorSet is set consistently within the constructor)", assert =>
            {
#pragma warning disable CtorSet // CtorSet should only be used in specific circumstances - suppress this, we're testing the runtime error handling
                var x = new SomethingWithStringId("abc");
                assert.Throws(
                    () => x.CtorSet(_ => _.Id, "abc"),
                    "CtorSet should throw if called outside of the constructor (since it should only be called once per property and the constructor should call it for all properties)"
                    );
            });
#pragma warning restore CtorSet // CtorSet should only be used in specific circumstances
        }
示例#2
0
        private static void CtorSetTests()
        {
            QUnit.Module("CtorSetTests");

            QUnit.Test("Simple string property CtorSet initialisation", assert =>
            {
                var x = new SomethingWithStringId("abc");
                assert.Equal(x.Id, "abc");
            });

            QUnit.Test("Simple string property CtorSet initialisation on 'Plain' autoProperty", assert =>
            {
                var x = new SomethingWithPlainAutoPropertyStringId("abc");
                assert.Equal(x.Id, "abc");
            });

            QUnit.Test("CtorSet may not be called outside of the constructor (only works if CtorSet is set consistently within the constructor)", assert =>
            {
                var x = new SomethingWithStringId("abc");
                assert.Throws(
                    () => x.CtorSet(_ => _.Id, "abc"),
                    "CtorSet should throw if called outside of the constructor (since it should only be called once per property and the constructor should call it for all properties)"
                    );
            });
        }
示例#3
0
        private static void WithTests()
        {
            QUnit.Module("WithTests");

            QUnit.Test("Simple string property update using With directly", assert =>
            {
                var x = new SomethingWithStringId("abc");
                x     = x.With(_ => _.Id, "def");
                assert.Equal(x.Id, "def");
            });

            QUnit.Test("With does not affect original instance", assert =>
            {
                var x0 = new SomethingWithStringId("abc");
                var x1 = x0.With(_ => _.Id, "def");
                assert.Equal(x0.Id, "abc");
                assert.Equal(x1.Id, "def");
            });

            QUnit.Test("Simple string property update of property on a base class using With directly", assert =>
            {
                // This test is just to ensure that there's no monkey business involved when targeting properties on a base class (as there are
                // with interface properties - see above)
                var x = new SecurityPersonDetails(1, "test", 10);
                x     = x.With(_ => _.Name, "test2");
                assert.Equal(x.Name, "test2");
            });

            QUnit.Test("Simple string property update using With indirectly", assert =>
            {
                var x         = new SomethingWithStringId("abc");
                var idUpdater = x.With(_ => _.Id);
                x             = idUpdater("def");
                assert.Equal(x.Id, "def");
            });

            QUnit.Test("Simple string property update using GetProperty and With", assert =>
            {
                var x = new SomethingWithStringId("abc");
                var propertyToUpdate = x.GetProperty(_ => _.Id);
                x = x.With(propertyToUpdate, "def");
                assert.Equal(x.Id, "def");
            });

            QUnit.Test("Single-element NonNullList<string> property update using With directly", assert =>
            {
                var x = new SomethingWithNonNullListStringValues(NonNullList.Of("abc", "def"));
                x     = x.With(_ => _.Values, 1, "xyz");
                assert.Equal(x.Values[1], "xyz");
            });

            QUnit.Test("Single-element NonNullList<string> property update using GetProperty and With", assert =>
            {
                var x = new SomethingWithNonNullListStringValues(NonNullList.Of("abc", "def"));
                var propertyToUpdate = x.GetProperty(_ => _.Values);
                x = x.With <SomethingWithNonNullListStringValues, string>(propertyToUpdate, 1, "xyz");
                assert.Equal(x.Values[1], "xyz");
            });

            QUnit.Test("Single-element Set<string> (legacy compatibility alias for NonNullList) property update using GetProperty and With", assert =>
            {
#pragma warning disable CS0618 // Ignore the fact that Set is obsolete
                var x = new SomethingWithNonNullListStringValues(Set.Of("abc", "def"));
#pragma warning restore CS0618
                var propertyToUpdate = x.GetProperty(_ => _.Values);
                x = x.With <SomethingWithNonNullListStringValues, string>(propertyToUpdate, 1, "xyz");
                assert.Equal(x.Values[1], "xyz");
            });

            QUnit.Test("The Validate method (if there is one defined with zero parameters) should be called after With", assert =>
            {
                var x = new SomethingWithNonZeroKey(123);
                assert.Throws(
                    () => x.With(_ => _.Key, (uint)0),
                    "The Validate method should be called after With"
                    );
            });

            // When first changing the Clone behaviour within ImmutabilityHelpers to work with Bridge 16 (which changes how properties are defined on objects), there was a
            // bug introduced where the updating properties on the clone would update the values on the original value too! These tests confirm that that bug is no more.
            QUnit.Test("Simple string property update against an interface using With directly", assert =>
            {
                // Inspired by issue https://github.com/ProductiveRage/Bridge.Immutable/issues/4
                IAmImmutableAndHaveName viaInterfacePerson = new PersonDetails(1, "test");
                viaInterfacePerson = viaInterfacePerson.With(_ => _.Name, "test2");
                assert.Equal(viaInterfacePerson.Name, "test2");
            });
            QUnit.Test("Double-check must-not-affect-original-instance when targeting property on base class", assert =>
            {
                var x0 = new SecurityPersonDetails(1, "test", 10);
                var x1 = x0.With(_ => _.Name, "test2");
                assert.Equal(x0.Name, "test");
                assert.Equal(x1.Name, "test2");
            });
        }