File: ArrayEqualsFailureMessageFixture.cs

package info (click to toggle)
nunit 2.6.4+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 13,092 kB
  • ctags: 14,310
  • sloc: cs: 87,766; xml: 5,858; cpp: 512; sh: 198; makefile: 48; ansic: 8
file content (240 lines) | stat: -rw-r--r-- 11,530 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// ****************************************************************
// Copyright 2007, Charlie Poole
// This is free software licensed under the NUnit license. You may
// obtain a copy of the license at http://nunit.org.
// ****************************************************************

using System;
using System.Collections;

namespace NUnit.Framework.Tests
{
	/// <summary>
	/// Summary description for ArrayEqualsFailureMessageFixture.
	/// </summary>
    [TestFixture]
    public class ArrayEqualsFailureMessageFixture : MessageChecker
    {
        [Test, ExpectedException(typeof(AssertionException))]
        public void ArraysHaveDifferentRanks()
        {
            int[] expected = new int[] { 1, 2, 3, 4 };
            int[,] actual = new int[,] { { 1, 2 }, { 3, 4 } };

            expectedMessage =
                "  Expected is <System.Int32[4]>, actual is <System.Int32[2,2]>" + Environment.NewLine;
            Expect(actual, EqualTo(expected));
        }

        [Test, ExpectedException(typeof(AssertionException))]
        public void ExpectedArrayIsLonger()
        {
            int[] expected = new int[] { 1, 2, 3, 4, 5 };
            int[] actual = new int[] { 1, 2, 3 };

            expectedMessage =
                "  Expected is <System.Int32[5]>, actual is <System.Int32[3]>" + Environment.NewLine +
                "  Values differ at index [3]" + Environment.NewLine +
                "  Missing:  < 4, 5 >";
            Expect(actual, EqualTo(expected));
        }

        [Test, ExpectedException(typeof(AssertionException))]
        public void ActualArrayIsLonger()
        {
            int[] expected = new int[] { 1, 2, 3 };
            int[] actual = new int[] { 1, 2, 3, 4, 5, 6, 7 };

            expectedMessage =
                "  Expected is <System.Int32[3]>, actual is <System.Int32[7]>" + Environment.NewLine +
                "  Values differ at index [3]" + Environment.NewLine +
                "  Extra:    < 4, 5, 6... >";
            Expect(actual, EqualTo(expected));
        }

        [Test, ExpectedException(typeof(AssertionException))]
        public void FailureOnSingleDimensionedArrays()
        {
            int[] expected = new int[] { 1, 2, 3 };
            int[] actual = new int[] { 1, 5, 3 };

            expectedMessage =
                "  Expected and actual are both <System.Int32[3]>" + Environment.NewLine +
                "  Values differ at index [1]" + Environment.NewLine +
                TextMessageWriter.Pfx_Expected + "2" + Environment.NewLine +
                TextMessageWriter.Pfx_Actual + "5" + Environment.NewLine;
            Expect(actual, EqualTo(expected));
        }

        [Test, ExpectedException(typeof(AssertionException))]
        public void DoubleDimensionedArrays()
        {
            int[,] expected = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
            int[,] actual = new int[,] { { 1, 3, 2 }, { 4, 0, 6 } };

            expectedMessage =
                "  Expected and actual are both <System.Int32[2,3]>" + Environment.NewLine +
                "  Values differ at index [0,1]" + Environment.NewLine +
                TextMessageWriter.Pfx_Expected + "2" + Environment.NewLine +
                TextMessageWriter.Pfx_Actual + "3" + Environment.NewLine;
            Expect(actual, EqualTo(expected));
        }

