expectations 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -135,4 +135,4 @@ expectations can be used for state based and behavior based testing.
135
135
 
136
136
  == Contributors
137
137
 
138
- Matt Mower, Ola Bini, George Malamidis, Brian Guthrie
138
+ Matt Mower, Ola Bini, George Malamidis, Brian Guthrie, Philippe Hanrigou, Steve McLarnon
@@ -0,0 +1,24 @@
1
+ module Expectations::DelegateExpectation
2
+ def execute
3
+ begin
4
+ mocha_setup
5
+ actual = instance_exec expected.mock, &block
6
+ if expected.verify(actual)
7
+ self.extend(Expectations::Results::Fulfilled)
8
+ else
9
+ self.extend(Expectations::Results::StateBasedFailure)
10
+ self.message = "Delegation may have occurred; however, the return value could not be verified. Did you specify something after the delegation method?"
11
+ end
12
+ rescue Mocha::ExpectationError => ex
13
+ self.extend(Expectations::Results::BehaviorBasedFailure)
14
+ self.message = "Expected #{expected.klass} to delegate #{expected.meth} to #{expected.subject}; however, #{expected.subject}.#{expected.meth} was never called"
15
+ rescue Exception => ex
16
+ self.extend(Expectations::Results::Error)
17
+ self.exception = ex
18
+ ensure
19
+ mocha_teardown
20
+ end
21
+ self
22
+ end
23
+
24
+ end
@@ -0,0 +1,22 @@
1
+ class Expectations::DelegateRecorder
2
+ attr_reader :klass, :subject, :meth
3
+ def initialize(klass, meth)
4
+ @klass, @subject_mock, @meth = klass, Mocha::Mock.new, meth
5
+ @subject_mock.expects(meth).returns(:the_subjects_response)
6
+ end
7
+
8
+ def to(subject)
9
+ @subject = subject
10
+ self
11
+ end
12
+
13
+ def mock
14
+ @klass.stubs(@subject).returns(@subject_mock)
15
+ @klass
16
+ end
17
+
18
+ def verify(actual)
19
+ @subject_mock.verify
20
+ actual == :the_subjects_response
21
+ end
22
+ end
@@ -5,10 +5,10 @@ class Expectations::Expectation
5
5
  def initialize(expected, &block)
6
6
  self.expected, self.block = expected, block
7
7
  self.file, self.line = eval "[__FILE__, __LINE__]", block.binding
8
- if expected.is_a?(Expectations::MockRecorder)
9
- extend(Expectations::BehaviorBasedExpectation)
10
- else
11
- extend(Expectations::StateBasedExpectation)
8
+ case
9
+ when expected.is_a?(Expectations::DelegateRecorder) then extend(Expectations::DelegateExpectation)
10
+ when expected.is_a?(Expectations::MockRecorder) then extend(Expectations::BehaviorBasedExpectation)
11
+ else extend(Expectations::StateBasedExpectation)
12
12
  end
13
13
  end
14
14
 
@@ -3,6 +3,10 @@ class Object
3
3
  Expectations::MockRecorder.new(self, method)
4
4
  end
5
5
 
6
+ def to_delegate(method)
7
+ Expectations::DelegateRecorder.new(self, method)
8
+ end
9
+
6
10
  def expectations_equal_to(other)
7
11
  self == other
8
12
  end
@@ -32,7 +32,9 @@ module Expectations::Results
32
32
  module StateBasedFailure
33
33
  include Expectations::Results
34
34
  char "F"
35
- def message
35
+ attr_writer :message
36
+ def message
37
+ return @message if @message
36
38
  result = "expected: <#{expected.inspect}> got: <#{actual.inspect}>"
37
39
  result += "\nstring details: #{expected.diff(actual)}" if expected.is_a?(String) && actual.is_a?(String)
38
40
  result
data/lib/expectations.rb CHANGED
@@ -16,6 +16,8 @@ require 'benchmark'
16
16
  require 'erb'
