Skip to content

Commit

Permalink
Merge pull request #2 from bradleyjucsc/master
Browse files Browse the repository at this point in the history
Sorting issue
  • Loading branch information
bleything committed Aug 1, 2012
2 parents dcfb2f0 + f4519d5 commit 464751f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
9 changes: 1 addition & 8 deletions lib/plist/generator.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions test/test_generator.rb
Expand Up @@ -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.