File: option_test.rb

package info (click to toggle)
ruby-trailblazer-option 0.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 132 kB
  • sloc: ruby: 212; sh: 4; makefile: 4
file content (205 lines) | stat: -rw-r--r-- 4,934 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
require "test_helper"

class OptionTest < Minitest::Spec
  describe "positional and kws" do
    def assert_result(result, block = nil)
      _(result).must_equal([{a: 1}, 2, {b: 3}, block])

      _(positional.inspect).must_equal %({:a=>1})
      _(keywords.inspect).must_equal %({:a=>2, :b=>3})
    end

    class Step
      def with_positional_and_keywords(options, a: nil, **more_options, &block)
        [options, a, more_options, block]
      end
    end

    WITH_POSITIONAL_AND_KEYWORDS = ->(options, a: nil, **more_options, &block) do
      [options, a, more_options, block]
    end

    class WithPositionalAndKeywords
      def self.call(options, a: nil, **more_options, &block)
        [options, a, more_options, block]
      end
    end

    let(:positional) { {a: 1} }
    let(:keywords)   { {a: 2, b: 3} }

    let(:block) { ->(*) { snippet } }

    describe ":method" do
      let(:option) { Trailblazer::Option(:with_positional_and_keywords) }

      it "passes through all args" do
        step = Step.new

        # positional = { a: 1 }
        # keywords   = { a: 2, b: 3 }
        assert_result option.(positional, keyword_arguments: keywords, exec_context: step)
      end

      it "allows passing a block, too" do
        step = Step.new

        assert_result option.(positional, keyword_arguments: keywords, exec_context: step, &block), block
      end
    end

    describe "lambda" do
      let(:option) { Trailblazer::Option(WITH_POSITIONAL_AND_KEYWORDS) }

      it "-> {} lambda" do
        step = Step.new

        assert_result option.(positional, **{keyword_arguments: keywords, exec_context: step})
      end
    end

    describe "Callable" do
      let(:option) { Trailblazer::Option(WithPositionalAndKeywords) }

      it "passes through all args" do
        assert_result option.(positional, keyword_arguments: keywords, exec_context: nil)
      end

      it "allows passing a block, too" do
        assert_result option.(positional, keyword_arguments: keywords, exec_context: nil, &block), block
      end
    end
  end

  describe "positionals" do
    def assert_result_pos(result)
      _(result).must_equal([1, 2, [3, 4]])
      _(positionals).must_equal [1, 2, 3, 4]
    end

    # In Ruby < 3.0, {*args} will grab both positionals and keyword arguments.
    class Step
      def with_positionals(a, b, *args)
        [a, b, args]
      end
    end

    WITH_POSITIONALS = ->(a, b, *args) do
      [a, b, args]
    end

    class WithPositionals
      def self.call(a, b, *args)
        [a, b, args]
      end
    end

    let(:positionals) { [1, 2, 3, 4] }

    it ":method" do
      step = Step.new

      option = Trailblazer::Option(:with_positionals)

      assert_result_pos option.(*positionals, exec_context: step)
    end

    it "-> {} lambda" do
      option = Trailblazer::Option(WITH_POSITIONALS)

      assert_result_pos option.(*positionals, exec_context: "something")
    end

    it "callable" do
      option = Trailblazer::Option(WithPositionals)

      assert_result_pos option.(*positionals, exec_context: "something")
    end
  end

  describe "keywords" do
    def assert_result_kws(result)
      _(keywords).must_equal({ a: 1, b: 2, c: 3, d: 4 })
      _(result).must_equal([1, 2, { c: 3, d: 4 }])
    end

    # In Ruby < 3.0, {*args} will grab both positionals and keyword arguments.
    class Step
      def with_keywords(a:, b:, **rest)
        [a, b, rest]
      end
    end

    WITH_KEYWORDS = ->(a:, b:, **rest) do
      [a, b, rest]
    end

    class WithKeywords
      def self.call(a:, b:, **rest)
        [a, b, rest]
      end
    end

    let(:keywords) { { a: 1, b: 2, c: 3, d: 4 } }

    it ":method" do
      step = Step.new

      option = Trailblazer::Option(:with_keywords)

      assert_result_kws option.(keyword_arguments: keywords, exec_context: step)
    end

    it "-> {} lambda" do
      option = Trailblazer::Option(WITH_KEYWORDS)

      assert_result_kws option.(keyword_arguments: keywords, exec_context: "something")
    end

    it "callable" do
      option = Trailblazer::Option(WithKeywords)

      assert_result_kws option.(keyword_arguments: keywords, exec_context: "something")
    end
  end

  describe "no arguments" do
    def assert_result_no_args(result)
      _(result).must_equal([])
    end

    class Step
      def with_no_args
        []
      end
    end

    class WithNoArgs
      def self.call
        []
      end
    end

    WITH_NO_ARGS = -> { [] }

    it ":method" do
      step = Step.new

      option = Trailblazer::Option(:with_no_args)

      assert_result_no_args option.(exec_context: step)
    end

    it "-> {} lambda" do
      option = Trailblazer::Option(WITH_NO_ARGS)

      assert_result_no_args option.(exec_context: "something")
    end

    it "callable" do
      option = Trailblazer::Option(WithNoArgs)

      assert_result_no_args option.(exec_context: "something")
    end
  end
end