コード例 #1
0
        public void AddConverter_MismatchingUnits_ThrowsArgumentException()
        {
            // Arrange
            var atob = CreateMockConverter("a", "b");
            var ctod = CreateMockConverter("c", "d");
            // Act/Assert
            CompositeConverter converter = new CompositeConverter();

            converter.AddConverter(atob.Object);
            Assert.Throws <ArgumentException>(() => converter.AddConverter(ctod.Object));
        }
コード例 #2
0
        public void AddConverter_MatchingUnits_DoesNotThrow()
        {
            // Arrange
            var atob = CreateMockConverter("a", "b");
            var btoc = CreateMockConverter("b", "c");
            // Act/Assert
            CompositeConverter converter = new CompositeConverter();

            converter.AddConverter(atob.Object);
            Assert.DoesNotThrow(() => converter.AddConverter(btoc.Object));
        }
コード例 #3
0
        public void Convert_SingleStep_InvokesConvert()
        {
            // Arrange
            var mockConverter            = CreateMockConverter("a", "b");
            CompositeConverter converter = new CompositeConverter();

            converter.AddConverter(mockConverter.Object);
            // Act
            converter.Convert(1.0);
            // Assert
            mockConverter.Verify(x => x.Convert(It.IsAny <double>()), Times.Once());
        }
コード例 #4
0
ファイル: ArrayTest.cs プロジェクト: he-dev/reusable
        public void Convert_StringArray_Int32Array()
        {
            var converter = new CompositeConverter
            {
                typeof(EnumerableToArrayConverter),
                typeof(StringToInt32Converter),
            };

            var result = converter.Convert(new[] { "3", "7" }, typeof(int[])) as int[];

            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Length);
            Assert.AreEqual(3, result[0]);
            Assert.AreEqual(7, result[1]);
        }
コード例 #5
0
        public void Convert_TwoSteps_InvokesConvert()
        {
            // Arrange
            var mockAtoB = CreateMockConverter("a", "b");
            var mockBtoC = CreateMockConverter("b", "c");
            CompositeConverter converter = new CompositeConverter();

            converter.AddConverter(mockAtoB.Object);
            converter.AddConverter(mockBtoC.Object);
            // Act
            converter.Convert(1.0);
            // Assert
            mockAtoB.Verify(x => x.Convert(It.IsAny <double>()), Times.Once());
            mockBtoC.Verify(x => x.Convert(It.IsAny <double>()), Times.Once());
        }
コード例 #6
0
ファイル: ConvertersFactory.cs プロジェクト: Jevvry/fp
        public override Result <IWordConverter> Create()
        {
            var converterNames   = wordsConfig.ConvertersNames;
            var convertersResult = converterNames
                                   .Select(name => Result.Of(() => services[name](), $"This converter {name} not supported"))
                                   .ToArray();

            if (convertersResult.Any(result => !result.IsSuccess))
            {
                return(convertersResult.Aggregate((working, current) => current.RefineError(working.Error)));
            }

            var compositeConverter = new CompositeConverter(convertersResult
                                                            .Select(res => res.Value)
                                                            .ToArray());

            return(compositeConverter);
        }
コード例 #7
0
        /// <summary>
        /// Creates a converter from a source unit to the target unit.
        /// </summary>
        /// <exception cref="NullReferenceException">Thrown when base unit hasn't been set.</exception>
        /// <exception cref="ArgumentException">Thrown when source and target unit is equal.</exception>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public IValueConverter CreateConverter(IUnit source, IUnit target)
        {
            if (!HasBaseUnit)
            {
                throw new NullReferenceException("BaseUnit not set yet.");
            }
            if (Equals(source, target))
            {
                throw new ArgumentException("Source and target unit are equal.");
            }

            LinkedList <Node>     path               = _root.FindPathBetween(source, target);
            LinkedListNode <Node> current            = path.First;
            CompositeConverter    compositeConverter = new CompositeConverter();

            while (current.Next != null)
            {
                IValueConverter converter = CreateAdjacentConverter(current.Value, current.Next.Value);
                compositeConverter.AddConverter(converter);
                current = current.Next;
            }
            return(compositeConverter);
        }
コード例 #8
0
        public void AddsEachConverterOnlyOnce()
        {
            var converter = new CompositeConverter(new BooleanToStringConverter(), new BooleanToStringConverter());

            Assert.That.Collection().CountEquals(1, converter);
        }
コード例 #9
0
        public void ctor_PassTwoDifferentConverters_CreatesWithTwoDifferentConverters()
        {
            var converter = new CompositeConverter(new BooleanToStringConverter(), new Int32ToStringConverter());

            Assert.That.Collection().CountEquals(2, converter);
        }
コード例 #10
0
        public void ctor_EmptyConverters_CreatesEmptyConverter()
        {
            var converter = new CompositeConverter();

            Assert.That.Collection().IsEmpty(converter);
        }