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 ShouldBeASymbolSpec extends Spec with ShouldMatchers with FileMocks {
21
22  describe("The be a ('symbol) syntax") {
23
24    it("should do nothing if the object has an appropriately named method, which returns true") {
25      fileMock should be a ('file)
26      isFileMock should be a ('file)
27    }
28
29    it("should throw TestFailedException if no <symbol> or is<Symbol> method exists") {
30      val ex1 = intercept[TestFailedException] {
31        noPredicateMock should be a ('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 a ('file)
37      }
38      ex2.getMessage should equal ("NoPredicateMock has neither a file nor an isFile method")
39    }
40
41    it("should do nothing if the object has an appropriately named method, which returns false when used with not") {
42      notFileMock should not { be a ('file) }
43      notFileMock should not be a ('file)
44      isNotFileMock should not { be a ('file) }
45      isNotFileMock should not be a ('file)
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 a ('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 a ('directory))
55      }
56      ex2.getMessage should equal ("NoPredicateMock has neither a directory nor an isDirectory method")
57      val ex3 = intercept[TestFailedException] {
58        noPredicateMock should not be a ('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 a ('directory)
63      }
64      ex4.getMessage should equal ("NoPredicateMock has neither a directory nor an isDirectory 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      fileMock should ((be a ('file)) and (be a ('file)))
69      fileMock should (be a ('file) and (be a ('file)))
70      fileMock should (be a ('file) and be a ('file))
71      isFileMock should ((be a ('file)) and (be a ('file)))
72      isFileMock should (be a ('file) and (be a ('file)))
73      isFileMock should (be a ('file) and be a ('file))
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      fileMock should ((be a ('directory)) or (be a ('file)))
79      fileMock should (be a ('directory) or (be a ('file)))
80      fileMock should (be a ('directory) or be a ('file))
81      isFileMock should ((be a ('directory)) or (be a ('file)))
82      isFileMock should (be a ('directory) or (be a ('file)))
83      isFileMock should (be a ('directory) or be a ('file))
84
85      fileMock should ((be a ('file)) or (be a ('directory)))
86      fileMock should (be a ('file) or (be a ('directory)))
87      fileMock should (be a ('file) or be a ('directory))
88      isFileMock should ((be a ('file)) or (be a ('directory)))
89      isFileMock should (be a ('file) or (be a ('directory)))
90      isFileMock should (be a ('file) or be a ('directory))
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      notFileMock should (not (be a ('file)) and not (be a ('file)))
96      notFileMock should ((not be a ('file)) and (not be a ('file)))
97      notFileMock should (not be a ('file) and not be a ('file))
98
99      isNotFileMock should (not (be a ('file)) and not (be a ('file)))
100      isNotFileMock should ((not be a ('file)) and (not be a ('file)))
101      isNotFileMock should (not be a ('file) and not be a ('file))
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      notFileMock should (not (be a ('file)) or not (be a ('file)))
107      notFileMock should ((not be a ('file)) or (not be a ('file)))
108      notFileMock should (not be a ('file) or not be a ('file))
109
110      isNotFileMock should (not (be a ('file)) or not (be a ('file)))
111      isNotFileMock should ((not be a ('file)) or (not be a ('file)))
112      isNotFileMock should (not be a ('file) or not be a ('file))
113
114      notFileMock should (not (be a ('directory)) or not (be a ('file)))
115      notFileMock should ((not be a ('directory)) or (not be a ('file)))
116      notFileMock should (not be a ('directory) or not be a ('file))
117
118      isNotFileMock should (not (be a ('directory)) or not (be a ('file)))
119      isNotFileMock should ((not be a ('directory)) or (not be a ('file)))
120      isNotFileMock should (not be a ('directory) or not be a ('file))
121    }
122
123    it("should throw TestFailedException if the object has an appropriately named method, which returns false") {
124      val caught1 = intercept[TestFailedException] {
125        notFileMock should be a ('file)
126      }
127      assert(caught1.getMessage === "NotFileMock was not a file")
128      val caught2 = intercept[TestFailedException] {
129        isNotFileMock should be a ('file)
130      }
131      assert(caught2.getMessage === "IsNotFileMock was not a file")
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        fileMock should not { be a ('file) }
137      }
138      assert(caught1.getMessage === "FileMock was a file")
139      val caught2 = intercept[TestFailedException] {
140        fileMock should not be a ('file)
141      }
142      assert(caught2.getMessage === "FileMock was a file")
143      val caught3 = intercept[TestFailedException] {
144        isFileMock should not { be a ('file) }
145      }
146      assert(caught3.getMessage === "IsFileMock was a file")
147      val caught4 = intercept[TestFailedException] {
148        isFileMock should not be a ('file)
149      }
150      assert(caught4.getMessage === "IsFileMock was a file")
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        fileMock should ((be a ('file)) and (be a ('directory)))
156      }
157      assert(caught1.getMessage === "FileMock was a file, but FileMock was not a directory")
158      val caught2 = intercept[TestFailedException] {
159        fileMock should (be a ('file) and (be a ('directory)))
160      }
161      assert(caught2.getMessage === "FileMock was a file, but FileMock was not a directory")
162      val caught3 = intercept[TestFailedException] {
163        fileMock should (be a ('file) and be a ('directory))
164      }
165      assert(caught3.getMessage === "FileMock was a file, but FileMock was not a directory")
166      val caught4 = intercept[TestFailedException] {
167        isFileMock should ((be a ('file)) and (be a ('directory)))
168      }
169      assert(caught4.getMessage === "IsFileMock was a file, but IsFileMock was not a directory")
170      val caught5 = intercept[TestFailedException] {
171        isFileMock should (be a ('file) and (be a ('directory)))
172      }
173      assert(caught5.getMessage === "IsFileMock was a file, but IsFileMock was not a directory")
174      val caught6 = intercept[TestFailedException] {
175        isFileMock should (be a ('file) and be a ('directory))
176      }
177      assert(caught6.getMessage === "IsFileMock was a file, but IsFileMock was not a directory")
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        notFileMock should ((be a ('file)) or (be a ('file)))
184      }
185      assert(caught1.getMessage === "NotFileMock was not a file, and NotFileMock was not a file")
186      val caught2 = intercept[TestFailedException] {
187        notFileMock should (be a ('file) or (be a ('file)))
188      }
189      assert(caught2.getMessage === "NotFileMock was not a file, and NotFileMock was not a file")
190      val caught3 = intercept[TestFailedException] {
191        notFileMock should (be a ('file) or be a ('file))
192      }
193      assert(caught3.getMessage === "NotFileMock was not a file, and NotFileMock was not a file")
194      val caught4 = intercept[TestFailedException] {
195        isNotFileMock should ((be a ('file)) or (be a ('file)))
196      }
197      assert(caught4.getMessage === "IsNotFileMock was not a file, and IsNotFileMock was not a file")
198      val caught5 = intercept[TestFailedException] {
199        isNotFileMock should (be a ('file) or (be a ('file)))
200      }
201      assert(caught5.getMessage === "IsNotFileMock was not a file, and IsNotFileMock was not a file")
202      val caught6 = intercept[TestFailedException] {
203        isNotFileMock should (be a ('file) or be a ('file))
204      }
205      assert(caught6.getMessage === "IsNotFileMock was not a file, and IsNotFileMock was not a file")
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        fileMock should (not (be a ('directory)) and not (be a ('file)))
212      }
213      assert(caught1.getMessage === "FileMock was not a directory, but FileMock was a file")
214      val caught2 = intercept[TestFailedException] {
215        fileMock should ((not be a ('directory)) and (not be a ('file)))
216      }
217      assert(caught2.getMessage === "FileMock was not a directory, but FileMock was a file")
218      val caught3 = intercept[TestFailedException] {
219        fileMock should (not be a ('directory) and not be a ('file))
220      }
221      assert(caught3.getMessage === "FileMock was not a directory, but FileMock was a file")
222      val caught4 = intercept[TestFailedException] {
223        isFileMock should (not (be a ('directory)) and not (be a ('file)))
224      }
225      assert(caught4.getMessage === "IsFileMock was not a directory, but IsFileMock was a file")
226      val caught5 = intercept[TestFailedException] {
227        isFileMock should ((not be a ('directory)) and (not be a ('file)))
228      }
229      assert(caught5.getMessage === "IsFileMock was not a directory, but IsFileMock was a file")
230      val caught6 = intercept[TestFailedException] {
231        isFileMock should (not be a ('directory) and not be a ('file))
232      }
233      assert(caught6.getMessage === "IsFileMock was not a directory, but IsFileMock was a file")
234      // Check that the error message "short circuits"
235      val caught7 = intercept[TestFailedException] {
236        fileMock should (not (be a ('file)) and not (be a ('directory)))
237      }
238      assert(caught7.getMessage === "FileMock was a file")
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        fileMock should (not (be a ('file)) or not (be a ('file)))
245      }
246      assert(caught1.getMessage === "FileMock was a file, and FileMock was a file")
247      val caught2 = intercept[TestFailedException] {
248        fileMock should ((not be a ('file)) or (not be a ('file)))
249      }
250      assert(caught2.getMessage === "FileMock was a file, and FileMock was a file")
251      val caught3 = intercept[TestFailedException] {
252        fileMock should (not be a ('file) or not be a ('file))
253      }
254      assert(caught3.getMessage === "FileMock was a file, and FileMock was a file")
255      val caught4 = intercept[TestFailedException] {
256        isFileMock should (not (be a ('file)) or not (be a ('file)))
257      }
258      assert(caught4.getMessage === "IsFileMock was a file, and IsFileMock was a file")
259      val caught5 = intercept[TestFailedException] {
260        isFileMock should ((not be a ('file)) or (not be a ('file)))
261      }
262      assert(caught5.getMessage === "IsFileMock was a file, and IsFileMock was a file")
263      val caught6 = intercept[TestFailedException] {
264        isFileMock should (not be a ('file) or not be a ('file))
265      }
266      assert(caught6.getMessage === "IsFileMock was a file, and IsFileMock was a file")
267    }
268  }
269}
270