        [Test, ExpectedException(typeof(AssertionException))]
        public void TripleDimensionedArrays()
        {
            int[, ,] expected = new int[,,] { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
            int[, ,] actual = new int[,,] { { { 1, 2 }, { 3, 4 } }, { { 0, 6 }, { 7, 8 } } };

            expectedMessage =
                "  Expected and actual are both <System.Int32[2,2,2]>" + Environment.NewLine +
                "  Values differ at index [1,0,0]" + Environment.NewLine +
                TextMessageWriter.Pfx_Expected + "5" + Environment.NewLine +
                TextMessageWriter.Pfx_Actual + "0" + Environment.NewLine;
            Expect(actual, EqualTo(expected));
        }

        [Test, ExpectedException(typeof(AssertionException))]
        public void FiveDimensionedArrays()
        {
            int[, , , ,] expected = new int[2, 2, 2, 2, 2] { { { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }, { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } } }, { { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }, { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } } } };
            int[, , , ,] actual = new int[2, 2, 2, 2, 2] { { { { { 1, 2 }, { 4, 3 } }, { { 5, 6 }, { 7, 8 } } }, { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } } }, { { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }, { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } } } };

            expectedMessage =
                "  Expected and actual are both <System.Int32[2,2,2,2,2]>" + Environment.NewLine +
                "  Values differ at index [0,0,0,1,0]" + Environment.NewLine +
                TextMessageWriter.Pfx_Expected + "3" + Environment.NewLine +
                TextMessageWriter.Pfx_Actual + "4" + Environment.NewLine;
            Expect(actual, EqualTo(expected));
        }

        [Test, ExpectedException(typeof(AssertionException))]
        public void JaggedArrays()
        {
            int[][] expected = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6, 7 }, new int[] { 8, 9 } };
            int[][] actual = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 0, 7 }, new int[] { 8, 9 } };

            expectedMessage =
                "  Expected and actual are both <System.Int32[3][]>" + Environment.NewLine +
                "  Values differ at index [1]" + Environment.NewLine +
                "    Expected and actual are both <System.Int32[4]>" + Environment.NewLine +
                "    Values differ at index [2]" + Environment.NewLine +
                TextMessageWriter.Pfx_Expected + "6" + Environment.NewLine +
                TextMessageWriter.Pfx_Actual + "0" + Environment.NewLine;
            Expect(actual, EqualTo(expected));
        }

        [Test, ExpectedException(typeof(AssertionException))]
        public void JaggedArrayComparedToSimpleArray()
        {
            int[] expected = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int[][] actual = new int[][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 0, 7 }, new int[] { 8, 9 } };

            expectedMessage =
                "  Expected is <System.Int32[9]>, actual is <System.Int32[3][]>" + Environment.NewLine +
                "  Values differ at index [0]" + Environment.NewLine +
                TextMessageWriter.Pfx_Expected + "1" + Environment.NewLine +
                TextMessageWriter.Pfx_Actual + "< 1, 2, 3 >" + Environment.NewLine;
            Expect(actual, EqualTo(expected));
        }

        [Test, ExpectedException(typeof(AssertionException))]
        public void ArraysWithDifferentRanksAsCollection()
        {
            int[] expected = new int[] { 1, 2, 3, 4 };
            int[,] actual = new int[,] { { 1, 0 }, { 3, 4 } };

            expectedMessage =
                "  Expected is <System.Int32[4]>, actual is <System.Int32[2,2]>" + Environment.NewLine +
                "  Values differ at expected index [1], actual index [0,1]" + Environment.NewLine +
                TextMessageWriter.Pfx_Expected + "2" + Environment.NewLine +
                TextMessageWriter.Pfx_Actual + "0" + Environment.NewLine;
            Expect(actual, EqualTo(expected).AsCollection);
        }

        [Test, ExpectedException(typeof(AssertionException))]
        public void ArraysWithDifferentDimensionsAsCollection()
        {
            int[,] expected = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
            int[,] actual = new int[,] { { 1, 2 }, { 3, 0 }, { 5, 6 } };

            expectedMessage =
                "  Expected is <System.Int32[2,3]>, actual is <System.Int32[3,2]>" + Environment.NewLine +
                "  Values differ at expected index [1,0], actual index [1,1]" + Environment.NewLine +
                TextMessageWriter.Pfx_Expected + "4" + Environment.NewLine +
                TextMessageWriter.Pfx_Actual + "0" + Environment.NewLine;
            Expect(actual, EqualTo(expected).AsCollection);
        }

        //		[Test,ExpectedException(typeof(AssertionException))]
        //		public void ExpectedArrayIsLonger()
        //		{
        //			string[] array1 = { "one", "two", "three" };
        //			string[] array2 = { "one", "two", "three", "four", "five" };
        //
        //			expectedMessage =
        //				"  Expected is <System.String[5]>, actual is <System.String[3]>" + Environment.NewLine +
        //				"  Values differ at index [3]" + Environment.NewLine +
        //				"  Missing:  < \"four\", \"five\" >";
        //			Expect(array1, EqualTo(array2));
        //		}

        [Test, ExpectedException(typeof(AssertionException))]
        public void SameLengthDifferentContent()
        {
            string[] array1 = { "one", "two", "three" };
            string[] array2 = { "one", "two", "ten" };

            expectedMessage =
                "  Expected and actual are both <System.String[3]>" + Environment.NewLine +
                "  Values differ at index [2]" + Environment.NewLine +
                "  Expected string length 3 but was 5. Strings differ at index 1." + Environment.NewLine +
                "  Expected: \"ten\"" + Environment.NewLine +
                "  But was:  \"three\"" + Environment.NewLine +
                "  ------------^" + Environment.NewLine;
            Expect(array1, EqualTo(array2));
        }

        [Test, ExpectedException(typeof(AssertionException))]
        public void ArraysDeclaredAsDifferentTypes()
        {
            string[] array1 = { "one", "two", "three" };
            object[] array2 = { "one", "three", "two" };

            expectedMessage =
                "  Expected is <System.Object[3]>, actual is <System.String[3]>" + Environment.NewLine +
                "  Values differ at index [1]" + Environment.NewLine +
                "  Expected string length 5 but was 3. Strings differ at index 1." + Environment.NewLine +
                "  Expected: \"three\"" + Environment.NewLine +
                "  But was:  \"two\"" + Environment.NewLine +
                "  ------------^" + Environment.NewLine;
            Expect(array1, EqualTo(array2));
        }

        [Test, ExpectedException(typeof(AssertionException))]
        public void ArrayAndCollection_Failure()
        {
            int[] a = new int[] { 1, 2, 3 };
            ArrayList b = new ArrayList();
            b.Add(1);
            b.Add(3);
            Assert.AreEqual(a, b);
        }

        [Test, ExpectedException(typeof(AssertionException))]
        public void DifferentArrayTypesEqualFails()
        {
            string[] array1 = { "one", "two", "three" };
            object[] array2 = { "one", "three", "two" };

            expectedMessage =
                "  Expected is <System.String[3]>, actual is <System.Object[3]>" + System.Environment.NewLine +
                "  Values differ at index [1]" + System.Environment.NewLine +
                "  Expected string length 3 but was 5. Strings differ at index 1." + System.Environment.NewLine +
                "  Expected: \"two\"" + System.Environment.NewLine +
                "  But was:  \"three\"" + System.Environment.NewLine +
                "  ------------^" + System.Environment.NewLine;
            Assert.AreEqual(array1, array2);
        }
    }
}