Sha256: df4fb8b0fbb35950a1607df37222b1d49260e380341d852eb0d40d25936f95f2

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 KB

Contents

# encoding: utf-8
require 'spec_helper'
require 'inactive_support/object'

describe Object do

  describe '#ctry' do
    it 'works with 1 argument' do
      expect('String'.ctry(:downcase)).to eq 'string'
    end

    it 'works with multiple arguments' do
      expect('Nurse I spy gypsies run'.ctry(:downcase, :reverse)).to eq 'nur seispyg yps i esrun'
    end

    it 'returns nil when called on nil' do
      expect(nil.ctry(:downcase, :upcase)).to eq nil
    end
  end

  describe '#blank' do
    it 'works with arrays' do
      expect([].blank?).to be true
      expect([1].blank?).to be false
    end

    it 'works with hashes' do
      expect({}.blank?).to be true
      expect({ name: 'Kip' }.blank?).to be false
    end

    it 'works with strings' do
      blank = [
        '',
        ' ',
        '   ',
        "\t",
        "\r",
        "\n"
      ]

      not_blank = [
        ' a ',
        'string'
      ]

      blank.each do |string|
        expect(string.blank?).to be true
      end

      not_blank.each do |string|
        expect(string.blank?).to be false
      end
    end
  end

  describe '#deep_dup' do

    it 'works with hashes' do
      original = {
        name: {
          first: 'Gavin'
        }
      }

      duplicate = original.deep_dup

      # modify duplicate
      expect do
        duplicate[:name][:first] = 'Dazen'
      end.not_to change { original[:name][:first] }

    end

    it 'works with arrays' do
      original = [1, 2, 3]
      modified = original.deep_dup

      expect do
        modified.unshift(0)
      end.not_to change { original }

      expect do
        modified.push(4)
      end.not_to change { original }
    end

    it 'works with strings' do
      original = "I ain't grouchy, "
      modified = original.deep_dup

      expect do
        modified += '“I just have a low threshold for stupidity'
      end.not_to change { original }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
inactive_support-1.3.0 spec/lib/object_spec.rb
inactive_support-1.2.0 spec/lib/object_spec.rb