Skip to content

Commit

Permalink
chore(sentiment): add float/integer validation
Browse files Browse the repository at this point in the history
  • Loading branch information
benzend committed Oct 18, 2024
1 parent 85adaed commit c91006d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/glare/ux_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ def initialize(choices:)
attr_reader :choices

def valid?
if choices.is_a?(Hash) && choices.size
missing_attributes = CHOICE_KEYS - choices.keys.map(&:to_s)
return true if missing_attributes.empty?
return false unless choices.is_a?(Hash) && choices.size

missing_attributes = CHOICE_KEYS - choices.keys.map(&:to_s)
return false unless missing_attributes.empty?

return false unless choices.values.all? do |v|
return v.to_i.to_s == v || v.to_f.to_s == v if v.is_a?(String)
true
end

false
true
end

def parse
Expand Down
14 changes: 14 additions & 0 deletions spec/glare/ux_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@
expect(data.valid?).to eq(true)
end

it "invalidates non float/integer-like values" do
data = Glare::UxMetrics::Sentiment::Parser.new(choices: {
helpful: 0.1,
innovative: 0.1,
simple: 0.1,
joyful: 0.1,
complicated: 0.1,
confusing: 0.1,
overwhelming: 0.1,
annoying: "hi"
})
expect(data.valid?).to eq(false)
end

it "invalidates invalid sentiment data" do
data = Glare::UxMetrics::Sentiment::Parser.new(choices: { helpful: 1 })
expect(data.valid?).to eq(false)
Expand Down

0 comments on commit c91006d

Please sign in to comment.