motion-expect 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.travis.yml +8 -0
  4. data/CHANGELOG.md +4 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +34 -0
  8. data/Rakefile +1 -0
  9. data/lib/motion-expect.rb +9 -0
  10. data/lib/motion-expect/bacon_context.rb +4 -0
  11. data/lib/motion-expect/exceptions.rb +4 -0
  12. data/lib/motion-expect/expectation.rb +37 -0
  13. data/lib/motion-expect/fail_message_renderer.rb +100 -0
  14. data/lib/motion-expect/matcher/be.rb +11 -0
  15. data/lib/motion-expect/matcher/be_false.rb +12 -0
  16. data/lib/motion-expect/matcher/be_generic.rb +7 -0
  17. data/lib/motion-expect/matcher/be_nil.rb +11 -0
  18. data/lib/motion-expect/matcher/be_true.rb +11 -0
  19. data/lib/motion-expect/matcher/be_within.rb +21 -0
  20. data/lib/motion-expect/matcher/change.rb +28 -0
  21. data/lib/motion-expect/matcher/end_with.rb +15 -0
  22. data/lib/motion-expect/matcher/eq.rb +11 -0
  23. data/lib/motion-expect/matcher/eql.rb +9 -0
  24. data/lib/motion-expect/matcher/have_generic.rb +16 -0
  25. data/lib/motion-expect/matcher/have_items.rb +22 -0
  26. data/lib/motion-expect/matcher/include.rb +15 -0
  27. data/lib/motion-expect/matcher/match.rb +11 -0
  28. data/lib/motion-expect/matcher/match_array.rb +21 -0
  29. data/lib/motion-expect/matcher/matchers.rb +91 -0
  30. data/lib/motion-expect/matcher/raise_error.rb +31 -0
  31. data/lib/motion-expect/matcher/respond_to.rb +28 -0
  32. data/lib/motion-expect/matcher/satisfy.rb +15 -0
  33. data/lib/motion-expect/matcher/single_method.rb +22 -0
  34. data/lib/motion-expect/matcher/start_with.rb +15 -0
  35. data/motion-expect.gemspec +22 -0
  36. data/run-tests.sh +3 -0
  37. data/spec_app/.repl_history +0 -0
  38. data/spec_app/Gemfile +5 -0
  39. data/spec_app/Rakefile +15 -0
  40. data/spec_app/app/app_delegate.rb +5 -0
  41. data/spec_app/resources/Default-568h@2x.png +0 -0
  42. data/spec_app/spec/helpers/bacon_context.rb +5 -0
  43. data/spec_app/spec/matchers/be_false_spec.rb +22 -0
  44. data/spec_app/spec/matchers/be_generic_spec.rb +28 -0
  45. data/spec_app/spec/matchers/be_nil_spec.rb +22 -0
  46. data/spec_app/spec/matchers/be_true_spec.rb +22 -0
  47. data/spec_app/spec/matchers/be_within_spec.rb +21 -0
  48. data/spec_app/spec/matchers/change_spec.rb +44 -0
  49. data/spec_app/spec/matchers/end_with_spec.rb +13 -0
  50. data/spec_app/spec/matchers/equality_spec.rb +37 -0
  51. data/spec_app/spec/matchers/have_generic_spec.rb +26 -0
  52. data/spec_app/spec/matchers/have_items_spec.rb +17 -0
  53. data/spec_app/spec/matchers/include_spec.rb +32 -0
  54. data/spec_app/spec/matchers/match_array_spec.rb +22 -0
  55. data/spec_app/spec/matchers/match_spec.rb +21 -0
  56. data/spec_app/spec/matchers/raise_error_spec.rb +65 -0
  57. data/spec_app/spec/matchers/respond_to_spec.rb +30 -0
  58. data/spec_app/spec/matchers/satisfy_spec.rb +17 -0
  59. data/spec_app/spec/matchers/start_with_spec.rb +13 -0
  60. metadata +131 -0
