1/*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21
22namespace FlatBuffers.Test
23{
24
25    public class AssertFailedException : Exception
26    {
27        private readonly object _expected;
28        private readonly object _actual;
29
30        public AssertFailedException(object expected, object actual)
31        {
32            _expected = expected;
33            _actual = actual;
34        }
35
36        public override string Message
37        {
38            get { return string.Format("Expected {0} but saw {1}", _expected, _actual); }
39        }
40    }
41
42    public class AssertArrayFailedException : Exception
43    {
44        private readonly int _index;
45        private readonly object _expected;
46        private readonly object _actual;
47
48        public AssertArrayFailedException(int index, object expected, object actual)
49        {
50            _index = index;
51            _expected = expected;
52            _actual = actual;
53        }
54
55        public override string Message
56        {
57            get { return string.Format("Expected {0} at index {1} but saw {2}", _expected, _index, _actual); }
58        }
59    }
60
61    public class AssertUnexpectedThrowException : Exception
62    {
63        private readonly object _expected;
64
65        public AssertUnexpectedThrowException(object expected)
66        {
67            _expected = expected;
68        }
69
70        public override string Message
71        {
72            get { return string.Format("Expected exception of type {0}", _expected); }
73        }
74    }
75
76    public static class Assert
77    {
78        public static void AreEqual<T>(T expected, T actual)
79        {
80            if (!expected.Equals(actual))
81            {
82                throw new AssertFailedException(expected, actual);
83            }
84        }
85
86        public static void ArrayEqual<T>(T[] expected, T[] actual)
87        {
88            if (expected.Length != actual.Length)
89            {
90                throw new AssertFailedException(expected, actual);
91            }
92
93            for(var i = 0; i < expected.Length; ++i)
94            {
95                if (!expected[i].Equals(actual[i]))
96                {
97                    throw new AssertArrayFailedException(i, expected, actual);
98                }
99            }
100        }
101
102        public static void IsTrue(bool value)
103        {
104            if (!value)
105            {
106                throw new AssertFailedException(true, value);
107            }
108        }
109
110        public static void IsFalse(bool value)
111        {
112            if (value)
113            {
114                throw new AssertFailedException(false, value);
115            }
116        }
117
118        public static void Throws<T>(Action action) where T : Exception
119        {
120            var caught = false;
121            try
122            {
123                action();
124            }
125            catch (T)
126            {
127                caught = true;
128            }
129
130            if (!caught)
131            {
132                throw new AssertUnexpectedThrowException(typeof (T));
133            }
134        }
135    }
136}
137