expectations 0.2.6 → 0.2.7

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.
@@ -11,4 +11,15 @@ class Expectations::Expectation
11
11
  end
12
12
  end
13
13
 
14
+ def mock(*args)
15
+ Expectations::StandardError.print "mock method called from #{caller.first.chomp(":in `__instance_exec0'")}\n"
16
+ super
17
+ end
18
+
19
+ def warn_for_expects
20
+ Object.__which_expects__ = ExpectationsExpectsMethod
21
+ yield
22
+ ensure
23
+ Object.__which_expects__ = MochaExpectsMethod
24
+ end
14
25
  end
@@ -1,4 +1,25 @@
1
1
  class Object
2
+ module MochaExpectsMethod
3
+ expects_method = Object.instance_method(:expects)
4
+ define_method :expects do |*args|
5
+ expects_method.bind(self).call(*args)
6
+ end
7
+ end
8
+
9
+ module ExpectationsExpectsMethod
10
+ def expects(*args)
11
+ Expectations::StandardError.print "expects method called from #{caller[2].chomp(":in `__instance_exec0'")}\n"
12
+ MochaExpectsMethod.instance_method(:expects).bind(self).call(*args)
13
+ end
14
+ end
15
+
16
+ attr_accessor :__which_expects__
17
+ include MochaExpectsMethod
18
+ include ExpectationsExpectsMethod
19
+ def expects(*args)
20
+ (__which_expects__ || MochaExpectsMethod).instance_method(:expects).bind(self).call(*args)
21
+ end
22
+
2
23
  def to
3
24
  Expectations::Recorder.new(self)
4
25
  end
@@ -2,8 +2,11 @@ module Expectations::RecordedExpectation
2
2
  def execute
3
3
  begin
4
4
  mocha_setup
5
- instance_exec(expected.subject!, &block) if block
6
- if expected.verify!
5
+ expected.subject!
6
+ warn_for_expects do
7
+ instance_exec(expected.subject, &block) if block
8
+ end
9
+ if expected.verify! && mocha_verify
7
10
  self.extend(Expectations::Results::Fulfilled)
8
11
  else
9
12
  self.extend(Expectations::Results::StateBasedFailure)
@@ -0,0 +1,12 @@
1
+ class Expectations::StandardError
2
+ def self.print(string)
3
+ print_suggestion
4
+ STDERR.print string
5
+ end
6
+
7
+ def self.print_suggestion
8
+ return if @suggestion_printed
9
+ @suggestion_printed = true
10
+ STDERR.print "Expectations allows you to to create multiple mock expectations, but suggests that you write another test instead.\n"
11
+ end
12
+ end
@@ -2,8 +2,15 @@ module Expectations::StateBasedExpectation
2
2
  def execute
3
3
  begin
4
4
  mocha_setup
5
- self.actual = instance_eval(&block)
6
- return self.extend(Expectations::Results::Fulfilled) if expected.expectations_equal_to(actual)
5
+ warn_for_expects do
6
+ self.actual = instance_eval(&block)
7
+ end
8
+ mocha_verify
9
+ if expected.expectations_equal_to(actual)
10
+ self.extend(Expectations::Results::Fulfilled)
11
+ else
12
+ self.extend(Expectations::Results::StateBasedFailure)
13
+ end
7
14
  rescue Exception => ex
8
15
  return self.extend(Expectations::Results::Fulfilled) if expected == ex.class
9
16
  self.extend(Expectations::Results::Error)
@@ -13,6 +20,5 @@ module Expectations::StateBasedExpectation
13
20
  ensure
14
21
  mocha_teardown
15
22
  end
16
- self.extend(Expectations::Results::StateBasedFailure)
17
23
  end
18
24
  end
@@ -1,29 +1,29 @@
1
1
  class Expectations::SuiteResults
2
2
  attr_accessor :out, :expectations
3
-
3
+
4
4
  def initialize(out)
5
5
  self.out, self.expectations = out, []
6
6
  out.print "Expectations "
7
7
  end
8
-
8
+
9
9
  def <<(expectation_result)
10
10
  out.print expectation_result.char
11
11
  self.expectations << expectation_result
12
12
  self
13
13
  end
14
-
14
+
15
15
  def succeeded?
