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 ShouldBeNullSpec extends Spec with ShouldMatchers with Checkers with ReturnsNormallyThrowsAssertion {
25
26  val nullMap: Map[Int, String] = null
27  val map = Map(1 -> "one", 2 -> "two")
28
29  describe("the be (null) syntax") {
30
31    it("should work in its basic for with or without not") {
32
33      map should not be (null)
34      map should not (be (null))
35      map should (not be (null))
36      map should (not (be (null)))
37      nullMap should be (null)
38      nullMap should (be (null))
39      // null should be (null) this doesn't compile. can't do implicits on the Null type, which is fine
40      // null should (be (null))
41
42      val caught1 = intercept[TestFailedException] {
43        map should be (null)
44      }
45      assert(caught1.getMessage === "Map(1 -> one, 2 -> two) was not null")
46
47      val caught2 = intercept[TestFailedException] {
48        map should (be (null))
49      }
50      assert(caught2.getMessage === "Map(1 -> one, 2 -> two) was not null")
51
52      val caught3 = intercept[TestFailedException] {
53        nullMap should not be (null)
54      }
55      assert(caught3.getMessage === "The reference was null")
56
57      val caught4 = intercept[TestFailedException] {
58        nullMap should not (be (null))
59      }
60      assert(caught4.getMessage === "The reference was null")
61
62      val caught5 = intercept[TestFailedException] {
63        nullMap should not ((be (null)))
64      }
65      assert(caught5.getMessage === "The reference was null")
66    }
67
68    it("should throw a NullPointerException if they try to short circuit with a null check first") {
69      // The reason I check this is I warn that this will happen in the ShouldMatcher scaladoc
70      intercept[NullPointerException] {
71        nullMap should (not be (null) and contain key (7))
72      }
73      intercept[NullPointerException] {
74        nullMap should (be (null) and contain key (7))
75      }
76    }
77
78    it("should compile and run when used with and (with or without not)") {
79
80      val caught1 = intercept[TestFailedException] {
81        Map(1 -> "one") should (contain key (7) and not be (null))
82      }
83      assert(caught1.getMessage === "Map(1 -> one) did not contain key 7")
84
85      val caught2 = intercept[TestFailedException] {
86        Map(1 -> "one") should (contain key (7) and not (be (null)))
87      }
88      assert(caught2.getMessage === "Map(1 -> one) did not contain key 7")
89
90      val caught3 = intercept[TestFailedException] {
91        Map(1 -> "one") should (contain key (7) and (not (be (null))))
92      }
93      assert(caught3.getMessage === "Map(1 -> one) did not contain key 7")
94
95      Map(1 -> "one") should (contain key (1) and not be (null))
96      Map(1 -> "one") should (contain key (1) and not (be (null)))
97      Map(1 -> "one") should (contain key (1) and (not (be (null))))
98
99      val caught4 = intercept[TestFailedException] {
100        Map(1 -> "one") should (contain key (1) and be (null))
101      }
102      assert(caught4.getMessage === "Map(1 -> one) contained key 1, but Map(1 -> one) was not null")
103
104      val caught5 = intercept[TestFailedException] {
105        Map(1 -> "one") should (contain key (1) and (be (null)))
106      }
107      assert(caught5.getMessage === "Map(1 -> one) contained key 1, but Map(1 -> one) was not null")
108
109      val caught6 = intercept[TestFailedException] {
110        Map(1 -> "one") should (be (null) and not be (null))
111      }
112      assert(caught6.getMessage === "Map(1 -> one) was not null")
113
114      val caught7 = intercept[TestFailedException] {
115        Map(1 -> "one") should (be (null) and not (be (null)))
116      }
117      assert(caught7.getMessage === "Map(1 -> one) was not null")
118
119      val caught8 = intercept[TestFailedException] {
120        Map(1 -> "one") should (be (null) and (not (be (null))))
121      }
122      assert(caught8.getMessage === "Map(1 -> one) was not null")
123
124      nullMap should (be (null) and be (null))
125      nullMap should (be (null) and (be (null)))
126
127      val caught9 = intercept[TestFailedException] {
128        nullMap should (be (null) and not be (null))
129      }
130      assert(caught9.getMessage === "The reference was null, but the reference was null")
131
132      val caught10 = intercept[TestFailedException] {
133        nullMap should (be (null) and not (be (null)))
134      }
135      assert(caught10.getMessage === "The reference was null, but the reference was null")
136
137      val caught11 = intercept[TestFailedException] {
138        nullMap should (be (null) and (not (be (null))))
139      }
140      assert(caught11.getMessage === "The reference was null, but the reference was null")
141    }
142
143    it("should compile and run when used with or (with or without not)") {
144
145      Map(1 -> "one") should (contain key (7) or not be (null))
146      Map(1 -> "one") should (contain key (7) or not (be (null)))
147      Map(1 -> "one") should (contain key (7) or (not (be (null))))
148
149      Map(1 -> "one") should (contain key (1) or not be (null))
150      Map(1 -> "one") should (contain key (1) or not (be (null)))
151      Map(1 -> "one") should (contain key (1) or (not (be (null))))
152
153      val caught4 = intercept[TestFailedException] {
154        Map(1 -> "one") should (contain key (7) or be (null))
155      }
156      assert(caught4.getMessage === "Map(1 -> one) did not contain key 7, and Map(1 -> one) was not null")
157
158      val caught5 = intercept[TestFailedException] {
159        Map(1 -> "one") should (contain key (7) or (be (null)))
160      }
161      assert(caught5.getMessage === "Map(1 -> one) did not contain key 7, and Map(1 -> one) was not null")
162
163      val caught6 = intercept[TestFailedException] {
164        Map(1 -> "one") should (be (null) or be (null))
165      }
166      assert(caught6.getMessage === "Map(1 -> one) was not null, and Map(1 -> one) was not null")
167
168      val caught7 = intercept[TestFailedException] {
169        Map(1 -> "one") should (be (null) or (be (null)))
170      }
171      assert(caught7.getMessage === "Map(1 -> one) was not null, and Map(1 -> one) was not null")
172
173      nullMap should (be (null) or be (null))
174      nullMap should (be (null) or (be (null)))
175      nullMap should (be (null) or not be (null))
176      nullMap should (be (null) or not (be (null)))
177      nullMap should (be (null) or (not (be (null))))
178      nullMap should (not be (null) or be (null))
179      nullMap should (not (be (null)) or be (null))
180    }
181  }
182}
183