Sha256: f34f015231030bc2458743e72d736d4fdce1292fb729cd68cd3e7add1f801b4b

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

require 'spec_helper'

describe Transproc do
  describe 'composition' do
    it 'allows composing two transformation functions' do
      input = '1'
      output = 1.0

      to_i = t(-> value { value.to_i })
      to_f = t(-> value { value.to_f })

      result = to_i >> to_f

      expect(result[input]).to eql(output)
    end
  end

  describe '.register' do
    it 'allows registering functions by name' do
      Transproc.register(:identity, -> value { value })

      value = 'hello world'

      result = t(:identity)[value]

      expect(result).to be(value)
    end

    it 'allows registering function by passing a block' do
      Transproc.register(:to_boolean1) { |value| value == 'true' }

      result = t(-> value { value.to_s }) >> t(:to_boolean1)

      expect(result[:true]).to be(true)
      expect(result[:false]).to be(false)
    end

    it 'raises a Transproc::FunctionAlreadyRegisteredError if a function is already registered' do
      Transproc.register(:bogus) {}
      expect { Transproc.register(:bogus) {} }.to raise_error(Transproc::FunctionAlreadyRegisteredError)
    end
  end

  describe 'nonextistent functions' do
    it 'raises a Transproc::FunctionNotFoundError if asking for function that is non exsistent' do
      expect {
        Transproc(:i_do_not_exist)
        raise('expected the :i_do_not_exist function to not exist')
      }.to raise_error(Transproc::FunctionNotFoundError)
    end
  end

  describe 'handling malformed input' do
    it 'raises a Transproc::MalformedInputError' do
      Transproc.register(:im_dangerous, ->(){
        raise ArgumentError.new('sorry, you got some bad apples in your input')
      })

      expect{
        Transproc(:im_dangerous)[hello: 'world']
      }.to raise_error(Transproc::MalformedInputError)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
transproc-0.2.1 spec/unit/transproc_spec.rb