Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adds should-not== for non-collections. documents equivalency assertions

  • Loading branch information...
commit addbe5aa52bed80b1d8fd2cc49fdbc68789e9c0a 1 parent f94bad7
Micah Martin authored
5 CHANGES.md
View
@@ -1,3 +1,8 @@
+# 2.5.0
+
+* should== for loose equality and collection containment equality
+* should-not== opposite of should==
+
# 2.4.0
* should-contain works with regular expressions, maps, and sequences
10 README.md
View
@@ -72,7 +72,7 @@ Checkout this example spec file. It would be located at `sample_project/spec/sam
(run-specs)
```
-### speclj.core namespace
+### `speclj.core` namespace
Your spec files should `:use` the `speclj.core` in it's entirety. It's a clean namespace and you're likely going to use all the definitions within it. Don't forget to pull in the library that you're testing as well (sample.core in this case).
```clojure
@@ -81,21 +81,21 @@ Your spec files should `:use` the `speclj.core` in it's entirety. It's a clean
[sample.core])
```
-### describe
+### `describe`
`describe` is the outer most container for specs. It takes a `String` name and any number of _spec components_.
```clojure
(describe "Truth" ...)
```
-### it
+### `it`
`it` specifies a _characteristic_ of the subject. This is where assertions go. Be sure to provide good names as the first parameter of `it` calls.
```clojure
(it "is true" ...)
```
-### should and should-not
+### `should` and `should-not`
Assertions. All assertions begin with `should`. `should` and `should-not` are just two of the many assertions available. They both take expressions that they will check for truthy-ness and falsy-ness respectively.
```clojure
@@ -110,7 +110,7 @@ At the very end of the file is an invocation of `(run-specs)`. This will invoke
(run-specs)
```
-## should Variants
+## `should` Variants (Assertions)
There are several ways to make assertions. They are documented on the wiki: [Should Variants](https://github.com/slagyr/speclj/wiki/Should-variants)
## Spec Components
111 spec/speclj/should_spec.clj
View
@@ -1,8 +1,7 @@
(ns speclj.should-spec
- (:use
- [speclj.core]
- [speclj.spec-helper]
- [speclj.util :only (endl)]))
+ (:use [speclj.core]
+ [speclj.spec-helper]
+ [speclj.util :only (endl)]))
(describe "Should Assertions: "
(it "should tests truthy"
@@ -114,85 +113,105 @@
(should-fail! (should== [1 2 3] [1 2 3 4])))
(it "reports extra items"
- (let [message (str "Expected collection contained: <[1 2 3]>" endl "Actual collection contained: <[1 2 3 4]>" endl "The extra elements were: <[4]> (using =)")]
+ (let [message (str "Expected contents: <[1 2 3]>" endl " got: <[1 2 3 4]>" endl " missing: <[]>" endl " extra: <[4]>")]
(should= message (failure-message (should== [1 2 3] [1 2 3 4])))))
(it "fails if target is missing items"
(should-fail! (should== [1 2 3] [1 2])))
(it "reports missing items"
- (let [message (str "Expected collection contained: <[1 2 3]>" endl "Actual collection contained: <[1 2]>" endl "The missing elements were: <[3]> (using =)")]
+ (let [message (str "Expected contents: <[1 2 3]>" endl " got: <[1 2]>" endl " missing: <[3]>" endl " extra: <[]>")]
(should= message (failure-message (should== [1 2 3] [1 2])))))
(it "fails if target is missing items and has extra items"
(should-fail! (should== [1 2 3] [1 2 4])))
(it "reports missing and extra items"
- (let [message (str "Expected collection contained: <[1 2 3]>" endl "Actual collection contained: <[1 2 4]>" endl "The missing elements were: <[3]>" endl "The extra elements were: <[4]> (using =)")]
+ (let [message (str "Expected contents: <[1 2 3]>" endl " got: <[1 2 4]>" endl " missing: <[3]>" endl " extra: <[4]>")]
(should= message (failure-message (should== [1 2 3] [1 2 4])))))
(it "fails if there are duplicates in the target"
(should-fail! (should== [1 5] [1 1 1 5])))
(it "reports extra duplicates"
- (let [message (str "Expected collection contained: <[1 5]>" endl "Actual collection contained: <[1 1 1 5]>" endl "The extra elements were: <[1 1]> (using =)")]
- (should= message (failure-message (should== [1 5] [1 1 1 5])))))
+ (let [message (str "Expected contents: <[1 5]>" endl " got: <[1 1 1 5]>" endl " missing: <[]>" endl " extra: <[1 1]>")]
+ (should= message (failure-message (should== [1 5] [1 1 1 5])))))
(it "fails if there are duplicates in the expected"
(should-fail! (should== [1 1 1 5] [1 5])))
(it "reports missing duplicates"
- (let [message (str "Expected collection contained: <[1 1 1 5]>" endl "Actual collection contained: <[1 5]>" endl "The missing elements were: <[1 1]> (using =)")]
- (should= message (failure-message (should== [1 1 1 5] [1 5])))))
+ (let [message (str "Expected contents: <[1 1 1 5]>" endl " got: <[1 5]>" endl " missing: <[1 1]>" endl " extra: <[]>")]
+ (should= message (failure-message (should== [1 1 1 5] [1 5])))))
(it "prints lazyseqs"
- (let [message (str "Expected collection contained: <(1 1 1 5)>" endl "Actual collection contained: <[1 5]>" endl "The missing elements were: <[1 1]> (using =)")]
- (should= message (failure-message (should== (lazy-seq [1 1 1 5]) [1 5])))))
+ (let [message (str "Expected contents: <(1 1 1 5)>" endl " got: <[1 5]>" endl " missing: <[1 1]>" endl " extra: <[]>")]
+ (should= message (failure-message (should== (lazy-seq [1 1 1 5]) [1 5])))))
(it "prints lists"
- (let [message (str "Expected collection contained: <(1 1 1 5)>" endl "Actual collection contained: <[1 5]>" endl "The missing elements were: <[1 1]> (using =)")]
- (should= message (failure-message (should== (list 1 1 1 5) [1 5])))))
+ (let [message (str "Expected contents: <(1 1 1 5)>" endl " got: <[1 5]>" endl " missing: <[1 1]>" endl " extra: <[]>")]
+ (should= message (failure-message (should== (list 1 1 1 5) [1 5])))))
(it "prints sets"
- (let [message (str "Expected collection contained: <[1 1 1 5]>" endl "Actual collection contained: <#{1 5}>" endl "The missing elements were: <[1 1]> (using =)")]
- (should= message (failure-message (should== [1 1 1 5] #{1 5})))))
+ (let [message (str "Expected contents: <[1 1 1 5]>" endl " got: <#{1 5}>" endl " missing: <[1 1]>" endl " extra: <[]>")]
+ (should= message (failure-message (should== [1 1 1 5] #{1 5})))))
- ))
+ ))
(context "should-not=="
- (it "fails if target contains all items"
- (should-fail! (should-not== [1 2 3] [1 2 3])))
- (it "fails if target contains all items out of order"
- (should-fail! (should-not== [1 2 3] [1 3 2])))
+ (context "numbers"
+ (it "tests loose equality"
+ (should-fail! (should-not== 1 1))
+ (should-fail! (should-not== 1 1.0))
+ (should-fail! (should-not== (int 1) (long 1)))
+ (should-pass! (should-not== 1 2))
+ (should-pass! (should-not== 1 2.0)))
+
+ (it "reports the error"
+ (let [error (str " Expected: <1>" endl "not to ==: <1> (using ==)")]
+ (should= error (failure-message (should-not== 1 1)))))
+
+ (it "reports the error with floats"
+ (let [error (str " Expected: <1.0>" endl "not to ==: <1> (using ==)")]
+ (should= error (failure-message (should-not== 1.0 1)))))
+
+ )
- (it "passes if target includes extra items"
- (should-pass! (should-not== [1 2 3] [1 2 3 4])))
+ (context "two collections"
+ (it "fails if target contains all items"
+ (should-fail! (should-not== [1 2 3] [1 2 3])))
- (it "passes if target is missing items"
- (should-pass! (should-not== [1 2 3] [1 2])))
+ (it "fails if target contains all items out of order"
+ (should-fail! (should-not== [1 2 3] [1 3 2])))
- (it "passes if target is missing items and has extra items"
- (should-pass! (should-not== [1 2 3] [1 2 4])))
+ (it "passes if target includes extra items"
+ (should-pass! (should-not== [1 2 3] [1 2 3 4])))
- (it "passes if there are duplicates in the target"
- (should-pass! (should-not== [1 5] [1 1 1 5])))
+ (it "passes if target is missing items"
+ (should-pass! (should-not== [1 2 3] [1 2])))
- (it "passes if there are duplicates in the expected"
- (should-pass! (should-not== [1 1 1 5] [1 5])))
+ (it "passes if target is missing items and has extra items"
+ (should-pass! (should-not== [1 2 3] [1 2 4])))
- (it "prints lazyseqs"
- (let [message (str "Expected: <(1 5)>" endl "not to contain: <[1 5]> (using =)")]
- (should= message (failure-message (should-not== (lazy-seq [1 5]) [1 5])))))
+ (it "passes if there are duplicates in the target"
+ (should-pass! (should-not== [1 5] [1 1 1 5])))
- (it "prints lists"
- (let [message (str "Expected: <(1 5)>" endl "not to contain: <[1 5]> (using =)")]
- (should= message (failure-message (should-not== (list 1 5) [1 5])))))
+ (it "passes if there are duplicates in the expected"
+ (should-pass! (should-not== [1 1 1 5] [1 5])))
- (it "prints sets"
- (let [message (str "Expected: <#{1 5}>" endl "not to contain: <[1 5]> (using =)")]
- (should= message (failure-message (should-not== (set [1 5]) [1 5])))))
+ (it "prints lazyseqs"
+ (let [message (str "Expected contents: <(1 5)>" endl " to differ from: <[1 5]>")]
+ (should= message (failure-message (should-not== (lazy-seq [1 5]) [1 5])))))
+ (it "prints lists"
+ (let [message (str "Expected contents: <(1 5)>" endl " to differ from: <[1 5]>")]
+ (should= message (failure-message (should-not== (list 1 5) [1 5])))))
+
+ (it "prints sets"
+ (let [message (str "Expected contents: <#{1 5}>" endl " to differ from: <[1 5]>")]
+ (should= message (failure-message (should-not== (set [1 5]) [1 5])))))
+ )
)
(it "should-contain checks for containmentship of precise strings"
@@ -272,7 +291,7 @@
(should-pass! (should-throw (throw (Throwable. "error"))))
(should-fail! (should-throw (+ 1 1)))
(should= (str "Expected java.lang.Throwable thrown from: (+ 1 1)" endl
- " but got: <nothing thrown>")
+ " but got: <nothing thrown>")
(failure-message (should-throw (+ 1 1)))))
(it "should-throw can test an expected throwable type"
@@ -281,10 +300,10 @@
(should-fail! (should-throw NullPointerException (throw (Exception.))))
(should-fail! (should-throw NullPointerException (+ 1 1)))
(should= (str "Expected java.lang.NullPointerException thrown from: (+ 1 1)" endl
- " but got: <nothing thrown>")
+ " but got: <nothing thrown>")
(failure-message (should-throw NullPointerException (+ 1 1))))
(should= (str "Expected java.lang.NullPointerException thrown from: (throw (Exception. \"some message\"))" endl
- " but got: java.lang.Exception: some message")
+ " but got: java.lang.Exception: some message")
(failure-message (should-throw NullPointerException (throw (Exception. "some message"))))))
(it "should-throw can test the message of the exception"
@@ -299,9 +318,9 @@
(should-pass! (should-not-throw (+ 1 1)))
(should-fail! (should-not-throw (throw (Throwable. "error"))))
(should= (str "Expected nothing thrown from: (throw (Throwable. \"error\"))" endl
- " but got: java.lang.Throwable: error")
+ " but got: java.lang.Throwable: error")
(failure-message (should-not-throw (throw (Throwable. "error"))))))
)
-(run-specs)
+(run-specs :stacktrace true)
38 speclj.iml
View
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
-<module type="JAVA_MODULE" version="4">
+<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="Clojure" name="Clojure">
<configuration />
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="false">
- <output url="file://$MODULE_DIR$/classes" />
- <output-test url="file://$MODULE_DIR$/classes" />
- <exclude-output />
+ <output url="file://$MODULE_DIR$/target/classes" />
+ <output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
+ <excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
@@ -27,39 +27,15 @@
<orderEntry type="module-library">
<library>
<CLASSES>
- <root url="jar://$MODULE_DIR$/lib/mmargs-1.2.0.jar!/" />
- </CLASSES>
- <JAVADOC />
- <SOURCES />
- </library>
- </orderEntry>
- <orderEntry type="module-library">
- <library>
- <CLASSES>
<root url="file://$MODULE_DIR$/src" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
- <orderEntry type="module-library">
- <library>
- <CLASSES>
- <root url="jar://$MODULE_DIR$/lib/fresh-1.0.2.jar!/" />
- </CLASSES>
- <JAVADOC />
- <SOURCES />
- </library>
- </orderEntry>
- <orderEntry type="module-library">
- <library>
- <CLASSES>
- <root url="jar://$MODULE_DIR$/lib/clojure-1.4.0-alpha3.jar!/" />
- </CLASSES>
- <JAVADOC />
- <SOURCES />
- </library>
- </orderEntry>
+ <orderEntry type="library" name="Maven: org.clojure:clojure:1.4.0" level="project" />
+ <orderEntry type="library" name="Maven: fresh:fresh:1.0.2" level="project" />
+ <orderEntry type="library" name="Maven: mmargs:mmargs:1.2.0" level="project" />
</component>
</module>
78 src/speclj/core.clj
View
@@ -1,18 +1,15 @@
(ns speclj.core
- (:use
- [speclj.running :only (submit-description run-and-report)]
- [speclj.reporting :only (report-message*)]
- [speclj.tags :only (describe-filter)]
- [speclj.config :only (active-reporters active-runner default-runner config-mappings default-config)]
- [speclj.components]
- [speclj.util :only (endl)])
- (:require
- [speclj.run.standard]
- [speclj.report.progress])
- (:import
- [speclj SpecFailure SpecPending]
- [java.util.regex Pattern]
- [clojure.lang IPersistentCollection IPersistentMap]))
+ (:use [speclj.running :only (submit-description run-and-report)]
+ [speclj.reporting :only (report-message*)]
+ [speclj.tags :only (describe-filter)]
+ [speclj.config :only (active-reporters active-runner default-runner config-mappings default-config)]
+ [speclj.components]
+ [speclj.util :only (endl)])
+ (:require [speclj.run.standard]
+ [speclj.report.progress])
+ (:import [speclj SpecFailure SpecPending]
+ [java.util.regex Pattern]
+ [clojure.lang IPersistentCollection IPersistentMap]))
(defmacro it
"body => any forms but aught to contain at least one assertion (should)
@@ -99,10 +96,10 @@
(it \"knows the meaining life\" (should= @meaning (the-meaning-of :life)))"
[name & body]
(let [var-name (with-meta (symbol name) {:dynamic true})]
- `(do
- (let [with-all-component# (new-with-all '~var-name (fn [] ~@body))]
- (declare ~var-name)
- with-all-component#))))
+ `(do
+ (let [with-all-component# (new-with-all '~var-name (fn [] ~@body))]
+ (declare ~var-name)
+ with-all-component#))))
(defn -to-s [thing]
(if (nil? thing) "nil" (str "<" (pr-str thing) ">")))
@@ -237,41 +234,48 @@
(recur r match-against (conj diff f)))))))
(defn difference-message [expected actual extra missing]
- (-> (str "Expected collection contained: " (-to-s expected) endl "Actual collection contained: " (-to-s actual))
- (#(if (empty? missing)
- %
- (str % endl "The missing elements were: " (-to-s missing))))
- (#(if (empty? extra)
- %
- (str % endl "The extra elements were: " (-to-s extra))))
- (#(str % " (using =)"))))
+ (str
+ "Expected contents: " (-to-s expected) endl
+ " got: " (-to-s actual) endl
+ " missing: " (-to-s missing) endl
+ " extra: " (-to-s extra)))
(defmulti -should== (fn [expected actual] [(type expected) (type actual)]))
+(defmulti -should-not== (fn [expected actual] [(type expected) (type actual)]))
(defmethod -should== [Object Object] [expected actual]
(when-not (== expected actual)
- (let [error (str "Expected: " (-to-s expected) endl " got: " (-to-s actual) " (using ==)")]
- (throw (SpecFailure. error)))))
+ (throw (SpecFailure. (str "Expected: " (-to-s expected) endl " got: " (-to-s actual) " (using ==)")))))
+
+(defmethod -should-not== [Object Object] [expected actual]
+ (when-not (not (== expected actual))
+ (throw (SpecFailure. (str " Expected: " (-to-s expected) endl "not to ==: " (-to-s actual) " (using ==)")))))
(defmethod -should== [IPersistentCollection IPersistentCollection] [expected actual]
(let [extra (coll-difference actual expected)
missing (coll-difference expected actual)]
(when-not (and (empty? extra) (empty? missing))
- (let [error-message (difference-message expected actual extra missing)]
- (throw (SpecFailure. error-message))))))
+ (throw (SpecFailure. (difference-message expected actual extra missing))))))
+
+(defmethod -should-not== [IPersistentCollection IPersistentCollection] [expected actual]
+ (let [extra (coll-difference actual expected)
+ missing (coll-difference expected actual)]
+ (when (and (empty? extra) (empty? missing))
+ (throw (SpecFailure. (str "Expected contents: " (-to-s expected) endl " to differ from: " (-to-s actual)))))))
(defmacro should==
- "Asserts loose equality"
+ "Asserts 'equivalency'.
+ When passed collections it will check that they have the same contents.
+ For anything else it will assert that clojure.core/== returns true."
[expected actual]
`(-should== ~expected ~actual))
(defmacro should-not==
- "Inverse of should=="
+ "Asserts 'non-equivalency'.
+ When passed collections it will check that they do NOT have the same contents.
+ For anything else it will assert that clojure.core/== returns false."
[expected actual]
- `(let [extra# (coll-difference ~actual ~expected)
- missing# (coll-difference ~expected ~actual)]
- (when (and (empty? extra#) (empty? missing#))
- (throw (SpecFailure. (str "Expected: " (-to-s ~expected) endl "not to contain: " (-to-s ~actual) " (using =)"))))))
+ `(-should-not== ~expected ~actual))
(defmacro should-not-be-nil
"Asserts that the form evaluates to a non-nil value"
@@ -342,7 +346,7 @@ are evaluated by evaluation the file as a script. Optional configuration paramt
(run-specs :stacktrace true :color false :reporter \"documentation\")"
(when (identical? (active-runner) @default-runner) ; Solo file run?
(let [config (apply hash-map configurations)
- config (merge (dissoc default-config :runner) config)]
+ config (merge (dissoc default-config :runner ) config)]
(with-bindings (config-mappings config)
(if-let [filter-msg (describe-filter)]
(report-message* (active-reporters) filter-msg))
2  src/speclj/version.clj
View
@@ -3,7 +3,7 @@
[clojure.string :as str]))
(def major 2)
-(def minor 4)
+(def minor 5)
(def tiny 0)
(def snapshot false)
(def string
Please sign in to comment.
Something went wrong with that request. Please try again.