16
16
  expectations.all? { |expectation| expectation.fulfilled? }
17
17
  end
18
-
18
+
19
19
  def fulfilled
20
20
  expectations.select { |expectation| expectation.fulfilled? }
21
21
  end
22
-
22
+
23
23
  def errors
24
24
  expectations.select { |expectation| expectation.error? }
25
25
  end
26
-
26
+
27
27
  def failures
28
28
  expectations.select { |expectation| expectation.failure? }
29
29
  end
@@ -33,29 +33,37 @@ class Expectations::SuiteResults
33
33
  run_time = 0.001 if run_time < 0.001
34
34
  out.puts "\nFinished in #{run_time.to_s.gsub(/(\d*)\.(\d{0,5}).*/,'\1.\2')} seconds"
35
35
  if succeeded?
36
- out.puts "\nSuccess: #{fulfilled.size} fulfilled"
36
+ print_success
37
37
  else
38
- out.puts "\nFailure: #{failures.size} failed, #{errors.size} errors, #{fulfilled.size} fulfilled"
39
- out.puts "\n--Errors--" if errors.any?
40
- errors.each do |error|
41
- out.puts " #{error.file}:#{error.line}:in `expect'" if ENV["TM_MODE"]
42
- out.puts "file <#{error.file}>"
43
- out.puts "line <#{error.line}>"
44
- out.puts "error <#{error.exception.message}>"
45
- out.puts "trace #{filter_backtrace(error.exception.backtrace)}"
46
- out.puts "#{error.message}" if error.message && error.message.any?
47
- out.puts "\n"
48
- end
49
- out.puts "\n--Failures--" if failures.any?
50
- failures.each do |failure|
51
- out.puts " #{failure.file}:#{failure.line}:in `expect'" if ENV["TM_MODE"]
52
- out.puts "file <#{failure.file}>"
53
- out.puts "line <#{failure.line}>"
54
- out.puts "#{failure.message}\n\n"
55
- end
38
+ print_fail
56
39
  end
57
40
  end
58
41
 
42
+ def print_success
43
+ out.puts "\nSuccess: #{fulfilled.size} fulfilled"
44
+ end
45
+
46
+ def print_fail
47
+ out.puts "\nFailure: #{failures.size} failed, #{errors.size} errors, #{fulfilled.size} fulfilled"
48
+ out.puts "\n--Errors--" if errors.any?
49
+ errors.each do |error|
50
+ out.puts " #{error.file}:#{error.line}:in `expect'" if ENV["TM_MODE"]
51
+ out.puts "file <#{error.file}>"
52
+ out.puts "line <#{error.line}>"
53
+ out.puts "error <#{error.exception.message}>"
54
+ out.puts "trace #{filter_backtrace(error.exception.backtrace)}"
55
+ out.puts "#{error.message}" if error.message && error.message.any?
56
+ out.puts "\n"
57
+ end
58
+ out.puts "\n--Failures--" if failures.any?
59
+ failures.each do |failure|
60
+ out.puts " #{failure.file}:#{failure.line}:in `expect'" if ENV["TM_MODE"]
61
+ out.puts "file <#{failure.file}>"
62
+ out.puts "line <#{failure.line}>"
63
+ out.puts "#{failure.message}\n\n"
64
+ end
65
+ end
66
+
59
67
  def write_junit_xml(path)
60
68
  FileUtils.rm_rf path if File.exist?(path)
61
69
  FileUtils.mkdir_p path
@@ -81,7 +89,7 @@ class Expectations::SuiteResults
81
89
  end
82
90
  end
83
91
  end
84
-
92
+
85
93
  def filter_backtrace(trace)
