mapricot 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
1
+ require 'test/unit'
2
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'mapricot'))
3
+
4
+ # !!!!!!!!!!!!!!!!!!
5
+ # Please look at test_mapricot_readme.rb first!!
6
+ # !!!!!!!!!!!!!!!!!!
7
+
8
+ class FeedA < Mapricot::Base
9
+ has_one :bar
10
+ end
11
+
12
+ class TestFeedA < Test::Unit::TestCase
13
+
14
+ def test_no_bar
15
+ feeda = FeedA.new(:xml => "<no></no>")
16
+ assert feeda.bar.nil?
17
+ end
18
+
19
+ def test_empty_bar
20
+ feeda = FeedA.new(:xml => "<bar></bar>")
21
+ assert feeda.bar.empty?
22
+ end
23
+
24
+ def test_full_bar
25
+ @feed = FeedA.new(:xml => "<bar>is full</bar>")
26
+ assert_equal "is full", @feed.bar
27
+ end
28
+ end
29
+
30
+
31
+
32
+
33
+ class FeedB < Mapricot::Base
34
+ has_many :bars
35
+ end
36
+
37
+ class TestFeedB < Test::Unit::TestCase
38
+ def test_many_empty_bars
39
+ feedb = FeedB.new(:xml => "<notag></notag>")
40
+ assert feedb.bars.is_a?(Array)
41
+ assert feedb.bars.empty?
42
+ end
43
+
44
+ def test_full_bars
45
+ feedb = FeedB.new(:xml => %(
46
+ <response>
47
+ <bar>wet</bar>
48
+ <bar>full</bar>
49
+ </response>
50
+ ))
51
+
52
+ assert_equal "wet", feedb.bars.first
53
+ assert_equal "full", feedb.bars.last
54
+ end
55
+ end
56
+
57
+
58
+
59
+ class FeedC < Mapricot::Base
60
+ has_many :bars, :xml
61
+ end
62
+
63
+ class Bar < Mapricot::Base
64
+ has_one :name
65
+ has_many :patrons
66
+ end
67
+
68
+ class TestFeedC < Test::Unit::TestCase
69
+ def test_many_empty_bars
70
+ feedc = FeedC.new(:xml => "<no></no>")
71
+ assert feedc.bars.empty?
72
+ end
73
+
74
+ def test_many_bars
75
+ feedc = FeedC.new(:xml => %(
76
+ <response>
77
+ <bar>
78
+ <name>maroon saloon</name>
79
+ <patron>brett</patron>
80
+ <patron>chris</patron>
81
+ <patron>garon</patron>
82
+ <patron>mitch</patron>
83
+ </bar>
84
+ </response>
85
+ ))
86
+ assert_equal "maroon saloon", feedc.bars.first.name
87
+ assert_equal ['brett', 'chris', 'garon', 'mitch'], feedc.bars.first.patrons
88
+ end
89
+ end
@@ -0,0 +1,105 @@
1
+ require 'test/unit'
2
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'mapricot'))
3
+
4
+
5
+ # -------------------------- Test Example 1 of README.txt ------------------ #
6
+ class SimpleUser < Mapricot::Base
7
+ has_one :id, :integer
8
+ has_one :name, :string
9
+ has_many :pets, :string
10
+ end
11
+
12
+
13
+ class TestSimpleUser < Test::Unit::TestCase
14
+ def setup
15
+ xml = %(
16
+ <user>
17
+ <id>1</id>
18
+ <name>Bob</name>
19
+ <pet>cat</pet>
20
+ <pet>dog</pet>
21
+ </user>
22
+ )
23
+
24
+ @user = SimpleUser.new(:xml => xml)
25
+ end
26
+
27
+ def test_mapping
28
+ assert_equal 1, @user.id
29
+ assert_equal "Bob", @user.name
30
+ assert_equal ["cat", "dog"], @user.pets
31
+ end
32
+ end
33
+ # -------------------------------------------------------------------------- #
34
+
35
+
36
+
37
+
38
+
39
+ # -------------------------- Test Example 2 of README.txt ------------------ #
40
+ class User < Mapricot::Base
41
+ has_one :id, :integer
42
+ has_one :name # Tag type will default to :string
43
+ has_many :pets
44
+ has_one :location, :xml
45
+ has_many :hobbies, :xml
46
+ end
47
+
48
+ class Location < Mapricot::Base
49
+ has_attribute :code
50
+ has_one :city
51
+ has_one :state
52
+ end
53
+
54
+ class Hobby < Mapricot::Base
55
+ has_one :description, :string
56
+ has_one :number_of_years, :integer
57
+ end
58
+
59
+ class TestUser < Test::Unit::TestCase
60
+ def setup
61
+ xml = %(
62
+ <user>
63
+ <id>2</id>
64
+ <name>Sally</name>
65
+ <location code="ny123">
66
+ <city>New York</city>
67
+ <state>NY</state>
68
+ </location>
69
+ <hobby>
70
+ <description>Skiing</description>
71
+ <number_of_years>2</number_of_years>
72
+ </hobby>
73
+ <hobby>
74
+ <description>Hiking</description>
75
+ <number_of_years>3</number_of_years>
76
+ </hobby>
77
+ </user>
78
+ )
79
+
80
+ @user = User.new(:xml => xml)
81
+ end
82
+
83
+ def test_id_and_name
84
+ assert_equal @user.id, 2
85
+ assert_equal @user.name, "Sally"
86
+ end
87
+
88
+ def test_location
89
+ assert_equal @user.location.city, "New York"
90
+ assert_equal @user.location.state, "NY"
91
+ assert_equal @user.location.code, "ny123"
92
+ end
93
+
94
+ def test_hobbies
95
+ assert_equal @user.hobbies[0].description, "Skiing"
96
+ assert_equal @user.hobbies[0].number_of_years, 2
97
+ assert_equal @user.hobbies[1].description, "Hiking"
98
+ assert_equal @user.hobbies[1].number_of_years, 3
99
+ end
100
+
101
+ def test_pets
102
+ assert @user.pets.empty? # Sally has no pets
103
+ end
104
+ end
105
+ # -------------------------------------------------------------------------- #
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mapricot
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 4
9
+ version: 0.0.4
10
+ platform: ruby
11
+ authors:
12
+ - Lou Zell
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-31 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: hpricot
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: nokogiri
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :runtime
43
+ version_requirements: *id002
44
+ - !ruby/object:Gem::Dependency
45
+ name: libxml-ruby
46
+ prerelease: false
47
+ requirement: &id003 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ type: :runtime
55
+ version_requirements: *id003
56
+ description: XML to object mapper with an interface similar to ActiveRecord associations.
57
+ email: lzell11@gmail.com
58
+ executables: []
59
+
60
+ extensions: []
61
+
62
+ extra_rdoc_files:
63
+ - README.rdoc
64
+ files:
65
+ - README.rdoc
66
+ - History.txt
67
+ - License.txt
68
+ - mapricot.gemspec
69
+ - examples/facebook_api.rb
70
+ - examples/lastfm_api.rb
71
+ - examples/lastfm_api_no_request.rb
72
+ - examples/readme_examples.rb
73
+ - examples/xml_with_attributes.rb
74
+ - test/suite.rb
75
+ - test/test_abstract_doc.rb
76
+ - test/test_mapricot.rb
77
+ - test/test_mapricot_readme.rb
78
+ - test/has_attribute/test_has_attribute.rb
79
+ - test/has_many/test_has_many_ids.rb
80
+ - test/has_many/test_has_many_nested.rb
81
+ - test/has_one/test_has_one_id.rb
82
+ - test/has_one/test_has_one_nested.rb
83
+ - lib/mapricot.rb
84
+ - lib/mapricot/abstract_doc.rb
85
+ - lib/mapricot/associations.rb
86
+ - lib/mapricot/attribute.rb
87
+ - lib/mapricot/base.rb
88
+ - benchmark/benchmarks.rb
89
+ has_rdoc: true
90
+ homepage: http://github.com/lzell/mapricot
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options:
95
+ - --main
96
+ - README.rdoc
97
+ - --title
98
+ - Mapricot
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ requirements: []
116
+
117
+ rubyforge_project:
118
+ rubygems_version: 1.3.6
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: XML to object mapper
122
+ test_files: []
123
+