File: option_test.rb

package info (click to toggle)
ruby-uber 0.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 216 kB
  • sloc: ruby: 466; makefile: 4
file content (47 lines) | stat: -rw-r--r-- 1,381 bytes parent folder | download | duplicates (3)
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
require "test_helper"
require "uber/option"

class OptionTest < Minitest::Spec
  Option = Uber::Option

  # proc
  it { Option[ ->(*args) { "proc! #{args.inspect}" } ].(1,2).must_equal "proc! [1, 2]" }
  it { Option[ lambda { "proc!" } ].().must_equal "proc!" }

  # proc with instance_exec
  it { Option[ ->(*args) { "#{self.class} #{args.inspect}" } ].(Object, 1, 2).must_equal "OptionTest [Object, 1, 2]" }
  it { Option[ ->(*args) { "#{self} #{args.inspect}" }, instance_exec: true ].(Object, 1, 2).must_equal "Object [1, 2]" }

  # static
  it { Option[true].().must_equal true }
  it { Option[nil].().must_equal nil }
  it { Option[false].().must_equal false }
  # args are ignored.
  it { Option[true].(1,2,3).must_equal true }

  # instance method
  class Hello
    def hello(*args); "Hello! #{args.inspect}" end
  end
  it { Option[:hello].(Hello.new).must_equal "Hello! []" }
  it { Option[:hello].(Hello.new, 1, 2).must_equal "Hello! [1, 2]" }

  #---
  # Callable
  class Callio
    include Uber::Callable
    def call(); "callable!" end
  end

  it { Option[Callio.new].().must_equal "callable!" }
end

# require "benchmark/ips"

# method = Uber::Option[->(context, options) { context }]
# proc = Uber::Option[A.new { |context, options| context }]

# Benchmark.ips do |x|
#   x.report(:method) { method.(Object, {}) }
#   x.report(:proc)   { proc.(Object, {}) }
# end