tiramisu 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,91 @@
1
+ module Tiramisu
2
+
3
+ def call_block block
4
+ {returned: block.call, caller: relative_source_location(block)}.freeze
5
+ rescue UncaughtThrowError => e
6
+ {raised: e, thrown: extract_thrown_symbol(e), caller: relative_source_location(block)}.freeze
7
+ rescue Exception => e
8
+ {raised: e, caller: relative_source_location(block)}.freeze
9
+ end
10
+
11
+ # extract thrown symbol from given exception
12
+ #
13
+ # @param exception
14
+ #
15
+ def extract_thrown_symbol exception
16
+ return unless exception.is_a?(Exception)
17
+ return unless s = exception.message.scan(/uncaught throw\W+(\w+)/).flatten[0]
18
+ s.to_sym
19
+ end
20
+
21
+ def identity_string type, label, block
22
+ '%s %s (%s:%s)' % [
23
+ blue(type),
24
+ label.inspect,
25
+ *relative_source_location(block)
26
+ ]
27
+ end
28
+
29
+ def relative_source_location block
30
+ return unless block
31
+ [
32
+ relative_location(block.source_location[0]),
33
+ block.source_location[1]
34
+ ]
35
+ end
36
+
37
+ def relative_location line
38
+ line.sub(/\A#{pwd}\/+/, '')
39
+ end
40
+
41
+ def pretty_backtrace e
42
+ Array(e.backtrace).map {|l| relative_location(l)}
43
+ end
44
+
45
+ def readline caller
46
+ file, line = caller_to_source_location(caller)
47
+ return unless file && line
48
+ lines = ((@__readlinecache__ ||= {})[file] ||= File.readlines(file))
49
+ return unless line = lines[line.to_i - 1]
50
+ line.sub(/(do|\{)\Z/, '').strip
51
+ end
52
+
53
+ def caller_to_source_location caller
54
+ file, line = caller.split(/:(\d+):in.+/)
55
+ [relative_location(file), line]
56
+ end
57
+
58
+ def load_files pattern_or_files
59
+ files = pattern_or_files.is_a?(Array) ? pattern_or_files : Dir[pwd(pattern_or_files)]
60
+ files.each {|f| load_file(f)}
61
+ end
62
+
63
+ def load_file file
64
+ augment_load_path(file)
65
+ require(file)
66
+ end
67
+
68
+ def augment_load_path file
69
+ # adding ./
70
+ $:.unshift(pwd) unless $:.include?(pwd)
71
+
72
+ # adding ./lib/
73
+ lib = pwd('lib')
74
+ unless $:.include?(lib)
75
+ $:.unshift(lib) if File.directory?(lib)
76
+ end
77
+
78
+ # adding file's dirname
79
+ dir = File.dirname(file)
80
+ $:.unshift(dir) unless $:.include?(dir)
81
+ end
82
+
83
+ def pwd *args
84
+ File.join(Dir.pwd, *args.map!(&:to_s))
85
+ end
86
+ end
87
+
88
+ require 'tiramisu/util/assert_raise'
89
+ require 'tiramisu/util/refute_raise'
90
+ require 'tiramisu/util/assert_throw'
91
+ require 'tiramisu/util/refute_throw'
@@ -0,0 +1,54 @@
1
+ module Tiramisu
2
+
3
+ def assert_raised_as_expected object, expected_type = nil, expected_message = nil, block = nil
4
+ f = assert_raised(object)
5
+ return f if f
6
+
7
+ return assert_raised_as_expected_by_block(object, block) if block
8
+
9
+ if expected_type
10
+ f = assert_raised_expected_type(object, expected_type)
11
+ return f if f
12
+ end
13
+
14
+ if expected_message
15
+ f = assert_raised_expected_message(object, expected_message)
16
+ return f if f
17
+ end
18
+ nil
19
+ end
20
+
21
+ def assert_raised object
22
+ return [
23
+ 'Expected a exception to be raised at %s' % object[:caller]
24
+ ] unless object[:raised]
25
+ nil
26
+ end
27
+
28
+ def assert_raised_as_expected_by_block object, block
29
+ return [
30
+ 'Looks like wrong or no error raised at %s' % object[:caller],
31
+ 'See validation block'
32
+ ] unless block.call(object[:raised])
33
+ nil
34
+ end
35
+
36
+ def assert_raised_expected_type object, expected_type
37
+ return [
38
+ 'Expected a %s to be raised at %s' % [expected_type, object[:caller]],
39
+ 'Instead a %s raised' % object[:raised].class
40
+ ] unless object[:raised].class == expected_type
41
+ nil
42
+ end
43
+
44
+ def assert_raised_expected_message object, expected_message
45
+ regexp = expected_message.is_a?(Regexp) ? expected_message : /\A#{expected_message}\z/
46
+ return [
47
+ 'Expected the exception raised at %s' % object[:caller],
48
+ 'to match "%s"' % regexp.source,
49
+ 'Instead it looks like',
50
+ pp(object[:raised].message)
51
+ ] unless object[:raised].message =~ regexp
52
+ nil
53
+ end
54
+ end
@@ -0,0 +1,40 @@
1
+ module Tiramisu
2
+
3
+ def assert_thrown_as_expected object, expected_symbol = nil, block = nil
4
+ f = assert_thrown(object)
5
+ return f if f
6
+
7
+ return assert_thrown_as_expected_by_block(object, block) if block
8
+
9
+ if expected_symbol
10
+ f = assert_expected_symbol_thrown(object, expected_symbol)
11
+ return f if f
12
+ end
13
+ nil
14
+ end
15
+
16
+ def assert_thrown_as_expected_by_block object, block
17
+ return [
18
+ 'Looks like wrong or no symbol thrown at %s' % object[:caller],
19
+ 'See validating block'
20
+ ] unless block.call(object[:thrown])
21
+ nil
22
+ end
23
+
24
+ def assert_thrown object
25
+ return [
26
+ 'Expected a symbol to be thrown at %s' % object[:caller]
27
+ ] unless object[:thrown]
28
+ nil
29
+ end
30
+
31
+ def assert_expected_symbol_thrown object, expected_symbol
32
+ return begin
33
+ [
34
+ 'Expected :%s to be thrown at %s' % [expected_symbol, object[:caller]],
35
+ 'Instead :%s thrown' % object[:thrown]
36
+ ]
37
+ end unless expected_symbol == object[:thrown]
38
+ nil
39
+ end
40
+ end
@@ -0,0 +1,48 @@
1
+ module Tiramisu
2
+
3
+ def refute_raised_as_expected object, expected_type, expected_message, block = nil
4
+ f = refute_raised(object, expected_type || expected_message)
5
+ return f if f
6
+
7
+ if expected_type
8
+ f = refute_raised_expected_type(object, expected_type)
9
+ return f if f
10
+ end
11
+
12
+ if expected_message
13
+ f = refute_raised_expected_message(object, expected_message)
14
+ return f if f
15
+ end
16
+ nil
17
+ end
18
+
19
+ def refute_raised object, should_raise = false
20
+ if should_raise
21
+ return [
22
+ 'Expected a exception to be raised at %s' % object[:caller]
23
+ ] unless object[:raised]
24
+ else
25
+ return [
26
+ 'A unexpected exception raised at %s' % object[:caller],
27
+ object[:raised]
28
+ ] if object[:raised]
29
+ end
30
+ nil
31
+ end
32
+
33
+ def refute_raised_expected_type object, expected_type
34
+ return [
35
+ 'Not expected a %s to be raised at %s' % [object[:raised].class, object[:caller]],
36
+ ] if object[:raised].class == expected_type
37
+ nil
38
+ end
39
+
40
+ def refute_raised_expected_message object, expected_message
41
+ regexp = expected_message.is_a?(Regexp) ? expected_message : /\A#{expected_message}\z/
42
+ return [
43
+ 'Not expected raised exception to match %s' % regexp.source,
44
+ object[:raised]
45
+ ] if object[:raised].message =~ regexp
46
+ nil
47
+ end
48
+ end
@@ -0,0 +1,34 @@
1
+ module Tiramisu
2
+
3
+ def refute_thrown_as_expected object, expected_symbol, block = nil
4
+ f = refute_thrown(object, expected_symbol)
5
+ return f if f
6
+
7
+ if expected_symbol
8
+ f = refute_expected_symbol_thrown(object, expected_symbol)
9
+ return f if f
10
+ end
11
+ nil
12
+ end
13
+
14
+ def refute_thrown object, should_throw = false
15
+ if should_throw
16
+ return [
17
+ 'Expected a symbol to be thrown at %s' % object[:caller]
18
+ ] unless object[:thrown]
19
+ else
20
+ return [
21
+ 'Not expected a symbol to be thrown at %s' % object[:caller]
22
+ ] if object[:thrown]
23
+ end
24
+ nil
25
+ end
26
+
27
+ def refute_expected_symbol_thrown object, expected_symbol
28
+ return [
29
+ 'Not expected :%s to be thrown' % expected_symbol,
30
+ 'at %s' % object[:caller]
31
+ ] if expected_symbol == object[:thrown]
32
+ nil
33
+ end
34
+ end
@@ -0,0 +1,98 @@
1
+ describe :assert do
2
+
3
+ it 'pass ==' do
4
+ x = mock(:==, :x)
5
+ x == :x
6
+ x.verify
7
+ end
8
+
9
+ it 'pass ===' do
10
+ x = mock(:===, :x)
11
+ x === :x
12
+ x.verify
13
+ end
14
+
15
+ it 'pass !=' do
16
+ x = mock(:!=, :x)
17
+ x != :x
18
+ x.verify
19
+ end
20
+
21
+ it 'pass >' do
22
+ x = mock(:>, :x)
23
+ x > :x
24
+ x.verify
25
+ end
26
+
27
+ it 'pass >=' do
28
+ x = mock(:>=, :x)
29
+ x >= :x
30
+ x.verify
31
+ end
32
+
33
+ it 'pass <' do
34
+ x = mock(:<, :x)
35
+ x < :x
36
+ x.verify
37
+ end
38
+
39
+ it 'pass <=' do
40
+ x = mock(:<=, :x)
41
+ x <= :x
42
+ x.verify
43
+ end
44
+
45
+ it 'pass eql?' do
46
+ x = mock(:eql?, :x)
47
+ x.eql? :x
48
+ x.verify
49
+ end
50
+
51
+ it 'pass equal?' do
52
+ x = mock(:equal?, :x)
53
+ x.equal? :x
54
+ x.verify
55
+ end
56
+
57
+ it 'pass =~' do
58
+ x = mock(:=~, :x)
59
+ x =~ :x
60
+ x.verify
61
+ end
62
+
63
+ it 'pass match' do
64
+ x = mock(:match, :x)
65
+ x.match :x
66
+ x.verify
67
+ end
68
+
69
+ it 'pass any?' do
70
+ m = mock(:any?)
71
+ m.any?
72
+ m.verify
73
+ end
74
+
75
+ it 'pass all?' do
76
+ x = mock(:all?, :x)
77
+ x.all? :x
78
+ x.verify
79
+ end
80
+
81
+ it 'pass start_with?' do
82
+ x = mock(:start_with?, :x)
83
+ x.start_with? :x
84
+ x.verify
85
+ end
86
+
87
+ it 'pass end_with?' do
88
+ x = mock(:end_with?, :x)
89
+ x.end_with? :x
90
+ x.verify
91
+ end
92
+
93
+ it 'pass respond_to?' do
94
+ x = mock(:respond_to?, :__id__, true)
95
+ x.respond_to? :__id__, true
96
+ x.verify
97
+ end
98
+ end
@@ -0,0 +1,65 @@
1
+ describe :context_inheritance_test do
2
+
3
+ it 'inherits from parent spec' do
4
+ x = nil
5
+ spec rand do
6
+ define_method(:set_x) {x = true}
7
+ context rand do
8
+ allocate.send(:set_x)
9
+ end
10
+ end
11
+ assert_equal true, x
12
+ end
13
+
14
+ it 'inherits from parent context' do
15
+ x = nil
16
+ spec rand do
17
+ context rand do
18
+ define_method(:set_x) {x = true}
19
+ context rand do
20
+ allocate.send(:set_x)
21
+ end
22
+ end
23
+ end
24
+ assert_equal true, x
25
+ end
26
+
27
+ it 'inherits from parent spec through parent context' do
28
+ x = nil
29
+ spec rand do
30
+ define_method(:set_x) {x = true}
31
+ context rand do
32
+ context rand do
33
+ allocate.send(:set_x)
34
+ end
35
+ end
36
+ end
37
+ assert_equal true, x
38
+ end
39
+
40
+ it 'does not inherit tests' do
41
+ spec_tests, context_tests = nil
42
+ spec rand do
43
+ test(:x) {}
44
+ context rand do
45
+ context_tests = tests.size
46
+ end
47
+ spec_tests = tests.size
48
+ end
49
+ assert_equal 1, spec_tests
50
+ assert_equal 0, context_tests
51
+ end
52
+
53
+ it 'does not inherit hooks' do
54
+ spec_hooks, context_hooks = nil
55
+ spec rand do
56
+ before {}
57
+ context rand do
58
+ context_hooks = hooks[:before].size
59
+ end
60
+ spec_hooks = hooks[:before].size
61
+ end
62
+ assert_equal 1, spec_hooks
63
+ assert_equal 0, context_hooks
64
+ end
65
+ end
@@ -0,0 +1,36 @@
1
+ describe :hooks do
2
+ it 'calls wildcard hooks' do
3
+ called = 0
4
+ spec rand do
5
+ before {called += 1}
6
+ test(:a) {}
7
+ test(:b) {}
8
+ run(:a)
9
+ run(:b)
10
+ end
11
+ assert_equal 2, called
12
+ end
13
+
14
+ it 'calls named hooks' do
15
+ called = 0
16
+ spec rand do
17
+ after(:b) {called += 1}
18
+ test(:a) {}
19
+ test(:b) {}
20
+ run(:a)
21
+ run(:b)
22
+ end
23
+ assert_equal 1, called
24
+ end
25
+
26
+ it 'prefers named hooks over wildcard ones' do
27
+ called = nil
28
+ spec rand do
29
+ around(:a) {called = :named}
30
+ around {called = :wildcard}
31
+ test(:a) {}
32
+ run(:a)
33
+ end
34
+ assert_equal :named, called
35
+ end
36
+ end