86
94
  patterns_to_strip = [/\/expectations\/lib\/expectations\//, /\/lib\/ruby\/1\.8\//]
87
95
  result = patterns_to_strip.inject(trace) do |result, element|
@@ -16,5 +16,4 @@ class Expectations::SuiteRunner
16
16
  def self.suite_eval(&block)
17
17
  self.instance.suite.instance_eval(&block)
18
18
  end
19
-
20
19
  end
data/lib/expectations.rb CHANGED
@@ -32,4 +32,5 @@ require File.expand_path(File.dirname(__FILE__) + '/expectations/suite_results')
32
32
  require File.expand_path(File.dirname(__FILE__) + '/expectations/expectation')
33
33
  require File.expand_path(File.dirname(__FILE__) + '/expectations/state_based_expectation')
34
34
  require File.expand_path(File.dirname(__FILE__) + '/expectations/mock_recorder')
35
- require File.expand_path(File.dirname(__FILE__) + '/expectations/results')
35
+ require File.expand_path(File.dirname(__FILE__) + '/expectations/results')
36
+ require File.expand_path(File.dirname(__FILE__) + '/expectations/standard_error')
data/rakefile.rb CHANGED
@@ -46,7 +46,7 @@ specification = Gem::Specification.new do |s|
46
46
  expect NoMethodError do
47
47
  Object.invalid_method_call
48
48
  end."
49
- s.version = "0.2.6"
49
+ s.version = "0.2.7"
50
50
  s.author = 'Jay Fields'
51
51
  s.description = "A lightweight unit testing framework. Tests (expectations) will be written as follows
52
52
  expect 2 do
@@ -24,6 +24,7 @@ Expectations do
24
24
  end
25
25
 
26
26
  expect Expectations::Results::BehaviorBasedFailure do
27
+ Expectations::StandardError.stubs(:print)
27
28
  suite = Expectations::Suite.new
28
29
  suite.expect Mocha::Mock.new.to.receive(:dial).with("2125551212").times(2) do |phone|
29
30
  phone.dial("2125551212")
@@ -32,6 +33,7 @@ Expectations do
32
33
  end
33
34
 
34
35
  expect Expectations::Results::BehaviorBasedFailure do
36
+ Expectations::StandardError.stubs(:print)
35
37
  suite = Expectations::Suite.new
36
38
  suite.expect(Object.to.receive(:deal)) { }
37
39
  suite.execute(Silent).expectations.first
@@ -44,6 +46,7 @@ Expectations do
44
46
  end
45
47
 
46
48
  expect Expectations::Results::Error do
49
+ Expectations::StandardError.stubs(:print)
47
50
  suite = Expectations::Suite.new
48
51
  suite.expect(2) do
49
52
  Object.expects(:bar).returns 2
@@ -53,6 +56,7 @@ Expectations do
53
56
  end
54
57
 
55
58
  expect Expectations::Results::Error do
59
+ Expectations::StandardError.stubs(:print)
56
60
  suite = Expectations::Suite.new
57
61
  suite.expect(1) do
58
62
  Object.expects(:give_me_three).with(3).returns 1
@@ -61,4 +65,42 @@ Expectations do
61
65
  suite.execute(Silent).expectations.first
62
66
  end
63
67
 
68
+ expect Expectations::Results::Error do
69
+ Expectations::StandardError.stubs(:print)
70
+ suite = Expectations::Suite.new
71
+ suite.expect(1) do
72
+ Object.expects(:foo)
73
+ end
74
+ suite.execute(Silent).expectations.first
75
+ end
76
+
77
+ expect Expectations::Results::Error do
78
+ Expectations::StandardError.stubs(:print)
79
+ suite = Expectations::Suite.new
80
+ suite.expect(1) do
81
+ mock(:foo => 1)
82
+ end
83
+ suite.execute(Silent).expectations.first
84
+ end
85
+
86
+ expect Expectations::Results::BehaviorBasedFailure do
87
+ Expectations::StandardError.stubs(:print)
88
+ suite = Expectations::Suite.new
89
+ suite.expect(Object.to.receive(:foo)) do
90
+ Object.foo
91
+ mock(:foo => 1)
92
+ end
93
+ suite.execute(Silent).expectations.first
94
+ end
95
+
96
+ expect Expectations::Results::BehaviorBasedFailure do
97
+ Expectations::StandardError.stubs(:print)
98
+ suite = Expectations::Suite.new
99
+ suite.expect(Object.to.receive(:foo)) do
100
+ Object.foo
101
+ Object.expects(:bar)
102
+ end
103
+ suite.execute(Silent).expectations.first
104
+ end
105
+
64
106
  end
@@ -1,17 +1,17 @@
1
1
  require File.dirname(__FILE__) + "/test_helper"
2
2
 
3
3
  Expectations do
4
-
4
+
5
5
  # State based expectation where a value equals another value
6
6
  expect 2 do
7
7
  1 + 1
8
8
  end
9
-
9
+
10
10
  # State based expectation where an exception is expected. Simply expect the Class of the intended exception
11
11
  expect NoMethodError do
12
12
  Object.no_method
13
13
  end
14
-
14
+
15
15
  # Behavior based test using a traditional mock
16
16
  expect mock.to.receive(:dial).with("2125551212").times(2) do |phone|
17
17
  phone.dial("2125551212")
@@ -34,34 +34,22 @@ Expectations do
34
34
  expect Object.to.receive(:deal) do
35
35
  Object.deal
36
36
  end
37
-
37
+
38
38
  # State based test utilizing a stub
39
39
  expect 2 do
40
40
  stub(:two => 2).two
41
41
  end
42
-
43
- # State based test utilizing a concrete mock
44
- expect 2 do
45
- Object.expects(:bar).returns 2
46
- Object.bar
47
- end
48
-
49
- # Behavior based test utilizing a stub and a concrete mock
50
- expect 1 do
51
- Object.expects(:give_me_three).with(3).returns 1
52
- Object.give_me_three(stub(:three=>3).three)
53
- end
54
-
42
+
55
43
  # State based test matching a Regexp
56
44
  expect /a string/ do
57
45
  "a string"
58
46
  end
59
-
47
+
60
48
  # State based test checking if actual is in the expected Range
61
49
  expect 1..5 do
62
50
  3
63
51
  end
64
-
52
+
65
53
  # State based test to determine if the object is an instance of the module
66
54
  expect Enumerable do
67
55
  []
@@ -71,33 +59,33 @@ Expectations do
71
59
  expect String do
72
60
  "a string"
73
61
  end
74
-
62
+
75
63
  # State based test to determine if the modules are the same
76
64
  expect Enumerable do
77
65
  Enumerable
78
66
  end
79
-
67
+
80
68
  # State based test to determine if the classes are the same
81
69
  expect String do
82
70
  String
83
71
  end
84
-
72
+
85
73
  # State based test with XML strings, whitespace between tags is ignored
86
74
  expect xml("<a><foo>bar</foo></a>") do
87
75
  "<a>\n\t<foo>bar</foo> \n</a>"
88
76
  end
89
-
77
+
90
78
  # State based test with XML strings, whitespace between tags is ignored
91
79
  expect xml(<<-eos) do
92
80
  <one>
93
- <two>
94
- <three>4</three>
95
- <five> 6 </five>
96
- </two>
81
+ <two>
82
+ <three>4</three>
83
+ <five> 6 </five>
84
+ </two>
97
85
  </one>
98
86
  eos
99
87
  "<one><two><three>4</three>
100
- <five> 6 </five>
88
+ <five> 6 </five>
101
89
  </two></one>"
102
90
  end
103
91
 
@@ -111,7 +99,7 @@ Expectations do
111
99
  expect klass.new.to.delegate(:save).to(:record) do |instance|
112
100
  instance.save(1)
113
101
  end
114
-
102
+
115
103
  # this is normally defined in the file specific to the class
116
104
  klass = Class.new do
117
105
  attr_accessor :started
@@ -120,7 +108,7 @@ Expectations do
120
108
  expect klass.new.to.be.started do |process|
121
109
  process.started = true
122
110
  end
123
-
111
+
124
112
  # this is normally defined in the file specific to the class
125
113
  klass = Class.new do
126
114
  attr_accessor :finished
@@ -129,8 +117,8 @@ Expectations do
129
117
  expect klass.new.to.have.finished do |process|
130
118
  process.finished = true
131
119
  end
132
-
120
+
133
121
  expect nil.to.be.nil?
134
122
  expect Object.not.to.be.nil?
135
-
123
+
136
124
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: expectations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jay Fields
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-28 00:00:00 -04:00
12
+ date: 2008-04-09 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,6 +42,7 @@ files:
42
42
  - lib/expectations/regexp.rb
43
43
  - lib/expectations/results.rb
44
44
  - lib/expectations/reverse_result.rb
45
+ - lib/expectations/standard_error.rb
45
46
  - lib/expectations/state_based_expectation.rb
46
47
  - lib/expectations/state_based_recorder.rb
47
48
  - lib/expectations/string.rb