Exemple #1
0
        public void Format_StringFieldWithNoAttributes_FieldIsNotFormatted()
        {
            var record = new MockRecord()
            {
                StringField = "test"
            };
            var property       = typeof(MockRecord).GetProperty(nameof(MockRecord.StringField));
            var formatter      = new TextRecordFieldFormatter();
            var formattedValue = formatter.Format(record.StringField, property);

            Assert.AreEqual(record.StringField, formattedValue);
        }
Exemple #2
0
        public void Format_NullStringFieldWithTransformAttribute_FormattedValueIsNull()
        {
            var record = new MockRecord()
            {
                StringFieldWithTransformAttribute = null
            };
            var property       = typeof(MockRecord).GetProperty(nameof(MockRecord.StringFieldWithTransformAttribute));
            var formatter      = new TextRecordFieldFormatter();
            var formattedValue = formatter.Format(record.StringFieldWithTransformAttribute, property);

            Assert.IsNull(formattedValue);
        }
Exemple #3
0
        public void Format_EmptyStringFieldWithToStringAttribute_FieldIsConvertedToStringViaAttribute()
        {
            var record = new MockRecord()
            {
                StringFieldWithToStringAttribute = String.Empty
            };
            var property       = typeof(MockRecord).GetProperty(nameof(MockRecord.StringFieldWithToStringAttribute));
            var formatter      = new TextRecordFieldFormatter();
            var formattedValue = formatter.Format(record.StringFieldWithToStringAttribute, property);

            Assert.AreEqual($"'{record.StringFieldWithToStringAttribute}'", formattedValue);
        }
Exemple #4
0
        public void Format_NullStringFieldWithToStringAndTransformAttributes_FieldIsConvertedToStringViaAttributeAndFormatted()
        {
            var record = new MockRecord()
            {
                StringFieldWithToStringAndTransformAttributes = null
            };
            var property       = typeof(MockRecord).GetProperty(nameof(MockRecord.StringFieldWithToStringAndTransformAttributes));
            var formatter      = new TextRecordFieldFormatter();
            var formattedValue = formatter.Format(record.StringFieldWithToStringAndTransformAttributes, property);

            Assert.AreEqual($"|''|", formattedValue);
        }
Exemple #5
0
        public void Format_NonNullOrEmptyNullableIntFieldWithToStringAndTransformAttributes_FieldIsConvertedToStringViaAttributeAndFormatted()
        {
            var record = new MockRecord()
            {
                NullableIntFieldWithToStringAndTransformAttributes = 10
            };
            var property       = typeof(MockRecord).GetProperty(nameof(MockRecord.NullableIntFieldWithToStringAndTransformAttributes));
            var formatter      = new TextRecordFieldFormatter();
            var formattedValue = formatter.Format(record.NullableIntFieldWithToStringAndTransformAttributes, property);

            Assert.AreEqual($"|'{record.NullableIntFieldWithToStringAndTransformAttributes}'|", formattedValue);
        }
Exemple #6
0
        public void Format_NullNullableIntFieldWithNullableTransformAttribute_FieldIsFormatted()
        {
            var record = new MockRecord()
            {
                NullableIntFieldWithNullableTransformAttribute = null
            };
            var property       = typeof(MockRecord).GetProperty(nameof(MockRecord.NullableIntFieldWithNullableTransformAttribute));
            var formatter      = new TextRecordFieldFormatter();
            var formattedValue = formatter.Format(record.NullableIntFieldWithNullableTransformAttribute, property);

            Assert.AreEqual($"|NULL|", formattedValue);
        }
Exemple #7
0
        public void Format_NullableIntFieldWithNoAttributes_FieldIsConvertedToStringAndNotFormatted()
        {
            var record = new MockRecord()
            {
                NullableIntField = 10
            };
            var property       = typeof(MockRecord).GetProperty(nameof(MockRecord.NullableIntField));
            var formatter      = new TextRecordFieldFormatter();
            var formattedValue = formatter.Format(record.NullableIntField, property);

            Assert.AreEqual("10", formattedValue);
        }
Exemple #8
0
        public void ApplyTransform_StringField_ExceptionIsThrown()
        {
            var record = new MockRecord()
            {
                StringField = "10"
            };
            var amount            = 2d;
            var property          = typeof(MockRecord).GetProperty(nameof(MockRecord.StringField));
            var divideByAttribute = new DivideByAttribute(amount);

            divideByAttribute.ApplyTransform(property, record);
        }
Exemple #9
0
        public void ApplyTransform_DecimalFieldWithDivideByZero_ExceptionIsThrown()
        {
            var record = new MockRecord()
            {
                DecimalField = 10m
            };
            var amount            = 0d;
            var property          = typeof(MockRecord).GetProperty(nameof(MockRecord.DecimalField));
            var divideByAttribute = new DivideByAttribute(amount);

            divideByAttribute.ApplyTransform(property, record);
        }
