diff options
Diffstat (limited to 'spec/lib/gitlab/redis')
-rw-r--r-- | spec/lib/gitlab/redis/boolean_spec.rb | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/spec/lib/gitlab/redis/boolean_spec.rb b/spec/lib/gitlab/redis/boolean_spec.rb new file mode 100644 index 00000000000..bfacf0c448b --- /dev/null +++ b/spec/lib/gitlab/redis/boolean_spec.rb @@ -0,0 +1,150 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Gitlab::Redis::Boolean do + subject(:redis_boolean) { described_class.new(bool) } + + let(:bool) { true } + let(:label_section) { "#{described_class::LABEL}#{described_class::DELIMITER}" } + + describe "#to_s" do + subject { redis_boolean.to_s } + + context "true" do + let(:bool) { true } + + it { is_expected.to eq("#{label_section}#{described_class::TRUE_STR}") } + end + + context "false" do + let(:bool) { false } + + it { is_expected.to eq("#{label_section}#{described_class::FALSE_STR}") } + end + end + + describe ".encode" do + subject { redis_boolean.class.encode(bool) } + + context "true" do + let(:bool) { true } + + it { is_expected.to eq("#{label_section}#{described_class::TRUE_STR}") } + end + + context "false" do + let(:bool) { false } + + it { is_expected.to eq("#{label_section}#{described_class::FALSE_STR}") } + end + end + + describe ".decode" do + subject { redis_boolean.class.decode(str) } + + context "valid encoded bool" do + let(:str) { "#{label_section}#{bool_str}" } + + context "true" do + let(:bool_str) { described_class::TRUE_STR } + + it { is_expected.to be(true) } + end + + context "false" do + let(:bool_str) { described_class::FALSE_STR } + + it { is_expected.to be(false) } + end + end + + context "partially invalid bool" do + let(:str) { "#{label_section}whoops" } + + it "raises an error" do + expect { subject }.to raise_error(described_class::NotAnEncodedBooleanStringError) + end + end + + context "invalid encoded bool" do + let(:str) { "whoops" } + + it "raises an error" do + expect { subject }.to raise_error(described_class::NotAnEncodedBooleanStringError) + end + end + end + + describe ".true?" do + subject { redis_boolean.class.true?(str) } + + context "valid encoded bool" do + let(:str) { "#{label_section}#{bool_str}" } + + context "true" do + let(:bool_str) { described_class::TRUE_STR } + + it { is_expected.to be(true) } + end + + context "false" do + let(:bool_str) { described_class::FALSE_STR } + + it { is_expected.to be(false) } + end + end + + context "partially invalid bool" do + let(:str) { "#{label_section}whoops" } + + it "raises an error" do + expect { subject }.to raise_error(described_class::NotAnEncodedBooleanStringError) + end + end + + context "invalid encoded bool" do + let(:str) { "whoops" } + + it "raises an error" do + expect { subject }.to raise_error(described_class::NotAnEncodedBooleanStringError) + end + end + end + + describe ".false?" do + subject { redis_boolean.class.false?(str) } + + context "valid encoded bool" do + let(:str) { "#{label_section}#{bool_str}" } + + context "true" do + let(:bool_str) { described_class::TRUE_STR } + + it { is_expected.to be(false) } + end + + context "false" do + let(:bool_str) { described_class::FALSE_STR } + + it { is_expected.to be(true) } + end + end + + context "partially invalid bool" do + let(:str) { "#{label_section}whoops" } + + it "raises an error" do + expect { subject }.to raise_error(described_class::NotAnEncodedBooleanStringError) + end + end + + context "invalid encoded bool" do + let(:str) { "whoops" } + + it "raises an error" do + expect { subject }.to raise_error(described_class::NotAnEncodedBooleanStringError) + end + end + end +end |