spec/unit/function_spec.rb in transproc-0.2.4 vs spec/unit/function_spec.rb in transproc-0.3.0

- old
+ new

@@ -1,12 +1,30 @@ require 'spec_helper' -describe 'Transproc::Function' do +describe Transproc::Function do + let(:hashes) { Transproc::HashTransformations } + + describe '#name' do + let(:block) { proc { |v| v } } + + it 'returns the name of the module function' do + expect(hashes[:symbolize_keys].name).to eql :symbolize_keys + end + + it 'returns the explicitly assigned name' do + expect(described_class.new(block, name: :identity).name).to eql :identity + end + + it 'returns the unnamed closure' do + expect(described_class.new(block).name).to eql block + end + end + describe '#>>' do it 'composes named functions' do - f1 = t(:symbolize_keys) - f2 = t(:rename_keys, user_name: :name) + f1 = hashes[:symbolize_keys] + f2 = hashes[:rename_keys, user_name: :name] f3 = f1 >> f2 expect(f3.to_ast).to eql( [ @@ -17,11 +35,11 @@ ] ) expect(f3['user_name' => 'Jane']).to eql(name: 'Jane') - f4 = f3 >> t(:nest, :details, [:name]) + f4 = f3 >> hashes[:nest, :details, [:name]] expect(f4.to_ast).to eql( [ :symbolize_keys, [], [ @@ -52,11 +70,11 @@ ] ) end it 'plays well with registered compositions' do - Transproc.register(:user_names, t(:symbolize_keys) + t(:rename_keys, user_name: :name)) + Transproc.register(:user_names, hashes[:symbolize_keys] + hashes[:rename_keys, user_name: :name]) f = t(:user_names) expect(f['user_name' => 'Jane']).to eql(name: 'Jane') expect(f.to_ast).to eql( [ @@ -68,12 +86,35 @@ ) end it 'plays well with registered functions' do Transproc.register(:to_s, t(:to_string)) - f = t(:to_s) + fn = t(:to_s) - expect(f[:ok]).to eql('ok') - expect(f.to_ast).to eql([:to_string, []]) + expect(fn[:ok]).to eql('ok') + expect(fn.to_ast).to eql([:to_string, []]) + end + end + + describe '#==' do + let(:fns) do + Module.new do + extend Transproc::Registry + import :wrap, from: Transproc::ArrayTransformations + end + end + + it 'returns true when the other is equal' do + left = fns[:wrap, :user, [:name, :email]] + right = fns[:wrap, :user, [:name, :email]] + + expect(left == right).to be(true) + end + + it 'returns false when the other is not a fn' do + left = fns[:wrap, :user, [:name, :email]] + right = 'boo!' + + expect(left == right).to be(false) end end end