• Stars
    star
    150
  • Rank 247,323 (Top 5 %)
  • Language
    Clojure
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Infix operators for test assertions

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.

(require '[clojure.test :refer :all]
         '[schema.core :as s])

(deftest my-test
  (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 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.

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

(deftest my-test
  (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
:= or :equals is equal to
:!= or :not-equals isn't equal to
:- or :conforms conforms to schema
:!- or :not-conforms doesn't conform to schema
:? or :satisfies satifies predicate
:!? or :not-satisfies doesn't satisfy predicate
:# or :matches matches regex
:!# or :not-matches doesn't match regex
:> or :⊃ or :superset is superset of
:!> or :⊅ or :not-superset isn't superset of
:< or :⊂ or :subset is subset of
:!< or :⊄ or :not-subset isn't subset of
:instanceof or :instance is instance of
:!instanceof or :not-instance isn't instance of

Valid test clauses

In each of these cases v represents the given value.

Test clause Meaning
Keyword Use the keyword as a function: (:kw v)
String Call get on the value with the string: (get v "x")
Long Cast the value to a vector, then look up the index with get: (get (vec v) 5)
Function Call the function on the value: (count v)

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 and numbers as well as keywords and functions. A (contrived) example:

(deftest contrived-test
  (given {:a {"b" '({} {} {:x 1})}}
    [:a "b" 2] := {:x 1}
    [:a "b" count] := 3))

Installation

Add the following dependency to your project.clj file

[juxt/iota "0.2.3"]

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.

More Repositories

1

bidi

Bidirectional URI routing
Clojure
991
star
2

aero

A small library for explicit, intentful configuration.
Clojure
743
star
3

yada

A powerful Clojure web library, full HTTP, full async - see https://juxt.pro/yada/index.html
HTML
735
star
4

tick

Time as a value.
Clojure
599
star
5

edge

A Clojure application foundation from JUXT
Clojure
503
star
6

joplin

Flexible datastore migration and seeding for Clojure projects
Clojure
316
star
7

juxt-accounting

Double-entry accounting software written in Clojure with Datomic.
Clojure
281
star
8

pack.alpha

Package clojure projects
Java
258
star
9

mach

A remake of make (in ClojureScript)
Clojure
246
star
10

jig

Jig is an application harness providing a beautifully interactive development experience for Clojure projects.
Clojure
229
star
11

clip

Light structure and support for dependency injection
Clojure
228
star
12

site

A web and API server, powered by xtdb.com
Clojure
135
star
13

modular

JavaScript
128
star
14

apex

A compendium of Clojure libraries for implementing web backends.
Clojure
123
star
15

bolt

An integrated security system for applications built on component
Clojure
123
star
16

jinx

jinx is not xml-schema (it's json-schema!)
Clojure
99
star
17

pull

Trees from tables
Clojure
96
star
18

roll

AWS Blue/Green deployment using Clojure flavoured devops
Clojure
77
star
19

reap

A Clojure library for decoding and encoding strings used by web protocols.
Clojure
74
star
20

dirwatch

A Clojure directory watcher, wrapping the JDK 7 java.nio.file.WatchService.
Clojure
67
star
21

skip

Skippy McSkipface - A general Clojure dependency tracker
Clojure
34
star
22

snap

Snapshot testing for Clojure and Clojurescript
Clojure
30
star
23

pack-datomic

Datomic Packer and Terraform setup
HCL
26
star
24

qcon2014

JavaScript
26
star
25

stoic

Marrying the Component Lifecycle pattern with distributed config.
Clojure
20
star
26

spin

Unbundling the web application-tier
Clojure
19
star
27

pick

A Clojure library for HTTP server-driven content negotiation.
Clojure
18
star
28

prop

Clojure
17
star
29

grab

A Clojure library for parsing and executing GraphQL queries.
Clojure
16
star
30

rest

A guide to coding a REST service
CSS
15
star
31

chatserver

Clojure
13
star
32

shop

The JUXT Shop - a sample application built on Crux
Clojure
11
star
33

fuzz

Example ClojureScript Project that happens to wrap Slack
Clojure
9
star
34

dotfiles

Recommended dotfiles used by JUXT
Emacs Lisp
9
star
35

ramp

Shell
8
star
36

card

Not sure what this is. A kind-of Zettelkasten?
TypeScript
8
star
37

chatclient

Clojure
8
star
38

hire

A technical interview test for potential employees
Clojure
7
star
39

rock

Hardened AMIs for Clojure deployments (Arch Linux)
Shell
7
star
40

clip-example

Example of using Clip
Clojure
7
star
41

datomic-extras

Clojure
7
star
42

lein-dockerstalk

Clojure
7
star
43

docker

Docker files
Makefile
7
star
44

plain-clojure-lambda

A recipe for building AWS Lambdas using plain clojure
Clojure
7
star
45

draw

A DSL for SVG diagrams
Clojure
6
star
46

trag

Graph transduction
Clojure
5
star
47

radar

JUXT Clojure Technology Radar
Clojure
5
star
48

pack-ek

HCL
4
star
49

adoc

A Clojure wrapper for asciidoctorj
Clojure
4
star
50

msf

Volunteer work for MSF
Clojure
4
star
51

astro-website

Source for the main JUXT website
JavaScript
3
star
52

vext

Clojure
3
star
53

azondi

MQTT goes reactive
CSS
3
star
54

pack-riemann

HCL
3
star
55

yada.cookbook

A cookbook of yada recipes
3
star
56

component-utils

Clojure
2
star
57

blip

Small library for fetching/injecting graphql definitions
Clojure
2
star
58

ping

Clojure
2
star
59

modular.co-dependency

Co-dependency support for com.stuartsierra.component
Clojure
2
star
60

http

2
star
61

lein-deploy-tar

A Leiningen plugin to upload tars to a Maven repository.
Clojure
2
star
62

aleph-issue

Testing aleph
Clojure
1
star
63

mksmarthack

A Clojure data hack on MK:Smart
Clojure
1
star
64

eventing-examples

Clojure
1
star
65

flow

Clojure
1
star
66

training-exercises-webapp

Training exercises
Clojure
1
star
67

example-code

Example code for a recent client course
Clojure
1
star
68

home-apps

Monorepo for SPAs that use home.juxt.site as a backend
TypeScript
1
star
69

sail

Clojure
1
star
70

clojars-mirrors

Re-releasing Clojars libraries onto Maven Central
Clojure
1
star
71

XT20.conf

Public materials for XT20
1
star
72

asciidoctor-stylesheet-factory

CSS
1
star