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._ 19 20class ShouldSameInstanceAsSpec extends Spec with ShouldMatchers { 21 22 describe("The be a ('symbol) syntax") { 23 24 val string = "Hi" 25 val obj: AnyRef = string 26 val otherString = new String("Hi") 27 28 it("should do nothing if the object is the same instance as another object") { 29 string should be theSameInstanceAs (string) 30 obj should be theSameInstanceAs (string) 31 string should be theSameInstanceAs (obj) 32 } 33 34 it("should do nothing if the object is not the same instance as another object, when used with not") { 35 otherString should not { be theSameInstanceAs (string) } 36 otherString should not be theSameInstanceAs (string) 37 } 38 39 it("should do nothing if the object is the same instnace as another object, when used in a logical-and expression") { 40 obj should ((be theSameInstanceAs (string)) and (be theSameInstanceAs (string))) 41 obj should (be theSameInstanceAs (string) and (be theSameInstanceAs (string))) 42 obj should (be theSameInstanceAs (string) and be theSameInstanceAs (string)) 43 } 44 45 it("should do nothing if the object is the same instance as another object, when used in a logical-or expression") { 46 47 obj should ((be theSameInstanceAs (otherString)) or (be theSameInstanceAs (string))) 48 obj should (be theSameInstanceAs (otherString) or (be theSameInstanceAs (string))) 49 obj should (be theSameInstanceAs (otherString) or be theSameInstanceAs (string)) 50 51 obj should ((be theSameInstanceAs (string)) or (be theSameInstanceAs (otherString))) 52 obj should (be theSameInstanceAs (string) or (be theSameInstanceAs (otherString))) 53 obj should (be theSameInstanceAs (string) or be theSameInstanceAs (otherString)) 54 } 55 56 it("should do nothing if the object is not the same instance as another object, when used in a logical-and expression with not") { 57 58 obj should (not (be theSameInstanceAs (otherString)) and not (be theSameInstanceAs (otherString))) 59 obj should ((not be theSameInstanceAs (otherString)) and (not be theSameInstanceAs (otherString))) 60 obj should (not be theSameInstanceAs (otherString) and not be theSameInstanceAs (otherString)) 61 } 62 63 it("should do nothing if the object is not the same instance as another object, when used in a logical-or expression with not") { 64 65 obj should (not (be theSameInstanceAs (string)) or not (be theSameInstanceAs (otherString))) 66 obj should ((not be theSameInstanceAs (string)) or (not be theSameInstanceAs (otherString))) 67 obj should (not be theSameInstanceAs (string) or not be theSameInstanceAs (otherString)) 68 69 obj should (not (be theSameInstanceAs (otherString)) or not (be theSameInstanceAs (string))) 70 obj should ((not be theSameInstanceAs (otherString)) or (not be theSameInstanceAs (string))) 71 obj should (not be theSameInstanceAs (otherString) or not be theSameInstanceAs (string)) 72 } 73 74 it("should throw TestFailedException if the object is not the same instance as another object") { 75 val caught1 = intercept[TestFailedException] { 76 otherString should be theSameInstanceAs (string) 77 } 78 assert(caught1.getMessage === "\"Hi\" was not the same instance as \"Hi\"") 79 } 80 81 it("should throw TestFailedException if the object is the same instance as another object, when used with not") { 82 val caught1 = intercept[TestFailedException] { 83 obj should not { be theSameInstanceAs (string) } 84 } 85 assert(caught1.getMessage === "\"Hi\" was the same instance as \"Hi\"") 86 val caught2 = intercept[TestFailedException] { 87 obj should not be theSameInstanceAs (string) 88 } 89 assert(caught2.getMessage === "\"Hi\" was the same instance as \"Hi\"") 90 } 91 92 it("should throw TestFailedException if the object is not the same instance as another object, when used in a logical-and expression") { 93 val caught1 = intercept[TestFailedException] { 94 obj should ((be theSameInstanceAs (string)) and (be theSameInstanceAs (otherString))) 95 } 96 assert(caught1.getMessage === "\"Hi\" was the same instance as \"Hi\", but \"Hi\" was not the same instance as \"Hi\"") 97 val caught2 = intercept[TestFailedException] { 98 obj should (be theSameInstanceAs (string) and (be theSameInstanceAs (otherString))) 99 } 100 assert(caught2.getMessage === "\"Hi\" was the same instance as \"Hi\", but \"Hi\" was not the same instance as \"Hi\"") 101 val caught3 = intercept[TestFailedException] { 102 obj should (be theSameInstanceAs (string) and be theSameInstanceAs (otherString)) 103 } 104 assert(caught3.getMessage === "\"Hi\" was the same instance as \"Hi\", but \"Hi\" was not the same instance as \"Hi\"") 105 } 106 107 it("should throw TestFailedException if the object is not the same instance as another object, when used in a logical-or expression") { 108 109 val caught1 = intercept[TestFailedException] { 110 obj should ((be theSameInstanceAs (otherString)) or (be theSameInstanceAs (otherString))) 111 } 112 assert(caught1.getMessage === "\"Hi\" was not the same instance as \"Hi\", and \"Hi\" was not the same instance as \"Hi\"") 113 val caught2 = intercept[TestFailedException] { 114 obj should (be theSameInstanceAs (otherString) or (be theSameInstanceAs (otherString))) 115 } 116 assert(caught2.getMessage === "\"Hi\" was not the same instance as \"Hi\", and \"Hi\" was not the same instance as \"Hi\"") 117 val caught3 = intercept[TestFailedException] { 118 obj should (be theSameInstanceAs (otherString) or be theSameInstanceAs (otherString)) 119 } 120 assert(caught3.getMessage === "\"Hi\" was not the same instance as \"Hi\", and \"Hi\" was not the same instance as \"Hi\"") 121 } 122 123 it("should throw TestFailedException if the object is the same instance as another object, when used in a logical-and expression with not") { 124 125 val caught1 = intercept[TestFailedException] { 126 obj should (not (be theSameInstanceAs (otherString)) and not (be theSameInstanceAs (string))) 127 } 128 assert(caught1.getMessage === "\"Hi\" was not the same instance as \"Hi\", but \"Hi\" was the same instance as \"Hi\"") 129 val caught2 = intercept[TestFailedException] { 130 obj should ((not be theSameInstanceAs (otherString)) and (not be theSameInstanceAs (string))) 131 } 132 assert(caught2.getMessage === "\"Hi\" was not the same instance as \"Hi\", but \"Hi\" was the same instance as \"Hi\"") 133 val caught3 = intercept[TestFailedException] { 134 obj should (not be theSameInstanceAs (otherString) and not be theSameInstanceAs (string)) 135 } 136 assert(caught3.getMessage === "\"Hi\" was not the same instance as \"Hi\", but \"Hi\" was the same instance as \"Hi\"") 137 // Check that the error message "short circuits" 138 val caught7 = intercept[TestFailedException] { 139 obj should (not (be theSameInstanceAs (string)) and not (be theSameInstanceAs (otherString))) 140 } 141 assert(caught7.getMessage === "\"Hi\" was the same instance as \"Hi\"") 142 } 143 144 it("should throw TestFailedException if the object has an appropriately named method, which returns true, when used in a logical-or expression with not") { 145 146 val caught1 = intercept[TestFailedException] { 147 obj should (not (be theSameInstanceAs (string)) or not (be theSameInstanceAs (string))) 148 } 149 assert(caught1.getMessage === "\"Hi\" was the same instance as \"Hi\", and \"Hi\" was the same instance as \"Hi\"") 150 val caught2 = intercept[TestFailedException] { 151 obj should ((not be theSameInstanceAs (string)) or (not be theSameInstanceAs (string))) 152 } 153 assert(caught2.getMessage === "\"Hi\" was the same instance as \"Hi\", and \"Hi\" was the same instance as \"Hi\"") 154 val caught3 = intercept[TestFailedException] { 155 obj should (not be theSameInstanceAs (string) or not be theSameInstanceAs (string)) 156 } 157 assert(caught3.getMessage === "\"Hi\" was the same instance as \"Hi\", and \"Hi\" was the same instance as \"Hi\"") 158 } 159 } 160} 161