Beispiel #1
0
        public void WithConvertBackFunction_UsingConverterParameter()
        {
            // with a wrong target type (use default error strategy)
            SequenceAssert.AreEqual(
                new object[] { null },
                MultiValueConverter.Create <string, int, bool>(convertBackFunction: e => null).ConvertBack(1, new[] { typeof(bool) }, true, null));

            // without a target type
            Assert.IsNull(MultiValueConverter.Create <string, int, bool>(convertBackFunction: e => null).ConvertBack(1, new Type[] { }, true, null));
            Assert.IsNull(
                MultiValueConverter.Create <string, int, bool>(convertBackFunction: e => null)
                .ConvertBack(1, new[] { typeof(string), null }, true, null));
            SequenceAssert.AreEqual(
                new[] { "a", "b" },
                MultiValueConverter.Create <string, int, bool>(convertBackFunction: e => new[] { "a", "b" }).ConvertBack(1, null, true, null));

            // with an unexpected parameter (use default error strategy)
            SequenceAssert.AreEqual(
                new object[] { null },
                MultiValueConverter.Create <string, int, bool>(convertBackFunction: e => null).ConvertBack(1, new[] { typeof(string) }, "p", null));
            SequenceAssert.AreEqual(
                new object[] { null },
                MultiValueConverter.Create <string, int, bool>(convertBackFunction: e => null).ConvertBack(1, new[] { typeof(string) }, null, null));

            // with an input value of an unexpected type (use default error strategy)
            SequenceAssert.AreEqual(
                new object[] { null },
                MultiValueConverter.Create <string, int, bool>(convertBackFunction: e => null).ConvertBack(true, new[] { typeof(string) }, true, null));
            SequenceAssert.AreEqual(
                new object[] { null },
                MultiValueConverter.Create <string, int, bool>(convertBackFunction: e => null).ConvertBack(null, new[] { typeof(string) }, true, null));

            // with a valid input value
            SequenceAssert.AreEqual(
                new[] { "a", "b" },
                MultiValueConverter.Create <string, int, bool>(convertBackFunction: e => new[] { "a", "b" })
                .ConvertBack(1, new[] { typeof(string), typeof(string) }, true, null));
            SequenceAssert.AreEqual(
                new[] { "1", "1" },
                MultiValueConverter.Create <string, int, bool>(
                    convertBackFunction: e =>
            {
                Assert.AreEqual(1, e.Value);
                Assert.AreEqual(true, e.Parameter);
                Assert.IsNull(e.Culture);

                return(new[] { e.Value.ToString(), e.Value.ToString() });
            }).ConvertBack(1, new[] { typeof(string), typeof(string) }, true, null));
            SequenceAssert.AreEqual(
                new[] { "1", "1" },
                MultiValueConverter.Create <string, int, bool>(
                    convertBackFunction: e =>
            {
                Assert.AreEqual(1, e.Value);
                Assert.AreEqual(false, e.Parameter);
                Assert.AreEqual(new CultureInfo("en-GB"), e.Culture);

                return(new[] { e.Value.ToString(), e.Value.ToString() });
            }).ConvertBack(1, new[] { typeof(string), typeof(string) }, false, new CultureInfo("en-GB")));
        }
        public void CompareMultiValueConverterArgs()
        {
            StructAssert.IsCorrect <MultiValueConverterArgs <int> >();

            var arg1 = default(MultiValueConverterArgs <int>);
            var arg2 = default(MultiValueConverterArgs <int>);

            Assert.AreEqual(
                "3",
                MultiValueConverter.Create <int, string>(
                    e =>
            {
                arg1 = e;

                return(e.Values.Sum().ToString());
            })
                .Convert(new object[] { 1, 2 }, typeof(string), null, new CultureInfo("en-GB")));
            Assert.AreEqual(
                "6",
                MultiValueConverter.Create <int, string>(
                    e =>
            {
                arg2 = e;

                return(e.Values.Sum().ToString());
            })
                .Convert(new object[] { 1, 2, 3 }, typeof(string), null, new CultureInfo("en-GB")));

            StructAssert.AreEqual(default(MultiValueConverterArgs <int>), (x, y) => x == y, (x, y) => x != y);
            StructAssert.AreEqual(arg1, (x, y) => x == y, (x, y) => x != y);
            StructAssert.AreEqual(arg2, (x, y) => x == y, (x, y) => x != y);
            StructAssert.AreNotEqual(arg1, arg2, (x, y) => x == y, (x, y) => x != y);

            new HashSet <MultiValueConverterArgs <int> > {
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            if (MultiValueConverter == null)
            {
                throw _multiValueConverterUnsetException;
            }
            var tailConverted = GroupConvertBack(value, Converters.ToArray().Reverse());

            return(MultiValueConverter.ConvertBack(tailConverted, targetTypes, parameter, culture));
        }
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (MultiValueConverter == null)
            {
                throw _multiValueConverterUnsetException;
            }
            var firstConvertedValue = MultiValueConverter.Convert(values, targetType, parameter, culture);

            return(GroupConvert(firstConvertedValue, Converters));
        }
