• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using System.Text;
21 
22 namespace Google.FlatBuffers.Test
23 {
24 
25     public class AssertFailedException : Exception
26     {
27         private readonly object _expected;
28         private readonly object _actual;
29 
AssertFailedException(object expected, object actual)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 
AssertArrayFailedException(int index, object expected, object actual)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 
AssertUnexpectedThrowException(object expected)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     {
AreEqual(T expected, T actual)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 
ArrayEqual(T[] expected, T[] actual)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 
ArrayEqual(ArraySegment<T> expected, T[] actual)102         public static void ArrayEqual<T>(ArraySegment<T> expected, T[] actual)
103         {
104 #if NETCOREAPP
105             ArrayEqual(expected.ToArray(), actual);
106 #else
107             if (expected.Count != actual.Length)
108             {
109                 throw new AssertFailedException(expected, actual);
110             }
111 
112             for (var i = 0; i < expected.Count; ++i)
113             {
114                 if (!expected.Array[expected.Offset + i].Equals(actual[i]))
115                 {
116                     throw new AssertArrayFailedException(i, expected, actual);
117                 }
118             }
119 #endif
120     }
121 
IsTrue(bool value)122         public static void IsTrue(bool value)
123         {
124             if (!value)
125             {
126                 throw new AssertFailedException(true, value);
127             }
128         }
129 
IsFalse(bool value)130         public static void IsFalse(bool value)
131         {
132             if (value)
133             {
134                 throw new AssertFailedException(false, value);
135             }
136         }
137 
138         public static void Throws<T>(Action action) where T : Exception
139         {
140             var caught = false;
141             try
142             {
143                 action();
144             }
145             catch (T)
146             {
147                 caught = true;
148             }
149 
150             if (!caught)
151             {
152                 throw new AssertUnexpectedThrowException(typeof (T));
153             }
154         }
155     }
156 }
157