Mercurial > repos > IBBoard.NUnit
comparison Constraints/ArrayContainsConstraint.cs @ 2:405baf327de2
Re #46: Add NUnit helper methods
* Improve additional information message
* Improve handling of mis-matched length arrays
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 21 Aug 2010 14:53:48 +0000 |
parents | 4dad6872ca5a |
children |
comparison
equal
deleted
inserted
replaced
1:4dad6872ca5a | 2:405baf327de2 |
---|---|
35 private bool DoMatch(object[] array) | 35 private bool DoMatch(object[] array) |
36 { | 36 { |
37 bool isMatch = true; | 37 bool isMatch = true; |
38 idx = NOT_FOUND; | 38 idx = NOT_FOUND; |
39 | 39 |
40 if (containedObjs.Length != array.Length) | 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) | |
41 { | 53 { |
42 isMatch = false; | 54 isMatch = false; |
43 } | 55 idx = max; |
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 } |
56 | 57 |
57 return isMatch; | 58 return isMatch; |
58 } | 59 } |
59 | 60 |
64 writer.Write("Empty array"); | 65 writer.Write("Empty array"); |
65 } | 66 } |
66 else if (idx == NOT_FOUND) | 67 else if (idx == NOT_FOUND) |
67 { | 68 { |
68 writer.WriteExpectedValue(containedObjs[0]); | 69 writer.WriteExpectedValue(containedObjs[0]); |
70 } | |
71 else if (idx >= containedObjs.Length) | |
72 { | |
73 writer.Write("End of array"); | |
69 } | 74 } |
70 else | 75 else |
71 { | 76 { |
72 writer.WriteExpectedValue(containedObjs[idx]); | 77 writer.WriteExpectedValue(containedObjs[idx]); |
73 } | 78 } |
83 } | 88 } |
84 else if (idx == NOT_FOUND) | 89 else if (idx == NOT_FOUND) |
85 { | 90 { |
86 writer.WriteActualValue(actualArray[0]); | 91 writer.WriteActualValue(actualArray[0]); |
87 } | 92 } |
93 else if (idx >= actualArray.Length) | |
94 { | |
95 writer.Write("End of array"); | |
96 } | |
88 else | 97 else |
89 { | 98 { |
90 writer.WriteActualValue(actualArray[idx]); | 99 writer.WriteActualValue(actualArray[idx]); |
91 } | 100 } |
92 } | 101 } |
93 | 102 |
94 public override void WriteMessageTo(MessageWriter writer) | 103 public override void WriteMessageTo(MessageWriter writer) |
95 { | 104 { |
96 base.WriteMessageTo(writer); | 105 base.WriteMessageTo(writer); |
97 | 106 |
98 if (idx != NOT_FOUND) | 107 if (idx >= containedObjs.Length) |
99 { | 108 { |
100 writer.WriteMessageLine("Incorrect value at {0}", idx); | 109 writer.WriteMessageLine("Unexpected value at index {0}", idx); |
110 } | |
111 else if (idx >= ((object[])actual).Length) | |
112 { | |
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); | |
101 } | 118 } |
102 } | 119 } |
103 } | 120 } |
104 } | 121 } |
105 | 122 |