Exemple #10
0
        public void Format_DelimiterNotInAnyFieldAndOneNullPropertyAndConditionalFieldQualifierProvided_NoFieldsQualified()
        {
            var formatter = new DelimitedTextRecordFormatter();

            formatter.FieldQualifier = "'";
            formatter.IsFieldQualifierConditional = true;

            var record          = new MockRecord("text", 10, null);
            var formattedRecord = formatter.Format(record);

            Assert.AreEqual("text,10,", formattedRecord);
        }
Exemple #11
0
        public void Format_DelimiterInFieldAndConditionalFieldQualifierProvided_FieldContainingQualifierIsQualified()
        {
            var formatter = new DelimitedTextRecordFormatter();

            formatter.FieldQualifier = "'";
            formatter.IsFieldQualifierConditional = true;

            var record          = new MockRecord("te,xt", 10, null);
            var formattedRecord = formatter.Format(record);

            Assert.AreEqual("'te,xt',10,", formattedRecord);
        }
        public void TryMap_FieldDisplayNameProviderThrowsException_ExceptionIsPropogated()
        {
            var source                   = new object();
            var record                   = new MockRecord();
            var recordFactory            = MockRepository.GenerateMock <IRecordFactory>();
            var fieldParser              = MockRepository.GenerateMock <IFieldParser>();
            var fieldDisplayNameProvider = MockRepository.GenerateMock <IFieldDisplayNameProvider>();

            recordFactory.Expect(x => x.Create(Arg <object> .Is.Equal(source))).Return(record).Repeat.Once();
            fieldParser.Expect(x => x.TryParse(
                                   Arg <PropertyInfo> .Matches(pi => pi.Name.Equals(nameof(MockRecord.StringField))),
                                   Arg <object> .Is.Equal("Text"),
                                   out Arg <object> .Out(null).Dummy,
                                   out Arg <string> .Out("Field could not be parsed.").Dummy))
            .Return(false)
            .Repeat.Once();
            fieldParser.Expect(x => x.TryParse(
                                   Arg <PropertyInfo> .Matches(pi => pi.Name.Equals(nameof(MockRecord.IntField))),
                                   Arg <object> .Is.Equal(1),
                                   out Arg <object> .Out(10).Dummy,
                                   out Arg <string> .Out(null).Dummy))
            .Return(true)
            .Repeat.Once();
            fieldParser.Expect(x => x.TryParse(
                                   Arg <PropertyInfo> .Matches(pi => pi.Name.Equals(nameof(MockRecord.NullableIntField))),
                                   Arg <object> .Is.Equal(2),
                                   out Arg <object> .Out(20).Dummy,
                                   out Arg <string> .Out(null).Dummy))
            .Return(true)
            .Repeat.Once();
            fieldDisplayNameProvider.Stub(x => x.GetFieldDisplayName(Arg <PropertyInfo> .Is.Anything)).Throw(new InternalTestFailureException());

            var recordMapper = new MockRecordMapper(recordFactory, fieldParser: fieldParser, fieldDisplayNameProvider: fieldDisplayNameProvider);

            recordMapper.TryReadSourceFieldFunc = new Func <PropertyInfo, object, Tuple <bool, object, string> >((pi, src) =>
            {
                switch (pi.Name)
                {
                case "StringField": return(new Tuple <bool, object, string>(true, "Text", null));

                case "IntField": return(new Tuple <bool, object, string>(true, 1, null));

                case "NullableIntField": return(new Tuple <bool, object, string>(true, 2, null));
                }

                Assert.Fail();

                return(null);
            });

            recordMapper.TryMap(source, out var mappedRecord, out var failures);
        }
        public void ApplyTransform_NotNullableTransformWithNullValue_FieldIsNotTransformed()
        {
            var record = new MockRecord()
            {
                FieldWithoutNullableTransformSupport = null
            };
            var property = typeof(MockRecord).GetProperty(nameof(MockRecord.FieldWithoutNullableTransformSupport));
            var transformFieldAttribute = property.GetCustomAttribute <TransformFieldAttribute>();

            transformFieldAttribute.ApplyTransform(property, record);

            Assert.IsNull(record.FieldWithoutNullableTransformSupport);
        }
Exemple #14
0
        public void ApplyTransform_DoubleField3_CeilingAppliedToProperty()
        {
            var record = new MockRecord()
            {
                DoubleField = 10.9d
            };
            var property         = typeof(MockRecord).GetProperty(nameof(MockRecord.DoubleField));
            var ceilingAttribute = new CeilingAttribute();

            ceilingAttribute.ApplyTransform(property, record);

            Assert.AreEqual(11d, record.DoubleField);
        }