Beispiel #5
0
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            if (MultiValueConverter == null)
            {
                throw new InvalidOperationException("To use the converter as a MultiValueConverter the MultiValueConverter property needs to be set.");
            }
            var tailConverted = GroupConvertBack(value, Converters.ToArray().Reverse(), parameter, culture);

            return(MultiValueConverter.ConvertBack(tailConverted, targetTypes, parameter, culture));
        }
Beispiel #6
0
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (MultiValueConverter == null)
            {
                throw new InvalidOperationException("To use the converter as a MultiValueConverter the MultiValueConverter property needs to be set.");
            }
            var firstConvertedValue = MultiValueConverter.Convert(values, targetType, parameter, culture);

            return(GroupConvert(firstConvertedValue, Converters, parameter, culture));
        }
Beispiel #7
0
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            for (int i = 0; i < values.Length; i++)
            {
                values[i] = PreProcessing.Convert(values[i], null, null, culture);
            }

            var value = MultiValueConverter.Convert(values, null, parameter, culture);

            return(PostProcessing.Convert(value, null, null, culture));
        }
Beispiel #8
0
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            value = PostProcessing.ConvertBack(value, null, null, culture);

            var values = MultiValueConverter.ConvertBack(value, null, parameter, culture);

            for (int i = 0; i < values.Length; i++)
            {
                values[i] = PreProcessing.ConvertBack(values[i], null, null, culture);
            }

            return(values);
        }
        public void WithConvertFunction_UsingConverterParameter()
        {
            // with a wrong target type (use default error strategy)
            Assert.IsNull(MultiValueConverter.Create <int, string?, bool>(e => null).Convert(new object[] { 1, 2 }, typeof(bool), true, null));

            // without a target type
            Assert.AreEqual("a", MultiValueConverter.Create <int, string, bool>(e => "a").Convert(new object[] { 1, 2 }, null, true, null));

            // with an unexpected parameter (use default error strategy)
            Assert.IsNull(MultiValueConverter.Create <int, string?, bool>(e => null).Convert(new object[] { 1, 2 }, typeof(string), "p", null));
            Assert.IsNull(MultiValueConverter.Create <int, string?, bool>(e => null).Convert(new object[] { 1, 2 }, typeof(string), null, null));

            // with an input value of an unexpected type (use default error strategy)
            Assert.IsNull(
                MultiValueConverter.Create <int, string?, bool>(e => null).Convert(new object?[] { true, null }, typeof(string), true, null));
            Assert.IsNull(
                MultiValueConverter.Create <int, string?, bool>(e => null).Convert(new object?[] { null, true }, typeof(string), true, null));
            Assert.IsNull(
                MultiValueConverter.Create <int, string?, bool>(e => null).Convert(ArrayUtils.GetEmpty <object>(), typeof(string), true, null));
            Assert.IsNull(MultiValueConverter.Create <int, string?, bool>(e => null).Convert(null, typeof(string), true, null));

            // with a valid input value
            Assert.AreEqual("a", MultiValueConverter.Create <int, string, bool>(e => "a").Convert(new object[] { 1, 2 }, typeof(string), true, null));
            Assert.AreEqual(
                "3",
                MultiValueConverter.Create <int, string, bool>(
                    e =>
            {
                SequenceAssert.AreEqual(new[] { 1, 2 }, e.Values);
                Assert.AreEqual(true, e.Parameter);
                Assert.IsNull(e.Culture);

                return(e.Values.Sum().ToString());
            })
                .Convert(new object[] { 1, 2 }, typeof(string), true, null));
            Assert.AreEqual(
                "3",
                MultiValueConverter.Create <int, string, bool>(
                    e =>
            {
                SequenceAssert.AreEqual(new[] { 1, 2 }, e.Values);
                Assert.AreEqual(false, e.Parameter);
                Assert.AreEqual(new CultureInfo("en-GB"), e.Culture);

                return(e.Values.Sum().ToString());
            })
                .Convert(new object[] { 1, 2 }, typeof(string), false, new CultureInfo("en-GB")));
        }
        public void NoFunctions_UsingConverterParameter()
        {
            // invalid error strategy
            ExceptionAssert.Throws <ArgumentOutOfRangeException>(
                () => MultiValueConverter.Create <int, string, bool>(errorStrategy: (ConverterErrorStrategy)int.MaxValue),
                "errorStrategy");

            // with ConverterErrorStrategy.ReturnDefaultValue (default)
            Assert.AreEqual(null, MultiValueConverter.Create <int, string, bool>().Convert(null, null, null, null));
            Assert.AreEqual(null, MultiValueConverter.Create <int, string, bool>().Convert(ArrayUtils.GetEmpty <object>(), null, null, null));
            Assert.AreEqual(null, MultiValueConverter.Create <int, string, bool>().Convert(new object[] { 1, 2 }, null, null, null));
            Assert.AreEqual(0, MultiValueConverter.Create <int, int, bool>().Convert(null, null, null, null));
            Assert.AreEqual(0, MultiValueConverter.Create <int, int, bool>().Convert(ArrayUtils.GetEmpty <object>(), null, null, null));
            Assert.AreEqual(0, MultiValueConverter.Create <int, int, bool>().Convert(new object[] { 1, 2 }, null, null, null));
            Assert.AreEqual(false, MultiValueConverter.Create <int, bool, bool>().Convert(null, null, null, null));
            Assert.AreEqual(false, MultiValueConverter.Create <int, bool, bool>().Convert(ArrayUtils.GetEmpty <object>(), null, null, null));
            Assert.AreEqual(false, MultiValueConverter.Create <int, bool, bool>().Convert(new object[] { 1, 2 }, null, null, null));

            Assert.IsNull(MultiValueConverter.Create <string, int, bool>().ConvertBack(1, null, null, null));
            SequenceAssert.AreEqual(
                ArrayUtils.GetEmpty <object>(),
                MultiValueConverter.Create <string, int, bool>().ConvertBack(1, ArrayUtils.GetEmpty <Type>(), null, null));
            SequenceAssert.AreEqual(
                new object[2],
                MultiValueConverter.Create <string, int, bool>().ConvertBack(1, new[] { typeof(int), typeof(string) }, null, null));
            Assert.IsNull(MultiValueConverter.Create <int, int, bool>().ConvertBack(1, null, null, null));
            SequenceAssert.AreEqual(
                ArrayUtils.GetEmpty <object>(),
                MultiValueConverter.Create <int, int, bool>().ConvertBack(1, ArrayUtils.GetEmpty <Type>(), null, null));
            SequenceAssert.AreEqual(
                new object[] { 0, 0 },
                MultiValueConverter.Create <int, int, bool>().ConvertBack(1, new[] { typeof(int), typeof(string) }, null, null));
            Assert.IsNull(MultiValueConverter.Create <bool, int, bool>().ConvertBack(1, null, null, null));
            SequenceAssert.AreEqual(
                ArrayUtils.GetEmpty <object>(),
                MultiValueConverter.Create <bool, int, bool>().ConvertBack(1, ArrayUtils.GetEmpty <Type>(), null, null));
            SequenceAssert.AreEqual(
                new object[] { false, false },
                MultiValueConverter.Create <bool, int, bool>().ConvertBack(1, new[] { typeof(int), typeof(string) }, null, null));

            // with ConverterErrorStrategy.UseFallbackOrDefaultValue
            Assert.AreEqual(
                DependencyProperty.UnsetValue,
                MultiValueConverter.Create <int, string, bool>(errorStrategy: ConverterErrorStrategy.UseFallbackOrDefaultValue)
                .Convert(null, null, null, null));
            Assert.AreEqual(
                DependencyProperty.UnsetValue,
                MultiValueConverter.Create <int, string, bool>(errorStrategy: ConverterErrorStrategy.UseFallbackOrDefaultValue)
                .Convert(ArrayUtils.GetEmpty <object>(), null, null, null));
            Assert.AreEqual(
                DependencyProperty.UnsetValue,
                MultiValueConverter.Create <int, string, bool>(errorStrategy: ConverterErrorStrategy.UseFallbackOrDefaultValue)
                .Convert(new object[] { 1, 2 }, null, null, null));

            Assert.IsNull(
                MultiValueConverter.Create <string, int, bool>(errorStrategy: ConverterErrorStrategy.UseFallbackOrDefaultValue)
                .ConvertBack(1, null, null, null));
            SequenceAssert.AreEqual(
                ArrayUtils.GetEmpty <object>(),
                MultiValueConverter.Create <string, int, bool>(errorStrategy: ConverterErrorStrategy.UseFallbackOrDefaultValue)
                .ConvertBack(1, ArrayUtils.GetEmpty <Type>(), null, null));
            SequenceAssert.AreEqual(
                new[] { DependencyProperty.UnsetValue, DependencyProperty.UnsetValue },
                MultiValueConverter.Create <string, int, bool>(errorStrategy: ConverterErrorStrategy.UseFallbackOrDefaultValue)
                .ConvertBack(1, new[] { typeof(int), typeof(string) }, null, null));

            // with ConverterErrorStrategy.DoNothing
            Assert.AreEqual(
                Binding.DoNothing,
                MultiValueConverter.Create <int, string, bool>(errorStrategy: ConverterErrorStrategy.DoNothing).Convert(null, null, null, null));
            Assert.AreEqual(
                Binding.DoNothing,
                MultiValueConverter.Create <int, string, bool>(errorStrategy: ConverterErrorStrategy.DoNothing)
                .Convert(ArrayUtils.GetEmpty <object>(), null, null, null));
            Assert.AreEqual(
                Binding.DoNothing,
                MultiValueConverter.Create <int, string, bool>(errorStrategy: ConverterErrorStrategy.DoNothing)
                .Convert(new object[] { 1, 2 }, null, null, null));

            Assert.IsNull(
                MultiValueConverter.Create <string, int, bool>(errorStrategy: ConverterErrorStrategy.DoNothing).ConvertBack(1, null, null, null));
            SequenceAssert.AreEqual(
                ArrayUtils.GetEmpty <object>(),
                MultiValueConverter.Create <string, int, bool>(errorStrategy: ConverterErrorStrategy.DoNothing)
                .ConvertBack(1, ArrayUtils.GetEmpty <Type>(), null, null));
            SequenceAssert.AreEqual(
                new[] { Binding.DoNothing, Binding.DoNothing },
                MultiValueConverter.Create <string, int, bool>(errorStrategy: ConverterErrorStrategy.DoNothing)
                .ConvertBack(1, new[] { typeof(int), typeof(string) }, null, null));
        }
Beispiel #11
0
 public IfExtension()
 {
     Converter = new MultiValueConverter(this);
 }
        /*
         * Have to list these constructor overloads here,
         * because the stupid xaml does not support the "params" keyword.
         * e.g.: ctor(params object[] converters) cannot be used.
         */

        public I18nStringExtension()
        {
            Converter = new MultiValueConverter(this);
            Args      = new ArgCollection(this);
        }
Beispiel #13
0
 public SwitchExtension()
 {
     Cases     = new CaseCollection(this);
     Converter = new MultiValueConverter(this);
 }