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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
require 'flipper/typecast'
RSpec.describe Flipper::Typecast do
{
nil => false,
'' => false,
0 => false,
1 => true,
'0' => false,
'1' => true,
true => true,
false => false,
'true' => true,
'false' => false,
}.each do |value, expected|
context "#to_boolean for #{value.inspect}" do
it "returns #{expected}" do
expect(described_class.to_boolean(value)).to be(expected)
end
end
end
{
nil => 0,
'' => 0,
0 => 0,
1 => 1,
'1' => 1,
'99' => 99,
}.each do |value, expected|
context "#to_integer for #{value.inspect}" do
it "returns #{expected}" do
expect(described_class.to_integer(value)).to be(expected)
end
end
end
{
nil => 0.0,
'' => 0.0,
0 => 0.0,
1 => 1.0,
1.1 => 1.1,
'0.01' => 0.01,
'1' => 1.0,
'99' => 99.0,
}.each do |value, expected|
context "#to_float for #{value.inspect}" do
it "returns #{expected}" do
expect(described_class.to_float(value)).to be(expected)
end
end
end
{
nil => 0,
'' => 0,
0 => 0,
0.0 => 0,
1 => 1,
1.1 => 1.1,
'0.01' => 0.01,
'1' => 1,
'1.1' => 1.1,
'99' => 99,
'99.9' => 99.9,
}.each do |value, expected|
context "#to_percentage for #{value.inspect}" do
it "returns #{expected}" do
expect(described_class.to_percentage(value)).to be(expected)
end
end
end
{
nil => Set.new,
'' => Set.new,
Set.new([1, 2]) => Set.new([1, 2]),
[1, 2] => Set.new([1, 2]),
}.each do |value, expected|
context "#to_set for #{value.inspect}" do
it "returns #{expected}" do
expect(described_class.to_set(value)).to eq(expected)
end
end
end
it 'raises argument error for integer value that cannot be converted to an integer' do
expect do
described_class.to_integer(['asdf'])
end.to raise_error(ArgumentError, %(["asdf"] cannot be converted to an integer))
end
it 'raises argument error for float value that cannot be converted to an float' do
expect do
described_class.to_float(['asdf'])
end.to raise_error(ArgumentError, %(["asdf"] cannot be converted to a float))
end
it 'raises argument error for bad integer percentage' do
expect do
described_class.to_percentage(['asdf'])
end.to raise_error(ArgumentError, %(["asdf"] cannot be converted to a percentage))
end
it 'raises argument error for bad float percentage' do
expect do
described_class.to_percentage(['asdf.0'])
end.to raise_error(ArgumentError, %(["asdf.0"] cannot be converted to a percentage))
end
it 'raises argument error for set value that cannot be converted to a set' do
expect do
described_class.to_set('asdf')
end.to raise_error(ArgumentError, %("asdf" cannot be converted to a set))
end
end
|