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 apple 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._ 19 20class ShouldBeAnSymbolSpec extends Spec with ShouldMatchers with FruitMocks { 21 22 describe("The be an ('symbol) syntax") { 23 24 it("should do nothing if the object has an appropriately named method, which returns true") { 25 appleMock should be an ('apple) 26 isAppleMock should be an ('apple) 27 } 28 29 it("should throw TestFailedException if no <symbol> or is<Symbol> method exists") { 30 val ex1 = intercept[TestFailedException] { 31 noPredicateMock should be an ('apple) 32 } 33 ex1.getMessage should equal ("NoPredicateMock has neither an apple nor an isApple method") 34 // Check message for name that starts with a consonant (should use a instead of an) 35 val ex2 = intercept[TestFailedException] { 36 noPredicateMock should be an ('crabApple) 37 } 38 ex2.getMessage should equal ("NoPredicateMock has neither a crabApple nor an isCrabApple method") 39 } 40 41 it("should do nothing if the object has an appropriately named method, which returns false when used with not") { 42 notAppleMock should not { be an ('apple) } 43 notAppleMock should not be an ('apple) 44 isNotAppleMock should not { be an ('apple) } 45 isNotAppleMock should not be an ('apple) 46 } 47 48 it("should throw TestFailedException if no <symbol> or is<Symbol> method exists, when used with not") { 49 val ex1 = intercept[TestFailedException] { 50 noPredicateMock should not { be an ('apple) } 51 } 52 ex1.getMessage should equal ("NoPredicateMock has neither an apple nor an isApple method") 53 val ex2 = intercept[TestFailedException] { 54 noPredicateMock should not (be an ('orange)) 55 } 56 ex2.getMessage should equal ("NoPredicateMock has neither an orange nor an isOrange method") 57 val ex3 = intercept[TestFailedException] { 58 noPredicateMock should not be an ('apple) 59 } 60 ex3.getMessage should equal ("NoPredicateMock has neither an apple nor an isApple method") 61 val ex4 = intercept[TestFailedException] { 62 noPredicateMock should not be an ('orange) 63 } 64 ex4.getMessage should equal ("NoPredicateMock has neither an orange nor an isOrange method") 65 } 66 67 it("should do nothing if the object has an appropriately named method, which returns true, when used in a logical-and expression") { 68 appleMock should ((be an ('apple)) and (be an ('apple))) 69 appleMock should (be an ('apple) and (be an ('apple))) 70 appleMock should (be an ('apple) and be an ('apple)) 71 isAppleMock should ((be an ('apple)) and (be an ('apple))) 72 isAppleMock should (be an ('apple) and (be an ('apple))) 73 isAppleMock should (be an ('apple) and be an ('apple)) 74 } 75 76 it("should do nothing if the object has an appropriately named method, which returns true, when used in a logical-or expression") { 77 78 appleMock should ((be an ('orange)) or (be an ('apple))) 79 appleMock should (be an ('orange) or (be an ('apple))) 80 appleMock should (be an ('orange) or be an ('apple)) 81 isAppleMock should ((be an ('orange)) or (be an ('apple))) 82 isAppleMock should (be an ('orange) or (be an ('apple))) 83 isAppleMock should (be an ('orange) or be an ('apple)) 84 85 appleMock should ((be an ('apple)) or (be an ('orange))) 86 appleMock should (be an ('apple) or (be an ('orange))) 87 appleMock should (be an ('apple) or be an ('orange)) 88 isAppleMock should ((be an ('apple)) or (be an ('orange))) 89 isAppleMock should (be an ('apple) or (be an ('orange))) 90 isAppleMock should (be an ('apple) or be an ('orange)) 91 } 92 93 it("should do nothing if the object has an appropriately named method, which returns false, when used in a logical-and expression with not") { 94 95 notAppleMock should (not (be an ('apple)) and not (be an ('apple))) 96 notAppleMock should ((not be an ('apple)) and (not be an ('apple))) 97 notAppleMock should (not be an ('apple) and not be an ('apple)) 98 99 isNotAppleMock should (not (be an ('apple)) and not (be an ('apple))) 100 isNotAppleMock should ((not be an ('apple)) and (not be an ('apple))) 101 isNotAppleMock should (not be an ('apple) and not be an ('apple)) 102 } 103 104 it("should do nothing if the object has an appropriately named method, which returns false, when used in a logical-or expression with not") { 105 106 notAppleMock should (not (be an ('apple)) or not (be an ('apple))) 107 notAppleMock should ((not be an ('apple)) or (not be an ('apple))) 108 notAppleMock should (not be an ('apple) or not be an ('apple)) 109 110 isNotAppleMock should (not (be an ('apple)) or not (be an ('apple))) 111 isNotAppleMock should ((not be an ('apple)) or (not be an ('apple))) 112 isNotAppleMock should (not be an ('apple) or not be an ('apple)) 113 114 notAppleMock should (not (be an ('orange)) or not (be an ('apple))) 115 notAppleMock should ((not be an ('orange)) or (not be an ('apple))) 116 notAppleMock should (not be an ('orange) or not be an ('apple)) 117 118 isNotAppleMock should (not (be an ('orange)) or not (be an ('apple))) 119 isNotAppleMock should ((not be an ('orange)) or (not be an ('apple))) 120 isNotAppleMock should (not be an ('orange) or not be an ('apple)) 121 } 122 123 it("should throw TestFailedException if the object has an appropriately named method, which returns false") { 124 val caught1 = intercept[TestFailedException] { 125 notAppleMock should be an ('apple) 126 } 127 assert(caught1.getMessage === "NotAppleMock was not an apple") 128 val caught2 = intercept[TestFailedException] { 129 isNotAppleMock should be an ('apple) 130 } 131 assert(caught2.getMessage === "IsNotAppleMock was not an apple") 132 } 133 134 it("should throw TestFailedException if the object has an appropriately named method, which returns true when used with not") { 135 val caught1 = intercept[TestFailedException] { 136 appleMock should not { be an ('apple) } 137 } 138 assert(caught1.getMessage === "AppleMock was an apple") 139 val caught2 = intercept[TestFailedException] { 140 appleMock should not be an ('apple) 141 } 142 assert(caught2.getMessage === "AppleMock was an apple") 143 val caught3 = intercept[TestFailedException] { 144 isAppleMock should not { be an ('apple) } 145 } 146 assert(caught3.getMessage === "IsAppleMock was an apple") 147 val caught4 = intercept[TestFailedException] { 148 isAppleMock should not be an ('apple) 149 } 150 assert(caught4.getMessage === "IsAppleMock was an apple") 151 } 152 153 it("should throw TestFailedException if the object has an appropriately named method, which returns false, when used in a logical-and expression") { 154 val caught1 = intercept[TestFailedException] { 155 appleMock should ((be an ('apple)) and (be an ('orange))) 156 } 157 assert(caught1.getMessage === "AppleMock was an apple, but AppleMock was not an orange") 158 val caught2 = intercept[TestFailedException] { 159 appleMock should (be an ('apple) and (be an ('orange))) 160 } 161 assert(caught2.getMessage === "AppleMock was an apple, but AppleMock was not an orange") 162 val caught3 = intercept[TestFailedException] { 163 appleMock should (be an ('apple) and be an ('orange)) 164 } 165 assert(caught3.getMessage === "AppleMock was an apple, but AppleMock was not an orange") 166 val caught4 = intercept[TestFailedException] { 167 isAppleMock should ((be an ('apple)) and (be an ('orange))) 168 } 169 assert(caught4.getMessage === "IsAppleMock was an apple, but IsAppleMock was not an orange") 170 val caught5 = intercept[TestFailedException] { 171 isAppleMock should (be an ('apple) and (be an ('orange))) 172 } 173 assert(caught5.getMessage === "IsAppleMock was an apple, but IsAppleMock was not an orange") 174 val caught6 = intercept[TestFailedException] { 175 isAppleMock should (be an ('apple) and be an ('orange)) 176 } 177 assert(caught6.getMessage === "IsAppleMock was an apple, but IsAppleMock was not an orange") 178 } 179 180 it("should throw TestFailedException if the object has an appropriately named method, which returns false, when used in a logical-or expression") { 181 182 val caught1 = intercept[TestFailedException] { 183 notAppleMock should ((be an ('apple)) or (be an ('apple))) 184 } 185 assert(caught1.getMessage === "NotAppleMock was not an apple, and NotAppleMock was not an apple") 186 val caught2 = intercept[TestFailedException] { 187 notAppleMock should (be an ('apple) or (be an ('apple))) 188 } 189 assert(caught2.getMessage === "NotAppleMock was not an apple, and NotAppleMock was not an apple") 190 val caught3 = intercept[TestFailedException] { 191 notAppleMock should (be an ('apple) or be an ('apple)) 192 } 193 assert(caught3.getMessage === "NotAppleMock was not an apple, and NotAppleMock was not an apple") 194 val caught4 = intercept[TestFailedException] { 195 isNotAppleMock should ((be an ('apple)) or (be an ('apple))) 196 } 197 assert(caught4.getMessage === "IsNotAppleMock was not an apple, and IsNotAppleMock was not an apple") 198 val caught5 = intercept[TestFailedException] { 199 isNotAppleMock should (be an ('apple) or (be an ('apple))) 200 } 201 assert(caught5.getMessage === "IsNotAppleMock was not an apple, and IsNotAppleMock was not an apple") 202 val caught6 = intercept[TestFailedException] { 203 isNotAppleMock should (be an ('apple) or be an ('apple)) 204 } 205 assert(caught6.getMessage === "IsNotAppleMock was not an apple, and IsNotAppleMock was not an apple") 206 } 207 208 it("should throw TestFailedException if the object has an appropriately named method, which returns true, when used in a logical-and expression with not") { 209 210 val caught1 = intercept[TestFailedException] { 211 appleMock should (not (be an ('orange)) and not (be an ('apple))) 212 } 213 assert(caught1.getMessage === "AppleMock was not an orange, but AppleMock was an apple") 214 val caught2 = intercept[TestFailedException] { 215 appleMock should ((not be an ('orange)) and (not be an ('apple))) 216 } 217 assert(caught2.getMessage === "AppleMock was not an orange, but AppleMock was an apple") 218 val caught3 = intercept[TestFailedException] { 219 appleMock should (not be an ('orange) and not be an ('apple)) 220 } 221 assert(caught3.getMessage === "AppleMock was not an orange, but AppleMock was an apple") 222 val caught4 = intercept[TestFailedException] { 223 isAppleMock should (not (be an ('orange)) and not (be an ('apple))) 224 } 225 assert(caught4.getMessage === "IsAppleMock was not an orange, but IsAppleMock was an apple") 226 val caught5 = intercept[TestFailedException] { 227 isAppleMock should ((not be an ('orange)) and (not be an ('apple))) 228 } 229 assert(caught5.getMessage === "IsAppleMock was not an orange, but IsAppleMock was an apple") 230 val caught6 = intercept[TestFailedException] { 231 isAppleMock should (not be an ('orange) and not be an ('apple)) 232 } 233 assert(caught6.getMessage === "IsAppleMock was not an orange, but IsAppleMock was an apple") 234 // Check that the error message "short circuits" 235 val caught7 = intercept[TestFailedException] { 236 appleMock should (not (be an ('apple)) and not (be an ('orange))) 237 } 238 assert(caught7.getMessage === "AppleMock was an apple") 239 } 240 241 it("should throw TestFailedException if the object has an appropriately named method, which returns true, when used in a logical-or expression with not") { 242 243 val caught1 = intercept[TestFailedException] { 244 appleMock should (not (be an ('apple)) or not (be an ('apple))) 245 } 246 assert(caught1.getMessage === "AppleMock was an apple, and AppleMock was an apple") 247 val caught2 = intercept[TestFailedException] { 248 appleMock should ((not be an ('apple)) or (not be an ('apple))) 249 } 250 assert(caught2.getMessage === "AppleMock was an apple, and AppleMock was an apple") 251 val caught3 = intercept[TestFailedException] { 252 appleMock should (not be an ('apple) or not be an ('apple)) 253 } 254 assert(caught3.getMessage === "AppleMock was an apple, and AppleMock was an apple") 255 val caught4 = intercept[TestFailedException] { 256 isAppleMock should (not (be an ('apple)) or not (be an ('apple))) 257 } 258 assert(caught4.getMessage === "IsAppleMock was an apple, and IsAppleMock was an apple") 259 val caught5 = intercept[TestFailedException] { 260 isAppleMock should ((not be an ('apple)) or (not be an ('apple))) 261 } 262 assert(caught5.getMessage === "IsAppleMock was an apple, and IsAppleMock was an apple") 263 val caught6 = intercept[TestFailedException] { 264 isAppleMock should (not be an ('apple) or not be an ('apple)) 265 } 266 assert(caught6.getMessage === "IsAppleMock was an apple, and IsAppleMock was an apple") 267 } 268 } 269} 270