On 29/09/06, <b class="gmail_sendername">Jay</b> &lt;<a href="mailto:jay@jayfields.com">jay@jayfields.com</a>&gt; wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I'm not sure if someone has already addressed this issue, but if not,<br>here goes.<br><br>I need the following behavior:<br>&nbsp;&nbsp; def test_generate(documentation = '/test_documentation/',<br>destination = '/destination/')<br>
&nbsp;&nbsp;&nbsp;&nbsp; yield_results = ['/', 'filename.yaml', 'blah.xml']<br>&nbsp;&nbsp;&nbsp;&nbsp; Find.expects(:find).with(documentation).yields(lambda<br>{ yield_results.shift } )<br>&nbsp;&nbsp;&nbsp;&nbsp; ...<br>&nbsp;&nbsp; end<br><br>So I made these changes...<br><br>Index: test/mocha/expectation_test.rb
<br>===================================================================<br>--- test/mocha/expectation_test.rb&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(revision 62)<br>+++ test/mocha/expectation_test.rb&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(working copy)<br>@@ -91,6 +91,11 @@<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert_equal parameters_for_yield, yielded_parameters
<br>&nbsp;&nbsp;&nbsp;&nbsp;end<br>+&nbsp;&nbsp;def test_should_yield_with_block_result<br>+&nbsp;&nbsp;&nbsp;&nbsp;expectation = new_expectation.yields( lambda { 'lambda return<br>value'&nbsp;&nbsp;} )<br>+&nbsp;&nbsp;&nbsp;&nbsp;expectation.invoke() { |*parameters| assert_equal ['lambda<br>return value'], parameters }
<br>+&nbsp;&nbsp;end<br>+<br>&nbsp;&nbsp;&nbsp;&nbsp;def test_should_return_specified_value<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expectation = new_expectation.returns(99)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert_equal 99, expectation.invoke<br>Index: lib/mocha/expectation.rb<br>===================================================================
<br>--- lib/mocha/expectation.rb&nbsp;&nbsp;&nbsp;&nbsp;(revision 62)<br>+++ lib/mocha/expectation.rb&nbsp;&nbsp;&nbsp;&nbsp;(working copy)<br>@@ -217,7 +217,12 @@<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def invoke<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@invoked += 1<br>-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield(*@parameters_to_yield) if yield? and block_given?
<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if yield? and block_given?<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params = @parameters_to_yield.collect do |element|<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;element.is_a?(Proc) ? element.call : element<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield(*params)<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@return_value.is_a?(Proc) ? @return_value.call : @return_value
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br><br>This test passes also, but I didn't think it was necessary for the<br>suite...<br>&nbsp;&nbsp; def test_should_yield_with_block_result_1123<br>&nbsp;&nbsp;&nbsp;&nbsp; expected_vals = [1,2,3]<br>&nbsp;&nbsp;&nbsp;&nbsp; expectation = new_expectation.yields( lambda
<br>{ expected_vals.shift }, 4 )<br>&nbsp;&nbsp;&nbsp;&nbsp; expectation.invoke() { |*parameters| assert_equal [1, 4],<br>parameters }<br>&nbsp;&nbsp;&nbsp;&nbsp; expectation.invoke() { |*parameters| assert_equal [2, 4],<br>parameters }<br>&nbsp;&nbsp;&nbsp;&nbsp; expectation.invoke
() { |*parameters| assert_equal [3, 4],<br>parameters }<br>&nbsp;&nbsp; end<br><br><br>Along the same thread, I thought about using a comma separated list<br>(similar to the new returns format); however, it wont work since the<br>parameters are captured as args and returned as one value.
<br><br>Thoughts?</blockquote><div><br>Thanks for your patch. I'm sorry not to have replied sooner - works been busy and then I was off sick.<br><br>I can see the need for <span style="font-family: courier new,monospace;">
yields()</span> to accept a <span style="font-family: courier new,monospace;">Proc</span> in the same way that <span style="font-family: courier new,monospace;">returns()</span> does. However, I'm not totally convinced that accepting multiple 
<span style="font-family: courier new,monospace;">Proc</span>s actually gains us anything - could you not rewrite your example test like this...<br><br>&nbsp;&nbsp; def test_should_yield_with_block_result_1123<br>&nbsp;&nbsp;&nbsp;&nbsp; expected_vals = [1,2,3]
<br>&nbsp;&nbsp;&nbsp;&nbsp; expectation = new_expectation.yields( lambda { [expected_vals.shift, 4] } )<br>&nbsp;&nbsp;&nbsp;&nbsp; expectation.invoke() { |*parameters| assert_equal [1, 4], parameters }<br>&nbsp;&nbsp;&nbsp;&nbsp; expectation.invoke() { |*parameters| assert_equal [2, 4], parameters }
<br>&nbsp;&nbsp;&nbsp;&nbsp; expectation.invoke() { |*parameters| assert_equal [3, 4], parameters }<br>&nbsp;&nbsp; end<br><br>I'm trying to decide which is the most consistent / unsurprising approach.<br><br>What do you think?<br></div></div>-- <br>James.
<br><a href="http://blog.floehopper.org">http://blog.floehopper.org</a>