Permalink
Browse files

Refactor the pretty printer to reduce complexity

And also reduce complexity of Element#inspect
  • Loading branch information...
1 parent 8cce887 commit 7fb51710e7a12b893b6bdeb494b86eea0e26b571 @ferrous26 ferrous26 committed Feb 2, 2013
Showing with 49 additions and 19 deletions.
  1. +40 −12 lib/accessibility/pretty_printer.rb
  2. +3 −6 lib/ax/element.rb
  3. +6 −1 test/sanity/accessibility/test_pretty_printer.rb
View
52 lib/accessibility/pretty_printer.rb
@@ -58,12 +58,11 @@ def pp_identifier
#
# @return [String]
def pp_position
- position = attribute :position
- if position
- " (#{position.x}, #{position.y})"
- else
- EMPTY_STRING
+ if attributes.include? :position
+ position = attribute :position
+ return " (#{position.x}, #{position.y})" if position
end
+ EMPTY_STRING
end
##
@@ -72,14 +71,13 @@ def pp_position
#
# @return [String]
def pp_children
- child_count = size_of :children
- if child_count > 1
- " #{child_count} children"
- elsif child_count == 1
- ONE_CHILD
- else # there are some odd edge cases
- EMPTY_STRING
+ if attributes.include? :children
+ child_count = size_of :children
+ return " #{child_count} children" if child_count > 1
+ return ONE_CHILD if child_count == 1
+ # there are some odd edge cases where 0 children are reported
end
+ EMPTY_STRING
end
##
@@ -93,6 +91,36 @@ def pp_checkbox attr
" #{attr}[#{attribute(attr) ? CHECKMARK : CROSS }]"
end
+ ##
+ # Safely create a {pp_checkbox} for the `KAXEnabledAttribute`
+ #
+ # If the receiver does not have the attribute then an empty
+ # string will be returned.
+ #
+ # @return [String]
+ def pp_enabled
+ if attributes.include? :enabled
+ pp_checkbox(:enabled)
+ else
+ EMPTY_STRING
+ end
+ end
+
+ ##
+ # Safely create a {pp_checkbox} for the `KAXFocusedAttribute`
+ #
+ # If the receiver does not have the attribute then an empty
+ # string will be returned.
+ #
+ # @return [String]
+ def pp_focused
+ if attributes.include? :focused
+ pp_checkbox(:focused)
+ else
+ EMPTY_STRING
+ end
+ end
+
private
View
9 lib/ax/element.rb
@@ -365,12 +365,9 @@ def method_missing method, *args, &block
#
# @return [String]
def inspect
- msg = "#<#{self.class}" << pp_identifier.to_s
- msg << pp_position if attributes.include? :position
- msg << pp_children if attributes.include? :children
- msg << pp_checkbox(:enabled) if attributes.include? :enabled
- msg << pp_checkbox(:focused) if attributes.include? :focused
- msg << '>'
+ "#<#{self.class}" << pp_identifier.to_s <<
+ pp_position << pp_children <<
+ pp_enabled << pp_focused << '>'
end
##
View
7 test/sanity/accessibility/test_pretty_printer.rb
@@ -66,6 +66,8 @@ def test_identifier_empty_string_as_final_fallback
end
def test_position
+ @attributes = [:position]
+
@attribute = CGPoint.new
assert_match /\(0\.0, 0\.0\)/, pp_position
@@ -75,11 +77,14 @@ def test_position
# this sometimes happens, even though it shouldn't
def test_position_is_nil
- @attribute = nil
+ @attributes = [:position]
+ @attribute = nil
assert_equal EMPTY_STRING, pp_position
end
def test_children_pluralizes_properly
+ @attributes = [:children]
+
[[2, /2 children/ ],
[9001, /9001 children/],
[3.14, /3.14 children/],

0 comments on commit 7fb5171

Please sign in to comment.