Class: SQLite3::TestEncoding

Inherits:
TestCase
  • Object
show all
Defined in:
lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb,
lib/sqlite3-1.5.3-x86_64-darwin/test/test_encoding.rb

Instance Method Summary collapse

Methods inherited from TestCase

#assert_nothing_raised

Instance Method Details

#setupObject



7
8
9
10
11
12
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb', line 7

def setup
  @db = SQLite3::Database.new(':memory:')
  @create = "create table ex(id int, data string)"
  @insert = "insert into ex(id, data) values (?, ?)"
  @db.execute(@create);
end

#test_blob_is_ascii8bitObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb', line 82

def test_blob_is_ascii8bit
  str = "猫舌"
  @db.execute('create table foo(data text)')
  stmt = @db.prepare('insert into foo(data) values (?)')
  stmt.bind_param(1, str.dup.force_encoding("ASCII-8BIT"))
  stmt.step

  string = @db.execute('select data from foo').first.first
  assert_equal Encoding.find('ASCII-8BIT'), string.encoding
  assert_equal str, string.force_encoding('UTF-8')
end

#test_blob_is_binaryObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb', line 70

def test_blob_is_binary
  str = "猫舌"
  @db.execute('create table foo(data text)')
  stmt = @db.prepare('insert into foo(data) values (?)')
  stmt.bind_param(1, SQLite3::Blob.new(str))
  stmt.step

  string = @db.execute('select data from foo').first.first
  assert_equal Encoding.find('ASCII-8BIT'), string.encoding
  assert_equal str, string.force_encoding('UTF-8')
end

#test_blob_with_eucjpObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb', line 94

def test_blob_with_eucjp
  str = "猫舌".encode("EUC-JP")
  @db.execute('create table foo(data text)')
  stmt = @db.prepare('insert into foo(data) values (?)')
  stmt.bind_param(1, SQLite3::Blob.new(str))
  stmt.step

  string = @db.execute('select data from foo').first.first
  assert_equal Encoding.find('ASCII-8BIT'), string.encoding
  assert_equal str, string.force_encoding('EUC-JP')
end

#test_db_with_eucjpObject



106
107
108
109
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb', line 106

def test_db_with_eucjp
  db = SQLite3::Database.new(':memory:'.encode('EUC-JP'))
  assert_equal(Encoding.find('UTF-8'), db.encoding)
end

#test_db_with_utf16Object



111
112
113
114
115
116
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb', line 111

def test_db_with_utf16
  utf16 = ([1].pack("I") == [1].pack("N")) ? "UTF-16BE" : "UTF-16LE"

  db = SQLite3::Database.new(':memory:'.encode(utf16))
  assert_equal(Encoding.find(utf16), db.encoding)
end

#test_default_internal_is_honoredObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb', line 48

def test_default_internal_is_honored
  warn_before = $-w
  $-w = false

  before_enc = Encoding.default_internal

  str = "壁に耳あり、障子に目あり"
  stmt = @db.prepare('insert into ex(data) values (?)')
  stmt.bind_param 1, str
  stmt.step

  Encoding.default_internal = 'EUC-JP'
  string = @db.execute('select data from ex').first.first

  assert_equal Encoding.default_internal, string.encoding
  assert_equal str.encode('EUC-JP'), string
  assert_equal str, string.encode(str.encoding)
ensure
  Encoding.default_internal = before_enc
  $-w = warn_before
end

#test_encodingObject



134
135
136
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb', line 134

def test_encoding
  assert_equal Encoding.find("UTF-8"), @db.encoding
end

#test_euc_jpObject



146
147
148
149
150
151
152
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb', line 146

def test_euc_jp
  str = "猫舌".encode('EUC-JP')
  @db.execute(@insert, [10, str])
  row = @db.execute("select data from ex")
  assert_equal @db.encoding, row.first.first.encoding
  assert_equal str.encode('UTF-8'), row.first.first
end

#test_insert_encodingObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb', line 29

def test_insert_encoding
  str = "foo"
  utf16 = ([1].pack("I") == [1].pack("N")) ? "UTF-16BE" : "UTF-16LE"
  db = SQLite3::Database.new(':memory:'.encode(utf16))
  db.execute @create
  stmt = db.prepare @insert

  ['US-ASCII', utf16, 'EUC-JP', 'UTF-8'].each_with_index do |enc,i|
    stmt.bind_param 1, i
    stmt.bind_param 2, str.encode(enc)
    stmt.to_a
    stmt.reset!
  end

  db.execute('select data from ex').flatten.each do |s|
    assert_equal str, s
  end
end

#test_select_encoding_on_utf_16Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb', line 14

def test_select_encoding_on_utf_16
  str = "foo"
  utf16 = ([1].pack("I") == [1].pack("N")) ? "UTF-16BE" : "UTF-16LE"
  db = SQLite3::Database.new(':memory:'.encode(utf16))
  db.execute @create
  db.execute "insert into ex (id, data) values (1, \"#{str}\")"

  stmt = db.prepare 'select * from ex where data = ?'
  ['US-ASCII', utf16, 'EUC-JP', 'UTF-8'].each do |enc|
    stmt.bind_param 1, str.encode(enc)
    assert_equal 1, stmt.to_a.length
    stmt.reset!
  end
end

#test_statement_eucjpObject



118
119
120
121
122
123
124
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb', line 118

def test_statement_eucjp
  str = "猫舌"
  @db.execute("insert into ex(data) values ('#{str}')".encode('EUC-JP'))
  row = @db.execute("select data from ex")
  assert_equal @db.encoding, row.first.first.encoding
  assert_equal str, row.first.first
end

#test_statement_utf8Object



126
127
128
129
130
131
132
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb', line 126

def test_statement_utf8
  str = "猫舌"
  @db.execute("insert into ex(data) values ('#{str}')")
  row = @db.execute("select data from ex")
  assert_equal @db.encoding, row.first.first.encoding
  assert_equal str, row.first.first
end

#test_utf_8Object



138
139
140
141
142
143
144
# File 'lib/sqlite3-1.5.3-arm64-darwin/test/test_encoding.rb', line 138

def test_utf_8
  str = "猫舌"
  @db.execute(@insert, [10, str])
  row = @db.execute("select data from ex")
  assert_equal @db.encoding, row.first.first.encoding
  assert_equal str, row.first.first
end