• Stars
    star
    199
  • Rank 194,919 (Top 4 %)
  • Language
    Elixir
  • Created over 11 years ago
  • Updated about 7 years ago

Reviews

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

Repository Details

A polite, well mannered and thoroughly upstanding testing framework for Elixir

Amrita

Build Status

A polite, well mannered and thoroughly upstanding testing framework for Elixir.

Elixir of life

Install

Add to your mix.exs

  defp deps do
    [
      {:amrita, "~>0.4", github: "josephwilk/amrita"}
    ]
  end

After adding Amrita as a dependency, to install please run:

mix deps.get

Getting started

Ensure you start Amrita in: test/test_helper.exs

Amrita.start

#Or if you want a more documentation focused formatter:

Amrita.start(formatter: Amrita.Formatter.Documentation)
  • Require test_helper.exs in every test (this will ensure Amrita is started):
  • Mix in Amrita.Sweet which will bring in everything you need to use Amrita:
Code.require_file "../test_helper.exs", __ENV__.file

defmodule ExampleFacts do
  use Amrita.Sweet

  fact "addition" do
    1 + 1 |> 2
  end
end

Run your tests through mix:

$ mix amrita # Run all your tests

$ mix amrita test/integration/t_mocks.ex # Run a specific file

$ mix amrita test/integration/t_mocks.ex:10 # Run a specific test at a line number

$ mix amrita --trace # Show execution time for slow tests

Now time to write some tests!

Prerequisites / Mocks

Amrita supports BDD style mocks.

Examples:

defmodule Polite do
  def swear? do
    false
  end

  def swear?(word) do
    false
  end
end

A Simple mock

fact "mock with a wildcard" do
  provided [Polite.swear? |> true] do
    Polite.swear? |> truthy
  end
end

Wildcard matchers for argument

fact "mock with a wildcard"
  provided [Polite.swear?(_) |> true] do
    Polite.swear?(:yes) |> truthy
    Polite.swear?(:whatever) |> truthy
  end
end

Powerful custom predicates for argument matching

fact "mock with a matcher function" do
  provided [Polite.swear?(fn arg -> arg =~ ~r"moo") |> false] do
    Polite.swear?("its ok to moo really") |> falsey
  end
end

Return values based on specific argument values

fact "mock with return based on argument" do
  provided [Polite.swear?(:pants) |> false,
            Polite.swear?(:bugger) |> true] do

    Funk.swear?(:pants) |> falsey
    Funk.swear?(:bugger) |> truthy
  end
end

Polite Errors explaining when things went wrong

Polite mock error message

Checkers

Amrita is also all about checker based testing!

Code.require_file "../test_helper.exs", __ENV__.file

defmodule ExampleFacts do
  use Amrita.Sweet

  facts "about Amrita checkers" do
    
    fact "`equals` checks equality" do
      1 - 10 |> equals -9      
      
      # For convience the default checker is equals
      # So we can write the above as
      1 - 10 |> -9
            
      # Pattern matching with tuples
      { 1, 2, { 3, 4 } } |> equals {1, _, { _, 4 } }

      # Which is the same as 
      { 1, 2, { 3, 4 } } |> {1, _, { _, 4 } }
    end

    fact "contains checks if an element is in a collection" do
      [1, 2, 4, 5] |> contains 4

      {6, 7, 8, 9} |> contains 9

      [a: 1, :b 2] |> contains {:a, 1}
    end

    fact "! negates a checker" do
      [1, 2, 3, 4] |> !contains 9999

     # or you can add a space, like this. Whatever tickles your fancy.

      [1, 2, 3, 4] |> ! contains 9999

      10 |> ! equal 11
    end

    fact "contains works with strings" do
      "mad hatters tea party" |> contains "hatters"

      "mad hatter tea party" |> contains ~r"h(\w+)er"
    end

    fact "has_prefix checks if the start of a collection matches" do
      [1, 2, 3, 4] |> has_prefix [1, 2]

      {1, 2, 3, 4} |> has_prefix {1, 2}

      "I cannot explain myself for I am not myself" |> has_prefix "I"
    end

    fact "has_prefix with a Set ignores the order" do
      {1, 2, 3, 4} |> has_prefix Set.new([{2, 1}])
    end

    fact "has_suffix checks if the end of a collection matches" do
      [1, 2, 3, 4 ,5] |> has_suffix [4, 5]

      {1, 2, 3, 4} |> has_suffix {3, 4}

      "I cannot explain myself for I am not myself" |> has_suffix "myself"
    end

    fact "has_suffix with a Set ignores the order" do
      {1, 2, 3, 4} |> has_suffix Set.new([{4, 3}])
    end

    fact "for_all checks if a predicate holds for all elements" do
      [2, 4, 6, 8] |> for_all even(&1)

      # or alternatively you could write

      [2, 4, 6, 8] |> Enum.all? even(&1)
    end

    fact "odd checks if a number is, well odd" do
      1 |> odd
    end

    fact "even checks is a number if even" do
      2 |> even
    end

    fact "roughly checks if a float within some +-delta matches" do
      0.1001 |> roughly 0.1
    end

    fact "falsey checks if expression evalulates to false" do
      nil |> falsey
    end

    fact "truthy checks if expression evaulates to true" do
      "" |> truthy
    end

    defexception Boom, message: "Golly gosh"

    fact "raises checks if an exception was raised" do
      fn -> raise Boom end |> raises ExampleFacts.Boom
    end
  end

  future_fact "I'm not run yet, just printed as a reminder. Like a TODO" do
    # Never run
    false |> truthy
  end

  fact "a fact without a body is much like a TODO"

  # Backwards compatible with ExUnit
  test "arithmetic" do
    assert 1 + 1 == 2
  end

