summaryrefslogtreecommitdiff
path: root/spec/ruby/language/or_spec.rb
blob: fb75e788f16d391288356cb668ac92e3bacf29d5 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require_relative '../spec_helper'

describe "The || operator" do
  it "evaluates to true if any of its operands are true" do
    if false || true || nil
      x = true
    end
    x.should == true
  end

  it "evaluated to false if all of its operands are false" do
    if false || nil
      x = true
    end
    x.should == nil
  end

  it "is evaluated before assignment operators" do
    x = nil || true
    x.should == true
  end

  it "has a lower precedence than the && operator" do
    x = 1 || false && x = 2
    x.should == 1
  end

  it "treats empty expressions as nil" do
    (() || true).should be_true
    (() || false).should be_false
    (true || ()).should be_true
    (false || ()).should be_nil
    (() || ()).should be_nil
  end

  it "has a higher precedence than 'break' in 'break true || false'" do
    # see also 'break true or false' below
    -> { break false || true }.call.should be_true
  end

  it "has a higher precedence than 'next' in 'next true || false'" do
    -> { next false || true }.call.should be_true
  end

  it "has a higher precedence than 'return' in 'return true || false'" do
    -> { return false || true }.call.should be_true
  end
end

describe "The or operator" do
  it "evaluates to true if any of its operands are true" do
    x = nil
    if false or true
      x = true
    end
    x.should == true
  end

  it "is evaluated after variables are assigned" do
    x = nil or true
    x.should == nil
  end

  it "has a lower precedence than the || operator" do
    x,y = nil
    x = true || false or y = 1
    y.should == nil
  end

  it "treats empty expressions as nil" do
    (() or true).should be_true
    (() or false).should be_false
    (true or ()).should be_true
    (false or ()).should be_nil
    (() or ()).should be_nil
  end

  it "has a lower precedence than 'break' in 'break true or false'" do
    # see also 'break true || false' above
    -> { eval "break true or false" }.should raise_error(SyntaxError, /void value expression/)
  end

  it "has a lower precedence than 'next' in 'next true or false'" do
    -> { eval "next true or false" }.should raise_error(SyntaxError, /void value expression/)
  end

  it "has a lower precedence than 'return' in 'return true or false'" do
    -> { eval "return true or false" }.should raise_error(SyntaxError, /void value expression/)
  end
end