public
Rubygem
Description: Ruby osx/plist extension for reading/writing property lists
Clone URL: git://github.com/kballard/osx-plist.git
kballard (author)
Fri Aug 12 18:56:02 -0700 2005
commit  a26c71fd3ee97db8c861b02a6c118e2fe03d1c03
tree    e6b902abbf279fceffa9b7dbd002cc0b9d5cf343
parent  089a7a748283d1937b5839a9aba32fc664cd5107
osx-plist / test.rb
100644 98 lines (84 sloc) 2.176 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require 'plist'
require 'stringio'
require 'test/unit'
 
class TestPlist < Test::Unit::TestCase
  def test_string
    plist = PropertyList.load("{foo = bar; }")
    assert_equal( { "foo" => "bar" }, plist )
 
    plist, format = PropertyList.load("{foo = bar; }", true)
    assert_equal( { "foo" => "bar" }, plist )
    assert_equal( :openstep, format )
    
    # make sure sources < 6 characters work
    plist = PropertyList.load("foo")
    assert_equal( "foo", plist )
    
    # make sure it works with format too
    plist, format = PropertyList.load("foo", true)
    assert_equal( "foo", plist )
    assert_equal( :openstep, format )
    
    assert_raise(PropertyListError) { PropertyList.load("") }
  end
 
  def setup_hash
    time = Time.gm(2005, 4, 28, 6, 32, 56)
    random = "\x23\x45\x67\x89"
    random.blob = true
    {
      "string!" => "indeedy",
      "bar" => [ 1, 2, 3 ],
      "foo" => {
        "correct?" => true,
        "pi" => 3.14159265,
        "random" => random,
        "today" => time,
      }
    }
  end
 
  def test_io
    plist, format = PropertyList.load(DATA, true)
 
    hash = setup_hash
 
    assert_equal(hash, plist)
    assert_equal(true, plist['foo']['random'].blob?)
    assert_equal(false, plist['string!'].blob?)
    
    assert_equal(:xml1, format)
  end
 
  def test_dump
    str = StringIO.new("", "w")
    hash = setup_hash
    PropertyList.dump(str, hash)
    hash2 = PropertyList.load(str.string)
    assert_equal(hash, hash2)
  end
 
  def test_to_plist
    assert_raise(PropertyListError) { "foo".to_plist(:openstep) }
    assert_equal("foo", PropertyList.load("foo".to_plist))
    hash = setup_hash()
    assert_equal(hash, PropertyList.load(hash.to_plist))
  end
end
 
__END__
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>string!</key>
  <string>indeedy</string>
  <key>bar</key>
  <array>
    <integer>1</integer>
    <integer>2</integer>
    <integer>3</integer>
  </array>
  <key>foo</key>
  <dict>
    <key>correct?</key>
    <true/>
    <key>pi</key>
    <real>3.14159265</real>
    <key>random</key>
    <data>
    I0VniQ==
    </data>
    <key>today</key>
    <date>2005-04-28T06:32:56Z</date>
  </dict>
</dict>
</plist>