-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
metadata_spec.rb
205 lines (182 loc) · 8.97 KB
/
metadata_spec.rb
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# -*- coding: utf-8 -*-
p# -*- coding: utf-8 -*-
require File.dirname(__FILE__) + '/spec_helper.rb'
require 'rubygems'
require 'nokogiri'
describe GEPUB::Metadata do
it 'should be initialized' do
metadata = GEPUB::Metadata.new
metadata.ns_prefix(GEPUB::XMLUtil::DC_NS).should == 'dc'
metadata.ns_prefix(GEPUB::XMLUtil::OPF_NS).should be_nil
end
it 'should be initialized with version 2.0' do
metadata = GEPUB::Metadata.new('2.0')
metadata.ns_prefix(GEPUB::XMLUtil::DC_NS).should == 'dc'
metadata.ns_prefix(GEPUB::XMLUtil::OPF_NS).should == 'opf'
end
context 'Parse Existing OPF' do
before do
@metadata = GEPUB::Package.parse_opf(File.open(File.dirname(__FILE__) + '/fixtures/testdata/test.opf'), '/package.opf').instance_eval{ @metadata }
end
it 'should parse title' do
@metadata.main_title.should == 'TheTitle'
@metadata.title_list.size.should == 2
@metadata.title.to_s.should == 'TheTitle'
end
it 'should parse main title with not first display-seq' do
metadata = GEPUB::Package.parse_opf(File.open(File.dirname(__FILE__) + '/fixtures/testdata/test2.opf'), '/package.opf').instance_eval{ @metadata }
metadata.title.to_s.should == 'TheTitle'
end
it 'should parse title-type' do
@metadata.title_list[0].refiner_list('title-type').size.should == 1
@metadata.title_list[0].refiner_list('title-type')[0].content.should == 'main'
@metadata.title_list[1].refiner_list('title-type').size.should == 1
@metadata.title_list[1].refiner_list('title-type')[0].content.should == 'collection'
end
it 'should parse identifier' do
@metadata.identifier_list.size.should == 2
@metadata.identifier.to_s.should == 'urn:uuid:1234567890'
@metadata.identifier_list[0].content.should == 'urn:uuid:1234567890'
@metadata.identifier_list[0].refiner('identifier-type').to_s.should == 'uuid'
@metadata.identifier_list[1].content.should == 'http://example.jp/epub/test/url'
@metadata.identifier_list[1].refiner('identifier-type').to_s.should == 'uri'
end
it 'should parse OPF2.0 meta node' do
@metadata.oldstyle_meta.size.should == 1
@metadata.oldstyle_meta[0].name == 'meta'
@metadata.oldstyle_meta[0]['name'] == 'cover'
@metadata.oldstyle_meta[0]['content'] == 'cover-image'
end
end
context 'Should parse OPF2.0' do
before do
@metadata = GEPUB::Package.parse_opf(File.open(File.dirname(__FILE__) + '/fixtures/testdata/package_2_0.opf'), '/package.opf').instance_eval{ @metadata }
end
it 'should parse title' do
@metadata.main_title.should == 'thetitle'
@metadata.title_list.size.should == 1
end
it 'should parse OPF2.0 meta node' do
@metadata.oldstyle_meta.size.should == 1
@metadata.oldstyle_meta[0].name == 'meta'
@metadata.oldstyle_meta[0]['name'] == 'cover'
@metadata.oldstyle_meta[0]['content'] == 'cover-image'
end
end
context 'Generate New OPF' do
it 'should write and read identifier' do
metadata = GEPUB::Metadata.new
metadata.add_identifier 'the_set_identifier', 'pub-id'
metadata.identifier.to_s.should == 'the_set_identifier'
metadata.identifier_list[0]['id'].should == 'pub-id'
end
it 'should write and read identifier with identifier-type' do
metadata = GEPUB::Metadata.new
metadata.add_identifier 'http://example.jp/book/url', 'pub-id', 'uri'
metadata.identifier.to_s.should == 'http://example.jp/book/url'
metadata.identifier_list[0]['id'].should == 'pub-id'
metadata.identifier_list[0].identifier_type.to_s.should == 'uri'
end
it 'should write and read title' do
metadata = GEPUB::Metadata.new
metadata.add_title('The Main Title')
metadata.title.to_s.should == 'The Main Title'
end
it 'should write and read title with type' do
metadata = GEPUB::Metadata.new
metadata.add_title('The Main Title', 'maintitle', GEPUB::TITLE_TYPE::MAIN)
metadata.title.to_s.should == 'The Main Title'
metadata.title.title_type.to_s.should == 'main'
end
it 'should write and read multipletitle with type' do
metadata = GEPUB::Metadata.new
metadata.add_title('The Main Title', 'maintitle', GEPUB::TITLE_TYPE::MAIN)
metadata.add_title('The Book Series', 'series', GEPUB::TITLE_TYPE::COLLECTION).set_group_position(1)
metadata.title.to_s.should == 'The Main Title'
metadata.title.title_type.to_s.should == 'main'
metadata.title_list[1].to_s.should == 'The Book Series'
metadata.title_list[1].title_type.to_s.should == 'collection'
metadata.title_list[1].group_position.to_s.should == '1'
end
it 'should handle alternate-script metadata of creator, not using method chain' do
metadata = GEPUB::Metadata.new
metadata.add_creator('TheCreator', 'author', 'aut').set_display_seq(1).set_file_as('Creator, The').add_alternates({ 'ja-JP' => '作成者' })
metadata.creator.to_s.should == 'TheCreator'
metadata.creator.to_s('ja').should == '作成者'
end
it 'should handle alternate-script metadata of creator, not using method chain' do
metadata = GEPUB::Metadata.new
m = metadata.add_creator('TheCreator', 'author', 'aut')
m.display_seq = 1
m.file_as = 'Creator, The'
m.add_alternates({ 'ja-JP' => '作成者' })
metadata.creator.to_s.should == 'TheCreator'
metadata.creator.to_s('ja').should == '作成者'
end
it 'should detect duplicate id' do
metadata = GEPUB::Metadata.new
metadata.add_creator('TheCreator', 'id', 'aut')
lambda { metadata.add_title('TheTitle', 'id') }.should raise_error(RuntimeError, "id 'id' is already in use.")
end
it 'should generate empty metadata xml' do
metadata = GEPUB::Metadata.new
builder = Nokogiri::XML::Builder.new { |xml|
xml.package('xmlns' => "http://www.idpf.org/2007/opf",'version' => "3.0",'unique-identifier' => "pub-id",'xml:lang' => "ja") {
metadata.to_xml(xml)
}
}
xml = Nokogiri::XML::Document.parse(builder.to_xml).at_xpath('//xmlns:metadata', { 'xmlns' => GEPUB::XMLUtil::OPF_NS})
xml.namespaces['xmlns:dc'].should == GEPUB::XMLUtil::DC_NS
end
it 'should handle date with Time object' do
metadata = GEPUB::Metadata.new
a = Time.parse '2012-02-27 20:00:00 UTC'
metadata.add_date(a, 'date')
metadata.date.to_s.should == '2012-02-27T20:00:00Z'
end
it 'should handle date with a not W3C-DTF string' do
metadata = GEPUB::Metadata.new
metadata.add_date('2012-02-28 05:00:00 +0900', 'date')
metadata.date.to_s.should == '2012-02-27T20:00:00Z'
end
it 'should generate metadata with id xml' do
metadata = GEPUB::Metadata.new
metadata.add_identifier('the_uid', nil)
builder = Nokogiri::XML::Builder.new { |xml|
xml.package('xmlns' => "http://www.idpf.org/2007/opf",'version' => "3.0",'unique-identifier' => "pub-id",'xml:lang' => "ja") {
metadata.to_xml(xml)
}
}
Nokogiri::XML::Document.parse(builder.to_xml).at_xpath('//dc:identifier', metadata.instance_eval {@namespaces}).content.should == 'the_uid'
end
it 'should generate metadata with creator refiner' do
metadata = GEPUB::Metadata.new
metadata.add_creator('TheCreator', nil, 'aut').set_display_seq(1).set_file_as('Creator, The').add_alternates({ 'ja-JP' => '作成者' })
builder = Nokogiri::XML::Builder.new { |xml|
xml.package('xmlns' => "http://www.idpf.org/2007/opf",'version' => "3.0",'unique-identifier' => "pub-id",'xml:lang' => "ja") {
metadata.to_xml(xml)
}
}
xml = Nokogiri::XML::Document.parse(builder.to_xml)
ns = metadata.instance_eval { @namespaces }
xml.at_xpath('//dc:creator', ns).content.should == 'TheCreator'
id = xml.at_xpath('//dc:creator', ns)['id']
xml.at_xpath("//xmlns:meta[@refines='##{id}' and @property='role']").content.should == 'aut'
xml.at_xpath("//xmlns:meta[@refines='##{id}' and @property='display-seq']").content.should == '1'
xml.at_xpath("//xmlns:meta[@refines='##{id}' and @property='file-as']").content.should == 'Creator, The'
xml.at_xpath("//xmlns:meta[@refines='##{id}' and @property='alternate-script' and @xml:lang='ja-JP']").content.should == '作成者'
end
it 'should generate metadata with old style meta tag' do
metadata = GEPUB::Metadata.new
metadata.add_creator('TheCreator', nil, 'aut').set_display_seq(1).set_file_as('Creator, The').add_alternates({ 'ja-JP' => '作成者' })
metadata.add_oldstyle_meta(nil, { 'name' => 'cover', 'content' => 'cover.jpg' })
builder = Nokogiri::XML::Builder.new { |xml|
xml.package('xmlns' => "http://www.idpf.org/2007/opf",'version' => "3.0",'unique-identifier' => "pub-id",'xml:lang' => "ja") {
metadata.to_xml(xml)
}
}
xml = Nokogiri::XML::Document.parse(builder.to_xml)
xml.xpath("//xmlns:meta[@name='cover' and @content='cover.jpg']").size.should == 1
end
end
end