2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-06 14:02:35 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Uniquify do
|
2017-02-16 02:05:10 -05:00
|
|
|
let(:uniquify) { described_class.new }
|
|
|
|
|
2017-02-06 14:02:35 -05:00
|
|
|
describe "#string" do
|
|
|
|
it 'returns the given string if it does not exist' do
|
2017-02-16 02:05:10 -05:00
|
|
|
result = uniquify.string('test_string') { |s| false }
|
2017-02-06 14:02:35 -05:00
|
|
|
|
|
|
|
expect(result).to eq('test_string')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the given string with a counter attached if the string exists' do
|
2017-02-16 02:05:10 -05:00
|
|
|
result = uniquify.string('test_string') { |s| s == 'test_string' }
|
2017-02-06 14:02:35 -05:00
|
|
|
|
|
|
|
expect(result).to eq('test_string1')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'increments the counter for each candidate string that also exists' do
|
2017-02-16 02:05:10 -05:00
|
|
|
result = uniquify.string('test_string') { |s| s == 'test_string' || s == 'test_string1' }
|
2017-02-06 14:02:35 -05:00
|
|
|
|
|
|
|
expect(result).to eq('test_string2')
|
|
|
|
end
|
|
|
|
|
2018-04-19 02:59:37 -04:00
|
|
|
it 'allows to pass an initial value for the counter' do
|
|
|
|
start_counting_from = 2
|
2018-04-19 05:31:01 -04:00
|
|
|
uniquify = described_class.new(start_counting_from)
|
|
|
|
|
|
|
|
result = uniquify.string('test_string') { |s| s == 'test_string' }
|
2018-04-19 02:59:37 -04:00
|
|
|
|
|
|
|
expect(result).to eq('test_string2')
|
|
|
|
end
|
|
|
|
|
2017-02-06 14:02:35 -05:00
|
|
|
it 'allows passing in a base function that defines the location of the counter' do
|
2017-02-16 02:05:10 -05:00
|
|
|
result = uniquify.string(-> (counter) { "test_#{counter}_string" }) do |s|
|
|
|
|
s == 'test__string'
|
|
|
|
end
|
2017-02-06 14:02:35 -05:00
|
|
|
|
|
|
|
expect(result).to eq('test_1_string')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|