Exemplo n.º 1
0
        public void DataTypeChecker_CheckTypes_Cycles_Failure()
        {
            foreach (var t in new[]
            {
                typeof(Cycle1),
                typeof(Cycle2),
                typeof(Cycle3),
                typeof(Cycle1[]),
                typeof(List <Cycle2>),
                typeof(Tuple <Cycle3>),
            })
            {
                Assert.ThrowsException <InvalidOperationException>(() => DataType.FromType(t));
                Assert.ThrowsException <InvalidOperationException>(() => DataType.FromType(t, allowCycles: false));

                Assert.ThrowsException <AggregateException>(() => DataType.Check(t));
                Assert.ThrowsException <AggregateException>(() => DataType.Check(t, allowCycles: false));

                {
                    Assert.IsFalse(DataType.TryCheck(t, out var err));
                    Assert.AreNotEqual(0, err.Count, "Type check failed for: " + t.Name);
                }

                {
                    Assert.IsFalse(DataType.TryCheck(t, allowCycles: false, out var err));
                    Assert.AreNotEqual(0, err.Count, "Type check failed for: " + t.Name);
                }

                Assert.IsFalse(DataType.TryFromType(t, out _));
                Assert.IsFalse(DataType.TryFromType(t, allowCycles: false, out _));
            }
        }
Exemplo n.º 2
0
        public void DataTypeChecker_CheckTypes_Failure_UnsupportedTypes()
        {
            var anon = new { x = 1, b = new bool[] { true }, c = new List <AppDomain[]>() };

            foreach (var t in new[]
            {
                typeof(AppDomain),
                typeof(ExpandoObject),

                typeof(RuntimeMethodHandle),
                typeof(RuntimeMethodHandle?),

                typeof(int[, ]),
                typeof(int).MakePointerType(),
                typeof(int).MakeByRefType(),
                typeof(IEnumerable <>),
                typeof(IEnumerable <>).GetGenericArguments()[0],
                typeof(List <>),

                typeof(IEnumerable <int>),
                typeof(Func <AppDomain>),
                typeof(Func <AppDomain, int>),
                typeof(Func <int, AppDomain>),

                anon.GetType(),
            })
            {
                {
                    Assert.IsFalse(DataType.TryCheck(t, out var err), "Type check failed for: " + t.Name);
                    Assert.AreNotEqual(0, err.Count, "Type check failed for: " + t.Name);
                }

                {
                    Assert.IsFalse(DataType.TryCheck(t, allowCycles: false, out var err), "Type check failed for: " + t.Name);
                    Assert.AreNotEqual(0, err.Count, "Type check failed for: " + t.Name);
                }

                {
                    Assert.IsFalse(DataType.TryCheck(t, allowCycles: true, out var err), "Type check failed for: " + t.Name);
                    Assert.AreNotEqual(0, err.Count, "Type check failed for: " + t.Name);
                }

                Assert.ThrowsException <AggregateException>(() => DataType.Check(t));
                Assert.ThrowsException <AggregateException>(() => DataType.Check(t, allowCycles: false));
                Assert.ThrowsException <AggregateException>(() => DataType.Check(t, allowCycles: true));

                Assert.ThrowsException <NotSupportedException>(() => DataType.FromType(t));
                Assert.ThrowsException <NotSupportedException>(() => DataType.FromType(t, allowCycles: false));
                Assert.ThrowsException <NotSupportedException>(() => DataType.FromType(t, allowCycles: true));

                Assert.IsFalse(DataType.TryFromType(t, out _));
                Assert.IsFalse(DataType.TryFromType(t, allowCycles: false, out _));
                Assert.IsFalse(DataType.TryFromType(t, allowCycles: true, out _));
            }
        }
Exemplo n.º 3
0
        public void DataTypeChecker_CheckTypes_Failure_InvalidTypes()
        {
            foreach (var t in new[]
            {
                typeof(DuplicateAttribute),
                typeof(MissingAttribute),
                typeof(EmptyAttribute1),
                typeof(EmptyAttribute2),
                typeof(EmptyAttribute3),
                typeof(WriteOnlyProperty1),
                typeof(WriteOnlyProperty2),
                typeof(CtorInvalidMapping),
                typeof(CtorMismatchedTypeMapping),
                typeof(CtorMissingMapping),

                typeof(EnumMissingMapping),
                typeof(EnumDuplicateMapping),
            })
            {
                {
                    Assert.IsFalse(DataType.TryCheck(t, out var err), "Type check failed for: " + t.Name);
                    Assert.AreNotEqual(0, err.Count, "Type check failed for: " + t.Name);
                }

                {
                    Assert.IsFalse(DataType.TryCheck(t, allowCycles: false, out var err), "Type check failed for: " + t.Name);
                    Assert.AreNotEqual(0, err.Count, "Type check failed for: " + t.Name);
                }

                {
                    Assert.IsFalse(DataType.TryCheck(t, allowCycles: true, out var err), "Type check failed for: " + t.Name);
                    Assert.AreNotEqual(0, err.Count, "Type check failed for: " + t.Name);
                }

                Assert.ThrowsException <AggregateException>(() => DataType.Check(t));
                Assert.ThrowsException <AggregateException>(() => DataType.Check(t, allowCycles: false));
                Assert.ThrowsException <AggregateException>(() => DataType.Check(t, allowCycles: true));

                Assert.ThrowsException <InvalidOperationException>(() => DataType.FromType(t));
                Assert.ThrowsException <InvalidOperationException>(() => DataType.FromType(t, allowCycles: false));
                Assert.ThrowsException <InvalidOperationException>(() => DataType.FromType(t, allowCycles: true));

                Assert.IsFalse(DataType.TryFromType(t, out _));
                Assert.IsFalse(DataType.TryFromType(t, allowCycles: false, out _));
                Assert.IsFalse(DataType.TryFromType(t, allowCycles: true, out _));
            }
        }
