Skip to content

Instantly share code, notes, and snippets.

@jaycfields
jaycfields / blog_expectations.clj
Created October 31, 2012 18:25
interaction passing
(ns blog-expectations
(:use expectations))
(expect (interaction (spit "/tmp/hello-world" "some data" :append true))
(do
(spit "/tmp/somewhere-else" "nil")
(spit "/tmp/hello-world" "some data" :append true)))
(expect (interaction (one "hello" {:a :b :c {:dd :ee :ff :gg}}))
(do
(one "hello")
(one "hello" "world" "here")
(one "hello" {:a 1 2 3})))
; expected: (one "hello" {:a :b, :c {:ff :gg, :dd :ee}}) once
;
; got: (one "hello")
; {:a :b, :c {:ff :gg, :dd :ee}} are in expected, but not in actual
@jaycfields
jaycfields / blog_expectations.clj
Created October 31, 2012 18:52
failing test with anything
(expect (interaction (one "hello" :anything))
(one "help" {:a 1 2 3}))
; expected: (one "hello" :anything) once
;
; got: (one "help" {2 3, :a 1})
; expected arg1: hello
; actual arg1: help
; matches: "hel"
; diverges: "lo"
@jaycfields
jaycfields / blog_expectations.clj
Created October 31, 2012 19:16
frozen in time
(ns blog-expectations
(:use expectations)
(:import [org.joda.time DateTime]))
(defn add-timestamp [m]
(assoc m :time (DateTime.)))
(expect {:time (DateTime. 1)}
(freeze-time (DateTime. 1)
(add-timestamp {})))
(ns blog-expectations
(:use expectations)
(:import [org.joda.time DateTime]))
(defn timestamp-event [m t]
(assoc m :time t))
(expect-let [now (DateTime.)]
{:time now}
(timestamp-event {} now))
@jaycfields
jaycfields / blog_expectations.clj
Created November 3, 2012 13:22
what's the what's the scenario
(ns blog-expectations
(:use expectations.scenarios))
(scenario
;;; real tests will call domain code,
;;; I avoid that here for simplicity
(let [x [1 2]
y (map-indexed vector x)]
(expect vector? x)
@jaycfields
jaycfields / blog_expectations.clj
Created November 3, 2012 13:25
generic scenario
(scenario
(do-work)
(do-more-work)
(expect a b)
(expect c d)
(expect e f))
@jaycfields
jaycfields / blog_expectations.clj
Created November 3, 2012 13:33
using given instead
(ns blog-expectations
(:use expectations))
(given [expected actual]
(expect expected
(let [x [1 2]
y (map-indexed vector x)]
actual))
vector? x
[0 1] (first y)
(ns blog-expectations
(:use expectations))
(expect vector? [1 2])
(given [x y-fn] (expect x
(y-fn
(map-indexed vector [1 2])))
[0 1] first
java.util.List identity)
(ns blog-expectations
(:use expectations))
(expect vector? [1 2])
(given [x y-fn] (expect x
(y-fn
(map-indexed vector [1 2])))
[0 1] first
java.util.List identity)