File: lambda_spec.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 (62 lines) | stat: -rw-r--r-- 2,067 bytes parent folder | download | duplicates (4)
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
require_relative '../../spec_helper'
require_relative 'fixtures/common'

describe "Proc#lambda?" do
  it "returns true if the Proc was created from a block with the lambda keyword" do
    -> {}.lambda?.should be_true
  end

  it "returns false if the Proc was created from a block with the proc keyword" do
    proc {}.lambda?.should be_false
  end

  it "returns false if the Proc was created from a block with Proc.new" do
    Proc.new {}.lambda?.should be_false
  end

  ruby_version_is ""..."3.3" do
    it "is preserved when passing a Proc with & to the lambda keyword" do
      suppress_warning {lambda(&->{})}.lambda?.should be_true
      suppress_warning {lambda(&proc{})}.lambda?.should be_false
    end
  end

  it "is preserved when passing a Proc with & to the proc keyword" do
    proc(&->{}).lambda?.should be_true
    proc(&proc{}).lambda?.should be_false
  end

  it "is preserved when passing a Proc with & to Proc.new" do
    Proc.new(&->{}).lambda?.should be_true
    Proc.new(&proc{}).lambda?.should be_false
  end

  it "returns false if the Proc was created from a block with &" do
    ProcSpecs.new_proc_from_amp{}.lambda?.should be_false
  end

  it "is preserved when the Proc was passed using &" do
    ProcSpecs.new_proc_from_amp(&->{}).lambda?.should be_true
    ProcSpecs.new_proc_from_amp(&proc{}).lambda?.should be_false
    ProcSpecs.new_proc_from_amp(&Proc.new{}).lambda?.should be_false
  end

  it "returns true for a Method converted to a Proc" do
    m = :foo.method(:to_s)
    m.to_proc.lambda?.should be_true
    ProcSpecs.new_proc_from_amp(&m).lambda?.should be_true
  end

  # [ruby-core:24127]
  it "is preserved when a Proc is curried" do
    ->{}.curry.lambda?.should be_true
    proc{}.curry.lambda?.should be_false
    Proc.new{}.curry.lambda?.should be_false
  end

  it "is preserved when a curried Proc is called without enough arguments" do
    -> x, y{}.curry.call(42).lambda?.should be_true
    proc{|x,y|}.curry.call(42).lambda?.should be_false
    Proc.new{|x,y|}.curry.call(42).lambda?.should be_false
  end
end