Skip to content

Commit

Permalink
Convert expected for comparisons (#19)
Browse files Browse the repository at this point in the history
* Convert expected values to float before comparison

This converts the expected values to a float before a `<` and `>`
comparison to prevent potential issues around the parsing of integer
values as strings when loading the YAML files. This is particularly of
issue with flat million numbers which are persisted in YAML as 6e+06
which is treated as a string when the YAML is parsed.
  • Loading branch information
bnbry authored Oct 22, 2024
1 parent 695c731 commit 9d02b5e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/toggles/feature.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Feature
Error = Class.new(StandardError)
Unknown = Class.new(Error)
Expand Down Expand Up @@ -75,8 +75,8 @@ def self.disabled?(*sym, **criteria)
end
}

Feature.operations[:gt] = ->(entity, attr_name, expected) { entity.send(attr_name) > expected }
Feature.operations[:lt] = ->(entity, attr_name, expected) { entity.send(attr_name) < expected }
Feature.operations[:gt] = ->(entity, attr_name, expected) { entity.send(attr_name) > expected.to_f }
Feature.operations[:lt] = ->(entity, attr_name, expected) { entity.send(attr_name) < expected.to_f }
Feature.operations[:range] = lambda { |args|
raise StandardError, 'Invalid range operation' if args.size != 2

Expand Down
2 changes: 1 addition & 1 deletion lib/toggles/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Toggles
VERSION = '0.5.0'
VERSION = '0.6.0'
end
10 changes: 9 additions & 1 deletion spec/toggles/feature/operation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

RSpec.describe 'operations' do
context 'and' do
subject { Feature.operations[:and] }
Expand All @@ -19,6 +19,10 @@
specify do
expect(subject.call(double(id: 50), :id, 40)).to eq true
expect(subject.call(double(id: 50), :id, 60)).to eq false
expect(subject.call(double(id: 5_500_000), :id, "6e+06")).to eq false
expect(subject.call(double(id: 5_500_000), :id, 6e+06)).to eq false
expect(subject.call(double(id: 6_500_000), :id, "6e+06")).to eq true
expect(subject.call(double(id: 6_500_000), :id, 6e+06)).to eq true
end
end

Expand All @@ -38,6 +42,10 @@
specify do
expect(subject.call(double(id: 50), :id, 60)).to eq true
expect(subject.call(double(id: 50), :id, 40)).to eq false
expect(subject.call(double(id: 5_500_000), :id, "6e+06")).to eq true
expect(subject.call(double(id: 5_500_000), :id, 6e+06)).to eq true
expect(subject.call(double(id: 6_500_000), :id, "6e+06")).to eq false
expect(subject.call(double(id: 6_500_000), :id, 6e+06)).to eq false
end
end

Expand Down

0 comments on commit 9d02b5e

Please sign in to comment.