• Stars
    star
    426
  • Rank 101,205 (Top 3 %)
  • Language
    Ruby
  • License
    Other
  • Created almost 16 years ago
  • Updated almost 2 years ago

Reviews

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

Repository Details

a small RSpec clone

Bacon – small RSpec clone.¶ ↑

"Truth will sooner come out from error than from confusion."
                                            ---Francis Bacon

Bacon is a small RSpec clone weighing less than 350 LoC but nevertheless providing all essential features.

Whirl-wind tour¶ ↑

require 'bacon'

describe 'A new array' do
  before do
    @ary = Array.new
  end

  it 'should be empty' do
    @ary.should.be.empty
    @ary.should.not.include 1
  end

  it 'should have zero size' do
    @ary.size.should.equal 0
    @ary.size.should.be.close 0.1, 0.5
  end

  it 'should raise on trying fetch any index' do
    lambda { @ary.fetch 0 }.
      should.raise(IndexError).
      message.should.match(/out of array/)

    # Alternatively:
    should.raise(IndexError) { @ary.fetch 0 }
  end

  it 'should have an object identity' do
    @ary.should.not.be.same_as Array.new
  end

  # Custom assertions are trivial to do, they are lambdas returning a
  # boolean vale:
  palindrome = lambda { |obj| obj == obj.reverse }
  it 'should be a palindrome' do
    @ary.should.be.a palindrome
  end

  it 'should have super powers' do
    should.flunk "no super powers found"
  end
end

Now run it:

$ bacon whirlwind.rb
A new array
- should be empty
- should have zero size
- should raise on trying fetch any index
- should have an object identity
- should be a palindrome
- should have super powers [FAILED]

Bacon::Error: no super powers found
	./whirlwind.rb:39: A new array - should have super powers
	./whirlwind.rb:38
	./whirlwind.rb:3

6 specifications (9 requirements), 1 failures, 0 errors

If you want shorter output, use the Test::Unit format:

$ bacon -q whirlwind.rb
.....F
Bacon::Error: no super powers found
	./whirlwind.rb:39: A new array - should have super powers
	./whirlwind.rb:38
	./whirlwind.rb:3

6 tests, 9 assertions, 1 failures, 0 errors

It also supports TAP:

$ bacon -p whirlwind.rb
ok 1        - should be empty
ok 2        - should have zero size
ok 3        - should raise on trying fetch any index
ok 4        - should have an object identity
ok 5        - should be a palindrome
not ok 6    - should have super powers: FAILED
# Bacon::Error: no super powers found
# 	./whirlwind.rb:39: A new array - should have super powers
# 	./whirlwind.rb:38
# 	./whirlwind.rb:3
1..6
# 6 tests, 9 assertions, 1 failures, 0 errors

$ bacon -p whirlwind.rb | taptap -q
Tests took 0.00 seconds.
FAILED tests 6
   6) should have super powers: FAILED

Failed 1/6 tests, 83.33% okay.

(taptap is available from chneukirchen.org/repos/taptap/)

As of Bacon 1.1, it also supports Knock:

$ bacon -k whirlwind.rb
ok - should be empty
ok - should have zero size
ok - should raise on trying fetch any index
ok - should have an object identity
ok - should be a palindrome
not ok - should have super powers: FAILED
# Bacon::Error: no super powers found
# 	./whirlwind.rb:39: A new array - should have super powers
# 	./whirlwind.rb:38
# 	./whirlwind.rb:3

$ bacon -k whirlwind.rb | kn-sum
6 tests, 1 failed (83.3333% succeeded)

(knock is available from github.com/chneukirchen/knock/)

Implemented assertions¶ ↑

  • should.<predicate> and should.be.<predicate>

  • should.equal

  • should.match

  • should.be.identical_to / should.be.same_as

  • should.raise(*exceptions) { }

  • should.change { }

  • should.throw(symbol) { }

  • should.satisfy { |object| }

Added core predicates¶ ↑

  • Object#true?

  • Object#false?

  • Proc#change?

  • Proc#raise?

  • Proc#throw?

  • Numeric#close?

before/after¶ ↑

before and after need to be defined before the first specification in a context and are run before and after each specification.

As of Bacon 1.1, before and after do nest in nested contexts.

Shared contexts¶ ↑

You can define shared contexts in Bacon like this:

shared "an empty container" do
  it "should have size zero" do
  end

  it "should be empty" do
  end
end

context "A new array" do
  behaves_like "an empty container"
end

These contexts are not executed on their own, but can be included with behaves_like in other contexts. You can use shared contexts to structure suites with many recurring specifications.

Matchers¶ ↑

Custom matchers are simply lambdas returning a boolean value, for example:

def shorter_than(max_size)
  lambda { |obj| obj.size < max_size }
end

[1,2,3].should.be shorter_than(5)

You can use modules and extend to group matchers for use in multiple contexts.

bacon standalone runner¶ ↑

