2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe IntegerSettingValidator do
|
2014-06-12 18:03:03 -04:00
|
|
|
describe "#valid_value?" do
|
|
|
|
shared_examples "for all IntegerSettingValidator opts" do
|
|
|
|
it "returns false for blank values" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?("")).to eq(false)
|
|
|
|
expect(validator.valid_value?(nil)).to eq(false)
|
2014-06-12 18:03:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if value is not a valid integer" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?("two")).to eq(false)
|
2014-06-12 18:03:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without min and max" do
|
|
|
|
subject(:validator) { described_class.new }
|
|
|
|
|
|
|
|
include_examples "for all IntegerSettingValidator opts"
|
|
|
|
|
|
|
|
it "returns true if value is a valid integer" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?(1)).to eq(true)
|
|
|
|
expect(validator.valid_value?("1")).to eq(true)
|
2017-04-20 03:49:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "defaults min to 0" do
|
|
|
|
expect(validator.valid_value?(-1)).to eq(false)
|
|
|
|
expect(validator.valid_value?("-1")).to eq(false)
|
|
|
|
expect(validator.valid_value?(0)).to eq(true)
|
|
|
|
expect(validator.valid_value?("0")).to eq(true)
|
|
|
|
end
|
|
|
|
|
2017-08-24 10:40:14 -04:00
|
|
|
it "defaults max to 2_000_000_000" do
|
|
|
|
expect(validator.valid_value?(2_000_000_001)).to eq(false)
|
|
|
|
expect(validator.valid_value?("2000000001")).to eq(false)
|
|
|
|
expect(validator.valid_value?(2_000_000_000)).to eq(true)
|
|
|
|
expect(validator.valid_value?("2000000000")).to eq(true)
|
2014-06-12 18:03:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with min" do
|
|
|
|
subject(:validator) { described_class.new(min: 2) }
|
|
|
|
|
|
|
|
include_examples "for all IntegerSettingValidator opts"
|
|
|
|
|
|
|
|
it "returns true if value is equal to min" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?(2)).to eq(true)
|
|
|
|
expect(validator.valid_value?("2")).to eq(true)
|
2014-06-12 18:03:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true if value is greater than min" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?(3)).to eq(true)
|
|
|
|
expect(validator.valid_value?("3")).to eq(true)
|
2014-06-12 18:03:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if value is less than min" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?(1)).to eq(false)
|
|
|
|
expect(validator.valid_value?("1")).to eq(false)
|
2014-06-12 18:03:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with max" do
|
|
|
|
subject(:validator) { described_class.new(max: 3) }
|
|
|
|
|
|
|
|
include_examples "for all IntegerSettingValidator opts"
|
|
|
|
|
|
|
|
it "returns true if value is equal to max" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?(3)).to eq(true)
|
|
|
|
expect(validator.valid_value?("3")).to eq(true)
|
2014-06-12 18:03:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true if value is less than max" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?(2)).to eq(true)
|
|
|
|
expect(validator.valid_value?("2")).to eq(true)
|
2014-06-12 18:03:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if value is greater than min" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?(4)).to eq(false)
|
|
|
|
expect(validator.valid_value?("4")).to eq(false)
|
2014-06-12 18:03:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with min and max" do
|
|
|
|
subject(:validator) { described_class.new(min: -1, max: 3) }
|
|
|
|
|
|
|
|
include_examples "for all IntegerSettingValidator opts"
|
|
|
|
|
|
|
|
it "returns true if value is in range" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?(-1)).to eq(true)
|
|
|
|
expect(validator.valid_value?(0)).to eq(true)
|
|
|
|
expect(validator.valid_value?(3)).to eq(true)
|
2014-06-12 18:03:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if value is out of range" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(validator.valid_value?(4)).to eq(false)
|
|
|
|
expect(validator.valid_value?(-2)).to eq(false)
|
2014-06-12 18:03:03 -04:00
|
|
|
end
|
|
|
|
end
|
2017-04-22 02:36:02 -04:00
|
|
|
|
|
|
|
context "when setting is hidden" do
|
|
|
|
subject(:validator) { described_class.new(hidden: true) }
|
|
|
|
|
|
|
|
it "does not impose default validations" do
|
|
|
|
expect(validator.valid_value?(-1)).to eq(true)
|
|
|
|
expect(validator.valid_value?(20_001)).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2014-06-12 18:03:03 -04:00
|
|
|
end
|
|
|
|
end
|