Sha256: 8b074af7d79ecb0cef1be54eb9d3928efc020bb6b2c2e63cdf8fca1ffe13335c

Contents?: true

Size: 1.86 KB

Versions: 17

Compression:

Stored size: 1.86 KB

Contents

require 'test_helper'

class ProcTest < ActiveSupport::TestCase

  def return_block(lambda = nil, &block)
    return block || lambda
  end

  test "#source returns a proc's source" do
    block = return_block { |f|
      do_something
    }
    assert_equal "{ |f|\n      do_something\n    }", block.source
  end

  test "#source works with a do block" do
    block = return_block do |f|
      do_something
    end

    assert_equal "do |f|\n      do_something\n    end", block.source
  end

  test "#source works with a single line block" do
    block = return_block { whatever }
    assert_equal '{ whatever }', block.source
  end

  test "#source works with a block containing a block" do
    block = return_block { whatever { another } }
    assert_equal '{ whatever { another } }', block.source
  end

  test "#source works with a stabby lambda" do
    block = return_block -> (something) { whatever }
    assert_equal '-> (something) { whatever }', block.source
  end

  test "#source work with a stabby lambda spanning lines" do
    block = return_block -> {
  whatever { foo }
}
    assert_equal "-> {\n  whatever { foo }\n}", block.source
  end

  test "#source_body returns the body of a proc" do
    block = return_block -> { whatever }
    assert_equal 'whatever', block.source_body
  end

  test "#source_body handles funky bodies" do
    block = return_block do
      something(funky) &-> { whatever }
    end
    assert_equal 'something(funky) &-> { whatever }', block.source_body
  end

  test "#source body raises exception for arity > 0" do
    block = return_block -> (foo) { whatever }
    assert_raises(RuntimeError) do
      block.source_body
    end
  end

  test ".from_source stores source of a dynamicly built proc for later inspection" do
    src = "-> { 'foo' }"
    assert_equal src, Proc.from_source(src).source
    assert_equal 'foo', Proc.from_source(src).call
  end

end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
express_admin-1.3.2 vendor/gems/express_templates/test/core_extensions/proc_test.rb
express_admin-1.3.1 vendor/gems/express_templates/test/core_extensions/proc_test.rb
express_templates-0.7.1 test/core_extensions/proc_test.rb
express_templates-0.7.0 test/core_extensions/proc_test.rb
express_admin-1.3.0 vendor/gems/express_templates/test/core_extensions/proc_test.rb
express_admin-1.2.1 vendor/gems/express_templates/test/core_extensions/proc_test.rb
express_admin-1.2.0 vendor/gems/express_templates/test/core_extensions/proc_test.rb
express_templates-0.5.0 test/core_extensions/proc_test.rb
express_templates-0.4.2 test/core_extensions/proc_test.rb
express_templates-0.4.1 test/core_extensions/proc_test.rb
express_templates-0.4.0 test/core_extensions/proc_test.rb
express_templates-0.3.6 test/core_extensions/proc_test.rb
express_templates-0.3.5 test/core_extensions/proc_test.rb
express_templates-0.3.4 test/core_extensions/proc_test.rb
express_templates-0.3.2 test/core_extensions/proc_test.rb
express_templates-0.3.1 test/core_extensions/proc_test.rb
express_templates-0.3.0 test/core_extensions/proc_test.rb