File: force_encoding_spec.rb

package info (click to toggle)
ruby-sequel 5.41.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,548 kB
  • sloc: ruby: 104,241; makefile: 3
file content (126 lines) | stat: -rw-r--r-- 3,432 bytes parent folder | download | duplicates (4)
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
require_relative "spec_helper"

describe "force_encoding plugin" do
  before do
    @c = Class.new(Sequel::Model)
    @c.columns :id, :x
    @c.plugin :force_encoding, 'UTF-8'
    @e1 = Encoding.find('UTF-8')
  end

  it "should force encoding to given encoding on load" do
    s = 'blah'.dup
    s.force_encoding('US-ASCII')
    o = @c.load(:id=>1, :x=>s)
    o.x.must_equal 'blah'
    o.x.encoding.must_equal @e1
  end
  
  it "should force encoding to given encoding when setting column values" do
    s = 'blah'.dup
    s.force_encoding('US-ASCII')
    o = @c.new(:x=>s)
    o.x.must_equal 'blah'
    o.x.encoding.must_equal @e1
  end
  
  it "should not force encoding of blobs to given encoding on load" do
    s = Sequel.blob('blah'.dup.force_encoding('BINARY'))
    o = @c.load(:id=>1, :x=>s)
    o.x.must_equal 'blah'
    o.x.encoding.must_equal Encoding.find('BINARY')
  end
  
  it "should not force encoding of blobs to given encoding when setting column values" do
    s = Sequel.blob('blah'.dup.force_encoding('BINARY'))
    o = @c.new(:x=>s)
    o.x.must_equal 'blah'
    o.x.encoding.must_equal Encoding.find('BINARY')
  end
  
  it "should work correctly when given a frozen string" do
    s = 'blah'.dup
    s.force_encoding('US-ASCII')
    s.freeze
    o = @c.new(:x=>s)
    o.x.must_equal 'blah'
    o.x.encoding.must_equal @e1
  end
  
  it "should have a forced_encoding class accessor" do
    s = 'blah'.dup
    s.force_encoding('US-ASCII')
    @c.forced_encoding = 'Windows-1258'
    o = @c.load(:id=>1, :x=>s)
    o.x.must_equal 'blah'
    o.x.encoding.must_equal Encoding.find('Windows-1258')
  end
  
  it "should not force encoding if forced_encoding is nil" do
    s = 'blah'.dup
    s.force_encoding('US-ASCII')
    @c.forced_encoding = nil
    o = @c.load(:id=>1, :x=>s)
    o.x.must_equal 'blah'
    o.x.encoding.must_equal Encoding.find('US-ASCII')
  end
  
  it "should work correctly when subclassing" do
    c = Class.new(@c)
    s = 'blah'.dup
    s.force_encoding('US-ASCII')
    o = c.load(:id=>1, :x=>s)
    o.x.must_equal 'blah'
    o.x.encoding.must_equal @e1
    
    c.plugin :force_encoding, 'UTF-16LE'
    s = String.new
    s.force_encoding('US-ASCII')
    o = c.load(:id=>1, :x=>s)
    o.x.must_equal ''
    o.x.encoding.must_equal Encoding.find('UTF-16LE')
    
    @c.plugin :force_encoding, 'UTF-32LE'
    s = String.new
    s.force_encoding('US-ASCII')
    o = @c.load(:id=>1, :x=>s)
    o.x.must_equal ''
    o.x.encoding.must_equal Encoding.find('UTF-32LE')
    
    s = String.new
    s.force_encoding('US-ASCII')
    o = c.load(:id=>1, :x=>s)
    o.x.must_equal ''
    o.x.encoding.must_equal Encoding.find('UTF-16LE')
  end
  
  it "should work when saving new model instances" do
    o = @c.new
    @c.dataset = DB[:a].with_extend do
      def first
        s = 'blah'.dup
        s.force_encoding('US-ASCII')
        {:id=>1, :x=>s}
      end
    end
    @c.instance_variable_set(:@fast_pk_lookup_sql, nil)
    o.save
    o.x.must_equal 'blah'
    o.x.encoding.must_equal @e1
  end
  
  it "should work when refreshing model instances" do
    o = @c.load(:id=>1, :x=>'as'.dup)
    @c.dataset = DB[:a].with_extend do
      def first
        s = 'blah'.dup
        s.force_encoding('US-ASCII')
        {:id=>1, :x=>s}
      end
    end
    @c.instance_variable_set(:@fast_pk_lookup_sql, nil)
    o.refresh
    o.x.must_equal 'blah'
    o.x.encoding.must_equal @e1
  end
end