Giter VIP home page Giter VIP logo

iota's Introduction

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.

iota's People

Contributors

malcolmsparks avatar nberger avatar alexanderkiel avatar danielcompton avatar facundoolano avatar onetom avatar

Stargazers

Witoslaw Koczewski avatar chan avatar Logan Linn avatar Micah Elliott avatar Alberto Fernández avatar Dmytro Polivenok avatar Darong Mean avatar Andrew Foltz-Morrison avatar Andrejs Agejevs avatar Simon Gray avatar Dustin Getz avatar  avatar Mihael Konjević avatar Rafa Leblic avatar Ahmed Hassan avatar tristan avatar Misha Kozik avatar Oliver Martell Núñez avatar Simion Iulian Belea avatar LPW avatar Yu-Xi Lim avatar  avatar Benedek Fazekas avatar hodlbod avatar Gabriel Nau avatar Kiran Gangadharan avatar Łukasz Korecki avatar Vitaly Kornilov avatar Médéric Hurier (Fmind) avatar Ethan McCue avatar Jan Rychter avatar Daniyal Ansari avatar Ahmed avatar  avatar Josef Pospíšil avatar DiGiTAL_CuRSe avatar  avatar Santiago Gepigon III avatar Pawel Ceranka avatar Mark Woodhall avatar Rahuλ Dé avatar alex a avatar Hoàng Minh Thắng avatar Vasily Kolesnikov avatar  avatar Guido Schmidt avatar Brian Scaturro avatar Sergey Toropenko avatar Jiří Špaček avatar greg avatar Lisovskii Vladislav avatar Nikita Prokopov avatar  avatar Orestis Markou avatar David Duthie avatar Andrew Jones avatar  avatar hamlet avatar Kent OHASHI avatar Eloi avatar Eunmin Kim avatar  avatar Arnout Roemers avatar Dmitrii Balakhonskii avatar Anler avatar Dominik Bylica avatar Adam Frey avatar  avatar Brendan avatar Malte Nottmeyer avatar Lucas Wiener avatar Antares avatar Alexander Solovyov avatar ayato-p avatar Andrew Tropin avatar Michael Wong avatar Matus Lestan avatar Ben Griffiths avatar Juan A. Ruz avatar Marcin Bielak avatar Mike Haney avatar Roland Kaercher avatar Jay Martin avatar Federico Carrone avatar Matt Luongo avatar liberborn avatar Bahul Neel Upadhyaya avatar Andrey Subbotin avatar Orlin M Bozhinov avatar Hugo de Oliveira avatar Augusto Elesbão avatar kazuhiro hara avatar  avatar Luke Snape avatar Kenji Nakamura avatar Leonid Kovnatskiy avatar Angus H. avatar  avatar clojj avatar Christophe Mees avatar

Watchers

Ivan Willig avatar Abhik Khanra avatar Kenji Nakamura avatar Jon Pither avatar  avatar  avatar James Cloos avatar Boris Kourtoukov avatar  avatar  avatar

iota's Issues

Throw an error if there are unfinished expressions

I accidentally didn't complete my last row of expressions, but Iota didn't warn about this because it's using partition and they were dropped. What do you think about throwing an error on invalid forms without complete tests?

Improve error messages

Currently the error messages returned by iota have gensyms and other internals which makes it harder to see the real test failure. For example:

clojure.test

expected: ["45"]
  actual: #{"15" "30" "45"}

iota

expected: (clojure.core/=
           ((juxt.iota/as-test-function
             [:body :keen.query/response :workspace/duration-ids])
            G__53963)
           #{"45"})
  actual: (not (clojure.core/= #{"15" "30" "45"} #{"45"}))

It might also be interesting to be able to show multiple failures for a single given in a single message, similar to https://github.com/facundoolano/restpect#test-reporter.

Any order operator

Would be of interest to have something like :=~ for comparing vectors where the order doesn't matter? So [:a :b :c] :=~ [:b :a :c]?

support only one assertion in given

Currently,

    (given {:a 123} := {:a 123 :b 456}) ;; => nil

It could return false.

Note: maybe it could be a separate macro - this would make an eventual future specification easier.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.