|
195 | 195 | end
|
196 | 196 | end
|
197 | 197 |
|
| 198 | + describe "#generate_from_value with invalid string inputs" do |
| 199 | + it "rejects non-numeric strings that convert to 0.0" do |
| 200 | + generator = described_class.new(trace_id: "abcdef1234567890abcdef1234567890") |
| 201 | + |
| 202 | + invalid_strings = ["invalid", "abc", "not_a_number", ""] |
| 203 | + |
| 204 | + invalid_strings.each do |invalid_string| |
| 205 | + result = generator.generate_from_value(invalid_string) |
| 206 | + |
| 207 | + expect(result).not_to eq(0.0) |
| 208 | + expect(described_class.valid?(result)).to be true |
| 209 | + expect(result).to be >= 0.0 |
| 210 | + expect(result).to be < 1.0 |
| 211 | + end |
| 212 | + end |
| 213 | + |
| 214 | + it "accepts valid numeric strings" do |
| 215 | + generator = described_class.new |
| 216 | + |
| 217 | + valid_strings = ["0.5", "0.0", "0.999999", "0", "0.000000"] |
| 218 | + |
| 219 | + valid_strings.each do |valid_string| |
| 220 | + result = generator.generate_from_value(valid_string) |
| 221 | + expect(result).to eq(valid_string.to_f) |
| 222 | + expect(described_class.valid?(result)).to be true |
| 223 | + end |
| 224 | + end |
| 225 | + |
| 226 | + it "rejects numeric strings that are out of valid range" do |
| 227 | + generator = described_class.new(trace_id: "abcdef1234567890abcdef1234567890") |
| 228 | + |
| 229 | + invalid_range_strings = ["1.0", "1.5", "-0.1", "-1.0"] |
| 230 | + |
| 231 | + invalid_range_strings.each do |invalid_string| |
| 232 | + result = generator.generate_from_value(invalid_string) |
| 233 | + |
| 234 | + expect(result).not_to eq(invalid_string.to_f) |
| 235 | + expect(described_class.valid?(result)).to be true |
| 236 | + expect(result).to be >= 0.0 |
| 237 | + expect(result).to be < 1.0 |
| 238 | + end |
| 239 | + end |
| 240 | + |
| 241 | + ["0.5abc", "abc0.5", "0..5", "0.5.0", "0.5e2", ".", "-"].each do |value| |
| 242 | + it "rejects #{value.inspect} and generates from trace_id" do |
| 243 | + generator = described_class.new(trace_id: "abcdef1234567890abcdef1234567890") |
| 244 | + |
| 245 | + result = generator.generate_from_value(value) |
| 246 | + |
| 247 | + expect(result).not_to eq(value.to_f) |
| 248 | + expect(described_class.valid?(result)).to be true |
| 249 | + expect(result).to be >= 0.0 |
| 250 | + expect(result).to be < 1.0 |
| 251 | + end |
| 252 | + end |
| 253 | + end |
| 254 | + |
198 | 255 | describe ".format" do
|
199 | 256 | it "formats float to 6 decimal places" do
|
200 | 257 | expect(described_class.format(0.123456789)).to eq("0.123456")
|
|
0 commit comments