bacon-expect 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd266c3a53a363e346aa4df5bb11091e35508acc
4
- data.tar.gz: edcb090d4d34de2bedf82da380a785d3d6d457ca
3
+ metadata.gz: bc5cacf63327158e64370a0bf0b59175b23f9f19
4
+ data.tar.gz: fd37fa78e8247be2d729564a724826ee41a240bb
5
5
  SHA512:
6
- metadata.gz: 8acc992ba59f6377cddf0355a71fb97b25940ad9310b2c58e50ca6fcf6bf9008d0c8474a26edfb29b6a51dabc83bc72e2c663dc0e3456a2082dd2e79e4b5d15a
7
- data.tar.gz: e26b9e08d218ea32f43b0153e5dfaca0d5677aec82c3aeac52360b7b8071da8d01f8a60aa4819dd29e2dea873deb1105a2705c8c2458aa88befa83c1217eef55
6
+ metadata.gz: 1d33f8128f35925f2b4a511ac1c9ef9bb23ce367ae90a74ce5b9413e3d9f686539436d4be12762b4ca072369cbfdf86534f9f760922aa8a323a713877f67250f
7
+ data.tar.gz: ed8b806dbcbe20d5d87c16f59db77b93678e9328c157ba93f7c513bdda72a0106396f742f37927f75c13d9072576957080833deb572b6db83c874718e0491883
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # BaconExpect
2
2
 
