BigW Consortium Gitlab

uniquify_spec.rb 998 Bytes
Newer Older
1 2 3
require 'spec_helper'

describe Uniquify, models: true do
4 5
  let(:uniquify) { described_class.new }

6 7
  describe "#string" do
    it 'returns the given string if it does not exist' do
8
      result = uniquify.string('test_string') { |s| false }
9 10 11 12 13

      expect(result).to eq('test_string')
    end

    it 'returns the given string with a counter attached if the string exists' do
14
      result = uniquify.string('test_string') { |s| s == 'test_string' }
15 16 17 18 19

      expect(result).to eq('test_string1')
    end

    it 'increments the counter for each candidate string that also exists' do
20
      result = uniquify.string('test_string') { |s| s == 'test_string' || s == 'test_string1' }
21 22 23 24 25

      expect(result).to eq('test_string2')
    end

    it 'allows passing in a base function that defines the location of the counter' do
26 27 28
      result = uniquify.string(-> (counter) { "test_#{counter}_string" }) do |s|
        s == 'test__string'
      end
29 30 31 32 33

      expect(result).to eq('test_1_string')
    end
  end
end