├── .gitignore ├── LICENSE ├── README.md ├── project.clj ├── src └── juxt │ └── iota.cljc └── test └── juxt ├── iota_test.cljc └── runner.cljs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | resources/public/js/testable.js 13 | *-init.clj 14 | /node_modules/ 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright © 2015 JUXT LTD. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # iota 2 | 3 | Infix Operators for Test Assertions 4 | 5 | ## What is this? 6 | 7 | When you are writing tests, it's common to want to check a number of properties against a given value. 8 | 9 | ```clojure 10 | (require '[clojure.test :refer :all] 11 | '[schema.core :as s]) 12 | 13 | (deftest my-test 14 | (let [response ...] 15 | (is (= (:status response) 200)) 16 | (is (= (count (get-in response [:headers "content-length"])) (count "Hello World!"))) 17 | (is (= (count (get-in response [:headers "content-type"])) "text/plain;charset=utf-8")) 18 | (is (nil? (s/check s/Str (get-in response [:headers "content-type"])))) 19 | (is (instance? java.nio.ByteBuffer response)) 20 | )) 21 | ``` 22 | 23 | This can get a bit cumbersome. 24 | 25 | __iota__ is a micro-library that provides a single macro, `juxt.iota/given`, that allows you to create triples, each of which expands into a `clojure.test/is` assertion. 26 | 27 | ```clojure 28 | (require '[clojure.test :refer :all] 29 | '[schema.core :as s] 30 | '[juxt.iota :refer [given]]) 31 | 32 | (deftest my-test 33 | (given response 34 | :status := 200 35 | :headers :⊃ {"content-length" (count "Hello World!") 36 | "content-type" "text/plain;charset=utf-8"} 37 | [:headers "content-type"] :- s/Str 38 | :body :instanceof java.nio.ByteBuffer)) 39 | ``` 40 | 41 | It's a little less typing, which might give you the extra time to add more checks, improving your testing. 42 | 43 | ## Valid operators 44 | 45 | Operator | Meaning 46 | ----------------------------------|------------------- 47 | `:=` or `:equals` | is equal to 48 | `:!=` or `:not-equals` | isn't equal to 49 | `:-` or `:conforms` | conforms to schema 50 | `:!-` or `:not-conforms` | doesn't conform to schema 51 | `:?` or `:satisfies` | satifies predicate 52 | `:!?` or `:not-satisfies` | doesn't satisfy predicate 53 | `:#` or `:matches` | matches regex 54 | `:!#` or `:not-matches` | doesn't match regex 55 | `:>` or `:⊃` or `:superset` | is superset of 56 | `:!>` or `:⊅` or `:not-superset` | isn't superset of 57 | `:<` or `:⊂` or `:subset` | is subset of 58 | `:!<` or `:⊄` or `:not-subset` | isn't subset of 59 | `:instanceof` or `:instance` | is instance of 60 | `:!instanceof` or `:not-instance` | isn't instance of 61 | 62 | ## Valid test clauses 63 | 64 | In each of these cases `v` represents the given value. 65 | 66 | Test clause | Meaning 67 | ---------------|------------------ 68 | Keyword | Use the keyword as a function: `(:kw v)` 69 | String | Call `get` on the value with the string: `(get v "x")` 70 | Long | Cast the value to a vector, then look up the index with `get`: `(get (vec v) 5)` 71 | Function | Call the function on the value: `(count v)` 72 | 73 | ## Paths 74 | 75 | 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 and numbers as well as keywords and functions. A (contrived) example: 76 | 77 | ```clojure 78 | (deftest contrived-test 79 | (given {:a {"b" '({} {} {:x 1})}} 80 | [:a "b" 2] := {:x 1} 81 | [:a "b" count] := 3)) 82 | ``` 83 | 84 | ## Installation 85 | 86 | Add the following dependency to your `project.clj` file 87 | 88 | ```clojure 89 | [juxt/iota "0.2.3"] 90 | ``` 91 | 92 | ## Copyright & License 93 | 94 | The MIT License (MIT) 95 | 96 | Copyright © 2015 JUXT LTD. 97 | 98 | 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: 99 | 100 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 101 | 102 | 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. 103 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright © 2015, JUXT LTD. 2 | 3 | (defproject juxt/iota "0.2.3" 4 | :description "Infix operators for test assertions" 5 | :url "http://github.com/juxt/iota" 6 | :license {:name "The MIT License" 7 | :url "http://opensource.org/licenses/MIT"} 8 | 9 | :dependencies [[prismatic/schema "0.4.2"]] 10 | 11 | :profiles {:dev {:dependencies [[org.clojure/clojure "1.7.0"] 12 | [org.clojure/clojurescript "1.7.48"]] 13 | :plugins [[lein-cljsbuild "1.1.0"] 14 | [lein-doo "0.1.4"]]}} 15 | :cljsbuild { 16 | :builds {:test {:source-paths ["src" "test"] 17 | :compiler {:output-to "resources/public/js/testable.js" 18 | :main 'juxt.runner 19 | :optimizations :none}} 20 | :node-test {:source-paths ["src" "test"] 21 | :compiler {:output-to "target/testable.js" 22 | :output-dir "target" 23 | :main 'juxt.runner 24 | :optimizations :none 25 | :hashbang false 26 | :target :nodejs}}}}) 27 | -------------------------------------------------------------------------------- /src/juxt/iota.cljc: -------------------------------------------------------------------------------- 1 | ;; Copyright © 2015, JUXT LTD. 2 | 3 | (ns juxt.iota 4 | (:require 5 | #?(:clj [clojure.test] 6 | :cljs [cljs.test :include-macros true]) 7 | [clojure.set :as set] 8 | [schema.core :as s])) 9 | 10 | ;; See the test at the end of this ns to understand the point of this code 11 | 12 | (defprotocol TestClause 13 | (as-test-function [_] "Take the clause and return a function which is applied to the value under test")) 14 | 15 | (extend-protocol TestClause 16 | #?(:clj clojure.lang.APersistentVector 17 | :cljs PersistentVector) 18 | ;; A function application 'path', which is simply a composed function, 19 | ;; left-to-right rather than right-to-left. 20 | (as-test-function [v] (apply comp (map as-test-function (reverse v)))) 21 | 22 | #?(:clj clojure.lang.Keyword 23 | :cljs Keyword) 24 | (as-test-function [k] k) 25 | 26 | #?(:clj clojure.lang.Fn 27 | :cljs function) 28 | (as-test-function [f] f) 29 | 30 | #?(:clj String 31 | :cljs string) 32 | (as-test-function [s] #(get % s)) 33 | 34 | #?(:clj Long 35 | :cljs long) 36 | (as-test-function [n] #(get (vec %) n))) 37 | 38 | (defn- cljs-env? 39 | "Take the &env from a macro, and tell whether we are expanding into cljs." 40 | [env] 41 | (boolean (:ns env))) 42 | 43 | (defmacro if-cljs 44 | "Return then if we are generating cljs code and else for Clojure code. 45 | https://groups.google.com/d/msg/clojurescript/iBY5HaQda4A/w1lAQi9_AwsJ" 46 | [then else] 47 | (if (cljs-env? &env) then else)) 48 | 49 | (defmacro is [& args] 50 | `(if-cljs 51 | (cljs.test/is ~@args) 52 | (clojure.test/is ~@args))) 53 | 54 | (defmacro given "Given v, assert the following…" 55 | {:style/indent 1} 56 | [v & body] 57 | (let [t (gensym)] 58 | `(do 59 | (let [~t ~v] 60 | ~@(for [[a b c] (partition 3 body)] 61 | (case b 62 | ;; Equals? 63 | (:= :equals) `(is (= ((as-test-function ~a) ~t) ~c)) 64 | (:!= :not-equals) `(is (not= ((as-test-function ~a) ~t) ~c)) 65 | 66 | ;; Schema checks 67 | (:- :conforms) `(is (nil? (s/check ~c ((as-test-function ~a) ~t)))) 68 | (:!- :not-conforms) `(is (not (nil? (s/check ~c ((as-test-function ~a) ~t))))) 69 | 70 | ;; Is? 71 | (:? :satisfies) `(is (~c ((as-test-function ~a) ~t))) 72 | (:!? :not-satisfies) `(is (not (~c ((as-test-function ~a) ~t)))) 73 | 74 | ;; Matches regex? 75 | (:# :matches) `(is (re-matches (re-pattern ~c) ((as-test-function ~a) ~t))) 76 | (:!# :not-matches) `(is (not (re-matches (re-pattern ~c) ((as-test-function ~a) ~t)))) 77 | 78 | ;; Is superset? 79 | (:> :⊃ :superset) `(is (set/superset? (set ((as-test-function ~a) ~t)) (set ~c))) 80 | (:!> :⊅ :not-superset) `(is (not (set/superset? (set ((as-test-function ~a) ~t)) (set ~c)))) 81 | 82 | ;; Is subset? 83 | (:< :⊂ :subset) `(is (set/subset? (set ((as-test-function ~a) ~t)) (set ~c))) 84 | (:!< :⊄ :not-subset) `(is (not (set/subset? (set ((as-test-function ~a) ~t)) (set ~c)))) 85 | 86 | ;; Is an instance of 87 | (:instanceof :instance) `(is (instance? ~c ((as-test-function ~a) ~t))) 88 | (:!instanceof :not-instance) `(is (not (instance? ~c ((as-test-function ~a) ~t)))) 89 | 90 | ;; Every satisfies predicate 91 | (:∀ :every) `(is (every? ~c ((as-test-function ~a) ~t))) 92 | (:!∀ :not-every) `(is (not (every? ~c ((as-test-function ~a) ~t)))) 93 | 94 | )))))) 95 | -------------------------------------------------------------------------------- /test/juxt/iota_test.cljc: -------------------------------------------------------------------------------- 1 | ;; Copyright © 2015, JUXT LTD. 2 | 3 | (ns juxt.iota-test 4 | (:require 5 | [juxt.iota #?(:clj :refer :cljs :refer-macros) [given]] 6 | #?(:clj [clojure.test :refer [deftest]] 7 | :cljs [cljs.test :refer-macros [deftest]]) 8 | [schema.core :as s])) 9 | 10 | (deftest myself 11 | (given {:foo "foo" :bar "bar"} 12 | identity := {:foo "foo" :bar "bar"} 13 | identity :- {:foo s/Str :bar s/Str} 14 | :foo := "foo" 15 | :foo :!= "bar" 16 | :foo :? string? 17 | count := 2 18 | count :!= 3 19 | [:foo count] := (count "foo") 20 | [:foo count] :!= 10 21 | :foo :# "fo+" 22 | :foo :!# "fo+d" 23 | identity :> {:foo "foo"} 24 | identity :< {:foo "foo" :bar "bar" :zip "zip"} 25 | ) 26 | (given [1 2 3] 27 | first := 1 28 | identity :- [s/Num] 29 | identity :< [1 2 3 4] 30 | identity :!< [1 3 4] 31 | identity :> [1 2] 32 | identity :!> [1 4] 33 | count := 3 34 | count :- s/Num 35 | ) 36 | ) 37 | -------------------------------------------------------------------------------- /test/juxt/runner.cljs: -------------------------------------------------------------------------------- 1 | (ns juxt.runner 2 | (:require [doo.runner :refer-macros [doo-tests]] 3 | [juxt.iota-test])) 4 | 5 | (doo-tests 'juxt.iota-test) 6 | --------------------------------------------------------------------------------