Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adds should-not-contain and documents it along with should-contain

  • Loading branch information...
commit 0fcace482d10213099a7220e55fa5bf6e5d3f457 1 parent 259e526
@slagyr authored
Showing with 83 additions and 15 deletions.
  1. +0 −4 TODO.md
  2. +41 −2 spec/speclj/should_spec.clj
  3. +42 −9 src/speclj/core.clj
View
4 TODO.md
@@ -1,8 +1,4 @@
# Features to add
-* refactor reporters to composite pattern
* .spec config file: available to plugins
-* exceptions during a spec run should not abort the rest of the examples ???
-* make should-contain accept regex, maps, seq
-* mocks
* limelight runner
View
43 spec/speclj/should_spec.clj
@@ -88,12 +88,23 @@
(should-fail! (should-contain "foo" "bar"))
(should-fail! (should-contain "foo" "Foo")))
+ (it "should-not-contain checks for non-containmentship of precise strings"
+ (should-fail! (should-not-contain "foo" "foobar"))
+ (should-pass! (should-not-contain "foo" "bar"))
+ (should-pass! (should-not-contain "foo" "Foo")))
+
(it "should-contain checks for containmentship of regular expressions"
(should-pass! (should-contain #"hello.*" "hello, world"))
(should-fail! (should-contain #"hello.*" "hola!"))
(should-pass! (should-contain #"tea" "I'm a little teapot"))
(should-fail! (should-contain #"coffee" "I'm a little teapot")))
+ (it "should-not-contain checks for non-containmentship of regular expressions"
+ (should-fail! (should-not-contain #"hello.*" "hello, world"))
+ (should-pass! (should-not-contain #"hello.*" "hola!"))
+ (should-fail! (should-not-contain #"tea" "I'm a little teapot"))
+ (should-pass! (should-not-contain #"coffee" "I'm a little teapot")))
+
(it "should-contain checks for containmentship of collection items"
(should-pass! (should-contain "tea" ["i'm" "a" "little" "tea" "pot"]))
(should-pass! (should-contain "tea" (list "i'm" "a" "little" "tea" "pot")))
@@ -101,8 +112,36 @@
(should-pass! (should-contain 1 [1 2 3]))
(should-fail! (should-contain "coffee" ["i'm" "a" "little" "tea" "pot"]))
(should-fail! (should-contain "coffee" (list "i'm" "a" "little" "tea" "pot")))
- (should-fail! (should-contain "coffee" (set ["i'm" "a" "little" "tea" "pot"])))
- )
+ (should-fail! (should-contain "coffee" (set ["i'm" "a" "little" "tea" "pot"]))))
+
+ (it "should-not-contain checks for non-containmentship of collection items"
+ (should-fail! (should-not-contain "tea" ["i'm" "a" "little" "tea" "pot"]))
+ (should-fail! (should-not-contain "tea" (list "i'm" "a" "little" "tea" "pot")))
+ (should-fail! (should-not-contain "tea" (set ["i'm" "a" "little" "tea" "pot"])))
+ (should-fail! (should-not-contain 1 [1 2 3]))
+ (should-pass! (should-not-contain "coffee" ["i'm" "a" "little" "tea" "pot"]))
+ (should-pass! (should-not-contain "coffee" (list "i'm" "a" "little" "tea" "pot")))
+ (should-pass! (should-not-contain "coffee" (set ["i'm" "a" "little" "tea" "pot"]))))
+
+ (it "should-contain checks for containmentship of keys"
+ (should-pass! (should-contain "foo" {"foo" :bar}))
+ (should-fail! (should-contain :bar {"foo" :bar}))
+ (should-pass! (should-contain 1 {"foo" :bar 1 2}))
+ (should-fail! (should-contain 2 {"foo" :bar 1 2})))
+
+ (it "should-not-contain checks for non-containmentship of keys"
+ (should-fail! (should-not-contain "foo" {"foo" :bar}))
+ (should-pass! (should-not-contain :bar {"foo" :bar}))
+ (should-fail! (should-not-contain 1 {"foo" :bar 1 2}))
+ (should-pass! (should-not-contain 2 {"foo" :bar 1 2})))
+
+ (it "should-contain errors on unhandles types"
+ (should-throw Exception "should-contain doesn't know how to handle these types: [java.lang.Long java.lang.Long]"
+ (should-contain 1 2)))
+
+ (it "should-not-contain errors on unhandles types"
+ (should-throw Exception "should-not-contain doesn't know how to handle these types: [java.lang.Long java.lang.Long]"
+ (should-not-contain 1 2)))
(it "should-not-be-nil checks for inequality with nil"
(should-fail! (should-not-be-nil nil))
View
51 src/speclj/core.clj
@@ -12,7 +12,7 @@
(:import
[speclj SpecFailure SpecPending]
[java.util.regex Pattern]
- [clojure.lang IPersistentCollection]))
+ [clojure.lang IPersistentCollection IPersistentMap]))
(defmacro it
"body => any forms but aught to contain at least one assertion (should)
@@ -159,28 +159,61 @@
`(should= nil ~form))
(defmulti -should-contain (fn [expected actual] [(type expected) (type actual)]))
+(defmulti -should-not-contain (fn [expected actual] [(type expected) (type actual)]))
(defmethod -should-contain [String String] [seeking within]
- (if (not (.contains within seeking))
+ (when (not (.contains within seeking))
(throw (SpecFailure. (str "Expected: " (-to-s seeking) endl "to be in: " (-to-s within) " (using .contains)")))))
+(defmethod -should-not-contain [String String] [seeking within]
+ (when (.contains within seeking)
+ (throw (SpecFailure. (str "Expected: " (-to-s seeking) endl "not to be in: " (-to-s within) " (using .contains)")))))
+
(defmethod -should-contain [Pattern String] [pattern actual]
- (if (empty? (re-seq pattern actual))
- (throw (SpecFailure. (str "Expected: " (-to-s actual) endl " to match: " (-to-s pattern) " (using re-seq)")))))
+ (when (empty? (re-seq pattern actual))
+ (throw (SpecFailure. (str "Expected: " (-to-s actual) endl "to match: " (-to-s pattern) " (using re-seq)")))))
+
+(defmethod -should-not-contain [Pattern String] [pattern actual]
+ (when (not (empty? (re-seq pattern actual)))
+ (throw (SpecFailure. (str "Expected: " (-to-s actual) endl "not to match: " (-to-s pattern) " (using re-seq)")))))
+
+(defmethod -should-contain [Object IPersistentMap] [element coll]
+ (when (not (contains? coll element))
+ (throw (SpecFailure. (str "Expected: " (-to-s element) endl "to be a key in: " (-to-s coll) " (using contains?)")))))
+
+(defmethod -should-not-contain [Object IPersistentMap] [element coll]
+ (when (contains? coll element)
+ (throw (SpecFailure. (str "Expected: " (-to-s element) endl "not to be a key in: " (-to-s coll) " (using contains?)")))))
(defmethod -should-contain [Object IPersistentCollection] [element coll]
- (if (not (some #(= element %) coll))
- (throw (SpecFailure. (str "Expected: " (-to-s element) endl "to be in: " (-to-s coll) " (using =)"))))
- )
+ (when (not (some #(= element %) coll))
+ (throw (SpecFailure. (str "Expected: " (-to-s element) endl "to be in: " (-to-s coll) " (using =)")))))
+
+(defmethod -should-not-contain [Object IPersistentCollection] [element coll]
+ (when (some #(= element %) coll)
+ (throw (SpecFailure. (str "Expected: " (-to-s element) endl "not to be in: " (-to-s coll) " (using =)")))))
(defmethod -should-contain :default [expected actual]
- (throw (Exception. (str "No matcher for " (type expected) (type actual)))))
+ (throw (Exception. (str "should-contain doesn't know how to handle these types: " [(type expected) (type actual)]))))
+
+(defmethod -should-not-contain :default [expected actual]
+ (throw (Exception. (str "should-not-contain doesn't know how to handle these types: " [(type expected) (type actual)]))))
(defmacro should-contain
- "Asserts that the string is contained within another string"
+ "Multi-purpose assertion of containment. Works strings, regular expressions, sequences, and maps.
+
+ (should-contain \"foo\" \"foobar\") ; looks for sub-string
+ (should-contain #\"hello.*\" \"hello, world\") ; looks for regular expression
+ (should-contain :foo {:foo :bar}) ; looks for a key in a map
+ (should-contain 3 [1 2 3 4]) ; looks for an object in a collection"
[expected actual]
`(-should-contain ~expected ~actual))
+(defmacro should-not-contain
+ "Multi-purpose assertion of non-containment. See should-contain as an example of opposite behavior."
+ [expected actual]
+ `(-should-not-contain ~expected ~actual))
+
(defmacro should-not-be-nil
"Asserts that the form evaluates to a non-nil value"
[form]
Please sign in to comment.
Something went wrong with that request. Please try again.