Skip to content

Commit

Permalink
Initial commit, ready for 0.1.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmsparks committed Jul 17, 2015
0 parents commit bc47a7d
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/target
/classes
/checkouts
pom.xml
pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
.hgignore
.hg/
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright © 2015 JUXT LTD.

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# iota

Infix Operators for Test Assertions

## What is this?

When you are writing tests, it's common to want to check a number of properties against a given value.

```clojure
(require '[schema.core :as s])

(let [response ...]
(is (= (:status response) 200))
(is (= (count (get-in response [:headers "content-length"])) (count "Hello World!")))
(is (= (count (get-in response [:headers "content-type"])) "text/plain;charset=utf-8"))
(is (nil? (s/check s/Str (get-in response [:headers "content-type"]))))
(is (instance? java.nio.ByteBuffer response))
)
```

This can get a bit cumbersome.

iota is a little library that provides a single macro, `iota.iota/given`, that allows you to create triples, each of which expands into a `clojure.test/is` assertion.

```clojure
(require '[schema.core :as s])
(require '[iota.iota :refer [given]])

(given response
:status := 200
:headers :⊃ {"content-length" (count "Hello World!")
"content-type" "text/plain;charset=utf-8"}
[:headers "content-type"] :- s/Str
:body :instanceof java.nio.ByteBuffer)
```

It's a little less typing, which might give you the extra time to add more checks, improving your testing.

## Valid operators

Operator | Meaning
-------------|-------------------
:= | is equal to
:!= | isn't equal to
:- | conforms to schema
:!- | doesn't conform to schema
:? | passes predicate
:!? | fails predicate
:# | matches regex
:!# | doesn't match regex
:> or :⊃ | is superset of
:!> or :⊅ | isn't superset of
:< or :⊂ | is subset of
:!< or :⊄ | isn't subset of
:instanceof | is instance of
:!instanceof | isn't instance of

## Paths

The left hand operand of your expressions can be a vector, which acts as a path. A bit like `->` but you can also use strings.

## Installation

Add the following dependency to your `project.clj` file

```clojure
[juxt/iota "0.0.1"]
```

## Copyright & License

The MIT License (MIT)

Copyright © 2015 JUXT LTD.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 changes: 11 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
;; Copyright © 2015, JUXT LTD.

(defproject juxt/iota "0.1.0"
:description "Infix operators for test assertions"
:url "http://github.com/juxt/iota"
:license {:name "The MIT License"
:url "http://opensource.org/licenses/MIT"}

:dependencies [[prismatic/schema "0.4.2"]]

:profiles {:dev {:dependencies [[org.clojure/clojure "1.7.0"]]}})
61 changes: 61 additions & 0 deletions src/iota/iota.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
;; Copyright © 2015, JUXT LTD.

(ns iota.iota
(:require
[clojure.test :refer :all]
[clojure.set :as set]))

;; See the test at the end of this ns to understand the point of this code

(defprotocol TestClause
(as-test-function [_] "Take the clause and return a function which is applied to the value under test"))

(extend-protocol TestClause
clojure.lang.APersistentVector
;; A function application 'path', which is simply a composed function,
;; left-to-right rather than right-to-left.
(as-test-function [v] (apply comp (map as-test-function (reverse v))))

clojure.lang.Keyword
(as-test-function [k] k)

clojure.lang.Fn
(as-test-function [f] f)

String
(as-test-function [s] #(get % s)))

(defmacro given [v & body]
(let [t (gensym)]
`(do
(let [~t ~v]
~@(for [[a b c] (partition 3 body)]
(case b
;; Equals?
:= `(is (= ((as-test-function ~a) ~t) ~c))
:!= `(is (not= ((as-test-function ~a) ~t) ~c))

;; Schema checks
:- `(is (nil? (s/check ~c ((as-test-function ~a) ~t))))
:!- `(is (not (nil? (s/check ~c ((as-test-function ~a) ~t)))))

;; Is?
:? `(is (~c ((as-test-function ~a) ~t)))
:!? `(is (not (~c ((as-test-function ~a) ~t))))

;; Matches regex?
:# `(is (re-matches (re-pattern ~c) ((as-test-function ~a) ~t)))
:!# `(is (not (re-matches (re-pattern ~c) ((as-test-function ~a) ~t))))

;; Is superset?
(:> :⊃) `(is (set/superset? (set ((as-test-function ~a) ~t)) (set ~c)))
(:!> :⊅) `(is (not (set/superset? (set ((as-test-function ~a) ~t)) (set ~c))))

;; Is subset?
(:< :⊂) `(is (set/subset? (set ((as-test-function ~a) ~t)) (set ~c)))
(:!< :⊄) `(is (not (set/subset? (set ((as-test-function ~a) ~t)) (set ~c))))

;; Is an instance of
:instanceof `(is (instance? ~c ((as-test-function ~a) ~t)))
:!instanceof `(is (not (instance? ~c ((as-test-function ~a) ~t))))
))))))
35 changes: 35 additions & 0 deletions test/iota/iota_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
;; Copyright © 2015, JUXT LTD.

(ns iota.iota-test
(:require
[iota.iota :refer :all]
[clojure.test :refer :all]
[schema.core :as s]))

(deftest myself
(given {:foo "foo" :bar "bar"}
identity := {:foo "foo" :bar "bar"}
identity :- {:foo s/Str :bar s/Str}
:foo := "foo"
:foo :!= "bar"
:foo :? string?
count := 2
count :!= 3
[:foo count] := (count "foo")
[:foo count] :!= 10
:foo :# "fo+"
:foo :!# "fo+d"
identity :> {:foo "foo"}
identity :< {:foo "foo" :bar "bar" :zip "zip"}
)
(given [1 2 3]
first := 1
identity :- [s/Num]
identity :< [1 2 3 4]
identity :!< [1 3 4]
identity :> [1 2]
identity :!> [1 4]
count := 3
count :- s/Num
)
)

0 comments on commit bc47a7d

Please sign in to comment.