public void SkipTakeTest()
        {
            // Arrange
            string             firstNameStart = TestUser.FirstName.Substring(0, 1);
            int                skip           = 1;
            int                take           = 2;
            IEnumerable <User> expected;
            IEnumerable <User> actual;

            // Act
            using (var context = new DirectoryContextMock()) {
                var queryBase = context.Users.Where(u => u.FirstName.StartsWith(firstNameStart));
                int total     = queryBase.Count();
                if (total <= skip + take)
                {
                    Assert.Inconclusive("Not enough elements for this test.");
                }

                expected = queryBase.ToArray().Skip(skip).Take(take);
                actual   = queryBase.Skip(skip).Take(take).ToArray();
            }

            // Assert
            SequenceAssert.AreEqual(expected.Select(u => u.LastName), actual.Select(u => u.LastName));
        }
Exemple #2
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 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));
        }