Permalink
Browse files

Merge pull request #2 from bradleyjucsc/master

Sorting issue
  • Loading branch information...
2 parents dcfb2f0 + f4519d5 commit 464751fc541b945fba95d111a023a06c9d36c907 @bleything committed Aug 1, 2012
Showing with 22 additions and 8 deletions.
  1. +1 −8 lib/plist/generator.rb
  2. +21 −0 test/test_generator.rb
View
9 lib/plist/generator.rb
@@ -78,7 +78,7 @@ def self.plist_node(element)
else
inner_tags = []
- element.keys.sort.each do |k|
+ element.keys.sort_by{|k| k.to_s }.each do |k|
v = element[k]
inner_tags << tag('key', CGI::escapeHTML(k.to_s))
inner_tags << plist_node(v)
@@ -213,13 +213,6 @@ def <<(val)
end
end
-# we need to add this so sorting hash keys works properly
-class Symbol #:nodoc:
- def <=> (other)
- self.to_s <=> other.to_s
- end
-end
-
class Array #:nodoc:
include Plist::Emit
end
View
21 test/test_generator.rb
@@ -51,4 +51,25 @@ def test_write_plist
File.unlink('test.plist')
end
+
+ # The hash in this test was failing with 'hsh.keys.sort',
+ # we are making sure it works with 'hsh.keys.sort_by'.
+ def test_sorting_keys
+ hsh = {:key1 => 1, :key4 => 4, 'key2' => 2, :key3 => 3}
+ output = Plist::Emit.plist_node(hsh)
+ expected = <<-STR
+<dict>
+ <key>key1</key>
+ <integer>1</integer>
+ <key>key2</key>
+ <integer>2</integer>
+ <key>key3</key>
+ <integer>3</integer>
+ <key>key4</key>
+ <integer>4</integer>
+</dict>
+ STR
+
+ assert_equal expected, output.gsub(/[\t]/, "\s\s")
+ end
end

0 comments on commit 464751f

Please sign in to comment.