File: numeric_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 (147 lines) | stat: -rw-r--r-- 3,667 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# frozen_string_literal: true

RSpec.describe Necromancer::NumericConverters, "#call" do
  describe ":string -> :float" do
    subject(:converter) {
      described_class::StringToFloatConverter.new(:string, :float)
    }

    {
      ""        => 0.0,
      "1"       => 1.0,
      "+1"      => 1.0,
      "1.2a"    => 1.2,
      "-1"      => -1.0,
      "1e1"     => 10.0,
      "1e-1"    => 0.1,
      "-1e1"    => -10.0,
      "-1e-1"   => -0.1,
      "1.0"     => 1.0,
      "1.0e+1"  => 10.0,
      "1.0e-1"  => 0.1,
      "-1.0e+1" => -10.0,
      "-1.0e-1" => -0.1,
      ".1"      => 0.1,
      ".1e+1"   => 1.0,
      ".1e-1"   => 0.01,
      "-.1e+1"  => -1.0,
      "-.1e-1"  => -0.01,
      " 1. 10 " => 1.0,
      "    1.0" => 1.0,
      " .1    " => 0.1,
      "  -1.1 " => -1.1,
      " -1 . 1" => -1.0
    }.each do |actual, expected|
      it "converts '#{actual}' to float value" do
        expect(converter.(actual)).to eql(expected)
      end
    end

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

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

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

    {
      ""        => 0,
      "1"       => 1,
      "1ab"     => 1,
      "+1"      => 1,
      "-1"      => -1,
      "1e+1"    => 1,
      "+1e-1"   => 1,
      "-1e1"    => -1,
      "-1e-1"   => -1,
      "1.0"     => 1,
      "1.0e+1"  => 1,
      "1.0e-1"  => 1,
      "-1.0e+1" => -1,
      "-1.0e-1" => -1,
      ".1"      => 0,
      ".1e+1"   => 0,
      ".1e-1"   => 0,
      "-.1e+1"  => 0,
      "-.1e-1"  => 0,
      " 1 00"   => 1,
      "  1  "   => 1,
      "  -1 "   => -1
    }.each do |actual, expected|
      it "converts '#{actual}' to float value" do
        expect(converter.(actual)).to eql(expected)
      end
    end

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

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

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

  describe ":string -> :numeric" do
    subject(:converter) {
      described_class::StringToNumericConverter.new(:string, :numeric)
    }

    {
      ""        => 0,
      "1"       => 1,
      "+1"      => 1,
      "-1"      => -1,
      "1e1"     => 10.0,
      "1e-1"    => 0.1,
      "-1e1"    => -10.0,
      "-1e-1"   => -0.1,
      "1.0"     => 1.0,
      "1.0e+1"  => 10.0,
      "1.0e-1"  => 0.1,
      "-1.0e+1" => -10.0,
      "-1.0e-1" => -0.1,
      ".1"      => 0.1,
      ".1e+1"   => 1.0,
      ".1e-1"   => 0.01,
      "-.1e+1"  => -1.0,
      "-.1e-1"  => -0.01,
      " 1 00"   => 1,
      "  1  "   => 1,
      "  -1 "   => -1,
      " 1. 10 " => 1.0,
      "    1.0" => 1.0,
      " .1    " => 0.1,
      "  -1.1 " => -1.1,
      " -1 . 1" => -1.0
    }.each do |actual, expected|
      it "converts '#{actual}' to '#{expected}'" do
        expect(converter.(actual)).to eql(expected)
      end
    end
  end
end