|
| 1 | +# frozen_string_literal: true |
| 2 | +require 'minitest/autorun' |
| 3 | +require 'pipeline' |
| 4 | +describe Pipeline do |
| 5 | + |
| 6 | + it 'is a module that refines the Object class and only that' do |
| 7 | + assert_kind_of Module, Pipeline |
| 8 | + assert_equal [Object], Pipeline.refinements.map(&:refined_class) |
| 9 | + assert_empty Pipeline.instance_methods |
| 10 | + assert_empty Pipeline.private_instance_methods |
| 11 | + end |
| 12 | + using Pipeline |
| 13 | + |
| 14 | + describe '`#method` shorthand' do |
| 15 | + it 'overrides `backticks` with a public delegate to #method or equivalent' do |
| 16 | + o = Object.new |
| 17 | + def o.echo = # do nothing for a phony name |
| 18 | + assert_equal o.method(:echo), o.`('echo') |
| 19 | + assert_equal o.method(:__id__), o.`(:__id__) |
| 20 | + pass if o.`(:initialize) |
| 21 | + end |
| 22 | + |
| 23 | + it 'renames Object#` to Object#sys' do |
| 24 | + assert_equal :`, Object.instance_method(:sys).original_name |
| 25 | + random_string = Kernel.rand.to_s |
| 26 | + assert_equal random_string, sys("echo #{random_string}").chomp |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + it 'defines Object#then_pipe that chain-calls each of the given _ToProc’s with the receiver as the first arg' do |
| 31 | + receiver, proc_return = Object.new, Object.new |
| 32 | + # It is neither required nor forbidden for `#then_pipe` to also |
| 33 | + # pass their blocks to their _ToProc arg(s) (block-ception Lol) |
| 34 | + a_proc = proc do|arg| |
| 35 | + assert_same receiver, arg |
| 36 | + proc_return |
| 37 | + end |
| 38 | + a_proc_like = Object.new |
| 39 | + a_proc_like.define_singleton_method(:to_proc) { a_proc } |
| 40 | + assert_same proc_return, receiver.then_pipe(a_proc_like) |
| 41 | + receiver.then_pipe(a_proc, ->{ assert_same proc_return, _1 }) |
| 42 | + end |
| 43 | +end |
0 commit comments