graft 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/test_helper.rb DELETED
@@ -1,38 +0,0 @@
1
- # http://sneaq.net/textmate-wtf
2
- $:.reject! { |e| e.include? 'TextMate' }
3
-
4
- require 'rubygems'
5
- require 'throat_punch'
6
-
7
- require File.dirname(__FILE__) + '/../lib/graft/xml'
8
- require File.dirname(__FILE__) + '/../lib/graft/json'
9
-
10
- class Test::Unit::TestCase
11
-
12
- def self.implementation_klass
13
- class_name = self.to_s.match(/([a-zA-Z]+)Test$/)[1]
14
- klass = Graft::Xml::Type.const_get(class_name)
15
-
16
- klass
17
- end
18
-
19
- def self.should_convert(source, options)
20
- klass = self.implementation_klass
21
- target = options[:to]
22
-
23
- should "be able to convert '#{source}' to #{target}" do
24
- o = klass.new(source)
25
- o.value.should == target
26
- end
27
- end
28
-
29
- def self.should_fail_when_converting(source)
30
- klass = self.implementation_klass
31
-
32
- should "fail when converting '#{source}'" do
33
- o = klass.new(source)
34
- lambda { o.value }.should raise_error(Graft::Xml::Type::ConversionError)
35
- end
36
- end
37
-
38
- end
@@ -1,29 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../test_helper'
2
-
3
- class HashTest < Test::Unit::TestCase
4
-
5
- context "An instance of Hash" do
6
-
7
- should "be able to extract a value using the / operator" do
8
- hash = {'key' => 'value'}
9
- (hash/'key').should == 'value'
10
- end
11
-
12
- should "be able to extract nested values using the / operator" do
13
- hash = {'user' => {'name' => 'luser'}}
14
- (hash/'user/name').should == 'luser'
15
- end
16
-
17
- should "return nil when attempting to extract a value that doesn't exist" do
18
- hash = {'user' => {'name' => 'luser'}}
19
- (hash/'user/username').should be(nil)
20
- end
21
-
22
- should "be able to fetch a value using a symbol" do
23
- hash = {'key' => 'value'}
24
- (hash/:key).should == 'value'
25
- end
26
-
27
- end
28
-
29
- end
@@ -1,51 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../test_helper'
2
-
3
- module Graft
4
- module Json
5
- class AttributeTest < Test::Unit::TestCase
6
-
7
- context "An instance of the Graft::Json::Attribute class" do
8
-
9
- should "know its name" do
10
- attr = Graft::Json::Attribute.new(:foo)
11
- attr.name.should == :foo
12
- end
13
-
14
- should "have a default source" do
15
- attr = Graft::Json::Attribute.new(:foo)
16
- attr.source.should == 'foo'
17
- end
18
-
19
- should "be able to extract a value from a hash" do
20
- data = {"first_name" => "Richerd"}
21
-
22
- attr = Graft::Json::Attribute.new(:first_name)
23
- attr.value_from(data).should == 'Richerd'
24
- end
25
-
26
- should "be able to extract a value from a hash when provided with a symbol for the source" do
27
- data = {"firstname" => "Richerd"}
28
-
29
- attr = Graft::Json::Attribute.new(:first_name, :firstname)
30
- attr.value_from(data).should == 'Richerd'
31
- end
32
-
33
- should "be able to extract a nested value from a JSON string" do
34
- data = {"user" => {"first_name" => "Richerd"}}
35
-
36
- attr = Graft::Json::Attribute.new(:first_name, 'user/first_name')
37
- attr.value_from(data).should == 'Richerd'
38
- end
39
-
40
- should "return nil when the value doesn't exist" do
41
- data = {"first_name" => "Richerd"}
42
-
43
- attr = Graft::Json::Attribute.new(:last_name, 'last_name')
44
- attr.value_from(data).should be(nil)
45
- end
46
-
47
- end
48
-
49
- end
50
- end
51
- end
@@ -1,86 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../test_helper'
2
-
3
- class EmptyJsonModel
4
- include Graft::Json::Model
5
- end
6
-
7
- class JsonModelWithAttributes
8
- include Graft::Json::Model
9
-
10
- attribute :name
11
- attribute :description, :from => 'desc'
12
- attribute :rating, :from => 'rsp/rating'
13
- end
14
-
15
- class JsonModelTest < Test::Unit::TestCase
16
-
17
- context "The EmptyJsonModel class" do
18
- should "have an empty list of attributes if none are supplied" do
19
- EmptyJsonModel.attributes.should == []
20
- end
21
-
22
- should "be able to return a collection of instances" do
23
- json_data = '{"results":[{"name":"one"},{"name":"two"}]}'
24
-
25
- EmptyJsonModel.expects(:new).with({'name' => 'one'}).returns('model_1')
26
- EmptyJsonModel.expects(:new).with({'name' => 'two'}).returns('model_2')
27
-
28
- collection = EmptyJsonModel.collection_from(json_data, 'results')
29
- collection.should == ['model_1', 'model_2']
30
- end
31
-
32
- should "be able to retrieve data from the source JSON string" do
33
- json_data = '{"name":"Graft"}'
34
- EmptyJsonModel.data_from(json_data).should == {'name' => 'Graft'}
35
- end
36
-
37
- should "be able to retrieve data from the source hash data" do
38
- data = {'name' => 'Graft'}
39
- EmptyJsonModel.data_from(data).should == {'name' => 'Graft'}
40
- end
41
- end
42
-
43
- context "The JsonModelWithAttributes class" do
44
- should "know the names of all its attributes" do
45
- JsonModelWithAttributes.attributes.map {|a| a.name.to_s }.should == %w(name description rating)
46
- end
47
- end
48
-
49
- context "An instance of the JsonModelWithAttributes class" do
50
-
51
- should "assign attributes upon initialization" do
52
- json_data = '{"name":"Graft"}'
53
- JsonModelWithAttributes.new(json_data).name.should == 'Graft'
54
- end
55
-
56
- should "have a reference to the original source data" do
57
- json_data = '{"name":"Graft"}'
58
- data = JSON.parse(json_data)
59
-
60
- m = JsonModelWithAttributes.new(json_data)
61
- m.source_data.should == data
62
- end
63
-
64
- context "when populating from JSON data" do
65
- setup do
66
- json_data = '{"name":"Graft", "desc":"library", "rsp":{"rating":10}}'
67
- @model = JsonModelWithAttributes.new
68
- @model.populate_from(json_data)
69
- end
70
-
71
- should "have a value for :name" do
72
- @model.name.should == 'Graft'
73
- end
74
-
75
- should "have a value for :description" do
76
- @model.description.should == 'library'
77
- end
78
-
79
- should "have a value for :rating" do
80
- @model.rating.should == 10
81
- end
82
- end
83
-
84
- end
85
-
86
- end
@@ -1,161 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../test_helper'
2
-
3
- module Graft
4
- module Xml
5
- class AttributeTest < Test::Unit::TestCase
6
- context "An instance of the Graft::Xml::Attribute class" do
7
-
8
- should "know the name of the attribute" do
9
- attr = Graft::Xml::Attribute.new('foo')
10
- attr.name.should == :foo
11
- end
12
-
13
- should "have a default type class" do
14
- attr = Graft::Xml::Attribute.new('foo')
15
- attr.type_class.should == Graft::Xml::Type::String
16
- end
17
-
18
- should "have a default source" do
19
- attr = Graft::Xml::Attribute.new(:foo)
20
- attr.sources.should == ['foo']
21
- end
22
-
23
- should "be able to assign multiple sources" do
24
- attr = Graft::Xml::Attribute.new(:foo, :string, ['foo1', 'foo2'])
25
- attr.sources.should == ['foo1', 'foo2']
26
- end
27
-
28
- should "pull the location from the source" do
29
- attr = Graft::Xml::Attribute.new('foo')
30
- attr.location('foo').should == 'foo'
31
- end
32
-
33
- should "return the location when splitting" do
34
- attr = Graft::Xml::Attribute.new('foo')
35
- attr.split('foo').should == ['foo', nil]
36
- end
37
-
38
- should "return the name for the location when splitting if the location isn't specified" do
39
- attr = Graft::Xml::Attribute.new('foo')
40
- attr.split('@bar').should == ['foo', 'bar']
41
- end
42
-
43
- should "allow the setting of the location information" do
44
- attr = Graft::Xml::Attribute.new('foo', :string, 'bar')
45
- attr.sources.should == ['bar']
46
- end
47
-
48
- should "allow the setting of the attribute value" do
49
- attr = Graft::Xml::Attribute.new('foo')
50
- attr.attribute('@bogon').should == 'bogon'
51
- end
52
-
53
- should "use the location as the attribute" do
54
- attr = Graft::Xml::Attribute.new('foo')
55
- attr.attribute('foo').should == 'foo'
56
- end
57
-
58
- should "use the attribute for the attribute if specified" do
59
- attr = Graft::Xml::Attribute.new(:id, :string, '@nsid')
60
- attr.attribute('@nsid').should == 'nsid'
61
- end
62
-
63
- should "be able to retrieve the node from the path" do
64
- document = Hpricot.XML('<name>Bassdrive</name>')
65
- expected = document.at('name')
66
-
67
- attr = Graft::Xml::Attribute.new(:name)
68
- attr.node_for(document, 'name').should == expected
69
- end
70
-
71
- should "be able to retrieve the node that contains the specified attribute" do
72
- document = Hpricot.XML('<user id="1337" />')
73
- expected = document.at('user')
74
-
75
- attr = Graft::Xml::Attribute.new(:id)
76
- attr.node_for(document, '@id').should == expected
77
- end
78
-
79
- should "be able to retrieve the node for the specified attribute" do
80
- document = Hpricot.XML('<user nsid="1337" />')
81
- expected = document.at('user')
82
-
83
- attr = Graft::Xml::Attribute.new(:id, :string, '@nsid')
84
- attr.node_for(document, '@nsid').should == expected
85
- end
86
-
87
- should "be able to pull simple values from an XML document" do
88
- document = Hpricot.XML('<name>Bassdrive</name>')
89
- attr = Graft::Xml::Attribute.new(:name)
90
- attr.value_from(document).should == 'Bassdrive'
91
- end
92
-
93
- should "be able to pull an attribute value from the current XML node" do
94
- document = Hpricot.XML('<user id="1337" />')
95
- attr = Graft::Xml::Attribute.new(:id)
96
- attr.value_from(document).should == '1337'
97
- end
98
-
99
- should "be able to pull a specific attribute value from the current XML node" do
100
- document = Hpricot.XML('<user nsid="1337" />')
101
- attr = Graft::Xml::Attribute.new(:id, :string, '@nsid')
102
- attr.value_from(document).should == '1337'
103
- end
104
-
105
- should "be able to pull an attribute value for a node and attribute" do
106
- document = Hpricot.XML('<station><genre slug="dnb">Drum & Bass</genre></station>')
107
- attr = Graft::Xml::Attribute.new(:slug, :string, 'station/genre@slug')
108
- attr.value_from(document).should == 'dnb'
109
- end
110
-
111
- should "be able to pull a value from a nested XML node" do
112
- document = Hpricot.XML('<rsp><user>blip</user></rsp>')
113
- attr = Graft::Xml::Attribute.new(:user)
114
- attr.value_from(document).should == 'blip'
115
- end
116
-
117
- should "return nil if it cannot find the specified node" do
118
- document = Hpricot.XML('<user id="1" />')
119
- attr = Graft::Xml::Attribute.new(:photoset, :string, '@nsid')
120
- attr.value_from(document).should be(nil)
121
- end
122
-
123
- should "be able to try a series of nodes to find a value" do
124
- document = Hpricot.XML('<photoid>123</photoid>')
125
-
126
- attr = Graft::Xml::Attribute.new(:id, :string, ['photo@nsid', 'photoid'])
127
- attr.value_from(document).should == '123'
128
- end
129
-
130
- should "be able to convert an integer value" do
131
- document = Hpricot.XML('<id>1</id>')
132
-
133
- attr = Graft::Xml::Attribute.new(:id, :integer)
134
- attr.value_from(document).should == 1
135
- end
136
-
137
- should "be able to convert a boolean value" do
138
- document = Hpricot.XML('<active>true</active>')
139
-
140
- attr = Graft::Xml::Attribute.new(:active, :boolean)
141
- attr.value_from(document).should == true
142
- end
143
-
144
- should "be able to convert a date value" do
145
- document = Hpricot.XML('<due_on>2009-08-01</due_on>')
146
-
147
- attr = Graft::Xml::Attribute.new(:due_on, :date)
148
- attr.value_from(document).should == Date.parse('2009-08-01')
149
- end
150
-
151
- should "be able to convert a time value" do
152
- document = Hpricot.XML('<created_at>2009-08-01 00:00:00</created_at>')
153
-
154
- attr = Graft::Xml::Attribute.new(:created_at, :time)
155
- attr.value_from(document).should == Time.parse('2009-08-01 00:00:00')
156
- end
157
-
158
- end
159
- end
160
- end
161
- end
@@ -1,173 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../test_helper'
2
-
3
- class EmptyXmlModel
4
- include Graft::Xml::Model
5
- end
6
-
7
- class XmlModelWithAttributes
8
- include Graft::Xml::Model
9
-
10
- attribute :name
11
- attribute :description, :from => 'desc'
12
- attribute :rating, :from => 'rating@value'
13
- attribute :size, :from => "node[@type='size']@value"
14
-
15
- end
16
-
17
- class XmlModelWithAttributeType
18
- include Graft::Xml::Model
19
-
20
- attribute :id, :type => :integer
21
- end
22
-
23
- class XmlModelTest < Test::Unit::TestCase
24
-
25
- context "The EmptyXmlModel class" do
26
- should "have an empty list of attributes if none are supplied" do
27
- EmptyXmlModel.attributes.should == []
28
- end
29
-
30
- should "be able to return a collection of XML nodes" do
31
- xml =<<-XML
32
- <?xml version="1.0" encoding="UTF-8"?>
33
- <things>
34
- <thing><id>1</id></thing>
35
- <thing><id>2</id></thing>
36
- </things>
37
- XML
38
-
39
- document = Hpricot.XML(xml)
40
-
41
- EmptyXmlModel.expects(:data_from).with(xml).returns(document)
42
-
43
- EmptyXmlModel.expects(:new).with((document/'things/thing').first).returns('model_1')
44
- EmptyXmlModel.expects(:new).with((document/'things/thing').last).returns('model_2')
45
-
46
- collection = EmptyXmlModel.collection_from(xml, 'things/thing')
47
- collection.should == ['model_1', 'model_2']
48
- end
49
-
50
- should "be able to retrieve data from the source XML string" do
51
- xml = '<name>Graft</name>'
52
- Hpricot.expects(:XML).with(xml).returns('document')
53
-
54
- EmptyXmlModel.data_from(xml).should == 'document'
55
- end
56
-
57
- should "be able to retrieve data from the source document data" do
58
- document = Hpricot.XML('<name>Graft</name>')
59
- EmptyXmlModel.data_from(document).should == document
60
- end
61
-
62
- end
63
-
64
- context "The XmlModelWithAttributes class" do
65
- should "know the names of all its attributes" do
66
- XmlModelWithAttributes.attributes.map {|a| a.name.to_s }.should == %w(name description rating size)
67
- end
68
- end
69
-
70
- context "The XmlModelWithAttributeType class" do
71
- should "know that it's attribute is of type :integer" do
72
- attribute = XmlModelWithAttributeType.attributes.first
73
- attribute.type_class.should == Graft::Xml::Type::Integer
74
- end
75
-
76
- should "be able to generate an XML representation of itself" do
77
-
78
- m = XmlModelWithAttributeType.new
79
- m.id = 1
80
-
81
- xml = String.new
82
- xml << '<?xml version="1.0" encoding="UTF-8"?>'
83
- xml << '<model>'
84
- xml << '<id>1</id>'
85
- xml << '</model>'
86
-
87
- m.to_xml('model').should == xml
88
-
89
- end
90
- end
91
-
92
- context "An instance of the EmptyXmlModel class" do
93
-
94
- should "return an empty hash when calling :to_hash" do
95
- m = EmptyXmlModel.new
96
- m.to_hash.should == {}
97
- end
98
-
99
- end
100
-
101
- context "An instance of the XmlModelWithAttributes class" do
102
-
103
- setup { @simple_xml = '<name>Graft</name>' }
104
-
105
- should "have default reader method for :name" do
106
- XmlModelWithAttributes.new.respond_to?(:name).should be(true)
107
- end
108
-
109
- should "be able to populate its data on initialization" do
110
- xml = Hpricot.XML(@simple_xml)
111
- XmlModelWithAttributes.new(xml).name.should == 'Graft'
112
- end
113
-
114
- should "have a reference to the original document" do
115
- xml = Hpricot.XML(@simple_xml)
116
- XmlModelWithAttributes.new(xml).source_data.should == xml
117
- end
118
-
119
- should "be able to populate from an XML string" do
120
- XmlModelWithAttributes.new(@simple_xml).name.should == 'Graft'
121
- end
122
-
123
- should "return a hash representation of itself" do
124
- m = XmlModelWithAttributes.new
125
-
126
- m.name = 'name'
127
- m.description = 'description'
128
- m.rating = '5'
129
- m.size = 'large'
130
-
131
- m.to_hash.should == {
132
- 'name' => 'name',
133
- 'description' => 'description',
134
- 'rating' => '5',
135
- 'size' => 'large'
136
- }
137
- end
138
-
139
- context "when populating data from an XML document" do
140
-
141
- setup do
142
- xml = <<-XML
143
- <name>Graft</name>
144
- <desc>A sweet Ruby library</desc>
145
- <rating value="100" />
146
- <node type="color" value="blue" />
147
- <node type="size" value="large" />
148
- XML
149
-
150
- @model = XmlModelWithAttributes.new
151
- @model.populate_from(Hpricot.XML(xml))
152
- end
153
-
154
- should "have the correct value for :name" do
155
- @model.name.should == 'Graft'
156
- end
157
-
158
- should "have the correct value for :description" do
159
- @model.description.should == 'A sweet Ruby library'
160
- end
161
-
162
- should "have the correct value for :rating" do
163
- @model.rating.should == '100'
164
- end
165
-
166
- # should "have the correct value for :size" do
167
- # @model.size.should == 'large'
168
- # end
169
-
170
- end
171
- end
172
-
173
- end
@@ -1,65 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../test_helper'
2
-
3
- module Graft
4
- module Xml
5
-
6
- class StringTest < Test::Unit::TestCase
7
- context "An instance of the Graft::Xml::Type::String class" do
8
-
9
- should_convert 'a string', :to => 'a string'
10
- should_convert '', :to => nil
11
-
12
- end
13
- end
14
-
15
- class BooleanTest < Test::Unit::TestCase
16
- context "An instance of the Graft::Xml::Type::Boolean class" do
17
-
18
- should_convert 'true', :to => true
19
- should_convert 'false', :to => false
20
- should_convert '0', :to => false
21
- should_convert '1', :to => true
22
- should_convert '', :to => nil
23
-
24
- should_fail_when_converting 'foo'
25
-
26
- end
27
- end
28
-
29
- class IntegerTest < Test::Unit::TestCase
30
- context "An instance of the Graft::Xml::Type::Integer class" do
31
-
32
- should_convert '1', :to => 1
33
- should_convert '', :to => nil
34
-
35
- should_fail_when_converting 'foo'
36
-
37
- end
38
- end
39
-
40
- class DateTest < Test::Unit::TestCase
41
-
42
- context "An instance of the Graft::Xml::Type::Date class" do
43
-
44
- should_convert '2008-08-01', :to => Date.parse('2008-08-01')
45
- should_convert '', :to => nil
46
-
47
- end
48
-
49
- end
50
-
51
- class TimeTest < Test::Unit::TestCase
52
-
53
- context "An instance of the Graft::Xml::Type::Time class" do
54
-
55
- should_convert '2008-07-28T16:57:10Z', :to => Time.parse('2008-07-28T16:57:10Z')
56
- should_convert '2008-12-25 18:26:55', :to => Time.parse('2008-12-25 18:26:55')
57
- should_convert '1230274722', :to => Time.at(1230274722)
58
- should_convert '', :to => nil
59
-
60
- end
61
-
62
- end
63
-
64
- end
65
- end