Exemplo n.º 4
0
        public void DataTypeChecker_CheckTypes_Cycles_Success()
        {
            foreach (var t in new[]
            {
                typeof(Cycle1),
                typeof(Cycle2),
                typeof(Cycle3),
                typeof(Cycle1[]),
                typeof(List <Cycle2>),
                typeof(Tuple <Cycle3>),
            })
            {
                Assert.IsNotNull(DataType.FromType(t, allowCycles: true));

                DataType.Check(t, allowCycles: true);

                Assert.IsTrue(DataType.TryCheck(t, allowCycles: true, out var err));
                Assert.AreEqual(0, err.Count, "Type check failed for: " + t.Name);

                Assert.IsTrue(DataType.TryFromType(t, allowCycles: true, out _));
            }
        }
Exemplo n.º 5
0
 public void DataType_Check_ArgumentChecking()
 {
     AssertEx.ThrowsException <ArgumentNullException>(() => DataType.Check(type: null), ex => Assert.AreEqual("type", ex.ParamName));
     AssertEx.ThrowsException <ArgumentNullException>(() => DataType.Check(type: null, allowCycles: false), ex => Assert.AreEqual("type", ex.ParamName));
 }
Exemplo n.º 6
0
        public void DataTypeChecker_CheckTypes_Success()
        {
            var anon = new { x = 1, b = new bool[] { true }, c = new List <int[]>() };
            var rec  = RuntimeCompiler.CreateRecordType(new[] { new KeyValuePair <string, Type>("bar", typeof(int)) }, valueEquality: true);

            foreach (var t in new[]
            {
                typeof(Unit),
                typeof(sbyte),
                typeof(byte),
                typeof(short),
                typeof(ushort),
                typeof(int),
                typeof(uint),
                typeof(long),
                typeof(ulong),
                typeof(float),
                typeof(double),
                typeof(decimal),
                typeof(bool),
                typeof(char),
                typeof(string),
                typeof(DateTime),
                typeof(DateTimeOffset),
                typeof(TimeSpan),
                typeof(Guid),
                typeof(Uri),

                typeof(sbyte?),
                typeof(byte?),
                typeof(short?),
                typeof(ushort?),
                typeof(int?),
                typeof(uint?),
                typeof(long?),
                typeof(ulong?),
                typeof(float?),
                typeof(double?),
                typeof(decimal?),
                typeof(bool?),
                typeof(char?),
                typeof(DateTime?),
                typeof(DateTimeOffset?),
                typeof(TimeSpan?),
                typeof(Guid?),

                typeof(void),
                typeof(ConsoleColor),
                typeof(ConsoleColor?),

                typeof(int[]),
                typeof(string[][]),
                typeof(bool[][][]),

                typeof(List <int>),
                typeof(List <bool?[]>),
                typeof(IList <int>),
                typeof(IList <bool?[]>),

                typeof(Tuple <short>),
                typeof(Tuple <short, byte>),
                typeof(Tuple <short, byte, double[]>),
                typeof(Tuple <short, byte, double[], decimal>),
                typeof(Tuple <short, byte, double[], decimal, long?>),
                typeof(Tuple <short, byte, double[], decimal, long?, float>),
                typeof(Tuple <short, byte, double[], decimal, long?, float, bool>),
                typeof(Tuple <short, byte, double[], decimal, long?, float, bool, Tuple <char> >),
                typeof(Tuple <short, byte, double[], decimal, long?, float, bool, Tuple <char, string> >),

                typeof(Func <int>),
                typeof(Func <int, int>),
                typeof(Func <int, string, bool>),
                typeof(Action),
                typeof(Action <int>),

                typeof(Expression),
                typeof(Expression <Func <int> >),

                typeof(Func <int?, Func <List <double[]>, Tuple <string, bool[]> > >),

                typeof(Bar),
                typeof(Bar[]),

                anon.GetType(),
                rec,
            })
            {
                {
                    Assert.IsTrue(DataType.TryCheck(t, out var err), "Type check failed for: " + t.Name);
                    Assert.AreEqual(0, err.Count, "Type check failed for: " + t.Name);
                }

                {
                    Assert.IsTrue(DataType.TryCheck(t, allowCycles: false, out var err), "Type check failed for: " + t.Name);
                    Assert.AreEqual(0, err.Count, "Type check failed for: " + t.Name);
                }

                {
                    Assert.IsTrue(DataType.TryCheck(t, allowCycles: true, out var err), "Type check failed for: " + t.Name);
                    Assert.AreEqual(0, err.Count, "Type check failed for: " + t.Name);
                }

                DataType.Check(t);
                Assert.IsNotNull(DataType.FromType(t));
                Assert.IsNotNull(DataType.FromType(t, allowCycles: false));
                Assert.IsNotNull(DataType.FromType(t, allowCycles: true));

                Assert.IsTrue(DataType.TryFromType(t, out _));
                Assert.IsTrue(DataType.TryFromType(t, allowCycles: false, out _));
                Assert.IsTrue(DataType.TryFromType(t, allowCycles: true, out _));
            }
        }