3
- Bring RSpec 3.0 expect syntax to MacBacon
3
+ Bring [RSpec 3.0 expect syntax](https://www.relishapp.com/rspec/rspec-expectations/v/3-0/docs/built-in-matchers) to RubyMotion's MacBacon.
4
+ ```ruby
5
+ expect(view("Tap me")).not_to be_hidden
6
+ ```
7
+
8
+ Expect syntax allows a cleaner implementation because it doesn't need to monkey-patch the Object class to install the `should` method.
4
9
 
5
10
  #Installation
6
11
  ```
@@ -10,3 +15,20 @@ gem install bacon-expect
10
15
  gem 'bacon-expect'
11
16
  ```
12
17
 
18
+ #Usage
19
+ This gem ports most of the built-in matchers found in [RSpec 3.0](https://www.relishapp.com/rspec/rspec-expectations/v/3-0/docs/built-in-matchers)
20
+
21
+ **A full list of matchers examples can be found in the [sample app specs.](spec_app/spec/matchers)**
22
+
23
+ Examples:
24
+ ```ruby
25
+ expect(nil).to be_nil
26
+ expect(true).not_to be_false
27
+
28
+ expect("super").to end_with("per")
29
+ expect{ 1/0 }.to raise_error(ZeroDivisionError)
30
+ expect{ raise ArgumentError.new("with a message") }.to raise_error(ArgumentError, /message/)
31
+ expect([1,2,3]).to have(3).items
32
+ expect("string").to respond_to(:upcase)
33
+ ```
34
+
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "bacon-expect"
7
- spec.version = "1.0.1"
7
+ spec.version = "1.0.2"
8
8
  spec.authors = ["Ignacio Piantanida"]
9
9
  spec.email = ["ijpiantanida@gmail.com"]
10
10
  spec.description = "RSpec's expect syntax in MacBacon"
@@ -1,3 +1,4 @@
1
1
  module BaconExpect
2
2
  class FailedExpectation < RuntimeError; end
3
+ class InvalidMatcher < RuntimeError; end
3
4
  end
@@ -12,28 +12,26 @@ module BaconExpect
12
12
  end
13
13
 
14
14
  def to(matcher)
15
- unless matcher.matches?(@subject, &@subject_block)
16
- raise matcher.fail!(@subject, &@subject_block)
17
- end
15
+ fail(matcher) unless matcher_passes(matcher)
18
16
  assert
19
17
  end
20
18
 
21
19
  def not_to(matcher)
22
- if matcher.matches?(@subject, &@subject_block)
23
- raise matcher.fail!(@subject, &@subject_block)
24
- end
20
+ fail(matcher) if matcher_passes(matcher)
25
21
  assert
26
22
  end
27
23
  alias_method :to_not, :not_to
28
24
 
25
+ def matcher_passes(matcher)
26
+ matcher.matches?(@subject, &@subject_block)
27
+ end
28
+
29
+ def fail(matcher)
30
+ raise matcher.fail!(@subject, &@subject_block)
31
+ end
32
+
29
33
  def assert
30
34
  true.should == true
31
35
  end
32
36
  end
33
- end
34
-
35
- module Spector
36
-
37
-
38
-
39
- end
37
+ end
@@ -0,0 +1,15 @@
1
+ module BaconExpect; module Matcher
2
+ class Be
3
+ def initialize(value)
4
+ @value = value
5
+ end
6
+
7
+ def matches?(subject)
8
+ subject.equal?(@value)
9
+ end
10
+
11
+ def fail!(subject)
12
+ raise FailedExpectation.new("#{subject} expected to be equal? to #{@value}")
13
+ end
14
+ end
15
+ end; end
@@ -0,0 +1,21 @@
1
+ module BaconExpect; module Matcher
2
+ class BeWithin
3
+ def initialize(range)
4
+ @range = range
5
+ end
6
+
7
+ def of(center_value)
8
+ @center_value = center_value
9
+ self
10
+ end
11
+
12
+ def matches?(subject)
13
+ raise InvalidMatcher.new("be_within matcher incomplete. Missing .of value") unless @center_value
14
+ (subject - @center_value).abs <= @range
15
+ end
16
+
17
+ def fail!(subject)
18
+ raise FailedExpectation.new("#{subject} expected to be within #{@range} of #{@center_value}")
19
+ end
20
+ end
21
+ end; end
@@ -0,0 +1,15 @@
1
+ module BaconExpect; module Matcher
2
+ class Eq
3
+ def initialize(value)
4
+ @value = value
5
+ end
6
+
7
+ def matches?(subject)
8
+ subject == @value
9
+ end
10
+
11
+ def fail!(subject)
12
+ raise FailedExpectation.new("#{subject} expected to be == to #{@value}")
13
+ end
14
+ end
15
+ end; end
@@ -5,11 +5,11 @@ module BaconExpect; module Matcher
5
5
  end
6
6
 
7
7
  def matches?(subject)
8
- subject == @value
8
+ subject.eql?(@value)
9
9
  end
10
10
 
11
11
  def fail!(subject)
12
- raise FailedExpectation.new("#{subject} expected to be == to #{@value}")
12
+ raise FailedExpectation.new("#{subject} expected to be eql? to #{@value}")
13
13
  end
14
14
  end
15
15
  end; end
@@ -16,11 +16,20 @@ module BaconExpect
16
16
  def raise_error(exception_class = Exception, message = "")
17
17
  RaiseError.new(exception_class, message)
18
18
  end
19
+ alias_method :raise_exception, :raise_error
19
20
 
20
21
  def eql(value)
21
22
  Eql.new(value)
22
23
  end
23
- alias_method :be, :eql
24
+
25
+ def be(value)
26
+ Be.new(value)
27
+ end
28
+ alias_method :equal, :be
29
+
30
+ def eq(value)
31
+ Eq.new(value)
32
+ end
24
33
 
25
34
  def match(regex)
26
35
  Match.new(regex)
@@ -59,6 +68,10 @@ module BaconExpect
59
68
  Change.new(change_block)
60
69
  end
61
70
 
71
+ def be_within(range)
72
+ BeWithin.new(range)
73
+ end
74
+
62
75
  def method_missing(method_name, *args, &block)
63
76
  string_method_name = method_name.to_s
64
77
  match_be = string_method_name.match(/^be_(.*)/)
@@ -0,0 +1,21 @@
1
+ describe "Matcher::BeWithin" do
2
+ it "passes when subject is within range" do
3
+ expect(27.5).to be_within(0.5).of(27.9)
4
+ expect(27.5).to be_within(0.5).of(28.0)
5
+ expect(27.5).to be_within(0.5).of(27.1)
6
+ expect(27.5).to be_within(0.5).of(27.0)
7
+ expect(27.5).not_to be_within(0.5).of(28.1)
8
+ expect(27.5).not_to be_within(0.5).of(26.9)
9
+ end
10
+
11
+ it "fails when subject is not within range" do
12
+ expect_failure{ expect(27.5).not_to be_within(0.5).of(28) }
13
+ expect_failure{ expect(27.5).not_to be_within(0.5).of(27) }
14
+ expect_failure{ expect(27.5).to be_within(0.5).of(28.1) }
15
+ expect_failure{ expect(27.5).to be_within(0.5).of(26.9) }
16
+ end
17
+
18
+ it "raises an exception when the matcher is not complete" do
19
+ expect{ expect(1).to be_within(10) }.to raise_error(BaconExpect::InvalidMatcher)
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ describe "Matcher::Equality" do
2
+ it 'eql passes when value is eql? to subject' do
3
+ expect(1).to eql(1)
4
+ end
5
+
6
+ it "eql fails when value is not eql? subject" do
7
+ expect_failure{ expect(1).to eql(1.0) }
8
+ end
9
+
10
+ it "eq passes if value is == to subject" do
11
+ expect(1).to eq(1.0)
12
+ end
13
+
14
+ it "eq fails if value is == to subject" do
15
+ expect_failure{ expect(1).to eq(2.0) }
16
+ end
17
+
18
+ it "be passes if value is same object as subject" do
19
+ object = Object.new
20
+ expect(object).to be(object)
21
+ end
22
+
23
+ it "be fails if value is not the same object as subject" do
24
+ expect_failure{ expect("super").to be("super") }
25
+ end
26
+
27
+ it "equal fails if value is not the same object as subject" do
28
+ expect_failure{ expect("super").to equal("super") }
29
+ end
30
+ end
@@ -54,4 +54,8 @@ describe "Matcher::RaiseError" do
54
54
  it "raise_error with a class and a regex argument fails if the block raises an exception of a different class" do
55
55
  expect_failure{ expect{ raise ArgumentError.new("with a message") }.to raise_error(ZeroDivisionError, /message/) }
56
56
  end
57
+
58
+ it "raise_exception is an alias of raise_error " do
59
+ expect{ 1/0 }.to raise_exception(ZeroDivisionError)
60
+ end
57
61
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bacon-expect
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ignacio Piantanida
@@ -53,14 +53,17 @@ files:
53
53
  - bacon-expect.gemspec
54
54
  - lib/bacon-expect.rb
55
55
  - lib/bacon-expect/bacon_context.rb
56
+ - lib/bacon-expect/exceptions.rb
56
57
  - lib/bacon-expect/expectation.rb
57
- - lib/bacon-expect/failed_expectation.rb
58
+ - lib/bacon-expect/matchers/be.rb
58
59
  - lib/bacon-expect/matchers/be_false.rb
59
60
  - lib/bacon-expect/matchers/be_generic.rb
60
61
  - lib/bacon-expect/matchers/be_nil.rb
61
62
  - lib/bacon-expect/matchers/be_true.rb
63
+ - lib/bacon-expect/matchers/be_within.rb
62
64
  - lib/bacon-expect/matchers/change.rb
63
65
  - lib/bacon-expect/matchers/end_with.rb
66
+ - lib/bacon-expect/matchers/eq.rb
64
67
  - lib/bacon-expect/matchers/eql.rb
65
68
  - lib/bacon-expect/matchers/have_generic.rb
66
69
  - lib/bacon-expect/matchers/have_items.rb
@@ -82,9 +85,10 @@ files:
82
85
  - spec_app/spec/matchers/be_generic_spec.rb
83
86
  - spec_app/spec/matchers/be_nil_spec.rb
84
87
  - spec_app/spec/matchers/be_true_spec.rb
88
+ - spec_app/spec/matchers/be_within_spec.rb
85
89
  - spec_app/spec/matchers/change_spec.rb
86
90
  - spec_app/spec/matchers/end_with_spec.rb
87
- - spec_app/spec/matchers/eql_spec.rb
91
+ - spec_app/spec/matchers/equality_spec.rb
88
92
  - spec_app/spec/matchers/have_generic_spec.rb
89
93
  - spec_app/spec/matchers/have_items_spec.rb
90
94
  - spec_app/spec/matchers/include_spec.rb
@@ -1,19 +0,0 @@
1
- describe "Matcher::Eql" do
2
- it 'eql passes when value is == to subject' do
3
- obj = Object.new
4
- expect(obj).to eql(obj)
5
- end
6
-
7
- it "eql fails when value is not == subject" do
8
- expect_failure{ expect(Object.new).to eql(Object.new) }
9
- end
10
-
11
- it "eql fails when value is nil and subject an Object" do
12
- expect_failure{ expect(Object.new).to eql(nil) }
13
- end
14
-
15
- it "be is an alias of eql" do
16
- obj = Object.new
17
- expect(obj).to be(obj)
18
- end
19
- end