-
Notifications
You must be signed in to change notification settings - Fork 4
/
start.rb
50 lines (39 loc) · 1.16 KB
/
start.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
# encoding: utf-8
require 'rubygems' rescue LoadError
require 'test/spec'
require 'mocha'
require 'fileutils'
$:.unshift(File.expand_path('../../lib', __FILE__))
require 'ensure'
module EncodingTestHelpers
EXAMPLES = {
'UTF-8' => 'पशुपतिरपि तान्यहानि कृच्छ्राद्',
'ISO-8859-1' => 'Prévisions météo de Météo-France',
'Shift_JIS' => 'こんにちは',
# 'UTF-16LE' => '',
# 'UTF-16BE' => ''
}
def examples
EXAMPLES.keys
end
def example(name, options={})
filename, contents = name.gsub(/-/, '_').downcase, ''
File.open(File.expand_path("../examples/#{filename}.txt", __FILE__), 'r:binary') do |file|
contents << file.read
end
contents.force_encoding(options[:mark_as] || Encoding.find(name))
return contents, EXAMPLES[name]
end
def integer_array_to_hex(ints)
ints.map { |i| "0x#{i.to_s(16)}" }
end
def string_to_bytes(string)
integer_array_to_hex string.unpack('C*')
end
def string_to_codepoints(string)
integer_array_to_hex string.unpack('U*')
end
end
class Test::Unit::TestCase
include EncodingTestHelpers
end