17
17
  require 'fileutils'
18
18
  require File.expand_path(File.dirname(__FILE__) + '/expectations/object')
19
+ require File.expand_path(File.dirname(__FILE__) + '/expectations/delegate_expectation')
20
+ require File.expand_path(File.dirname(__FILE__) + '/expectations/delegate_recorder')
19
21
  require File.expand_path(File.dirname(__FILE__) + '/expectations/xml_string')
20
22
  require File.expand_path(File.dirname(__FILE__) + '/expectations/regexp')
21
23
  require File.expand_path(File.dirname(__FILE__) + '/expectations/range')
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.1.6"
49
+ s.version = "0.1.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
@@ -1,24 +1,24 @@
1
1
  require File.dirname(__FILE__) + "/../test_helper"
2
2
 
3
3
  Expectations do
4
- expect true do
5
- Expectations::Expectation.new(1) { 2 }.execute.is_a?(Expectations::Results::StateBasedFailure)
4
+ expect Expectations::Results::StateBasedFailure do
5
+ Expectations::Expectation.new(1) { 2 }.execute
6
6
  end
7
7
 
8
- expect true do
9
- Expectations::Expectation.new(1) { 1 }.execute.is_a?(Expectations::Results::Fulfilled)
8
+ expect Expectations::Results::Fulfilled do
9
+ Expectations::Expectation.new(1) { 1 }.execute
10
10
  end
11
11
 
12
- expect true do
13
- Expectations::Expectation.new(1) { raise }.execute.is_a?(Expectations::Results::Error)
12
+ expect Expectations::Results::Error do
13
+ Expectations::Expectation.new(1) { raise }.execute
14
14
  end
15
15
 
16
16
  expect "undefined method `no_method' for Object:Class" do
17
17
  Expectations::Expectation.new(ArgumentError) { Object.no_method }.execute.exception.to_s
18
18
  end
19
19
 
20
- expect true do
21
- Expectations::Expectation.new(NoMethodError) { Object.no_method }.execute.is_a?(Expectations::Results::Fulfilled)
20
+ expect Expectations::Results::Fulfilled do
21
+ Expectations::Expectation.new(NoMethodError) { Object.no_method }.execute
22
22
  end
23
23
 
24
24
  expect nil do
@@ -70,10 +70,12 @@ Expectations do
70
70
  String
71
71
  end
72
72
 
73
+ # State based test with XML strings, whitespace between tags is ignored
73
74
  expect xml("<a><foo>bar</foo></a>") do
74
75
  "<a>\n\t<foo>bar</foo> \n</a>"
75
76
  end
76
77
 
78
+ # State based test with XML strings, whitespace between tags is ignored
77
79
  expect xml(<<-eos) do
78
80
  <one>
79
81
  <two>
@@ -84,7 +86,18 @@ Expectations do
84
86
  eos
85
87
  "<one><two><three>4</three>
86
88
  <five> 6 </five>
87
- </two></one>"
89
+ </two></one>"
90
+ end
91
+
92
+ # this is normally defined in the file specific to the class
93
+ klass = Class.new do
94
+ def save(arg)
95
+ record.save(arg)
96
+ end
97
+ end
98
+ # State based delegation test
99
+ expect klass.new.to_delegate(:save).to(:record) do |instance|
100
+ instance.save(1)
88
101
  end
89
102
 
90
103
  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.1.6
4
+ version: 0.1.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-01-23 00:00:00 -08:00
12
+ date: 2008-02-15 00:00:00 -10:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,6 +23,8 @@ extra_rdoc_files:
23
23
  - README
24
24
  files:
25
25
  - lib/expectations/behavior_based_expectation.rb
26
+ - lib/expectations/delegate_expectation.rb
27
+ - lib/expectations/delegate_recorder.rb
26
28
  - lib/expectations/expectation.rb
27
29
  - lib/expectations/mock_recorder.rb
28
30
  - lib/expectations/module.rb