Пример #1
0
        public void ThrowsOnMissingConstructor_TooManyParameters()
        {
            // Arrange

            var propertyMap = PropertyHelpers.GetPropertyMap <TooFewParameters>();

            // Act

            var exception = Record.Exception(
                () => GetImmutableSetterConstructor <TooManyParameters>(propertyMap)
                );

            // Assert

            Assert.IsType <NotSupportedException>(exception);

            Assert.StartsWith(
                "The TooManyParameters class lacks a constructor which is " +
                "compatible with the following signature: TooManyParameters(",
                exception.Message
                );

            Assert.Contains("String Foo", exception.Message, StringComparison.OrdinalIgnoreCase);
            Assert.Contains("Guid Bar", exception.Message, StringComparison.OrdinalIgnoreCase);
        }
Пример #2
0
        public void DecoratedConstructor_DecoratedIncompatible()
        {
            // Arrange

            var propertyMap = PropertyHelpers.GetPropertyMap <DecoratedIncompatible>();

            // Act

            var exception = Record.Exception(
                () => GetImmutableSetterConstructor <DecoratedIncompatible>(propertyMap)
                );

            // Assert

            Assert.IsType <InvalidOperationException>(exception);

            Assert.StartsWith(
                "The DecoratedIncompatible class has a constructor decorated " +
                "with ImmutableSetterConstructorAttribute that is incompatible " +
                "with the following signature: DecoratedIncompatible(",
                exception.Message
                );

            Assert.Contains("String Foo", exception.Message, StringComparison.OrdinalIgnoreCase);
            Assert.Contains("Guid Bar", exception.Message, StringComparison.OrdinalIgnoreCase);
        }
Пример #3
0
        public void GetPropertyMap_IgnoreExclusiveSetters()
        {
            // Arrange & Act

            var propertyMap = PropertyHelpers.GetPropertyMap <ExclusiveSetter>();

            // Assert

            Assert.Equal(1, propertyMap.Count);
            AssertContainsProperty(propertyMap, nameof(ExclusiveSetter.Foo));
        }
Пример #4
0
        public void GetPropertyMap_IgnorePrivateGetter()
        {
            // Arrange & Act

            var propertyMap = PropertyHelpers.GetPropertyMap <PrivateGetter>();

            // Assert

            Assert.Equal(1, propertyMap.Count);
            AssertContainsProperty(propertyMap, nameof(PrivateGetter.Foo));
        }
Пример #5
0
        public void GetPropertyMap_IgnoreCustomGetters()
        {
            // Arrange & Act

            var propertyMap = PropertyHelpers.GetPropertyMap <NonAutoProperties>();

            // Assert

            Assert.Equal(2, propertyMap.Count);
            AssertContainsProperty(propertyMap, nameof(NonAutoProperties.ValidGetterOnly));
            AssertContainsProperty(propertyMap, nameof(NonAutoProperties.ValidWithSetter));
        }
Пример #6
0
        public void GetPropertyMap_Simple()
        {
            // Arrange & Act

            var propertyMap = PropertyHelpers.GetPropertyMap <Simple>();

            // Assert

            Assert.Equal(2, propertyMap.Count);
            AssertContainsProperty(propertyMap, nameof(Simple.Foo));
            AssertContainsProperty(propertyMap, nameof(Simple.Bar));
        }
Пример #7
0
        public void DecoratedConstructor_ValidDecorated()
        {
            // Arrange

            var propertyMap = PropertyHelpers.GetPropertyMap <ValidDecorated>();
            var expected    = GetExpectedConstructor <ValidDecorated>();

            // Act

            var actual = GetImmutableSetterConstructor <ValidDecorated>(propertyMap);

            // Assert

            Assert.NotNull(actual);
            Assert.Equal(expected, actual);
        }
Пример #8
0
        public void FindsMatching_IgnoresStaticMembers()
        {
            // Arrange

            var expected    = GetExpectedConstructor <IgnoresStaticMembers>();
            var propertyMap = PropertyHelpers.GetPropertyMap <IgnoresStaticMembers>();

            // Act

            var actual = GetImmutableSetterConstructor <IgnoresStaticMembers>(propertyMap);

            // Assert

            Assert.NotNull(actual);
            Assert.Equal(expected, actual);
        }
Пример #9
0
        public void FindsMatching_WithProtectedConstructor()
        {
            // Arrange

            var propertyMap = PropertyHelpers.GetPropertyMap <WithProtectedConstructor>();
            var expected    = GetExpectedConstructor <WithProtectedConstructor>();

            // Act

            var actual = GetImmutableSetterConstructor <WithProtectedConstructor>(propertyMap);

            // Assert

            Assert.NotNull(actual);
            Assert.Equal(expected, actual);
        }
Пример #10
0
        static ImmutableBase()
        {
            ordinalIgnoreCase = StringComparer.OrdinalIgnoreCase;
            var propertiesByName = PropertyHelpers.GetPropertyMap <TImmutable>();

            var constructor =
                ConstructorHelpers
                .GetImmutableSetterConstructor <TImmutable>(propertiesByName);

            createImmutable = constructor.CreateArrayFunc <TImmutable>();

            var properties =
                constructor
                .GetParameters()
                .Select(parameter => propertiesByName[parameter.Name])
                .ToImmutableArray();

            var provider = AggregatePropertyHandlerProvider.Create(
                new StringPropertyHandlerProvider(),
                new SinglePropertyHandlerProvider(),
                new DoublePropertyHandlerProvider(),
                new NullablePropertyHandlerProvider <Single>(new SinglePropertyHandlerProvider()),
                new NullablePropertyHandlerProvider <Double>(new DoublePropertyHandlerProvider()),
                new EnumerablePropertyHandlerProvider(),
                new DefaultPropertyHandlerProvider()
                );

            var handlersBuilder       = ImmutableArray.CreateBuilder <IPropertyHandler>();
            var handlersByNameBuilder =
                ImmutableDictionary.CreateBuilder <String, IPropertyHandler>(ordinalIgnoreCase);

            foreach (var property in properties)
            {
                var handler = provider.Create(property);

                handlersBuilder.Add(handler);
                handlersByNameBuilder.Add(handler.PropertyName, handler);
            }

            handlers        = handlersBuilder.ToImmutable();
            handlersByName  = handlersByNameBuilder.ToImmutable();
            stringFormatter = new DefaultStringFormatter();
        }
Пример #11
0
        public void GetPropertyMap_ThrowsOnDuplicatePropertyNames()
        {
            // Act

            var exception = Record.Exception(
                () => PropertyHelpers.GetPropertyMap <DuplicatePropertyNames>()
                );

            // Assert

            Assert.IsType <NotSupportedException>(exception);

            Assert.Equal(
                $"ImmutableBase<DuplicatePropertyNames> requires property " +
                $"names to be unique regardless of case, but two or more " +
                $"properties share the name of 'foo'.",
                exception.Message,
                ignoreCase: true
                );
        }
Пример #12
0
        public void DecoratedConstructor_TooManyDecorated()
        {
            // Arrange

            var propertyMap = PropertyHelpers.GetPropertyMap <IgnoresStaticMembers>();

            // Act

            var exception = Record.Exception(
                () => GetImmutableSetterConstructor <TooManyDecorated>(propertyMap)
                );

            // Assert

            Assert.IsType <InvalidOperationException>(exception);

            Assert.Equal(
                "The TooManyDecorated class has 2 constructors decorated " +
                "with the ImmutableSetterConstructorAttribute. Only one " +
                "constructor is allowed to have this attribute at a time.",
                exception.Message
                );
        }