minitest-spec-expect 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a50c5c9f12125db25fb9e4987a71522e32bf0d5
4
+ data.tar.gz: 63509bc103ada84823b2614234a69b22388a687a
5
+ SHA512:
6
+ metadata.gz: a9ef9c3cc729a0749f296c3fd37930a5e240a4904a3dcf3d423bf052302f51f9524c312b18c32f011bd232fca2baea3904a942bc863dc1555b27cf96e2a1b428
7
+ data.tar.gz: dc346ef3e862b222b1bc6adfc3707517a5a3bb9de42c03c4d304cd6797dcd97e9a9e549d6785b8ab97a9acd59c056c61e4978812461ce589df490f22fb6a1721
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - rbx-19mode
6
+ - jruby-19mode
7
+ script: rake
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ #### version 0.1.0 - *May 23, 2013*
2
+ * Added ability to pass a block to `#expect`. When a block is passed to it, the expect object responds
3
+ to `#to_throw`, `#to_raise`, `#to_output`, and `#to_be_silent`. Big thanks to
4
+ [@splattael](https://github.com/splattael).
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # minitest-spec-expect
2
- Expect syntax for [minitest](http://docs.seattlerb.org/minitest/index.html). Made out of love for
3
- both rspec2's expect syntax and minitest's lightweight feel.
2
+
3
+ [![Build Status](https://travis-ci.org/davejachimiak/minitest-spec-expect.png?branch=master)](https://travis-ci.org/davejachimiak/minitest-spec-expect)
4
+
5
+ Expect syntax for [MiniTest](http://docs.seattlerb.org/minitest/index.html). Made out of love for
6
+ both rspec2's expect syntax and MiniTest's lightweight feel.
4
7
  ## Install
5
8
  In your Gemfile:
6
9
  ```ruby
@@ -15,7 +18,7 @@ Then require it in the file that you require `minitest/autorun`:
15
18
  require 'minitest/spec/expect'
16
19
  ```
17
20
  ## Usage
18
- Wrap the object under test in an `expect()` object. Then call a minitest expectation on it,
21
+ Wrap the object under test in an `expect()` object. Then call a MiniTest expectation on it,
19
22
  substituting `must` and `wont` with `to` and `to_not`.
20
23
 
21
24
  For example:
@@ -47,10 +50,8 @@ describe Integer do
47
50
  end
48
51
  ```
49
52
  ## API
50
- Please see the minitest [expectation](http://docs.seattlerb.org/minitest/Minitest/Expectations.html)
51
- and [assertion](http://docs.seattlerb.org/minitest/Minitest/Assertions.html) docs for more details
52
- on implementations for the `must` and `wont` methods transposed to `to` and `to_not` methods transposed
53
- here.
53
+ Please see the MiniTest [expectation](http://docs.seattlerb.org/minitest/Minitest/Expectations.html)
54
+ docs for details on corresponding `must_*` and `wont_*` methods.
54
55
  ### `#*be`
55
56
  ```ruby
56
57
  expect(1).to_be :<, 2
@@ -90,8 +91,8 @@ an_object = Object.new
90
91
  expect(Object.new).to_not_be_same_as an_object
91
92
  ```
92
93
  ### `#to_be_silent`
93
- (`#must_be_silent` availble with Minitest 5.0)
94
94
  ```ruby
95
+ expect { '' }.to_be_silent
95
96
  expect(->{''}).to_be_silent
96
97
  ```
97
98
  ### `#*be_within_epsilon`
@@ -111,16 +112,17 @@ expect(['tim', 'eric']).to_not_include 'brule'
111
112
  ```
112
113
  ### `#*match`
113
114
  ```ruby
114
- expect(/(fart)*/).to_match 'fartfartfartfart'
115
- expect(/fart/).to_not_match 'barf'
115
+ expect('fartfartfartfart').to_match /(fart)*/
116
+ expect('bart').to_not_match /fart/
116
117
  ```
117
118
  ### `#to_output`
118
- (`#must_output` availble with Minitest 5.0)
119
119
  ```ruby
120
+ expect { puts 'barf' }.to_output "barf\n"
120
121
  expect(->{ puts 'barf' }).to_output "barf\n"
121
122
  ```
122
123
  ### `#to_raise`
123
124
  ```ruby
125
+ expect { raise RuntimeError }.to_raise RuntimeError
124
126
  expect(->{ raise RuntimeError }).to_raise RuntimeError
125
127
  ```
126
128
  ### `#*respond_to`
@@ -130,19 +132,15 @@ expect(Object.new).to_not_respond_to :fart_factory
130
132
  ```
131
133
  ### `#*throw`
132
134
  ```ruby
135
+ expect { throw StandardError }.to_throw StandardError
133
136
  expect(->{ throw StandardError }).to_throw StandardError
134
137
  ```
135
138
  ## Contribute
136
139
  1. Fork the repo.
137
140
  2. Create a branch.
138
- 3. Make sure specs are green (`$ rake test`)
139
- 3. Open a pull request.
140
-
141
- ##### A note on the current specs
142
- The specs at `spec/integration_spec.rb` test a couple of minitest expectations that aren't included
143
- in previous minitest versions to 5.0. To install the newest minitest, do `$ sudo gem install
144
- minitest`. Because of the way Ruby installs and loads its native libraries, `sudo` before
145
- `gem install` is necessary, unfortunately.
141
+ 3. Code.
142
+ 4. Ensure the specs are green (`$ rake`)
143
+ 5. Open a pull request.
146
144
 
147
145
  ## License
148
146
  The MIT License (MIT)
data/Rakefile CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'rake/testtask'
2
2
 
3
- desc 'run the tests'
3
+ desc 'Run the tests'
4
+ task :default => :test
5
+
6
+ desc 'Run the tests'
4
7
  Rake::TestTask.new do |t|
5
- t.pattern = 'spec/integration_spec.rb'
8
+ t.pattern = 'spec/*.rb'
6
9
  end
@@ -1,21 +1,46 @@
1
1
  module Kernel
2
- def expect object
3
- MiniTest::Spec::Expect.new object
2
+ def expect arg=NO_ARG, &block
3
+ raise_errors arg, block
4
+ MiniTest::Spec::Expect.create arg, &block
4
5
  end
6
+
7
+ private
8
+
9
+ def raise_errors object, block
10
+ if object == NO_ARG
11
+ raise ArgumentError, 'must pass an argument or a block' if !block
12
+ else
13
+ raise ArgumentError, 'cannot handle both an argument and a block' if block
14
+ end
15
+ end
16
+
17
+ NO_ARG = Object.new
5
18
  end
6
19
 
7
20
  require 'minitest/spec'
8
21
 
9
22
  class MiniTest::Spec::Expect
10
- require 'minitest/spec/expect_syntax'
23
+ OBJECT_UNDER_TEST = 'object'
11
24
 
12
- OBJECT = 'object'
25
+ attr_reader OBJECT_UNDER_TEST
13
26
 
14
- attr_reader OBJECT.to_sym
27
+ class_eval <<-EOM
28
+ def initialize #{OBJECT_UNDER_TEST}
29
+ @#{OBJECT_UNDER_TEST} = #{OBJECT_UNDER_TEST}
30
+ end
31
+ EOM
15
32
 
16
- def initialize object
17
- @object = object
33
+ def self.create arg, &block
34
+ block ? ForBlock.new(block) : ForArg.new(arg)
18
35
  end
19
36
 
20
- MiniTest::Spec::ExpectSyntax.new(self).set_expectations
37
+ class ForArg < self
38
+ require 'minitest/spec/expect_syntax_for_arg'
39
+ MiniTest::Spec::ExpectSyntaxForArg.new(self).set_expectations
40
+ end
41
+
42
+ class ForBlock < self
43
+ require 'minitest/spec/expect_syntax_for_block'
44
+ MiniTest::Spec::ExpectSyntaxForBlock.new(self).set_expectations
45
+ end
21
46
  end
@@ -1,3 +1,3 @@
1
1
  class MiniTest::Spec::Expect
2
- VERSION = '0.0.2'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -1,5 +1,6 @@
1
1
  class MiniTest::Spec::ExpectSyntax
2
- TRANSPOSITIONS = {'must' => 'to', 'wont' => 'to_not'}
2
+ TRANSPOSITIONS = { 'must' => 'to', 'wont' => 'to_not' }
3
+ TRANSPOSITION_REGEXP = Regexp.union TRANSPOSITIONS.keys
3
4
 
4
5
  attr_reader :expect_class
5
6
 
@@ -8,15 +9,15 @@ class MiniTest::Spec::ExpectSyntax
8
9
  end
9
10
 
10
11
  def set_expectations
11
- spec_expectation_names.each do |spec_expectation_name|
12
- set_expectation spec_expectation_name
12
+ expectation_names.each do |expectation_name|
13
+ set_expectation expectation_name
13
14
  end
14
15
  end
15
16
 
16
17
  private
17
18
 
18
- def spec_expectation_names
19
- MiniTest::Expectations.instance_methods
19
+ def expectation_names
20
+ raise NotImplementedError
20
21
  end
21
22
 
22
23
  def set_expectation expectation_name
@@ -28,17 +29,12 @@ class MiniTest::Spec::ExpectSyntax
28
29
  def expect_method expectation_name
29
30
  """
30
31
  def #{expect_method_name expectation_name } *args
31
- #{expect_class::OBJECT}.#{expectation_name} *args
32
+ #{expect_class::OBJECT_UNDER_TEST}.#{expectation_name} *args
32
33
  end
33
34
  """
34
35
  end
35
36
 
36
37
  def expect_method_name expectation_name
37
- TRANSPOSITIONS.inject('') do |memo, transposition|
38
- string = memo.empty? ? expectation_name.to_s : memo
39
-
40
- string.gsub transposition.first, transposition.last
41
- end
38
+ expectation_name.to_s.gsub TRANSPOSITION_REGEXP, TRANSPOSITIONS
42
39
  end
43
40
  end
44
-
@@ -0,0 +1,9 @@
1
+ require 'minitest/spec/expect_syntax'
2
+
3
+ class MiniTest::Spec::ExpectSyntaxForArg < MiniTest::Spec::ExpectSyntax
4
+ private
5
+
6
+ def expectation_names
7
+ MiniTest::Expectations.instance_methods
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ require 'minitest/spec/expect_syntax'
2
+
3
+ class MiniTest::Spec::ExpectSyntaxForBlock < MiniTest::Spec::ExpectSyntax
4
+ BLOCK_PASSING_EXPECTATION_REGEXES = [/silent/, /output/, /raise/, /throw/]
5
+
6
+ private
7
+
8
+ def expectation_names
9
+ MiniTest::Expectations.instance_methods.select do |method|
10
+ detect_block_passing_expectation method
11
+ end
12
+ end
13
+
14
+ def detect_block_passing_expectation method
15
+ BLOCK_PASSING_EXPECTATION_REGEXES.detect do |regex|
16
+ regex.match method
17
+ end
18
+ end
19
+ end
@@ -65,6 +65,7 @@ describe 'expect syntax' do
65
65
  end
66
66
 
67
67
  it 'supports must_be_silent as to_be_silent' do
68
+ expect { '' }.to_be_silent
68
69
  expect(->{''}).to_be_silent
69
70
  end
70
71
 
@@ -93,18 +94,20 @@ describe 'expect syntax' do
93
94
  end
94
95
 
95
96
  it 'supports must_match as to_match' do
96
- expect(/(fart)*/).to_match 'fartfartfartfart'
97
+ expect('fartfartfartfart').to_match /(fart)*/
97
98
  end
98
99
 
99
100
  it 'supports wont_match as to_not_match' do
100
- expect(/fart/).to_not_match 'barf'
101
+ expect('bart').to_not_match /fart/
101
102
  end
102
103
 
103
104
  it 'supports must_output as to_output' do
105
+ expect { puts 'barf' }.to_output "barf\n"
104
106
  expect(->{ puts 'barf' }).to_output "barf\n"
105
107
  end
106
108
 
107
109
  it 'supports must_raise as to_raise' do
110
+ expect { raise RuntimeError }.to_raise RuntimeError
108
111
  expect(->{ raise RuntimeError }).to_raise RuntimeError
109
112
  end
110
113
 
@@ -117,6 +120,7 @@ describe 'expect syntax' do
117
120
  end
118
121
 
119
122
  it 'supports must_throw as to_throw' do
123
+ expect { throw StandardError }.to_throw StandardError
120
124
  expect(->{ throw StandardError }).to_throw StandardError
121
125
  end
122
126
  end
@@ -0,0 +1,46 @@
1
+ $:.unshift File.expand_path("../../lib", __FILE__)
2
+
3
+ require 'minitest/spec/expect'
4
+ require 'minitest/autorun'
5
+
6
+ describe Kernel do
7
+ describe '#expect' do
8
+ it 'explodes if nothing is passed to it' do
9
+ ->{ expect }.must_raise ArgumentError, 'must pass an argument or a block'
10
+ end
11
+
12
+ it 'explodes if both an arg and a block passed to it' do
13
+ ->{ expect(:straw_man) { :cinder } }.
14
+ must_raise ArgumentError, 'cannot handle both an argument and a block'
15
+ end
16
+
17
+ describe 'when a block is passed' do
18
+ before do
19
+ expectations = MiniTest::Expectations.instance_methods
20
+ block_expectation_regexes = [/silent/, /output/, /raise/, /throw/]
21
+
22
+ non_block_expectations = expectations.reject do |method|
23
+ block_expectation_regexes.detect do |regex|
24
+ method.to_s.match regex
25
+ end
26
+ end
27
+
28
+ transpositions = { 'must' => 'to', 'wont' => 'to_not' }
29
+
30
+ @non_block_expect_methods = non_block_expectations.map do |expectation|
31
+ non_block_expect_method = transpositions.inject('') do |memo, transposition|
32
+ string = memo.empty? ? expectation.to_s : memo
33
+
34
+ string.gsub transposition.first, transposition.last
35
+ end.to_sym
36
+ end
37
+ end
38
+
39
+ it "won't respond to non-block-passing expectations" do
40
+ @non_block_expect_methods.each do |method|
41
+ expect { :muscular_son }.wont_respond_to method
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-spec-expect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Dave Jachimiak
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-05-16 00:00:00.000000000 Z
11
+ date: 2013-05-23 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Expect syntax for minitest specs.
15
14
  email: dave.jachimiak@gmail.com
@@ -18,36 +17,41 @@ extensions: []
18
17
  extra_rdoc_files: []
19
18
  files:
20
19
  - .gitignore
20
+ - .travis.yml
21
+ - CHANGELOG.md
21
22
  - README.md
22
23
  - Rakefile
23
24
  - lib/minitest/spec/expect.rb
24
25
  - lib/minitest/spec/expect/version.rb
25
26
  - lib/minitest/spec/expect_syntax.rb
27
+ - lib/minitest/spec/expect_syntax_for_arg.rb
28
+ - lib/minitest/spec/expect_syntax_for_block.rb
26
29
  - minitest-spec-expect.gemspec
27
30
  - spec/integration_spec.rb
31
+ - spec/kernel_spec.rb
28
32
  homepage: http://github.com/davejachimiak/minitest-spec-expect
29
33
  licenses: []
34
+ metadata: {}
30
35
  post_install_message:
31
36
  rdoc_options: []
32
37
  require_paths:
33
38
  - lib
34
39
  required_ruby_version: !ruby/object:Gem::Requirement
35
- none: false
36
40
  requirements:
37
- - - ! '>='
41
+ - - '>='
38
42
  - !ruby/object:Gem::Version
39
43
  version: '0'
40
44
  required_rubygems_version: !ruby/object:Gem::Requirement
41
- none: false
42
45
  requirements:
43
- - - ! '>='
46
+ - - '>='
44
47
  - !ruby/object:Gem::Version
45
48
  version: '0'
46
49
  requirements: []
47
50
  rubyforge_project:
48
- rubygems_version: 1.8.25
51
+ rubygems_version: 2.0.2
49
52
  signing_key:
50
- specification_version: 3
53
+ specification_version: 4
51
54
  summary: Use the expect syntax with transposed minitest expectations.
52
55
  test_files:
53
56
  - spec/integration_spec.rb
57
+ - spec/kernel_spec.rb