public void ErrorWhenBindingStringFormatAndExplicitStringFormat()
            {
                var converter = new LengthConverter
                {
                    StringFormat = "F1 mm"
                };

                var providerMock = new ServiceProviderMock
                {
                    BindingStringFormat = "F2 cm"
                };

                converter.ProvideValue(providerMock.Object);
                var length = Length.FromMillimetres(1234);

                Wpf.Is.DesignMode = true;
                var ex       = Assert.Throws <InvalidOperationException>(() => converter.Convert(length, typeof(string), null, null));
                var expected = "ValueFormat cannot be set when Binding.StringFormat is a unit format.\r\n" +
                               "Ambiguous units StringFormat: {0:F1} mm Binding.StringFormat: {0:F2} cm";

                Assert.AreEqual(expected, ex.Message);

                Wpf.Is.DesignMode = false;
                var convert = converter.Convert(length, typeof(string), null, null);

                Assert.AreEqual(expected, convert);
            }
            public void WithBindingStringFormat(Type targetType, string stringFormat)
            {
                var converter    = new LengthConverter();
                var providerMock = new ServiceProviderMock
                {
                    BindingStringFormat = stringFormat
                };

                converter.ProvideValue(providerMock.Object);
                var length = Length.FromMillimetres(1234);
                var actual = converter.Convert(length, targetType, null, null);

                Assert.AreEqual(length, actual);
            }
Exemple #3
0
            public void BindingStringFormatHappyPath(string stringFormat)
            {
                var converter    = new LengthConverter();
                var providerMock = new ServiceProviderMock
                {
                    BindingStringFormat = stringFormat
                };

                converter.ProvideValue(providerMock.Object);
                var length  = Length.FromMillimetres(12.3);
                var convert = converter.Convert(length, typeof(string), null, CultureInfo.InvariantCulture);

                Assert.AreEqual(length, convert);
                Assert.AreEqual(LengthUnit.Centimetres, converter.Unit);
                Assert.AreEqual(UnitInput.SymbolRequired, converter.UnitInput);
            }
            public void WithBindingStringFormatNull(Type targetType, object expected)
            {
                var converter = new LengthConverter {
                    Unit = LengthUnit.Metres
                };
                var providerMock = new ServiceProviderMock
                {
                    BindingStringFormat = null
                };

                converter.ProvideValue(providerMock.Object);
                var length = Length.FromMillimetres(1234);
                var actual = converter.Convert(length, targetType, null, CultureInfo.GetCultureInfo("sv-SE"));

                Assert.AreEqual(expected, actual);
            }
            public void ErrorWhenProvideValueTargetThrows()
            {
                var converter = new LengthConverter {
                    Unit = LengthUnit.Metres
                };
                var providerMock = new ServiceProviderMock();

                providerMock.ProvideValueTargetMock.Setup(x => x.TargetObject).Throws <NullReferenceException>();
                Wpf.Is.DesignMode = true;
                Assert.Throws <InvalidOperationException>(() => converter.ProvideValue(providerMock.Object));

                Wpf.Is.DesignMode = false;
                var length = Length.FromMillimetres(1234);
                var actual = converter.Convert(length, typeof(string), null, null);

                Assert.AreEqual("Touching provideValueTarget?.TargetObject threw", actual);
            }
            public void WithExplicitUnitAndBindingNonUnitStringFormat(Type targetType, object expected)
            {
                var converter = new LengthConverter
                {
                    Unit = LengthUnit.Centimetres,
                };

                var providerMock = new ServiceProviderMock
                {
                    BindingStringFormat = "{}{0:F2}"
                };

                converter.ProvideValue(providerMock.Object);
                var length = Length.FromMillimetres(12);
                var actual = converter.Convert(length, targetType, null, null);

                Assert.AreEqual(expected, actual);
            }