@@ -0,0 +1,15 @@
1
+ module MotionExpect; module Matcher
2
+ class Include
3
+ def initialize(*values)
4
+ @values = *values
5
+ end
6
+
7
+ def matches?(subject)
8
+ @values.all?{|v| subject.include?(v)}
9
+ end
10
+
11
+ def fail!(subject, negated)
12
+ raise FailedExpectation.new(FailMessageRenderer.message_for_include(negated, subject, @values))
13
+ end
14
+ end
15
+ end; end
@@ -0,0 +1,11 @@
1
+ module MotionExpect; module Matcher
2
+ class Match < SingleMethod
3
+ def initialize(value)
4
+ super(:match, value)
5
+ end
6
+
7
+ def fail_message(subject, negated)
8
+ FailMessageRenderer.message_for_match(negated, subject, @values.first)
9
+ end
10
+ end
11
+ end; end
@@ -0,0 +1,21 @@
1
+ module MotionExpect; module Matcher
2
+ class MatchArray
3
+ def initialize(expected_array)
4
+ @expected_array = expected_array
5
+ end
6
+
7
+ def matches?(subject_array)
8
+ return false unless subject_array.size == @expected_array.size
9
+ array_copy = subject_array.dup
10
+ @expected_array.all? do |item|
11
+ has = array_copy.include?(item)
12
+ array_copy.delete(item) if has
13
+ has
14
+ end
15
+ end
16
+
17
+ def fail!(subject_array, negated)
18
+ raise FailedExpectation.new(FailMessageRenderer.message_for_match_array(negated, subject_array, @expected_array))
19
+ end
20
+ end
21
+ end; end
@@ -0,0 +1,91 @@
1
+ module MotionExpect
2
+ module Matcher
3
+ module BaconContext
4
+ def be_nil
5
+ BeNil.new
6
+ end
7
+
8
+ def be_true
9
+ BeTrue.new
10
+ end
11
+
12
+ def be_false
13
+ BeFalse.new
14
+ end
15
+
16
+ def raise_error(exception_class = Exception, message = "")
17
+ RaiseError.new(exception_class, message)
18
+ end
19
+ alias_method :raise_exception, :raise_error
20
+
21
+ def eql(value)
22
+ Eql.new(value)
23
+ end
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
33
+
34
+ def match(regex)
35
+ Match.new(regex)
36
+ end
37
+
38
+ def match_array(array)
39
+ MatchArray.new(array)
40
+ end
41
+ alias_method :contain_exactly, :match_array
42
+
43
+ def include(*values)
44
+ Include.new(*values)
45
+ end
46
+
47
+ def have(number)
48
+ HaveItems.new(number)
49
+ end
50
+
51
+ def satisfy(&block)
52
+ Satisfy.new(&block)
53
+ end
54
+
55
+ def respond_to(method_name)
56
+ RespondTo.new(method_name)
57
+ end
58
+
59
+ def start_with(substring)
60
+ StartWith.new(substring)
61
+ end
62
+
63
+ def end_with(substring)
64
+ EndWith.new(substring)
65
+ end
66
+
67
+ def change(&change_block)
68
+ Change.new(change_block)
69
+ end
70
+
71
+ def be_within(range)
72
+ BeWithin.new(range)
73
+ end
74
+
75
+ def method_missing(method_name, *args, &block)
76
+ string_method_name = method_name.to_s
77
+ match_be = string_method_name.match(/^be_(.*)/)
78
+ if match_be
79
+ BeGeneric.new(match_be[1], *args)
80
+ else
81
+ match_have = string_method_name.match(/^have_(.*)/)
82
+ if match_have
83
+ HaveGeneric.new(match_have[1], *args)
84
+ else
85
+ super
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,31 @@
1
+ module MotionExpect; module Matcher
2
+ class RaiseError
3
+ def initialize(error_class = Exception, message = "")
4
+ @error_class = error_class.is_a?(Class) ? error_class : Exception
5
+ @error_message = (error_class.is_a?(String) || error_class.is_a?(Regexp)) ? error_class : message
6
+ end
7
+
8
+ def matches?(value, &block)
9
+ begin
10
+ block.call
11
+ false
12
+ rescue Exception => e
13
+ @rescued_exception = e
14
+ exception_matches(e)
15
+ end
16
+ end
17
+
18
+ def exception_matches(e)
19
+ e.is_a?(@error_class) && (
20
+ (@error_message.is_a?(String) && e.message.include?(@error_message)) ||
21
+ (@error_message.is_a?(Regexp) && @error_message.match(e.message))
22
+ )
23
+ end
24
+
25
+ def fail!(subject, negated)
26
+ show_class = @error_class != Exception
27
+ show_message = !@error_message.is_a?(String) || !@error_message.empty?
28
+ raise FailedExpectation.new(FailMessageRenderer.message_for_raise_error(negated, show_class, @error_class, show_message, @error_message, @rescued_exception))
29
+ end
30
+ end
31
+ end; end
@@ -0,0 +1,28 @@
1
+ module MotionExpect; module Matcher
2
+ class RespondTo
3
+ def initialize(method_name)
4
+ @method_name = method_name
5
+ end
6
+
7
+ def with(number_of_args)
8
+ @number_of_args = number_of_args
9
+ self
10
+ end
11
+
12
+ def arguments
13
+ self
14
+ end
15
+ alias_method :argument, :arguments
16
+
17
+ def matches?(subject)
18
+ valid = true
19
+ valid &&= subject.respond_to?(@method_name)
20
+ valid &&= subject.method(@method_name).arity == @number_of_args if valid && @number_of_args
21
+ valid
22
+ end
23
+
24
+ def fail!(subject, negated)
25
+ raise FailedExpectation.new(FailMessageRenderer.message_for_respond_to(negated, subject, @method_name, @number_of_args))
26
+ end
27
+ end
28
+ end; end
@@ -0,0 +1,15 @@
1
+ module MotionExpect; module Matcher
2
+ class Satisfy
3
+ def initialize(&condition_block)
4
+ @condition_block = condition_block
5
+ end
6
+
7
+ def matches?(*values)
8
+ @condition_block.call(*values)
9
+ end
10
+
11
+ def fail!(subject, negated)
12
+ raise FailedExpectation.new(FailMessageRenderer.message_for_satisfy(negated))
13
+ end
14
+ end
15
+ end; end
@@ -0,0 +1,22 @@
1
+ module MotionExpect
2
+ module Matcher
3
+ class SingleMethod
4
+ def initialize(method_name, *values)
5
+ @values = values
6
+ @method_name = method_name
7
+ end
8
+
9
+ def matches?(subject)
10
+ subject.send(@method_name, *@values)
11
+ end
12
+
13
+ def fail!(subject, negated)
14
+ raise FailedExpectation.new(self.fail_message(subject, negated))
15
+ end
16
+
17
+ def fail_message(subject, negated = false)
18
+ FailMessageRenderer.message_for_be(negated, subject, @method_name, @values)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,15 @@
1
+ module MotionExpect; module Matcher
2
+ class StartWith
3
+ def initialize(start_string)
4
+ @start_string = start_string
5
+ end
6
+
7
+ def matches?(subject)
8
+ subject[0...@start_string.size] == @start_string
9
+ end
10
+
11
+ def fail!(subject, negated)
12
+ raise FailedExpectation.new(FailMessageRenderer.message_for_start_with(negated, subject, @start_string))
13
+ end
14
+ end
15
+ end; end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "motion-expect"
7
+ spec.version = "2.0.0"
8
+ spec.authors = ["Ignacio Piantanida", "Lori Olson"]
9
+ spec.email = ["lori@wndx.com"]
10
+ spec.description = "RSpec's expect syntax in MacBacon"
11
+ spec.summary = "Bring RSpec 3.0 expect syntax to MacBacon"
12
+ spec.homepage = "https://github.com/rubymotion-community/motion-expect"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ end
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env sh
2
+ cd spec_app
3
+ rake spec
File without changes
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake'
4
+ # Add your dependencies here:
5
+ gem 'motion-expect', :path => ".."
@@ -0,0 +1,15 @@
1
+ # -*- coding: utf-8 -*-
2
+ $:.unshift("/Library/RubyMotion/lib")
3
+ $:.unshift("~/.rubymotion/rubymotion-templates")
4
+ require 'motion/project/template/ios'
5
+
6
+ begin
7
+ require 'bundler'
8
+ Bundler.require
9
+ rescue LoadError
10
+ end
11
+
12
+ Motion::Project::App.setup do |app|
13
+ # Use `rake config' to see complete project settings.
14
+ app.name = 'sample'
15
+ end
@@ -0,0 +1,5 @@
1
+ class AppDelegate
2
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
3
+ true
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Bacon::Context
2
+ def expect_failure(fail_message = "", &block)
3
+ expect(&block).to raise_error(MotionExpect::FailedExpectation, fail_message)
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ describe "Matcher::BeFalse" do
2
+ it 'be_false passes when value is false' do
3
+ expect(false).to be_false
4
+ end
5
+
6
+ it "be_false fails when value is true" do
7
+ expect_failure("true expected to be false"){ expect(true).to be_false }
8
+ end
9
+
10
+ it "be_false fails when value is an Object" do
11
+ object = Object.new
12
+ expect_failure("#{object} expected to be false"){ expect(object).to be_false }
13
+ end
14
+
15
+ it "be_false fails when value is false but asked for not_to" do
16
+ expect_failure("false not expected to be false"){ expect(false).not_to be_false }
17
+ end
18
+
19
+ it "be_false passes when value is true but asked for not_to" do
20
+ expect(true).not_to be_false
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ describe "Matcher::BeGeneric" do
2
+ it 'be_empty passes when value is empty' do
3
+ expect([]).to be_empty
4
+ end
5
+
6
+ it "be_empty fails when value is not empty" do
7
+ expect_failure("[2, 3] #empty? expected to return true"){ expect([2, 3]).to be_empty }
8
+ end
9
+
10
+ it "be_kind_of passes when value is of the given type" do
11
+ expect("i'm a string").to be_kind_of(String)
12
+ end
13
+
14
+ it "be_kind_of fails when value is not of the given type" do
15
+ expect_failure("\"i'm a string\" #kind_of?(TrueClass) expected to return true"){ expect("i'm a string").to be_kind_of(TrueClass) }
16
+ end
17
+
18
+ it "be_amazing passes when the value responds to amazing? and returns true" do
19
+ class TestClass; def amazing?; true; end; end
20
+ expect(TestClass.new).to be_amazing
21
+ end
22
+
23
+ it "be_amazing fails when the value responds to amazing? and returns false" do
24
+ class TestClass; def amazing?; false; end; end
25
+ object = TestClass.new
26
+ expect_failure("#{object.inspect} #amazing? expected to return true"){ expect(object).to be_amazing }
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ describe "Matcher::BeNil" do
2
+ it 'be_nil passes when value is nil' do
3
+ expect(nil).to be_nil
4
+ end
5
+
6
+ it "be_nil fails when value is true" do
7
+ expect_failure("true expected to be nil"){ expect(true).to be_nil }
8
+ end
9
+
10
+ it "be_nil fails when value is false" do
11
+ expect_failure("false expected to be nil"){ expect(false).to be_nil }
12
+ end
13
+
14
+ it "be_nil fails when value is an Object" do
15
+ object = Object.new
16
+ expect_failure("#{object} expected to be nil"){ expect(object).to be_nil }
17
+ end
18
+
19
+ it "be_nil fails when value is nil but asked not to" do
20
+ expect_failure("nil not expected to be nil"){ expect(nil).not_to be_nil }
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ describe "Matcher::BeTrue" do
2
+ it 'be_true passes when value is true' do
3
+ expect(true).to be_true
4
+ end
5
+
6
+ it "be_true fails when value is false" do
7
+ expect_failure("false expected to be true"){ expect(false).to be_true }
8
+ end
9
+
10
+ it "be_true fails when value is an Object" do
11
+ object = Object.new
12
+ expect_failure("#{object} expected to be true"){ expect(object).to be_true }
13
+ end
14
+
15
+ it "be_true fails when value is true but asked for not_to" do
16
+ expect_failure("true not expected to be true"){ expect(true).not_to be_true }
17
+ end
18
+
19
+ it "be_true passes when value is false but asked for not_to" do
20
+ expect(false).to_not be_true
21
+ end
22
+ end
@@ -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("27.5 not expected to be within 0.5 of 28"){ expect(27.5).not_to be_within(0.5).of(28) }
13
+ expect_failure("27.5 not expected to be within 0.5 of 27"){ expect(27.5).not_to be_within(0.5).of(27) }
14
+ expect_failure("27.5 expected to be within 0.5 of 28.5"){ expect(27.5).to be_within(0.5).of(28.5) }
15
+ expect_failure("27.5 expected to be within 0.5 of 26.5"){ expect(27.5).to be_within(0.5).of(26.5) }
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(MotionExpect::InvalidMatcher)
20
+ end
21
+ end