Module: LazyMethodTest

Defined in:
lib/lazy_method_test.rb

Defined Under Namespace

Classes: Foo

Instance Method Summary collapse

Instance Method Details

#test_call_basic_methods(t) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lazy_method_test.rb', line 13

def test_call_basic_methods(t)
  ary = []
  [
    [-> { $stdout.method(:puts) }, -> { $stdout.method.puts }],
    [-> { 1.method(:+) }, -> { 1.method.+ }],
    [-> { 1.method(:+@) }, -> { +(1.method) }],
    [-> { ary.method(:[]) }, -> { ary.method[0] }],
    [-> { ary.method(:[]=) }, -> { ary.method.[]=(0, 1) }],
  ].each do |expect_case, test_case|
    unless expect_case.call == test_case.call
      path, line = test_case.source_location
      code = File.open(path) { |f| (line - 1).times { f.gets }; f.gets }
      t.error("LazyMethod methods are should return Method object as much as possible")
      t.log("return #{test_case.call}, expect #{expect_case.call}")
      t.log("#{File.basename(path)}:#{line}: #{code.chomp}")
    end
  end
end

#test_method_methods(t) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lazy_method_test.rb', line 52

def test_method_methods(t)
  foo = Foo.new
  LazyMethod::METHOD_METHODS.each do |m|
    s = foo.method.__send__(m)
    unless LazyMethod === s
      t.error("should be LazyMethod instance got #{s}")
    end

    expect = foo.method(:bar).__send__(m)

    next if m == :to_proc

    unless expect == s.bar
      t.error("call #{m} expect #{expect}, got #{s.bar}")
    end
  end
end

#test_name_error(t) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lazy_method_test.rb', line 32

def test_name_error(t)
  [
    -> { method.nothing },
    -> { +method },
    -> { method[0] },
  ].each do |test_case|
    begin
      test_case.call
    rescue NameError
    else
      t.error("expect raise error but nothing raised")
    end
  end
end

#test_puts(t) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/lazy_method_test.rb', line 5

def test_puts(t)
  io = StringIO.new
  io.puts method
  unless /#<LazyMethod/ =~ io.string
    t.error(io.string)
  end
end