aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/arel/nodes/case_test.rb
blob: 89861488dfb745fc2e1e71a22ca65cdccf9cd85e (plain) (blame)
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
# frozen_string_literal: true

require_relative "../helper"

module Arel
  module Nodes
    class NodesTest < Arel::Spec
      describe "Case" do
        describe "#initialize" do
          it "sets case expression from first argument" do
            node = Case.new "foo"

            assert_equal "foo", node.case
          end

          it "sets default case from second argument" do
            node = Case.new nil, "bar"

            assert_equal "bar", node.default
          end
        end

        describe "#clone" do
          it "clones case, conditions and default" do
            foo = Nodes.build_quoted "foo"

            node = Case.new
            node.case = foo
            node.conditions = [When.new(foo, foo)]
            node.default = foo

            dolly = node.clone

            assert_equal dolly.case, node.case
            assert_not_same dolly.case, node.case

            assert_equal dolly.conditions, node.conditions
            assert_not_same dolly.conditions, node.conditions

            assert_equal dolly.default, node.default
            assert_not_same dolly.default, node.default
          end
        end

        describe "equality" do
          it "is equal with equal ivars" do
            foo = Nodes.build_quoted "foo"
            one = Nodes.build_quoted 1
            zero = Nodes.build_quoted 0

            case1 = Case.new foo
            case1.conditions = [When.new(foo, one)]
            case1.default = Else.new zero

            case2 = Case.new foo
            case2.conditions = [When.new(foo, one)]
            case2.default = Else.new zero

            array = [case1, case2]

            assert_equal 1, array.uniq.size
          end

          it "is not equal with different ivars" do
            foo = Nodes.build_quoted "foo"
            bar = Nodes.build_quoted "bar"
            one = Nodes.build_quoted 1
            zero = Nodes.build_quoted 0

            case1 = Case.new foo
            case1.conditions = [When.new(foo, one)]
            case1.default = Else.new zero

            case2 = Case.new foo
            case2.conditions = [When.new(bar, one)]
            case2.default = Else.new zero

            array = [case1, case2]

            assert_equal 2, array.uniq.size
          end
        end
      end
    end
  end
end