-s, --specdox            do AgileDox-like output (default)
-q, --quiet              do Test::Unit-like non-verbose output
-p, --tap                do TAP (Test Anything Protocol) output
-k, --knock              do Knock output
-o, --output FORMAT      do FORMAT (SpecDox/TestUnit/Tap) output
-Q, --no-backtrace       don't print backtraces  
-a, --automatic          gather tests from ./test/, include ./lib/
-n, --name NAME          runs tests matching regexp NAME
-t, --testcase TESTCASE  runs tests in TestCases matching regexp TESTCASE

If you don’t want to use the standalone runner, run Bacon.summary_on_exit to install an exit handler showing the summary.

Object#should¶ ↑

You can use Object#should outside of contexts, where the result of assertion will be returned as a boolean. This is nice for demonstrations, quick checks and doctest tests.

>> require 'bacon'
>> (1 + 1).should.equal 2
=> true
>> (6*9).should.equal 42
=> false

Bacon with autotest¶ ↑

Since version 1.0, there is autotest support. You need to tag your test directories (test/ or spec/) by creating an .bacon file there. Autotest then will find it. bin/bacon needs to be in PATH or RUBYPATH.

Converting specs¶ ↑

spec-converter is a simple tool to convert test-unit or dust style tests to test/spec specs.

It can be found at github.com/relevance/spec_converter.

Thanks to¶ ↑

  • Michael Fellinger, for fixing Bacon for 1.9 and various improvements.

  • Gabriele Renzi, for implementing Context#should.

  • James Tucker, for the autotest support.

  • Yossef Mendelssohn, for nested contexts.

  • everyone contributing bug fixes.

