1/* 2 * Copyright 2001-2008 Artima, Inc. 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 */ 16package org.scalatest.matchers 17 18import org.scalatest._ 19import org.scalatest.prop.Checkers 20import org.scalacheck._ 21import Arbitrary._ 22import Prop._ 23 24class ShouldBeAnySpec extends Spec with ShouldMatchers with Checkers with ReturnsNormallyThrowsAssertion { 25 26 // Checking for equality with "be" 27 describe("The be token") { 28 29 it("should compare arrays structurally") { 30 Array(1, 2) should be (Array(1, 2)) 31 } 32 33 it("should do nothing when equal") { 34 1 should be (1) 35 36 // objects should equal themselves 37 check((s: String) => returnsNormally(s should be (s))) 38 check((i: Int) => returnsNormally(i should be (i))) 39 40 // a string should equal another string with the same value 41 check((s: String) => returnsNormally(s should be (new String(s)))) 42 } 43 44 it("should do nothing when not equal and used with not") { 45 1 should not { be (2) } 46 1 should not be (2) 47 48 // unequal objects should not equal each other 49 check((s: String, t: String) => s != t ==> returnsNormally(s should not { be (t) })) 50 check((s: String, t: String) => s != t ==> returnsNormally(s should not be (t))) 51 } 52 53 it("should do nothing when equal and used in a logical-and expression") { 54 1 should (be (1) and be (2 - 1)) 55 } 56 57 it("should do nothing when equal and used in multi-part logical expressions") { 58 59 // Just to make sure these work strung together 60 1 should (be (1) and be (1) and be (1) and be (1)) 61 1 should (be (1) and be (1) or be (1) and be (1) or be (1)) 62 1 should ( 63 be (1) and 64 be (1) or 65 be (1) and 66 be (1) or 67 be (1) 68 ) 69 } 70 71 it("should do nothing when equal and used in a logical-or expression") { 72 1 should { be (1) or be (2 - 1) } 73 } 74 75 it("should do nothing when not equal and used in a logical-and expression with not") { 76 1 should { not { be (2) } and not { be (3 - 1) }} 77 1 should { not be (2) and (not be (3 - 1)) } 78 1 should (not be (2) and not be (3 - 1)) 79 } 80 81 it("should do nothing when not equal and used in a logical-or expression with not") { 82 1 should { not { be (2) } or not { be (3 - 1) }} 83 1 should { not be (2) or (not be (3 - 1)) } 84 1 should (not be (2) or not be (3 - 1)) 85 } 86 87 it("should throw an assertion error when not equal") { 88 val caught1 = intercept[TestFailedException] { 89 1 should be (2) 90 } 91 assert(caught1.getMessage === "1 was not equal to 2") 92 93 // unequal objects used with "a should equal (b)" should throw an TestFailedException 94 check((s: String, t: String) => s != t ==> throwsTestFailedException(s should be (t))) 95 96 val caught2 = intercept[TestFailedException] { 97 1 should not (not be (2)) 98 } 99 assert(caught2.getMessage === "1 was not equal to 2") 100 101 val s: String = null 102 val caught3 = intercept[TestFailedException] { 103 s should be ("hi") 104 } 105 assert(caught3.getMessage === "\"hi\" was not null") 106 } 107 108 it("should throw an assertion error when equal but used with should not") { 109 val caught1 = intercept[TestFailedException] { 110 1 should not { be (1) } 111 } 112 assert(caught1.getMessage === "1 was equal to 1") 113 114 val caught2 = intercept[TestFailedException] { 115 1 should not be (1) 116 } 117 assert(caught2.getMessage === "1 was equal to 1") 118 119 // the same object used with "a should not { equal (a) } should throw TestFailedException 120 check((s: String) => throwsTestFailedException(s should not { be (s) })) 121 check((i: Int) => throwsTestFailedException(i should not { be (i) })) 122 check((s: String) => throwsTestFailedException(s should not be (s))) 123 check((i: Int) => throwsTestFailedException(i should not be (i))) 124 125 // two different strings with the same value used with "s should not { be (t) } should throw TestFailedException 126 check((s: String) => throwsTestFailedException(s should not { be (new String(s)) })) 127 check((s: String) => throwsTestFailedException(s should not be (new String(s)))) 128 129 val caught3 = intercept[TestFailedException] { 130 1 should not (not (not be (1))) 131 } 132 assert(caught3.getMessage === "1 was equal to 1") 133 } 134 135 it("should throw an assertion error when not equal and used in a logical-and expression") { 136 val caught = intercept[TestFailedException] { 137 1 should { be (5) and be (2 - 1) } 138 } 139 assert(caught.getMessage === "1 was not equal to 5") 140 } 141 142 it("should throw an assertion error when not equal and used in a logical-or expression") { 143 val caught = intercept[TestFailedException] { 144 1 should { be (5) or be (5 - 1) } 145 } 146 assert(caught.getMessage === "1 was not equal to 5, and 1 was not equal to 4") 147 } 148 149 it("should throw an assertion error when equal and used in a logical-and expression with not") { 150 151 val caught1 = intercept[TestFailedException] { 152 1 should { not { be (1) } and not { be (3 - 1) }} 153 } 154 assert(caught1.getMessage === "1 was equal to 1") 155 156 val caught2 = intercept[TestFailedException] { 157 1 should { not be (1) and (not be (3 - 1)) } 158 } 159 assert(caught2.getMessage === "1 was equal to 1") 160 161 val caught3 = intercept[TestFailedException] { 162 1 should (not be (1) and not be (3 - 1)) 163 } 164 assert(caught3.getMessage === "1 was equal to 1") 165 166 val caught4 = intercept[TestFailedException] { 167 1 should { not { be (2) } and not { be (1) }} 168 } 169 assert(caught4.getMessage === "1 was not equal to 2, but 1 was equal to 1") 170 171 val caught5 = intercept[TestFailedException] { 172 1 should { not be (2) and (not be (1)) } 173 } 174 assert(caught5.getMessage === "1 was not equal to 2, but 1 was equal to 1") 175 176 val caught6 = intercept[TestFailedException] { 177 1 should (not be (2) and not be (1)) 178 } 179 assert(caught6.getMessage === "1 was not equal to 2, but 1 was equal to 1") 180 } 181 182 it("should throw an assertion error when equal and used in a logical-or expression with not") { 183 184 val caught1 = intercept[TestFailedException] { 185 1 should { not { be (1) } or not { be (2 - 1) }} 186 } 187 assert(caught1.getMessage === "1 was equal to 1, and 1 was equal to 1") 188 189 val caught2 = intercept[TestFailedException] { 190 1 should { not be (1) or { not be (2 - 1) }} 191 } 192 assert(caught2.getMessage === "1 was equal to 1, and 1 was equal to 1") 193 194 val caught3 = intercept[TestFailedException] { 195 1 should (not be (1) or not be (2 - 1)) 196 } 197 assert(caught3.getMessage === "1 was equal to 1, and 1 was equal to 1") 198 } 199 } 200} 201