[MacRuby] #674: Shoulda fails because of oddity with block_given?
#674: Shoulda fails because of oddity with block_given? ---------------------------------+------------------------------------------ Reporter: marick@… | Owner: lsansonetti@… Type: defect | Status: new Priority: minor | Milestone: Component: MacRuby | Keywords: ---------------------------------+------------------------------------------ The attached file shows that shoulda (on 0.5 and a recent version of 0.7) behaves incorrectly. The important part is this: {{{ should "test something" do puts "I should be printed" end }}} The correct output shows that a test has been run: {{{ 1274 $ ruby shoulda-problem.rb ... I should be printed }}} With Macruby, you get this: {{{ 1275 $ macruby shoulda-problem.rb * DEFERRED: Samples should test something. }}} The problem is in this code: {{{ def should(name, options = {}, &blk) if Shoulda.current_context block_given? ? Shoulda.current_context.should(name, options, &blk) : Shoulda.current_context.should_eventually(name) else context_name = self.name.gsub(/Test/, "") context = Shoulda::Context.new(context_name, self) do ==> block_given? ? should(name, options, &blk) : should_eventually(name) end context.build end end }}} In Ruby 1.8.6 (and 1.9, I suppose, because Shoulda is said to work on 1.9), the block_given? inside a block given to an initializer is supposed to evaluate to true. (!!!) In Macruby, it evaluates to False. This problem is easy to patch (and I'd be inclined to think the Shoulda people should, given that the code is tricksy). Still, it is a deviation from reference Ruby behavior. -- Ticket URL: <http://www.macruby.org/trac/ticket/674> MacRuby <http://macruby.org/>
#674: Shoulda fails because of oddity with block_given? ---------------------------------+------------------------------------------ Reporter: marick@… | Owner: lsansonetti@… Type: defect | Status: new Priority: minor | Milestone: Component: MacRuby | Keywords: ---------------------------------+------------------------------------------ Comment(by marick@…): The file with the code I quoted above is shoulda-2.10.3/lib/context.rb Note that there are two different definitions of should() in the file. -- Ticket URL: <http://www.macruby.org/trac/ticket/674#comment:1> MacRuby <http://macruby.org/>
#674: Shoulda fails because of oddity with block_given? ---------------------------------+------------------------------------------ Reporter: marick@… | Owner: lsansonetti@… Type: defect | Status: new Priority: minor | Milestone: Component: MacRuby | Keywords: ---------------------------------+------------------------------------------ Comment(by lsansonetti@…): I have troubles reducing it. {{{ $ ./miniruby -e "def foo(&b); 1.times { p block_given? }; end; foo{}" true }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/674#comment:2> MacRuby <http://macruby.org/>
#674: Shoulda fails because of oddity with block_given? ---------------------------------+------------------------------------------ Reporter: marick@… | Owner: lsansonetti@… Type: defect | Status: new Priority: minor | Milestone: Component: MacRuby | Keywords: ---------------------------------+------------------------------------------ Comment(by marick@…): It's more complicated. It seems as if block_given? acts like a local variable bound within the block, so that it retains its value even when the block is called at some arbitrary later point. Here's a simpler example: {{{ def called(&blk) $block = blk end def should(&blk) called do puts "In MRI, you would see true instead of #{block_given?}" end end should do "some block thing" end $block.call }}} 1.8.6 produces this: {{{ 1388 $ ruby block_given.rb In MRI, you would see true instead of true }}} instead of this from a recent 0.7: {{{ 1389 $ macruby block_given.rb In MRI, you would see true instead of false }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/674#comment:3> MacRuby <http://macruby.org/>
#674: Shoulda fails because of oddity with block_given? ---------------------------------+------------------------------------------ Reporter: marick@… | Owner: lsansonetti@… Type: defect | Status: new Priority: minor | Milestone: Component: MacRuby | Keywords: ---------------------------------+------------------------------------------ Comment(by marick@…): This code is maybe clearer about what's going on: {{{ def should(&a_block_that_is_never_called) stash_block { puts "block_given? is #{block_given?}." } end def stash_block(&block) $block = block end should do "something, but not in this test" end $block.call }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/674#comment:4> MacRuby <http://macruby.org/>
#674: Shoulda fails because of oddity with block_given? ---------------------------------+------------------------------------------ Reporter: marick@… | Owner: lsansonetti@… Type: defect | Status: new Priority: minor | Milestone: Component: MacRuby | Keywords: ---------------------------------+------------------------------------------ Comment(by lsansonetti@…): Thanks a lot for the reduction, added in the test_vm suite as r4017. -- Ticket URL: <http://www.macruby.org/trac/ticket/674#comment:5> MacRuby <http://macruby.org/>
participants (1)
-
MacRuby