/
normaliser_spec.rb
125 lines (91 loc) · 3.38 KB
/
normaliser_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
# coding: utf-8
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
require 'onix'
context "ONIX::Normaliser", "with a simple short tag file" do
before(:each) do
@data_path = File.join(File.dirname(__FILE__),"..","data")
@filename = File.join(@data_path, "short_tags.xml")
@outfile = @filename + ".new"
end
after(:each) do
File.unlink(@outfile) if File.file?(@outfile)
end
specify "should correctly convert short tag file to reference tag" do
ONIX::Normaliser.process(@filename, @outfile)
File.file?(@outfile).should be_true
content = File.read(@outfile)
content.include?("<m174>").should be_false
content.include?("<FromCompany>").should be_true
end
end
context "ONIX::Normaliser", "with an ISO-8859-1 file" do
before(:each) do
@data_path = File.join(File.dirname(__FILE__),"..","data")
@filename = File.join(@data_path, "iso_8859_1.xml")
@outfile = @filename + ".new"
end
after(:each) do
File.unlink(@outfile) if File.file?(@outfile)
end
specify "should correctly convert an iso-8859-1 file to UTF-8" do
ONIX::Normaliser.process(@filename, @outfile)
File.file?(@outfile).should be_true
content = File.read(@outfile)
content.include?("ISO-8859-1").should be_false
content.include?("UTF-8").should be_true
`isutf8 #{File.expand_path(@outfile)}`.strip.should eql("")
end
end
context "ONIX::Normaliser", "with an file using entities" do
before(:each) do
@data_path = File.join(File.dirname(__FILE__),"..","data")
@filename = File.join(@data_path, "entities.xml")
@outfile = @filename + ".new"
end
after(:each) do
File.unlink(@outfile) if File.file?(@outfile)
end
specify "should correctly convert named entities to numeric entities" do
ONIX::Normaliser.process(@filename, @outfile)
File.file?(@outfile).should be_true
content = File.read(@outfile)
content.include?("–").should be_false
content.include?("–").should be_true
end
end
context "ONIX::Normaliser", "with a utf8 file that has no declared encoding" do
before(:each) do
@data_path = File.join(File.dirname(__FILE__),"..","data")
@filename = File.join(@data_path, "no_encoding.xml")
@outfile = @filename + ".new"
end
after(:each) do
File.unlink(@outfile) if File.file?(@outfile)
end
# this is to test for a bug where an exception was raised on files that
# had no declared encoding
specify "should add a utf-8 marker to the file" do
ONIX::Normaliser.process(@filename, @outfile)
File.file?(@outfile).should be_true
content = File.read(@outfile)
content.include?("encoding=\"UTF-8\"").should be_true
end
end
context "ONIX::Normaliser", "with a utf8 file that has illegal control chars" do
before(:each) do
@data_path = File.join(File.dirname(__FILE__),"..","data")
@filename = File.join(@data_path, "control_chars.xml")
@outfile = @filename + ".new"
end
after(:each) do
File.unlink(@outfile) if File.file?(@outfile)
end
# this is to test for a bug where an exception was raised on files that
# had no declared encoding
specify "should remove all control chars except LF, CR and TAB" do
ONIX::Normaliser.process(@filename, @outfile)
File.file?(@outfile).should be_true
content = File.read(@outfile)
content.include?("<TitleText>OXFORDPICTURE DICTIONARY CHINESE</TitleText>").should be_true
end
end