|
| 1 | +require 'spec_helper' |
| 2 | +require 'command_mapper/types/dec' |
| 3 | + |
| 4 | +describe CommandMapper::Types::Dec do |
| 5 | + describe "#initialize" do |
| 6 | + context "when initialized with no keyword arguments" do |
| 7 | + it "must set #range to nil" do |
| 8 | + expect(subject.range).to be(nil) |
| 9 | + end |
| 10 | + end |
| 11 | + |
| 12 | + context "when initialized with range: ..." do |
| 13 | + let(:range) { 1.0..1.5 } |
| 14 | + |
| 15 | + subject { described_class.new(range: range) } |
| 16 | + |
| 17 | + it "must set #range" do |
| 18 | + expect(subject.range).to eq(range) |
| 19 | + end |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + describe "#validate" do |
| 24 | + context "when given an Integer" do |
| 25 | + let(:value) { 1 } |
| 26 | + |
| 27 | + it "must return true" do |
| 28 | + expect(subject.validate(value)).to be(true) |
| 29 | + end |
| 30 | + |
| 31 | + context "when initialized with range: ..." do |
| 32 | + let(:range) { 2.0..10.0 } |
| 33 | + |
| 34 | + subject { described_class.new(range: range) } |
| 35 | + |
| 36 | + context "and the value is within the range of values" do |
| 37 | + let(:value) { 4.0 } |
| 38 | + |
| 39 | + it "must return true" do |
| 40 | + expect(subject.validate(value)).to be(true) |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + context "but the value is not within the range of values" do |
| 45 | + let(:value) { 0.0 } |
| 46 | + |
| 47 | + it "must return [false, \"(...) not within the range of acceptable values (...)\"]" do |
| 48 | + expect(subject.validate(value)).to eq( |
| 49 | + [false, "(#{value.inspect}) not within the range of acceptable values (#{range.inspect})"] |
| 50 | + ) |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + context "when given a String" do |
| 57 | + context "and it contains only digits" do |
| 58 | + let(:value) { "0123456789" } |
| 59 | + |
| 60 | + it "must return true" do |
| 61 | + expect(subject.validate(value)).to be(true) |
| 62 | + end |
| 63 | + |
| 64 | + context "when initialized with range: ..." do |
| 65 | + let(:range) { 2.0..10.0 } |
| 66 | + |
| 67 | + subject { described_class.new(range: range) } |
| 68 | + |
| 69 | + context "and the value is within the range of values" do |
| 70 | + let(:value) { '4.0' } |
| 71 | + |
| 72 | + it "must return true" do |
| 73 | + expect(subject.validate(value)).to be(true) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + context "but the value is not within the range of values" do |
| 78 | + let(:value) { '0.0' } |
| 79 | + |
| 80 | + it "must return [false, \"(...) not within the range of acceptable values (...)\"]" do |
| 81 | + expect(subject.validate(value)).to eq( |
| 82 | + [false, "(#{value.inspect}) not within the range of acceptable values (#{range.inspect})"] |
| 83 | + ) |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + context "but the String contains a newline" do |
| 89 | + let(:value) { "01234.\n56789" } |
| 90 | + |
| 91 | + it "must return [false, \"contains non-decimal characters (...)\"]" do |
| 92 | + expect(subject.validate(value)).to eq( |
| 93 | + [false, "contains non-decimal characters (#{value.inspect})"] |
| 94 | + ) |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + context "but it contains non-digits" do |
| 100 | + let(:value) { "12.abc34" } |
| 101 | + |
| 102 | + it "must return [false, \"contains non-decimal characters (...)\"]" do |
| 103 | + expect(subject.validate(value)).to eq( |
| 104 | + [false, "contains non-decimal characters (#{value.inspect})"] |
| 105 | + ) |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + context "when given another type of Object" do |
| 111 | + context "and it defines a #to_f method" do |
| 112 | + let(:value) { 1 } |
| 113 | + |
| 114 | + it "must return true" do |
| 115 | + expect(subject.validate(value)).to be(true) |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + context "when initialized with range: ..." do |
| 120 | + let(:range) { 2.0..10.0 } |
| 121 | + |
| 122 | + subject { described_class.new(range: range) } |
| 123 | + |
| 124 | + context "and the value is within the range of values" do |
| 125 | + let(:value) { 4 } |
| 126 | + |
| 127 | + it "must return true" do |
| 128 | + expect(subject.validate(value)).to be(true) |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + context "but the value is not within the range of values" do |
| 133 | + let(:value) { 0 } |
| 134 | + |
| 135 | + it "must return [false, \"(...) not within the range of acceptable values (...)\"]" do |
| 136 | + expect(subject.validate(value)).to eq( |
| 137 | + [false, "(#{value.inspect}) not within the range of acceptable values (#{range.inspect})"] |
| 138 | + ) |
| 139 | + end |
| 140 | + end |
| 141 | + end |
| 142 | + |
| 143 | + context "but it does not define a #to_f method" do |
| 144 | + let(:value) { Object.new } |
| 145 | + |
| 146 | + it "must return [false, \"value cannot be converted into a Float\"]" do |
| 147 | + expect(subject.validate(value)).to eq( |
| 148 | + [false, "cannot be converted into a Float (#{value.inspect})"] |
| 149 | + ) |
| 150 | + end |
| 151 | + end |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + describe "#format" do |
| 156 | + context "when given a String" do |
| 157 | + let(:value) { "12345.67890" } |
| 158 | + |
| 159 | + it "must return the same String" do |
| 160 | + expect(subject.format(value)).to eq(value) |
| 161 | + end |
| 162 | + end |
| 163 | + |
| 164 | + context "when given an Intger" do |
| 165 | + let(:value) { 12345.67890 } |
| 166 | + |
| 167 | + it "must convert the Integer into a String" do |
| 168 | + expect(subject.format(value)).to eq(value.to_s) |
| 169 | + end |
| 170 | + end |
| 171 | + |
| 172 | + context "when given another type of Object" do |
| 173 | + let(:value) { 1 } |
| 174 | + |
| 175 | + it "must call #to_i then #to_s" do |
| 176 | + expect(subject.format(value)).to eq(value.to_f.to_s) |
| 177 | + end |
| 178 | + end |
| 179 | + end |
| 180 | +end |
0 commit comments