Exemple #15
0
        public void ApplyTransform_NonStringField_ExceptionIsThrown()
        {
            var record = new MockRecord()
            {
                IntField = 1
            };
            var oldValue             = "A";
            var newValue             = "X";
            var property             = typeof(MockRecord).GetProperty(nameof(MockRecord.IntField));
            var replaceTextAttribute = new ReplaceTextAttribute(oldValue, newValue);

            replaceTextAttribute.ApplyTransform(property, record);
        }
        public void ApplyTransform_DoubleFieldWithFractionalValue_ValueIsTruncated()
        {
            var record = new MockRecord()
            {
                DoubleField = 10.5d
            };
            var property = typeof(MockRecord).GetProperty(nameof(MockRecord.DoubleField));
            var truncateDecimalAttribute = new TruncateDecimalAttribute();

            truncateDecimalAttribute.ApplyTransform(property, record);

            Assert.AreEqual(10d, record.DoubleField);
        }
Exemple #17
0
        public void ApplyTransform_NullableDecimalField_CeilingAppliedToProperty()
        {
            var record = new MockRecord()
            {
                NullableDecimalField = 10.1m
            };
            var property         = typeof(MockRecord).GetProperty(nameof(MockRecord.NullableDecimalField));
            var ceilingAttribute = new CeilingAttribute();

            ceilingAttribute.ApplyTransform(property, record);

            Assert.AreEqual(11m, record.NullableDecimalField);
        }
        public void ApplyTransform_NullableDoubleFieldWithNullValue_ValueIsUnchanged()
        {
            var record = new MockRecord()
            {
                NullableDoubleField = null
            };
            var property = typeof(MockRecord).GetProperty(nameof(MockRecord.NullableDoubleField));
            var truncateDecimalAttribute = new TruncateDecimalAttribute();

            truncateDecimalAttribute.ApplyTransform(property, record);

            Assert.IsNull(record.NullableDoubleField);
        }
        public void ApplyTransform_NullableTransformWithNonNullValue_FieldIsTransformed()
        {
            var record = new MockRecord()
            {
                FieldWithNullableTransformSupport = "Text"
            };
            var property = typeof(MockRecord).GetProperty(nameof(MockRecord.FieldWithNullableTransformSupport));
            var transformFieldAttribute = property.GetCustomAttribute <TransformFieldAttribute>();

            transformFieldAttribute.ApplyTransform(property, record);

            Assert.AreEqual("NewValue", record.FieldWithNullableTransformSupport);
        }
        public void ApplyTransform_StringFieldWithNullValue_ValueIsUnchanged()
        {
            var record = new MockRecord()
            {
                StringField = null
            };
            var property           = typeof(MockRecord).GetProperty(nameof(MockRecord.StringField));
            var trimStartAttribute = new TrimStartAttribute();

            trimStartAttribute.ApplyTransform(property, record);

            Assert.IsNull(record.StringField);
        }
Exemple #21
0
        public void ApplyTransform_StringFieldWithMixedCaseValue_TextIsLowered()
        {
            var record = new MockRecord()
            {
                StringField = "Test"
            };
            var property         = typeof(MockRecord).GetProperty(nameof(MockRecord.StringField));
            var toLowerAttribute = new ToLowerAttribute();

            toLowerAttribute.ApplyTransform(property, record);

            Assert.AreEqual("test", record.StringField);
        }
Exemple #22
0
        public void ApplyTransform_DecimalField1_FloorAppliedToProperty()
        {
            var record = new MockRecord()
            {
                DecimalField = 10m
            };
            var property       = typeof(MockRecord).GetProperty(nameof(MockRecord.DecimalField));
            var FloorAttribute = new FloorAttribute();

            FloorAttribute.ApplyTransform(property, record);

            Assert.AreEqual(10m, record.DecimalField);
        }
Exemple #23
0
        public void ApplyTransform_NullableDoubleField_FloorAppliedToProperty()
        {
            var record = new MockRecord()
            {
                NullableDoubleField = 10.1d
            };
            var property       = typeof(MockRecord).GetProperty(nameof(MockRecord.NullableDoubleField));
            var FloorAttribute = new FloorAttribute();

            FloorAttribute.ApplyTransform(property, record);

            Assert.AreEqual(10d, record.NullableDoubleField);
        }
        public void ApplyTransform_InvalidType_ExceptionIsThrown()
        {
            var record = new MockRecord()
            {
                StringField = "Test"
            };
            var decimals         = 0;
            var midpointRounding = MidpointRounding.AwayFromZero;
            var property         = typeof(MockRecord).GetProperty(nameof(MockRecord.StringField));
            var roundToAttribute = new RoundToAttribute(decimals, midpointRounding);

            roundToAttribute.ApplyTransform(property, record);
        }
