/
force_encoding_spec.rb
121 lines (110 loc) · 3.07 KB
/
force_encoding_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
require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")
if RUBY_VERSION >= '1.9.0'
describe "force_encoding plugin" do
before do
@c = Class.new(Sequel::Model) do
end
@c.columns :id, :x
@c.plugin :force_encoding, 'UTF-8'
@e1 = Encoding.find('UTF-8')
end
specify "should force encoding to given encoding on load" do
s = 'blah'
s.force_encoding('US-ASCII')
o = @c.load(:id=>1, :x=>s)
o.x.should == 'blah'
o.x.encoding.should == @e1
end
specify "should force encoding to given encoding when setting column values" do
s = 'blah'
s.force_encoding('US-ASCII')
o = @c.new(:x=>s)
o.x.should == 'blah'
o.x.encoding.should == @e1
end
specify "should have a forced_encoding class accessor" do
s = 'blah'
s.force_encoding('US-ASCII')
@c.forced_encoding = 'Windows-1258'
o = @c.load(:id=>1, :x=>s)
o.x.should == 'blah'
o.x.encoding.should == Encoding.find('Windows-1258')
end
specify "should not force encoding if forced_encoding is nil" do
s = 'blah'
s.force_encoding('US-ASCII')
@c.forced_encoding = nil
o = @c.load(:id=>1, :x=>s)
o.x.should == 'blah'
o.x.encoding.should == Encoding.find('US-ASCII')
end
specify "should work correctly when subclassing" do
c = Class.new(@c)
s = 'blah'
s.force_encoding('US-ASCII')
o = c.load(:id=>1, :x=>s)
o.x.should == 'blah'
o.x.encoding.should == @e1
c.plugin :force_encoding, 'UTF-16LE'
s = ''
s.force_encoding('US-ASCII')
o = c.load(:id=>1, :x=>s)
o.x.should == ''
o.x.encoding.should == Encoding.find('UTF-16LE')
@c.plugin :force_encoding, 'UTF-32LE'
s = ''
s.force_encoding('US-ASCII')
o = @c.load(:id=>1, :x=>s)
o.x.should == ''
o.x.encoding.should == Encoding.find('UTF-32LE')
s = ''
s.force_encoding('US-ASCII')
o = c.load(:id=>1, :x=>s)
o.x.should == ''
o.x.encoding.should == Encoding.find('UTF-16LE')
end
specify "should work when saving new model instances" do
o = @c.new
ds = MODEL_DB[:a]
def ds.first
s = 'blah'
s.force_encoding('US-ASCII')
{:id=>1, :x=>s}
end
@c.dataset = ds
o.save
o.x.should == 'blah'
o.x.encoding.should == @e1
end
specify "should work when refreshing model instances" do
o = @c.load(:id=>1, :x=>'as')
ds = MODEL_DB[:a]
def ds.first
s = 'blah'
s.force_encoding('US-ASCII')
{:id=>1, :x=>s}
end
@c.dataset = ds
o.refresh
o.x.should == 'blah'
o.x.encoding.should == @e1
end
specify "should work when used with the identity_map plugin if the identity_map plugin is setup first" do
@c = Class.new(Sequel::Model) do
end
@c.columns :id, :x
@c.plugin :identity_map
@c.plugin :force_encoding, 'UTF-8'
@c.with_identity_map do
o = @c.load(:id=>1)
s = 'blah'
s.force_encoding('US-ASCII')
@c.load(:id=>1, :x=>s)
o.x.should == 'blah'
o.x.encoding.should == @e1
end
end
end
else
skip_warn "force_encoding plugin: only works on ruby 1.9"
end