renvy 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,56 @@
1
+ # REnvy
2
+ #### Rspec-like matching for Test::Unit.
3
+
4
+ REnvy lets you use a syntax similar to RSpec on your Test::Unit tests. It
5
+ monkey-patches Object to translate RSpec-like sugar into plain Test::Unit
6
+ `assert` matchers.
7
+
8
+ ```
9
+ $ gem install renvy
10
+ ```
11
+
12
+ Then you may use it as so:
13
+
14
+ ```ruby
15
+ obj.should == 2 # => assert_equal 2, obj
16
+ obj.should ~= /regex/ # => assert_match /regex/, obj
17
+ obj.should != 3 # => assert_not_equal 3, obj
18
+ obj.should.be.true! # => assert obj
19
+ obj.should.be.false!
20
+
21
+ # Anything else will just pass thru (.be is optional):
22
+ obj.should.nil? # => assert obj.nil?
23
+ obj.should.be.nil? # => assert obj.nil?
24
+ obj.should.respond_to?(:freeze) # => assert obj.respond_to?(:freeze)
25
+
26
+ # You can also use should_not:
27
+ obj.should_not == 3
28
+ obj.should_not.be.nil?
29
+
30
+ should.raise(Error) { lol }
31
+ should_not.raise { puts "hi" }
32
+ ```
33
+
34
+ ### Extending
35
+
36
+ Create your new matcher in a module, then use `REnvy::Should.add`.
37
+
38
+ ```ruby
39
+ module DanceMatcher
40
+ def boogie_all_night!
41
+ if positive?
42
+ test.assert left.respond_to?(:dance)
43
+ else
44
+ test.assert ! left.respond_to?(:dance)
45
+ end
46
+ end
47
+ end
48
+
49
+ REnvy::Should.add DanceMatcher
50
+
51
+ # Then in your tests, use:
52
+ dancer.should.boogie_all_night!
53
+ ```
54
+
55
+ (You may also use `REnvy::Should::Be.add` to restrict it to the `.should.be`
56
+ context.)
@@ -0,0 +1,3 @@
1
+ task(:test) {
2
+ Dir['./test/*.rb'].each { |f| load f }
3
+ }
@@ -0,0 +1,141 @@
1
+ require 'test/unit'
2
+
3
+ # Rspec-like matching for Test::Unit.
4
+ #
5
+ # == Usage
6
+ #
7
+ # obj.should == 2
8
+ # obj.should ~= /regex/
9
+ # obj.should != 3
10
+ # obj.should.be.true!
11
+ # obj.should.be.false!
12
+ #
13
+ # # Anything else will just pass thru:
14
+ # obj.should.nil? # same as: assert obj.nil?
15
+ # obj.should.be.nil? # same as: assert obj.nil?
16
+ # obj.should.respond_to?(:freeze)
17
+ #
18
+ # # You can also use should_not:
19
+ # obj.should_not == 3
20
+ # obj.should_not.be.nil?
21
+ #
22
+ # should.raise(Error) { lol }
23
+ # should_not.raise { puts "hi" }
24
+ #
25
+ module REnvy
26
+ VERSION = "0.1.1"
27
+
28
+ def self.version
29
+ VERSION
30
+ end
31
+
32
+ class Should
33
+ attr_reader :left
34
+
35
+ def self.init(test)
36
+ @@test = test
37
+ end
38
+
39
+ def self.add(extension)
40
+ self.send :include, extension
41
+ end
42
+
43
+ def initialize(left, neg=false)
44
+ @left = left
45
+ @neg = neg
46
+ end
47
+
48
+ def negative?
49
+ @neg
50
+ end
51
+
52
+ def positive?
53
+ !@neg
54
+ end
55
+
56
+ def test
57
+ @@test
58
+ end
59
+
60
+ def ==(right)
61
+ if positive?
62
+ test.send :assert_equal, right, left
63
+ else
64
+ test.send :assert_not_equal, right, left
65
+ end
66
+ end
67
+
68
+ def !=(right)
69
+ if positive?
70
+ test.send :assert_not_equal, right, left
71
+ else
72
+ test.send :assert_equal, right, left
73
+ end
74
+ end
75
+
76
+ def =~(right)
77
+ if positive?
78
+ test.send :assert_match, right, left
79
+ else
80
+ test.send :assert, ! (left =~ right)
81
+ end
82
+ end
83
+
84
+ def be
85
+ Be.new(left, @neg)
86
+ end
87
+
88
+ def raise(ex=StandardError, &blk)
89
+ if positive?
90
+ test.send :assert_raises, ex, &blk
91
+ else
92
+ test.send :assert_nothing_raised, &blk
93
+ end
94
+ end
95
+
96
+ def method_missing(meth, *args, &blk)
97
+ result = left.send(meth, *args, &blk)
98
+ if positive?
99
+ @test.send :assert, result
100
+ else
101
+ @test.send :assert, ! result
102
+ end
103
+ end
104
+ end
105
+
106
+ class Should::Be < Should
107
+ def true!
108
+ if positive?
109
+ test.send :assert, left
110
+ else
111
+ test.send :assert, ! left
112
+ end
113
+ end
114
+
115
+ def false!
116
+ if positive?
117
+ test.send :assert, ! left
118
+ else
119
+ test.send :assert, left
120
+ end
121
+ end
122
+ end
123
+ end
124
+
125
+ class Object
126
+ def should
127
+ REnvy::Should.new(self)
128
+ end
129
+
130
+ def should_not
131
+ REnvy::Should.new(self, true)
132
+ end
133
+ end
134
+
135
+ class Test::Unit::TestCase
136
+ alias old_setup setup
137
+ def setup(&blk)
138
+ old_setup &blk
139
+ REnvy::Should.init self
140
+ end
141
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path('../../lib/renvy', __FILE__)
2
+
3
+ module REnvy::ArrayMatcher
4
+ def like(right)
5
+ super unless left.is_a?(Array) && right.is_a?(Array)
6
+ if positive?
7
+ test.assert_equal left.sort, right.sort
8
+ else
9
+ test.assert_not_equal left.sort, right.sort
10
+ end
11
+ end
12
+ end
13
+
14
+ REnvy::Should.add REnvy::ArrayMatcher
15
+
16
+ class ExtensionTest < Test::Unit::TestCase
17
+ def test_extension
18
+ a = %w(a b c)
19
+ b = %w(b c a)
20
+
21
+ a.should.be.like b
22
+ end
23
+
24
+ def test_extension_2
25
+ a = %w(a b c)
26
+ b = %w(b c A)
27
+
28
+ a.shouldnt.be.like b
29
+ end
30
+
31
+ def test_extension_3
32
+ a = %w(a b c)
33
+ b = 2
34
+
35
+ should.raise(NoMethodError) { a.should.be.like b }
36
+ end
37
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path('../../lib/renvy', __FILE__)
2
+
3
+ class REnvyTest < Test::Unit::TestCase
4
+ def test_should
5
+ 2.should == 2
6
+ 2.shouldnt == 3
7
+
8
+ 2.should != 3
9
+ 2.shouldnt != 2
10
+
11
+ "hi".should =~ /hi/
12
+ "hi".shouldnt =~ /HI/
13
+
14
+ true.should.be.true!
15
+ "ye".should.be.true!
16
+ true.shouldnt.be.false!
17
+
18
+ false.should.be.false!
19
+ false.shouldnt.be.true!
20
+
21
+ @foo.should.be.nil?
22
+ 1000.shouldnt.be.nil?
23
+
24
+ "".should.respond_to?(:empty?)
25
+ "".shouldnt.respond_to?(:lolwhat)
26
+
27
+ shouldnt.raise { 2 + 2 }
28
+ should.raise(ZeroDivisionError) { 2 / 0 }
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: renvy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.1
6
+ platform: ruby
7
+ authors:
8
+ - Rico Sta. Cruz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-17 00:00:00 +08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: test-unit
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description: REnvy lets you use a syntax similar to RSpec on your Test::Unit tests.
28
+ email:
29
+ - rico@sinefunc.com
30
+ executables: []
31
+
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - lib/renvy.rb
38
+ - test/extension_test.rb
39
+ - test/renvy_test.rb
40
+ - README.md
41
+ - Rakefile
42
+ has_rdoc: true
43
+ homepage: http://github.com/rstacruz/renvy
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.6.2
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: RSpec-like syntax for Test::Unit.
70
+ test_files: []
71
+