test_encoding.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # -*- coding: utf-8 -*-
  2. require 'helper'
  3. module SQLite3
  4. class TestEncoding < SQLite3::TestCase
  5. def setup
  6. @db = SQLite3::Database.new(':memory:')
  7. @create = "create table ex(id int, data string)"
  8. @insert = "insert into ex(id, data) values (?, ?)"
  9. @db.execute(@create);
  10. end
  11. def test_default_internal_is_honored
  12. warn_before = $-w
  13. $-w = false
  14. before_enc = Encoding.default_internal
  15. str = "壁に耳あり、障子に目あり"
  16. stmt = @db.prepare('insert into ex(data) values (?)')
  17. stmt.bind_param 1, str
  18. stmt.step
  19. Encoding.default_internal = 'EUC-JP'
  20. string = @db.execute('select data from ex').first.first
  21. assert_equal Encoding.default_internal, string.encoding
  22. assert_equal str.encode('EUC-JP'), string
  23. assert_equal str, string.encode(str.encoding)
  24. ensure
  25. Encoding.default_internal = before_enc
  26. $-w = warn_before
  27. end
  28. def test_blob_is_binary
  29. str = "猫舌"
  30. @db.execute('create table foo(data text)')
  31. stmt = @db.prepare('insert into foo(data) values (?)')
  32. stmt.bind_param(1, SQLite3::Blob.new(str))
  33. stmt.step
  34. string = @db.execute('select data from foo').first.first
  35. assert_equal Encoding.find('ASCII-8BIT'), string.encoding
  36. assert_equal str, string.force_encoding('UTF-8')
  37. end
  38. def test_blob_is_ascii8bit
  39. str = "猫舌"
  40. @db.execute('create table foo(data text)')
  41. stmt = @db.prepare('insert into foo(data) values (?)')
  42. stmt.bind_param(1, str.dup.force_encoding("ASCII-8BIT"))
  43. stmt.step
  44. string = @db.execute('select data from foo').first.first
  45. assert_equal Encoding.find('ASCII-8BIT'), string.encoding
  46. assert_equal str, string.force_encoding('UTF-8')
  47. end
  48. def test_blob_with_eucjp
  49. str = "猫舌".encode("EUC-JP")
  50. @db.execute('create table foo(data text)')
  51. stmt = @db.prepare('insert into foo(data) values (?)')
  52. stmt.bind_param(1, SQLite3::Blob.new(str))
  53. stmt.step
  54. string = @db.execute('select data from foo').first.first
  55. assert_equal Encoding.find('ASCII-8BIT'), string.encoding
  56. assert_equal str, string.force_encoding('EUC-JP')
  57. end
  58. def test_db_with_eucjp
  59. db = SQLite3::Database.new(':memory:'.encode('EUC-JP'))
  60. assert_equal(Encoding.find('UTF-8'), db.encoding)
  61. end
  62. def test_db_with_utf16
  63. utf16 = ([1].pack("I") == [1].pack("N")) ? "UTF-16BE" : "UTF-16LE"
  64. db = SQLite3::Database.new(':memory:'.encode(utf16))
  65. assert_equal(Encoding.find(utf16), db.encoding)
  66. end
  67. def test_statement_eucjp
  68. str = "猫舌"
  69. @db.execute("insert into ex(data) values ('#{str}')".encode('EUC-JP'))
  70. row = @db.execute("select data from ex")
  71. assert_equal @db.encoding, row.first.first.encoding
  72. assert_equal str, row.first.first
  73. end
  74. def test_statement_utf8
  75. str = "猫舌"
  76. @db.execute("insert into ex(data) values ('#{str}')")
  77. row = @db.execute("select data from ex")
  78. assert_equal @db.encoding, row.first.first.encoding
  79. assert_equal str, row.first.first
  80. end
  81. def test_encoding
  82. assert_equal Encoding.find("UTF-8"), @db.encoding
  83. end
  84. def test_utf_8
  85. str = "猫舌"
  86. @db.execute(@insert, [10, str])
  87. row = @db.execute("select data from ex")
  88. assert_equal @db.encoding, row.first.first.encoding
  89. assert_equal str, row.first.first
  90. end
  91. def test_euc_jp
  92. str = "猫舌".encode('EUC-JP')
  93. @db.execute(@insert, [10, str])
  94. row = @db.execute("select data from ex")
  95. assert_equal @db.encoding, row.first.first.encoding
  96. assert_equal str.encode('UTF-8'), row.first.first
  97. end
  98. end if RUBY_VERSION >= '1.9.1'
  99. end