Exemple #25
0
        public void ApplyTransform_StringFieldWithEmptyValue_ValueIsUnchanged()
        {
            var record = new MockRecord()
            {
                StringField = ""
            };
            var property         = typeof(MockRecord).GetProperty(nameof(MockRecord.StringField));
            var toLowerAttribute = new ToLowerAttribute();

            toLowerAttribute.ApplyTransform(property, record);

            Assert.AreEqual(String.Empty, record.StringField);
        }
Exemple #26
0
        public void Format_TextRecordFieldFormatterThrowsException_ExceptionIsPropogated()
        {
            var textRecordFieldFormatter = MockRepository.GenerateMock <ITextRecordFieldFormatter>();
            var formatter = new FixedWidthTextRecordFormatter(fieldFormatter: textRecordFieldFormatter);
            var record    = new MockRecord("text", 10, -1);

            textRecordFieldFormatter.Expect(x => x.Format(Arg <object> .Is.Equal("text"), Arg <PropertyInfo> .Matches(y => y.Name == nameof(MockRecord.StringField)))).Throw(new InternalTestFailureException());
            textRecordFieldFormatter.Expect(x => x.Format(Arg <object> .Is.Equal(10), Arg <PropertyInfo> .Matches(y => y.Name == nameof(MockRecord.IntField)))).Throw(new InternalTestFailureException());
            textRecordFieldFormatter.Expect(x => x.Format(Arg <object> .Is.Equal(-1), Arg <PropertyInfo> .Matches(y => y.Name == nameof(MockRecord.NullableIntField)))).Throw(new InternalTestFailureException());
            textRecordFieldFormatter.Expect(x => x.Format(Arg <object> .Is.Anything, Arg <PropertyInfo> .Matches(y => y.Name == nameof(MockRecord.NotUsed)))).Repeat.Never();

            var formattedRecord = formatter.Format(record);
        }
        public void ApplyTransform_StringFieldWithOnlySpaces_ValueIsTrimmedToEmpty()
        {
            var record = new MockRecord()
            {
                StringField = "   "
            };
            var property           = typeof(MockRecord).GetProperty(nameof(MockRecord.StringField));
            var trimStartAttribute = new TrimStartAttribute();

            trimStartAttribute.ApplyTransform(property, record);

            Assert.AreEqual("", record.StringField);
        }
Exemple #28
0
        public void Format_FieldPaddingStrategyThrowsException_ExceptionIsPropogated()
        {
            var fieldPaddingStrategy = MockRepository.GenerateMock <ITextRecordFieldPaddingStrategy>();
            var formatter            = new FixedWidthTextRecordFormatter(fieldPaddingStrategy: fieldPaddingStrategy);
            var record = new MockRecord("text", 10, -1);

            fieldPaddingStrategy.Expect(x => x.Pad(Arg <string> .Is.Anything, Arg <int> .Is.Anything, Arg <PropertyInfo> .Matches(y => y.Name == nameof(MockRecord.StringField)))).Throw(new InternalTestFailureException());
            fieldPaddingStrategy.Expect(x => x.Pad(Arg <string> .Is.Anything, Arg <int> .Is.Anything, Arg <PropertyInfo> .Matches(y => y.Name == nameof(MockRecord.IntField)))).Throw(new InternalTestFailureException());
            fieldPaddingStrategy.Expect(x => x.Pad(Arg <string> .Is.Anything, Arg <int> .Is.Anything, Arg <PropertyInfo> .Matches(y => y.Name == nameof(MockRecord.NullableIntField)))).Throw(new InternalTestFailureException());
            fieldPaddingStrategy.Expect(x => x.Pad(Arg <string> .Is.Anything, Arg <int> .Is.Anything, Arg <PropertyInfo> .Matches(y => y.Name == nameof(MockRecord.NotUsed)))).Repeat.Never();

            var formattedRecord = formatter.Format(record);
        }
        public void ApplyTransform_StringFieldWithSpacesOnLeftAndRight_TextIsTrimmedAtEnd()
        {
            var record = new MockRecord()
            {
                StringField = "  test  "
            };
            var property           = typeof(MockRecord).GetProperty(nameof(MockRecord.StringField));
            var trimStartAttribute = new TrimStartAttribute();

            trimStartAttribute.ApplyTransform(property, record);

            Assert.AreEqual("test  ", record.StringField);
        }
        public void ApplyTransform_NonStringField_ExceptionIsThrown()
        {
            var record = new MockRecord()
            {
                IntField = 1
            };
            var totalWidth        = 5;
            var paddingChar       = ' ';
            var property          = typeof(MockRecord).GetProperty(nameof(MockRecord.IntField));
            var padRightAttribute = new PadRightAttribute(totalWidth, paddingChar);

            padRightAttribute.ApplyTransform(property, record);
        }