Пример #1
0
        public void TwoWayMulti()
        {
            // Convert char a and int i values to string of a repeated i times
            var converter = new FuncMultiConverter <string, bool>(
                (values) =>
            {
                var c = (char)values[0];
                var l = (int)values[1];
                return(new string(c, l));
            },
                (text) =>
            {
                return(new object[] {
                    text?.Length > 0 ? text[0] : '\0',
                    text?.Length ?? 0
                });
            })
                            .AssertConvert(new object[] { 'a', 2 }, true, "aa", twoWay: true)
                            .AssertConvert(new object[] { 'b', 4 }, false, "bbbb", twoWay: true);

            Assert.That(converter.Convert(new object[] { 'a', 2 }, null, null, CultureInfo.InvariantCulture), Is.EqualTo("aa"));
            var backValues = converter.ConvertBack(null, null, null, CultureInfo.InvariantCulture);

            Assert.That((char)backValues[0], Is.EqualTo('\0'));
            Assert.That((int)backValues[1], Is.EqualTo(0));
        }
Пример #2
0
        public void TwoWayMultiWithParam()
        {
            // Convert char a and int i values to string of a repeated i times, or double that if parameter was true
            var converter = new FuncMultiConverter <string, bool>(
                (values, addOne) =>
            {
                var c = (char)values[0];
                var l = (int)values[1];
                if (addOne)
                {
                    l++;
                }
                return(new string(c, l));
            },
                (text, addOne) =>
            {
                return(new object[] {
                    text?.Length > 0 ? text[0] : '\0',
                    (text?.Length ?? 0) - (addOne ? 1 : 0)
                });
            })
                            .AssertConvert(new object[] { 'a', 2 }, true, "aaa", twoWay: true)
                            .AssertConvert(new object[] { 'b', 4 }, false, "bbbb", twoWay: true);

            Assert.That(converter.Convert(new object[] { 'a', 2 }, null, null, CultureInfo.InvariantCulture), Is.EqualTo("aa"));
            var backValues = converter.ConvertBack(null, null, null, CultureInfo.InvariantCulture);

            Assert.That((char)backValues[0], Is.EqualTo('\0'));
            Assert.That((int)backValues[1], Is.EqualTo(0));
        }