end

Assertion Syntax with |>

The syntax for assertions is as follows:

# Equality check
ACTUAL |> [EXPECTED]
# Not equal check
ACTUAL |> ! [EXPECTED]

# Using a checker function
ACTUAL |> CHECKER [EXPECTED]
# or negative form
ACTUAL |> !CHECKER [EXPECTED]

##Custom checkers

Its simple to create your own checkers:

  defchecker a_thousand(actual) do
    rem(actual, 1000) |> equals 0
  end

  fact "about 1000s" do
    1000 |> a_thousand   # true
    1200 |> ! a_thousand # true
  end

Polite error messages:

Amrita tries its best to be polite with its errors:

Polite error message

Amrita with Dynamo

Checkout an example using Amrita with Dynamo: https://github.com/elixir-amrita/amrita_with_dynamo

Plugins

See the wiki for various IDE plugins for Amrita: https://github.com/josephwilk/amrita/wiki/Plugins

Amrita Development

Hacking on Amrita.

###Running tests

Amrita runs tests against Elixir's latest stable release and against Elixir master. Make is your friend for running these tests:

# Run lastest stable and elixir master
make ci

# Run tests against your current Elixir install
make

Docs

http://josephwilk.github.io/amrita/docs

Bloody good show

Thanks for reading me, I appreciate it.

Have a good day.

Maybe drink some tea.

Its good for the constitution.

Tea

##License (The MIT License)

Copyright (c) 2014-2016 Joseph Wilk

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

semanticpy

A collection of semantic functions for python - including Latent Semantic Analysis(LSA)
Python
159
star
2

rsemantic

A document vector search with flexible matrix transforms. Currently supports Latent semantic analysis and Term frequency - inverse document frequency
Ruby
146
star
3

image-resizer

Resize/Crop/Rotate/Pad images in Clojure without any native install. Oh and do it Fast.
Clojure
135
star
4

musical-creativity

Models of Musical Creativity (in Clojure)
Clojure
131
star
5

circuit-breaker

Circuit breaker for Clojure
Clojure
129
star
6

tlearn-rb

Recurrent Neural Network library for Ruby
C
96
star
7

pairwise

Ruby based tool for selecting a smaller number of test input combinations (using pairwise generation) rather than exhaustively testing all possible permutations.
Ruby
84
star
8

shaderview

Light show for live coding
GLSL
75
star
9

strangeloop2014

Come with us now on a journey ♪
Clojure
64
star
10

mud

MUD is a layer over Overtone to make live composition more powerful and immediate.
Clojure
60
star
11

hystrix-event-stream-clj

Easy way to output a Hystrix event stream in Clojure.
Clojure
55
star
12

functions-as-patterns

Exploring patterns as a means of understanding and documenting functions.
Clojure
46
star
13

