Assert.cs revision 557c88c0396220e79e9a43c07f8393a5c68b739d
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace FlatBuffers.Test
7{
8
9    public class AssertFailedException : Exception
10    {
11        private readonly object _expected;
12        private readonly object _actual;
13
14        public AssertFailedException(object expected, object actual)
15        {
16            _expected = expected;
17            _actual = actual;
18        }
19
20        public override string Message
21        {
22            get { return string.Format("Expected {0} but saw {1}", _expected, _actual); }
23        }
24    }
25
26    public class AssertUnexpectedThrowException : Exception
27    {
28        private readonly object _expected;
29
30        public AssertUnexpectedThrowException(object expected)
31        {
32            _expected = expected;
33        }
34
35        public override string Message
36        {
37            get { return string.Format("Expected exception of type {0}", _expected); }
38        }
39    }
40
41    public static class Assert
42    {
43        public static void AreEqual<T>(T expected, T actual)
44        {
45            if (!expected.Equals(actual))
46            {
47                throw new AssertFailedException(expected, actual);
48            }
49        }
50
51        public static void IsTrue(bool value)
52        {
53            if (!value)
54            {
55                throw new AssertFailedException(true, value);
56            }
57        }
58
59        public static void Throws<T>(Action action) where T : Exception
60        {
61            var caught = false;
62            try
63            {
64                action();
65            }
66            catch (T)
67            {
68                caught = true;
69            }
70
71            if (!caught)
72            {
73                throw new AssertUnexpectedThrowException(typeof (T));
74            }
75        }
76    }
77}
78