File: boolean_spec.rb

package info (click to toggle)
ruby-necromancer 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 308 kB
  • sloc: ruby: 1,578; sh: 4; makefile: 4
file content (82 lines) | stat: -rw-r--r-- 2,174 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true

RSpec.describe Necromancer::BooleanConverters, "#call" do
  describe ":string -> :boolean" do
    subject(:converter) {
      described_class::StringToBooleanConverter.new(:string, :boolean)
    }

    it "passes through boolean value" do
      expect(converter.(true)).to eq(true)
    end

    %w[true TRUE t T 1 y Y YES yes on ON].each do |value|
      it "converts '#{value}' to true value" do
        expect(converter.(value)).to eq(true)
      end
    end

    %w[false FALSE f F 0 n N NO No no off OFF].each do |value|
      it "converts '#{value}' to false value" do
        expect(converter.(value)).to eq(false)
      end
    end

    it "raises error for empty string strict mode" do
      expect {
        converter.("", strict: true)
      }.to raise_error(Necromancer::ConversionTypeError)
    end

    it "fails to convert unkonwn value FOO" do
      expect {
        converter.("FOO", strict: true)
      }.to raise_error(Necromancer::ConversionTypeError)
    end
  end

  describe ":boolean -> :integer" do
    subject(:converter) {
      described_class::BooleanToIntegerConverter.new(:boolean, :integer)
    }

    {
      true      => 1,
      false     => 0,
      "unknown" => "unknown"
    }.each do |input, obj|
      it "converts #{input.inspect} to #{obj.inspect}" do
        expect(converter.(input)).to eq(obj)
      end
    end

    it "fails to convert in strict mode" do
      expect {
        converter.("unknown", strict: true)
      }.to raise_error(
        Necromancer::ConversionTypeError,
        "'unknown' could not be converted from `boolean` into `integer`"
      )
    end
  end

  describe ":integer -> :boolean" do
    subject(:converter) { described_class::IntegerToBooleanConverter.new }

    {
      1         => true,
      0         => false,
      "unknown" => "unknown"
    }.each do |input, obj|
      it "converts #{input.inspect} to #{obj.inspect}" do
        expect(converter.(input)).to eq(obj)
      end
    end

    it "fails to convert in strict mode" do
      expect  {
        converter.("1", strict: true)
      }.to raise_error(Necromancer::ConversionTypeError)
    end
  end
end