Skip to content

A platform for community discussion. Free, open, simple.

DEV: Normalize (ok)lch values in computed style matcher (#36404)

Jarek Radoszb81f4104ef3b
diff --git a/spec/support/computed_style_matcher.rb b/spec/support/computed_style_matcher.rb
index a8abc3a0a73..465cdfeda14 100644
--- a/spec/support/computed_style_matcher.rb
+++ b/spec/support/computed_style_matcher.rb
@@ -1,7 +1,10 @@
# frozen_string_literal: true
RSpec::Matchers.define :have_computed_style do |expected|
- match { |element| computed_style(element, expected.keys.first) == expected.values.first }
+ match do |element|
+ actual = normalize(computed_style(element, expected.keys.first))
+ actual == normalize(expected.values.first)
+ end
failure_message do |element|
actual = computed_style(element, expected.keys.first)
@@ -15,4 +18,26 @@ RSpec::Matchers.define :have_computed_style do |expected|
def computed_style(element, property)
element.evaluate_script("getComputedStyle(this)['#{property}']")
end
+
+ def normalize(value)
+ match =
+ /
+ \A(?<prefix>(?:ok)?lch)
+ \(
+ (?<l>.+)(?<symbol>%?)\s
+ (?<c>.+)\s
+ (?<h>.+)
+ \)\Z
+ /x.match(
+ value,
+ )
+
+ return value if !match
+
+ l = format("%.2f", BigDecimal(match[:l]).truncate(2))
+ c = format("%.2f", BigDecimal(match[:c]).truncate(2))
+ h = format("%.2f", BigDecimal(match[:h]).truncate(2))
+
+ "#{match[:prefix]}(#{l}#{match[:symbol]} #{c} #{h})"
+ end
end