Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added the basics of the DSL with minor tests.

  • Loading branch information...
commit 986b83a8b88a7cf8d07f640894d6a900cafb3b5d 1 parent 8f44acc
@ratnikov authored
View
6 Gemfile
@@ -0,0 +1,6 @@
+source :rubygems
+
+group :development do
+ gem 'test-unit', '2.0.9'
+ gem 'rr', :git => 'http://github.com/btakita/rr.git'
+end
View
10 Rakefile
@@ -0,0 +1,10 @@
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |test|
+ test.test_files = FileList["test/**/*_test.rb"]
+ test.libs << 'test'
+ test.verbose = false
+end
+
+desc "Run the test suite"
+task :default => :test
View
3  lib/exemplar.rb
@@ -0,0 +1,3 @@
+require 'exemplar/loggable'
+require 'exemplar/example'
+require 'exemplar/runner'
View
36 lib/exemplar/example.rb
@@ -0,0 +1,36 @@
+module Exemplar
+ class Example
+ include Loggable
+
+ attr_reader :name, :block
+
+ def initialize(name, options, block)
+ @name, @block = name, block
+
+ runner.register(self)
+ end
+
+ def run
+ log "Running example #{name}..."
+
+ block_return = block.call(self)
+
+ log "Done running example #{name}"
+
+ block_return
+ rescue Exception => error
+ log "Whoops, seems like something went wrong. Here's the error message: %s." % error.message
+ nil
+ end
+
+ private
+
+ def runner
+ Exemplar::Runner
+ end
+ end
+end
+
+def example(name, options = nil, &block)
+ Exemplar::Example.new(name, options, block)
+end
View
17 lib/exemplar/loggable.rb
@@ -0,0 +1,17 @@
+module Exemplar
+ module Loggable
+ class << self
+ def log?
+ @log.nil? ? true : @log
+ end
+
+ def log=(log)
+ @log = log
+ end
+ end
+
+ def log(message)
+ puts message if Loggable.log?
+ end
+ end
+end
View
19 lib/exemplar/runner.rb
@@ -0,0 +1,19 @@
+module Exemplar
+ module Runner
+ extend self
+
+ def register(example)
+ examples << example
+ end
+
+ def run
+ examples.each { |example| example.run }
+ end
+
+ def examples
+ @examples ||= []
+ end
+ end
+end
+
+Kernel.at_exit { Exemplar::Runner.run }
View
19 test/example_test.rb
@@ -0,0 +1,19 @@
+require 'test_helper'
+
+module Exemplar
+ class ExampleTest < Test::Unit::TestCase
+ def test_declaring_example
+ test_example = example("test example") { }
+
+ assert_equal [ test_example ], Exemplar::Runner.examples, "Should register the example with the runner"
+ end
+
+ def test_running_example
+ test_example = example("test example") { |example| self.invoke_something(example); :block_return }
+
+ mock( self ).invoke_something(test_example)
+
+ assert_equal :block_return, test_example.run, "Should run the example block and return whatever it returns"
+ end
+ end
+end
View
14 test/runner_test.rb
@@ -0,0 +1,14 @@
+require 'test_helper'
+
+class RunnerTest < Test::Unit::TestCase
+ def test_examples_running
+ run_examples = []
+
+ example1 = example("first test example") { |e| run_examples << e }
+ example2 = example("second test example") { |e| run_examples << e }
+
+ Exemplar::Runner.run
+
+ assert_equal [ example1, example2 ], run_examples, "Should run the examples in order declared"
+ end
+end
View
21 test/test_helper.rb
@@ -0,0 +1,21 @@
+
+begin
+ require 'bundler'
+
+ Bundler.setup
+rescue LoadError => error
+ warn "Bundler doesn't seem to be installed. Please install bundler and run: bundle install"
+ exit
+end
+
+require 'test/unit'
+require 'rr'
+
+require 'exemplar'
+
+# disable the logging for the tests
+Exemplar::Loggable.log = false
+
+class Test::Unit::TestCase
+ include RR::Adapters::TestUnit
+end
Please sign in to comment.
Something went wrong with that request. Please try again.