History¶ ↑

  • January 7, 2008: First public release 0.9.

  • July 6th, 2008: Second public release 1.0.

    • Add Context#should as a shortcut for Context#it(‘should ’ + _).

    • describe now supports multiple arguments for better organization.

    • Empty specifications are now erroneous.

    • after-blocks run in the case of exceptions too.

    • Autotest support.

    • Bug fixes.

  • November 30th, 2008: Third public release 1.1.

    • Nested before/after.

    • Add -Q/–no-backtraces to not show details about failed specifications.

    • Add Knock output.

    • Bug fixes.

  • December 21th, 2012: Fourth public release 1.2.0.

    • #satisfy will not pass arguments anymore to the block, use lexical scope.

    • Fixed Context#change?.

    • Add support for finding specs named like spec/*/_spec.rb.

    • Contexts nest properly.

    • Timer in TestUnitOutput.

    • Nested output for SpecDoxOutput.

    • Small cleanups and more tests.

Contact¶ ↑

Please mail bugs, suggestions and patches to <[email protected]>.

Git repository (patches rebased on HEAD are most welcome): github.com/chneukirchen/bacon git://github.com/chneukirchen/bacon.git

Copying¶ ↑

Copyright © 2007, 2008, 2012 Christian Neukirchen <purl.org/net/chneukirchen>

Bacon is freely distributable under the terms of an MIT-style license. See COPYING or www.opensource.org/licenses/mit-license.php.

Behavior-Driven Development

<behaviour-driven.org/>

RSpec

<rspec.rubyforge.org/>

test/spec

<test-spec.rubyforge.org/>

Christian Neukirchen

<chneukirchen.org/>

More Repositories

1

nq

Unix command line queue utility
C
2,791
star
2

styleguide

443
star
3

mblaze

Unix utilities to deal with Maildir
C
441
star
4

cwm

portable version of OpenBSD's cwm(1) window manager
C
343
star
5

hrmpf

hrmpf rescue system, built on Void Linux
Shell
294
star
6

snooze

run a command at a particular time
C
191
star
7

xe

simple xargs and apply replacement
C
176
star
8

dosfetch

NeoFetch clone for DOS
Pascal
140
star
9

lr

list files, recursively
C
139
star
10

xtools

a few helpers for working with XBPS
Shell
123
star
11

extrace

trace exec() calls system-wide
C
116
star
12

rum

a gRand Unified Mapper for Rack
Ruby
106
star
13

redo-c

An implementation of the redo build system in portable C with zero dependencies
C
101
star
14

outils

port of some non-standard OpenBSD tools to Linux
C
76
star
15

gitsum

basic darcsum feelalike for Git
Emacs Lisp
67
star
16

ignite

OBSOLETE: use Void Linux
Shell
65
star
17

sabotage

an experimental distribution based on musl libc and busybox
Shell
63
star
18

rdumpfs

a rsync-based dump file system backup tool
Shell
59
star
19

leahutils

description of leahutils
53
star
20

obase

a port of the OpenBSD userland to Linux | unmaintained: use outils
C
51
star
21

colfm

A console, column-oriented file manager
Ruby
46
star
22

trivium

Trivium, my minimalist blogging engine
Ruby
39
star
23

fail

crash in various possible ways
C
38
star
24

rwc

report when files change
C
37
star
25

rps

Ruby Packaging Standard
Ruby
30
star
26

sq

a 7x15 pixel font inspired by Codec and Quadraat Sans Mono
Makefile
30
star
27

knock

Knock is a simplification of the Test Anything Protocol used by Perl and others.
Ruby
29
star
28

virtualrb

Virtualize Ruby installations
Ruby
25
star
29

te

tiny emacs
C
23
star
30

challis

a soft lightweight cloth
Ruby
22
star
31

nb

Nota Bene, a quick note-taking tool for Emacs
Emacs Lisp
22
star
32

tools

various simple tools, not worth a project on their own
Ruby
19
star
33

notyet

a text-based recursive task tracker
Ruby
19
star
34

5x13

a condensed pixel font built on 6x13 ("fixed")
Makefile
18
star
35

wibget

WibGet, a minimalist, but convenient Git web frontend
Ruby
17
star
36

git-merge-pr

apply GitHub pull request from command-line
Shell
17
star
37

libste

C string library based on string ends
C
17
star
38

vuxi

a minimalist static Wiki compiler
Ruby
15
star
39

rdd

random data dumper
C
15
star
40

qed-caltech

C
15
star
41

yam

Yam, a functional language running as JavaScript
JavaScript
14
star
42

reap

run process until all its spawned processes are dead
C
14
star
43

necho

minimal, sensible alternatives to echo(1)
C
14
star
44

rup

a sane and simple Ruby package manager
14
star
45

hyx

terminal hex editor
C
13
star
46

signify

portable version of OpenBSD's signify(1) signature tool
C
13
star
47

coma

a console mail user agent | obsolete: use mblaze
Ruby
13
star
48

lywsd03mmc-exporter

a Prometheus exporter for the LYWSD03MMC BLE thermometer
Go
12
star
49

coset-mirror

(experimental) Mirror of the coset darcs repository
Ruby
12
star
50

tt

a 9term-compatible terminal in Ruby/Tk
Ruby
12
star
51

hittpd

efficient, no-frills HTTP 1.1 server
C
11
star
52

daiquiri

a Rack-based resourceful web framework
11
star
53

wcal

ISO weekly calendar
C
11
star
54

px

search for processes and print top(1)-like status
C
11
star
55

rs

rs(1) — reshape a data array (from OpenBSD) | unmaintained: use outils
C
11
star
56

rack-mirror

OUTDATED mirror of Rack's darcs repository, use github.com/chneukirchen/rack
Ruby
10
star
57

amok

a compact mock library
Ruby
9
star
58

rubyports

Hookin-based RubyPorts system
Ruby
9
star
59

xlossage

display pressed keys in X11 in a readable way
C
8
star
60

sgn

pseudonymous digital signatures
Emacs Lisp
7
star
61

ji

a minimalist forum software
Ruby
7
star
62

vmenu

personal fork of vis-menu/slmenu
C
7
star
63

revisit

a TODO list for the future
Ruby
7
star
64

clitter

a ncurses twitter client
Ruby
7
star
65

schell

a lispy shell scripting language
Scheme
7
star
66

arr

(re)arrange and select fields on each line
Groff
7
star
67

rnl

remove trailing newlines
Shell
7
star
68

htping

periodically send HTTP requests
Go
7
star
69

netpbm-mirror

git-svn mirror of netpbm (updated manually)
C
6
star
70

mlog

merge log files by timestamp
C
6
star
71

listening

check if a TCP server is listening
C
5
star
72

atxec

run command expanding arguments from file or environment
Perl
5
star
73

adventofcode2019

Advent of Code (adventofcode.com) in k and Perl 5
Perl
5
star
74

musl-chris2

my sandbox for playing with musl
C
5
star
75

lrep

literate read-eval-print
Ruby
5
star
76

rexample

Ruby
4
star
77

tap3

check output/error/status of a command against a specification
Perl
4
star
78

btac

print file in reverse order
C
4
star
79

rmeta

a OMeta implementation for Ruby
4
star
80

sson

S-Expression Standard Object Notation
Ruby
4
star
81

adventofcode2015

Advent of Code (adventofcode.com) in K
Ruby
4
star
82

noir

a new programming language
JavaScript
4
star
83

adventofcode2022

Advent of Code 2022 in Racket and Zig.
Racket
3
star
84

dwm-chris2

Personal dwm fork.
C
3
star
85

holes

find runs of zero bytes
C
3
star
86

adventofcode2020

Advent of Code 2020 (adventofcode.com) in J and Clojure
Clojure
3
star
87

parseopt

a collection of command line argument parsers
Shell
3
star
88

rc

C
3
star
89

xdu

display the output of "du" in an X window
C
3
star
90

pds

parallel data substitution
Ruby
3
star
91

gleam-codespace

A codespace to work with Gleam
Dockerfile
2
star
92

actions-archive-all

generate tarballs including submodules from GitHub Actions
Shell
2
star
93

docker-lab-bgp

A small BGP lab in Docker
2
star
94

literate-janet

Makefile
2
star
95

adventofcode2021

Advent of Code 2021 (adventofcode.com) in BQN and Clojure
Clojure
2
star
96

adventofcode2017

adventofcode.com 2017
C++
2
star
97

conference-sponsors

Conference Sponsor tracking
2
star
98

systas

Systas Scheme (archival copy)
C
2
star
99

adventofcode2016

Advent of Code 2016 (adventofcode.com) in K and J
Ruby
2
star
100

metaid

metaid SVN checkout as of 2006-07-06
Ruby
2
star