2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-06-18 10:49:21 -04:00
|
|
|
|
|
|
|
describe StringSettingValidator do
|
|
|
|
|
|
|
|
describe '#valid_value?' do
|
|
|
|
shared_examples "for all StringSettingValidator opts" do
|
|
|
|
it "returns true for blank values" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?('')).to eq(true)
|
|
|
|
expect(validator.valid_value?(nil)).to eq(true)
|
2014-06-18 10:49:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a regex' do
|
|
|
|
subject(:validator) { described_class.new(regex: 'bacon') }
|
|
|
|
|
|
|
|
include_examples "for all StringSettingValidator opts"
|
|
|
|
|
|
|
|
it "returns true if value matches the regex" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?('The bacon is delicious')).to eq(true)
|
2014-06-18 10:49:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if the value doesn't match the regex" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?('The vegetables are delicious')).to eq(false)
|
2014-06-18 10:49:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "test some other regexes" do
|
|
|
|
v = described_class.new(regex: '^(chocolate|banana)$')
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(v.valid_value?('chocolate')).to eq(true)
|
|
|
|
expect(v.valid_value?('chocolates')).to eq(false)
|
2014-06-18 10:49:21 -04:00
|
|
|
|
|
|
|
v = described_class.new(regex: '^[\w]+$')
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(v.valid_value?('the_file')).to eq(true)
|
|
|
|
expect(v.valid_value?('the_file.bat')).to eq(false)
|
2014-06-18 10:49:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with min' do
|
|
|
|
subject(:validator) { described_class.new(min: 2) }
|
|
|
|
|
|
|
|
include_examples "for all StringSettingValidator opts"
|
|
|
|
|
|
|
|
it "returns true if length is ok" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?('ok')).to eq(true)
|
|
|
|
expect(validator.valid_value?('yep long enough')).to eq(true)
|
2014-06-18 10:49:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if too short" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?('x')).to eq(false)
|
2014-06-18 10:49:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with max' do
|
|
|
|
subject(:validator) { described_class.new(max: 5) }
|
|
|
|
|
|
|
|
include_examples "for all StringSettingValidator opts"
|
|
|
|
|
|
|
|
it "returns true if length is ok" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?('Z')).to eq(true)
|
|
|
|
expect(validator.valid_value?('abcde')).to eq(true)
|
2014-06-18 10:49:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if too long" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?('banana')).to eq(false)
|
2014-06-18 10:49:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'combinations of options' do
|
|
|
|
it "min and regex" do
|
|
|
|
v = described_class.new(regex: '^[\w]+$', min: 3)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(v.valid_value?('chocolate')).to eq(true)
|
|
|
|
expect(v.valid_value?('hi')).to eq(false)
|
|
|
|
expect(v.valid_value?('game.exe')).to eq(false)
|
2014-06-18 10:49:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "max and regex" do
|
|
|
|
v = described_class.new(regex: '^[\w]+$', max: 5)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(v.valid_value?('chocolate')).to eq(false)
|
|
|
|
expect(v.valid_value?('a_b_c')).to eq(true)
|
|
|
|
expect(v.valid_value?('a b c')).to eq(false)
|
2014-06-18 10:49:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "min and max" do
|
|
|
|
v = described_class.new(min: 3, max: 5)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(v.valid_value?('chocolate')).to eq(false)
|
|
|
|
expect(v.valid_value?('a')).to eq(false)
|
|
|
|
expect(v.valid_value?('a b c')).to eq(true)
|
|
|
|
expect(v.valid_value?('a b')).to eq(true)
|
2014-06-18 10:49:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "min, max, and regex" do
|
|
|
|
v = described_class.new(min: 3, max: 12, regex: 'bacon')
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(v.valid_value?('go bacon!')).to eq(true)
|
|
|
|
expect(v.valid_value?('sprinkle bacon on your cereal')).to eq(false)
|
|
|
|
expect(v.valid_value?('ba')).to eq(false)
|
2014-06-18 10:49:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|