File: spec.rb

package info (click to toggle)
ruby-test-unit-context 0.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 188 kB
  • sloc: ruby: 810; makefile: 2
file content (59 lines) | stat: -rw-r--r-- 2,114 bytes parent folder | download | duplicates (3)
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
53
54
55
56
57
58
59
module Test::Unit::Context
  module Spec
    
    PREFIX = nil # :nodoc:
    SUFFIX = 'Test'.freeze # :nodoc:
    
    # Spec style Test::Unit::TestCase (sub-classes) :
    # 
    #   describe User do
    #     it "creates a new instance" do
    #       assert_nothing_raised { User.new }
    #     end
    #   end
    # 
    # You need to `require 'test/unit/context/spec'` ( e.g. in test_helper.rb).
    #
    # The generated Test::Unit::TestCase sub-class follows the "Test" suffix 
    # naming convention e.g. `describe User` ends up as `UserTest < TestCase`.
    #
    def describe(*args, &block) 
      name = args.map { |arg| arg.is_a?(Class) ? arg.name : arg }.join
      class_name = Helpers.to_const_name(name.freeze, PREFIX, SUFFIX)
      self_klass = self.is_a?(Class) ? self : self.class # nested describes
      if self_klass.const_defined?(class_name)
        klass = self_klass.const_get(class_name)
        if klass <= Test::Unit::TestCase
          warn "duplicate test-case describe with args #{args.inspect} " << 
                "found at #{caller.first} it is going to be merged with " << 
                "the previous TestCase definition"
        else
          raise "could not create a test-case with args #{args.inspect} " << 
                "as a constant #{class_name} is already defined and is not " << 
                "another TestCase definition"
        end
      else
        # NOTE: context inherits from nesting case but describe should not :
        #is_test_case = ( self_klass <= Test::Unit::TestCase )
        #klass = Class.new is_test_case ? self_klass : Test::Unit::TestCase
        klass = Class.new(Test::Unit::TestCase)
        klass.send(:define_method, :name) { name }
        klass.extend Methods
        self_klass.const_set(class_name, klass)
      end
      klass.class_eval(&block)
      klass
    end
    
    module Methods
      
      # It is an alias for Test::Unit's test method.
      # @see Test::Unit::TestCase#test
      def it(*args, &block); test(*args, &block); end
      
    end
    
  end
end

Object.send :include, Test::Unit::Context::Spec