summaryrefslogtreecommitdiff
path: root/test/test_narf_plist.rb
blob: e7ae9d830e7cb6e29de20c9203547827a2fc7df9 (plain)
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
99
100
101
102
103
104
105
106
107
require 'test/unit'
require 'pp'

require 'plist'

class TestPlist < Test::Unit::TestCase
  def test_Plist_parse_xml
    result = Plist::parse_xml("test/AlbumData.xml")

    # dict
    assert_kind_of( Hash, result )
    assert_equal( ["List of Albums",
                   "Minor Version",
                   "Master Image List",
                   "Major Version",
                   "List of Keywords",
                   "Archive Path",
                   "List of Rolls",
                   "Application Version"],
                  result.keys )

    # array
    assert_kind_of( Array, result["List of Rolls"] )
    assert_equal( [ {"PhotoCount"=>1,
                     "KeyList"=>["7"],
                     "Parent"=>999000,
                     "Album Type"=>"Regular",
                     "AlbumName"=>"Roll 1",
                     "AlbumId"=>6}],
                  result["List of Rolls"] )
    
    # string
    assert_kind_of( String, result["Application Version"] )
    assert_equal( "5.0.4 (263)", result["Application Version"] )

    # integer
    assert_kind_of( Integer, result["Major Version"] )
    assert_equal( 2, result["Major Version"] )

    # true
    assert_kind_of( TrueClass, result["List of Albums"][0]["Master"] )
    assert( result["List of Albums"][0]["Master"] )

    # false
    assert_kind_of( FalseClass, result["List of Albums"][1]["SlideShowUseTitles"] )
    assert( ! result["List of Albums"][1]["SlideShowUseTitles"] )

  end

  #def test_load_something_big
  #  plist = Plist::parse_xml( "~/Pictures/iPhoto Library/AlbumData.xml" )
  #end


  # date fields are credited to 
  def test_date_fields
    result = Plist::parse_xml("test/Cookies.plist")
    assert_kind_of( DateTime, result.first['Expires'] )
    assert_equal( "2007-10-25T12:36:35Z", result.first['Expires'].to_s )
  end

  def test_to_plist
    assert_equal( Plist::_xml("<string>Hello, World</string>"),     "Hello, World".to_plist )
    assert_equal( Plist::_xml("<real>151936595.697543</real>"),     151936595.697543.to_plist )
    assert_equal( Plist::_xml("<date>2006-04-21T16:47:58Z</date>"), DateTime.parse("2006-04-21T16:47:58Z").to_plist )
    assert_equal( Plist::_xml("<integer>999000</integer>"),         999000.to_plist )
    assert_equal( Plist::_xml("<false/>"),                          false.to_plist )
    assert_equal( Plist::_xml("<true/>"),                           true.to_plist )

    assert_equal( Plist::_xml("<array>\n\t<true/>\n\t<false/>\n</array>"),
                  [ true, false ].to_plist )

    assert_equal( Plist::_xml("<dict>\n\t<key>False</key>\n\t<false/>\n\t<key>True</key>\n\t<true/>\n</dict>"),
                  { 'True' => true, 'False' => false }.to_plist )

    source = File.open("test/AlbumData.xml") { |f| f.read }

    result = Plist::parse_xml(source)
  
    assert_equal( result, Plist::parse_xml(result.to_plist) )

    File.delete('hello.plist') if File.exists?('hello.plist')
    "Hello, World".save_plist('hello.plist')
    assert_equal( Plist::_xml("<string>Hello, World</string>"),
                  File.open('hello.plist') {|f| f.read }        )
    File.delete('hello.plist') if File.exists?('hello.plist')
  end

  # this functionality is credited to Mat Schaffer,
  # who discovered the plist with the data tag
  # supplied the test data, and provided the parsing code.
  def test_data
    data = Plist::parse_xml("test/example_data.plist");
    assert_equal( File.open("test/example_data.jpg"){|f| f.read }, data['image'].read )
    assert_equal( File.open("test/example_data.plist"){|f| f.read }, data.to_plist )

    data['image'] = StringIO.new( File.open("test/example_data.jpg"){ |f| f.read } )
    File.open('temp.plist', 'w'){|f| f.write data.to_plist }
    assert_equal( File.open("test/example_data.plist"){|f| f.read }, data.to_plist )

    File.delete('temp.plist') if File.exists?('hello.plist')

  end

end

__END__