japanese-mosaic-logic-puzzle

Problem for Programming challenge October
Ruby
42
star
14

rspec-rr

Helping Rspec and Rspec-rails play nicely with RR the test double framework
Ruby
27
star
15

stereotype

A fixture library for setting up test data in Clojure
Clojure
26
star
16

pronounce

Find how to pronounce words by breaking them up into their phones.
Ruby
24
star
17

overtone.device.launchpad

Using Launchpad with Overtone and Clojure
Clojure
23
star
18

overtone.orchestra

Orchestra for Overtone
Clojure
21
star
19

overtone.synths

A collection of synths
Clojure
17
star
20

closure-bricks.el

What would scheme bricks look like in emacs...
Emacs Lisp
13
star
21

cucumber_cocktails

A collection of my Cucumber utils
Ruby
10
star
22

finger-smudge

Music for Machines. Exploring the smudges of fingerprinting digital music.
Clojure
10
star
23

iwfms

A Intelligent Workflow Management System using Prolog and PHP that uses artificial intelligence planning methodologies and Event Calculus workflow specifications.
PHP
9
star
24

shader-pi

Interactive Shaders for SonicPi
Ruby
8
star
25

ofxEmacsEditor

Emacs editor for openframeworks
C++
8
star
26

overtone.device.push

Using Ableton push with Overtone and Clojure
8
star
27

creative-machine

Experiments in creative AI
Ruby
8
star
28

synthatron

Generator of synths for use with Supercollider. Evolving synths for evil or good.
Clojure
7
star
29

midje-junit-formatter

Junit Formatter for Midje
Clojure
6
star
30

irobat

The Iphone and Cucumber sitting in a tree K-I-S-S-I-N-G. EXPERIMENTAL!
Ruby
6
star
31

qscintilla

QScintilla version (2.9/2.10) that introduces transparent background but not text.
C++
6
star
32

rr-tmbundle

RR (Double Ruby) test double framework - TextMate bundle
5
star
33

nicos-ip

Corrupted Sonic Pi. Highly dangerous. May infect thought.
Ruby
4
star
34

presentations

Joseph Wilk's Presentations
4
star
35

the-nature-of-sound

♫ Clojure library for exploring the nature of sounds. May contain large traces of C++.
C
4
star
36

josephwilk.github.io

A place for pretty pictures and noises without detailed explanations.
HTML
3
star
37

fake_execution

Fake out execution in Ruby and test what was executed
Ruby
3
star
38

patterns

Collection of patterns from workshops
3
star
39

codemash2014

Making noise, that is definitely sound and might also scrap by as music
Clojure
3
star
40

fluxtone

An experiment looking at an OSC layer for Overtone <-> Fluxus
Clojure
3
star
41

software-craftsmanship-katas

Katas and source code. Experiments with different styles of coding and testing.
Ruby
2
star
42

dotfiles

Joseph Wilk's dotfiles
Shell
2
star
43

aocny

Ruby
1
star
44

vim-cucumber

Vim Runtime files for Cucumber features
Vim Script
1
star
45

midi-mash

Do the midi mash.
Clojure
1
star
46

overtone.device.monome

Clojure Experiments with an infinite plane projected onto a Monome with movement x,y
Clojure
1
star
47

prometheus-clj

Clojure
1
star
48

alice

1
star
49

overtone-midi-bug

Investigating problem with midi devices freezing.
Clojure
1
star
50

code-sounds

Like code smells only louder
Clojure
1
star
51

stereotype-clj

stereotype-clj now lives here: https://github.com/josephwilk/stereotype
1
star
52

raptor_demo

Demo of raptor
Ruby
1
star
53

fujin

Clojure
1
star
54

sample-meta

Mining data on samples using sox/aubio
Clojure
1
star
55

fake-ns

1
star
56

clj-dirt

Bindings over the DIRT sample pack used in Tidal
1
star
57

clojure-horde3d

Experiments with Clojure and Horde3d Games/Graphics engine
Clojure
1
star
58

aoc

Ruby
1
star
59

macro_tools

Macro tools for Elixir
Elixir
1
star
60

poetic-computation

Exploring code as an art material
1
star
61

spa2011

Cucumber examples used in workshop at SPA2011
1
star