alloy / microspec

A very simple test/spec like clone used for MacRuby, without the rspec assertion syntax. Especially for mon ami Laurent ;)

This URL has Read+Write access

alloy (author)
Thu Jan 15 08:43:12 -0800 2009
microspec / microspec.rb
100644 52 lines (45 sloc) 1.336 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require "test/unit"
 
# The following are just some simple hacks to be able to write tests with a
# spec like description. This should ease the process of moving tests to run
# with mspec for instance.
 
module Test::Unit
  class TestCase
    class << self
      # Runs before each test case.
      def before(&block)
        define_method("setup", &block)
      end
      
      # Runs after each test case.
      def after(&block)
        define_method("teardown", &block)
      end
      
      # Defines a test case.
      def it(name, &block)
        define_method("test_#{name}", &block)
      end
    end
  end
  
  class Failure
    # Return the failure message in a more spec like way.
    def long_display
      file_location, method_name = @location.first.split(':in `')
      "#{@test_name} `#{method_name.sub(/^test_/, '')} [#{file_location}]\n#{@message}"
    end
  end
end
 
module Kernel
  private
  
  # Defines a new test case with the given +description+ and optionally a test
  # case superclass.
  #
  # describe "Foo in general" do
  # it "should instantiate" do
  # assert Foo.new.nil?
  # end
  # end
  def describe(description, superclass = Test::Unit::TestCase, &definition)
    klass = Class.new(superclass, &definition)
    klass.class_eval "def name; '#{description}' end"
    klass
  end
end