1
|
1 // This file (Contain.cs) is a part of the IBBoard.NUnit project and is copyright 2010 IBBoard
|
|
2 //
|
|
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
|
|
4 using System;
|
|
5 using NUnit.Framework.Constraints;
|
|
6 using NUnit.Framework;
|
|
7 namespace IBBoard.NUnit.Constraints
|
|
8 {
|
|
9 /// <summary>
|
|
10 /// Requires a list of objects to appear in a given order from the start of an array
|
|
11 /// </summary>
|
|
12 public class ArrayContainsConstraint<T> : Constraint
|
|
13 {
|
|
14 private static readonly int NOT_FOUND = -1;
|
|
15 private T[] containedObjs;
|
|
16 private int idx = NOT_FOUND;
|
|
17
|
|
18 public ArrayContainsConstraint(params T[] containedObjects)
|
|
19 {
|
|
20 containedObjs = containedObjects;
|
|
21 }
|
|
22
|
|
23 public override bool Matches(object actual)
|
|
24 {
|
|
25 this.actual = actual;
|
|
26
|
|
27 if (!(actual is Array))
|
|
28 {
|
|
29 throw new ArgumentException("The actual value must be an array");
|
|
30 }
|
|
31
|
|
32 return DoMatch((object[])this.actual);
|
|
33 }
|
|
34
|
|
35 private bool DoMatch(object[] array)
|
|
36 {
|
|
37 bool isMatch = true;
|
|
38 idx = NOT_FOUND;
|
|
39
|
2
|
40 int max = Math.Min(array.Length, containedObjs.Length);
|
|
41
|
|
42 for (int i = 0; i < max; i++)
|
|
43 {
|
|
44 if (!EqualityChecker.AreEqual(containedObjs[i], array[i]))
|
|
45 {
|
|
46 isMatch = false;
|
|
47 idx = i;
|
|
48 break;
|
|
49 }
|
|
50 }
|
|
51
|
|
52 if (isMatch && containedObjs.Length != array.Length)
|
1
|
53 {
|
|
54 isMatch = false;
|
2
|
55 idx = max;
|
1
|
56 }
|
|
57
|
|
58 return isMatch;
|
|
59 }
|
|
60
|
|
61 public override void WriteDescriptionTo(MessageWriter writer)
|
|
62 {
|
|
63 if (containedObjs.Length == 0)
|
|
64 {
|
|
65 writer.Write("Empty array");
|
|
66 }
|
|
67 else if (idx == NOT_FOUND)
|
|
68 {
|
|
69 writer.WriteExpectedValue(containedObjs[0]);
|
|
70 }
|
2
|
71 else if (idx >= containedObjs.Length)
|
|
72 {
|
|
73 writer.Write("End of array");
|
|
74 }
|
1
|
75 else
|
|
76 {
|
|
77 writer.WriteExpectedValue(containedObjs[idx]);
|
|
78 }
|
|
79 }
|
|
80
|
|
81 public override void WriteActualValueTo(MessageWriter writer)
|
|
82 {
|
|
83 object[] actualArray = ((object[])actual);
|
|
84
|
|
85 if (actualArray.Length == 0)
|
|
86 {
|
|
87 writer.Write("Empty array");
|
|
88 }
|
|
89 else if (idx == NOT_FOUND)
|
|
90 {
|
|
91 writer.WriteActualValue(actualArray[0]);
|
|
92 }
|
2
|
93 else if (idx >= actualArray.Length)
|
|
94 {
|
|
95 writer.Write("End of array");
|
|
96 }
|
1
|
97 else
|
|
98 {
|
|
99 writer.WriteActualValue(actualArray[idx]);
|
|
100 }
|
|
101 }
|
|
102
|
|
103 public override void WriteMessageTo(MessageWriter writer)
|
|
104 {
|
|
105 base.WriteMessageTo(writer);
|
|
106
|
2
|
107 if (idx >= containedObjs.Length)
|
|
108 {
|
|
109 writer.WriteMessageLine("Unexpected value at index {0}", idx);
|
|
110 }
|
|
111 else if (idx >= ((object[])actual).Length)
|
1
|
112 {
|
2
|
113 writer.WriteMessageLine("Expected value at index {0}", idx);
|
|
114 }
|
|
115 else if (idx != NOT_FOUND)
|
|
116 {
|
|
117 writer.WriteMessageLine("Incorrect value at index {0}", idx);
|
1
|
118 }
|
|
119 }
|
|
120 }
|
|
121 }
|
|
122
|