File: test_passing_block.rb

package info (click to toggle)
ruby3.3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,620 kB
  • sloc: ruby: 1,244,308; ansic: 836,474; yacc: 28,074; pascal: 6,748; sh: 3,913; python: 1,719; cpp: 1,158; makefile: 742; asm: 712; javascript: 394; lisp: 97; perl: 62; awk: 36; sed: 23; xml: 4
file content (65 lines) | stat: -rw-r--r-- 2,286 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# frozen_string_literal: false
require 'test/unit'

class TestFuncall < Test::Unit::TestCase
  module Relay
    def self.target(*args, **kw, &block)
      yield(*args, **kw) if block
    end
  end
  require '-test-/funcall'

  def test_funcall_extra_args
    assert_equal 'TestFuncall', TestFuncall.extra_args_name,
	 '[ruby-core:85266] [Bug #14425]'
  end

  def test_with_funcall2
    ok = nil
    Relay.with_funcall2("feature#4504") {|arg| ok = arg || true}
    assert_nil(ok)
  end

  def test_with_funcall_passing_block
    ok = nil
    Relay.with_funcall_passing_block("feature#4504") {|arg| ok = arg || true}
    assert_equal("feature#4504", ok)
  end

  def test_with_funcall_passing_block_kw
    block = ->(*a, **kw) { [a, kw] }
    assert_equal([[1], {}], Relay.with_funcall_passing_block_kw(0, 1, &block))
    assert_equal([[{a: 1}], {}], Relay.with_funcall_passing_block_kw(0, a: 1, &block))
    assert_equal([[], {a: 1}], Relay.with_funcall_passing_block_kw(1, a: 1, &block))
    assert_equal([[1], {a: 1}], Relay.with_funcall_passing_block_kw(1, 1, a: 1, &block))
    assert_equal([[], {a: 1}], Relay.with_funcall_passing_block_kw(3, a: 1, &block))
  end

  def test_with_funcallv_public_kw
    o = Object.new
    def o.foo(*args, **kw)
      [args, kw]
    end
    def o.bar(*args, **kw)
      [args, kw]
    end
    o.singleton_class.send(:private, :bar)
    def o.baz(arg)
      arg
    end
    assert_equal([[1], {}], Relay.with_funcallv_public_kw(o, :foo, 0, 1))
    assert_equal([[{a: 1}], {}], Relay.with_funcallv_public_kw(o, :foo, 0, a: 1))
    assert_equal([[], {a: 1}], Relay.with_funcallv_public_kw(o, :foo, 1, a: 1))
    assert_equal([[1], {a: 1}], Relay.with_funcallv_public_kw(o, :foo, 1, 1, a: 1))
    assert_equal([[], {a: 1}], Relay.with_funcallv_public_kw(o, :foo, 3, a: 1))
  end

  def test_with_yield_splat_kw
    block = ->(*a, **kw) { [a, kw] }
    assert_equal([[1], {}], Relay.with_yield_splat_kw(0, [1], &block))
    assert_equal([[{a: 1}], {}], Relay.with_yield_splat_kw(0, [{a: 1}], &block))
    assert_equal([[], {a: 1}], Relay.with_yield_splat_kw(1, [{a: 1}], &block))
    assert_equal([[1], {a: 1}], Relay.with_yield_splat_kw(1, [1, {a: 1}], &block))
    assert_equal([[], {a: 1}], Relay.with_yield_splat_kw(3, [{a: 1}], &block))
  end
end