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
|
|
40 if (containedObjs.Length != array.Length)
|
|
41 {
|
|
42 isMatch = false;
|
|
43 }
|
|
44 else
|
|
45 {
|
|
46 for (int i = 0; i < containedObjs.Length; i++)
|
|
47 {
|
|
48 if (!EqualityChecker.AreEqual(containedObjs[i], array[i]))
|
|
49 {
|
|
50 isMatch = false;
|
|
51 idx = i;
|
|
52 break;
|
|
53 }
|
|
54 }
|
|
55 }
|
|
56
|
|
57 return isMatch;
|
|
58 }
|
|
59
|
|
60 public override void WriteDescriptionTo(MessageWriter writer)
|
|
61 {
|
|
62 if (containedObjs.Length == 0)
|
|
63 {
|
|
64 writer.Write("Empty array");
|
|
65 }
|
|
66 else if (idx == NOT_FOUND)
|
|
67 {
|
|
68 writer.WriteExpectedValue(containedObjs[0]);
|
|
69 }
|
|
70 else
|
|
71 {
|
|
72 writer.WriteExpectedValue(containedObjs[idx]);
|
|
73 }
|
|
74 }
|
|
75
|
|
76 public override void WriteActualValueTo(MessageWriter writer)
|
|
77 {
|
|
78 object[] actualArray = ((object[])actual);
|
|
79
|
|
80 if (actualArray.Length == 0)
|
|
81 {
|
|
82 writer.Write("Empty array");
|
|
83 }
|
|
84 else if (idx == NOT_FOUND)
|
|
85 {
|
|
86 writer.WriteActualValue(actualArray[0]);
|
|
87 }
|
|
88 else
|
|
89 {
|
|
90 writer.WriteActualValue(actualArray[idx]);
|
|
91 }
|
|
92 }
|
|
93
|
|
94 public override void WriteMessageTo(MessageWriter writer)
|
|
95 {
|
|
96 base.WriteMessageTo(writer);
|
|
97
|
|
98 if (idx != NOT_FOUND)
|
|
99 {
|
|
100 writer.WriteMessageLine("Incorrect value at {0}", idx);
|
|
101 }
|
|
102 }
|
|
103 }
|
|
104 }
|
|
105
|