writeexcel 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,12 @@ Write to a cross-platform Excel binary file.
4
4
 
5
5
  == Recent Changes
6
6
 
7
+ v0.5.0
8
+ * use 1.9-compatible Encoding interface for Ruby 1.8 encodings class; support US-ASCII and ASCII-8BIT. by Jeremy Weathers
9
+ The original project by cxn03651 has its own odd encoding scheme that is not compatible with other
10
+ software (e.g. nokogiri) that expects strings encodings to be compatible with Ruby 1.9 Encodings.
11
+ The purpose of this fork is to make the encoding system for Ruby 1.8 compatible with the Ruby 1.9 encoding system.
12
+
7
13
  v0.4.3
8
14
  * test passed under Ruby 1.9.2
9
15
  * Bug fix. Format#set_format_properties, Formula#convert_number found under Ruby 1.9.2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.3
1
+ 0.5.0
@@ -1,312 +1,326 @@
1
- # coding: utf-8
2
- #
3
- # Why would we ever use Ruby 1.8.7 when we can backport with something
4
- # as simple as this?
5
- #
6
- # copied from prawn.
7
- # modified by Hideo NAKAMURA
8
- #
9
- unless defined?(Encoding)
10
- class Encoding # :nodoc:
11
- class ConverterNotFoundError < StandardError; end
12
- class UndefinedConversionError < StandardError; end
13
-
14
- ASCII = 0
15
- BINARY = 1
16
- UTF_8 = 2
17
- EUCJP = 3
18
- SJIS = 4
19
- UTF_16LE = 5
20
- UTF_16BE = 6
21
- end
22
- end
23
-
24
- class String #:nodoc:
25
- def first_line
26
- self.each_line { |line| return line }
27
- end
28
- unless "".respond_to?(:lines)
29
- alias_method :lines, :to_a
30
- end
31
- unless "".respond_to?(:each_char)
32
- def each_char #:nodoc:
33
- # copied from jcode
34
- if block_given?
35
- scan(/./m) { |x| yield x }
36
- else
37
- scan(/./m)
38
- end
39
- end
40
- end
41
-
42
- unless "".respond_to?(:encode)
43
- @encoding = Encoding::UTF_8
44
-
45
- def encode(encoding) # :nodoc:
46
- require 'nkf'
47
- @encoding ||= Encoding::UTF_8
48
- if @encoding == Encoding::UTF_8
49
- # supported only $KCODE = 'u'. so @encoding.nil? means UTF_8.
50
- case encoding
51
- when /ASCII/i
52
- if self.mbchar?('UTF8')
53
- raise Encoding::UndefinedConversionError
54
- else
55
- str = String.new(self)
56
- str.force_encoding(encoding)
57
- str
58
- end
59
- when /BINARY/i
60
- if self.mbchar?('UTF8')
61
- raise Encoding::UndefinedConversionError
62
- else
63
- str = String.new(self)
64
- str.force_encoding(encoding)
65
- str
66
- end
67
- when /UTF_8/i
68
- raise Encoding::ConverterNotFoundError
69
- when /EUCJP/i, /SJIS/i
70
- enc = encoding =~ /EUCJP/i ? 'e' : 's'
71
- str = NKF.nkf("-#{enc} -m0 -W", self)
72
- str.force_encoding(encoding)
73
- str
74
- when /UTF_16LE/i, /UTF_16BE/i
75
- raise Encoding::ConverterNotFoundError
76
- else
77
- raise "Sorry, encoding #{encoding} is not supported by WriteExcel."
78
- end
79
- elsif @encoding == Encoding::ASCII
80
- case encoding
81
- when /ASCII/i, /BINARY/i, /EUCJP/i, /SJIS/i
82
- str = String.new(self)
83
- str.force_encoding(encoding)
84
- str
85
- when /UTF_8/i, /UTF_16LE/i, /UTF_16BE/i
86
- raise Encoding::ConverterNotFoundError
87
- else
88
- raise "Sorry, encoding #{encoding} is not supported by WriteExcel."
89
- end
90
- elsif @encoding == Encoding::BINARY
91
- case encoding
92
- when /ASCII/i, /EUCJP/i, /SJIS/i
93
- if self.ascii_only? || self.mbchar?('UTF8') || self.mbchar?('EUCJP') || self.mbchar?('SJIS')
94
- raise Encoding::UndefinedConversionError
95
- else
96
- str = String.new(self)
97
- str.force_encoding(encoding)
98
- str
99
- end
100
- when /BINARY/i
101
- self
102
- when /UTF_8/i, /UTF_16LE/i, /UTF_16BE/i
103
- raise Encoding::ConverterNotFoundError
104
- else
105
- raise "Sorry, encoding #{encoding} is not supported by WriteExcel."
106
- end
107
- elsif @encoding == Encoding::EUCJP || @encoding == Encoding::SJIS
108
- type = @encoding == Encoding::EUCJP ? 'EUCJP' : 'SJIS'
109
- inenc = @encoding == Encoding::EUCJP ? 'e' : 's'
110
- case encoding
111
- when /ASCII/i
112
- if self.mbchar?(type)
113
- raise Encoding::UndefinedConversionError
114
- else
115
- str = String.new(self)
116
- str.force_encoding(encoding)
117
- str
118
- end
119
- when /BINARY/i
120
- if self.mbchar?(type)
121
- raise Encoding::UndefinedConversionError
122
- else
123
- str = String.new(self)
124
- str.force_encoding(encoding)
125
- str
126
- end
127
- when /UTF_8/i
128
- raise Encoding::ConverterNotFoundError
129
- when /EUCJP/i, /SJIS/i
130
- outenc = encoding =~ /EUCJP/i ? 'E' : 'S'
131
- str = NKF.nkf("-#{inenc} -#{outenc}", self)
132
- str.force_encoding(encoding)
133
- str
134
- when /UTF_16LE/i, /UTF_16BE/i
135
- raise Encoding::ConverterNotFoundError
136
- else
137
- raise "Sorry, encoding #{encoding} is not supported by WriteExcel."
138
- end
139
- elsif @encoding == Encoding::UTF_16LE || @encoding == Encoding::UTF_16BE
140
- enc = @encoding == Encoding::UTF_16LE ? 'L' : 'B'
141
- utf8 = NKF.nkf("-w -m0 -W16#{enc}", self)
142
- case encoding
143
- when /ASCII/i
144
- if utf8.mbchar?
145
- raise Encoding::UndefinedConversionError
146
- else
147
- str = String.new(self)
148
- str.force_encoding(encoding)
149
- str
150
- end
151
- when /BINARY/i
152
- if utf8.mbchar?
153
- raise Encoding::UndefinedConversionError
154
- else
155
- str = String.new(self)
156
- str.force_encoding(encoding)
157
- str
158
- end
159
- when /UTF_8/i
160
- raise Encoding::ConverterNotFoundError
161
- when /EUCJP/i, /SJIS/i
162
- str = String.new(self)
163
- str.force_encoding(encoding)
164
- str
165
- when /UTF_16LE/i, /UTF_16BE/i
166
- raise Encoding::ConverterNotFoundError
167
- else
168
- raise "Sorry, encoding #{encoding} is not supported by WriteExcel."
169
- end
170
- else
171
- end
172
- end
173
-
174
- def self_with_encoding(encoding)
175
- if encoding =~ /ASCII/i
176
- @encoding = Encoding::ASCII
177
- elsif encoding =~ /BINARY/i
178
- @encoding = Encoding::BINARY
179
- elsif encoding =~ /UTF_8/i
180
- @encoding = Encoding::UTF_8
181
- elsif encoding =~ /EUCJP/i
182
- @encoding = Encoding::EUCJP
183
- elsif encoding =~ /SJIS/i
184
- @encoding = Encoding::SJIS
185
- elsif encoding =~ /UTF_16LE/i
186
- @encoding = Encoding::UTF_16LE
187
- elsif encoding =~ /UTF_16BE/i
188
- @encoding = Encoding::UTF_16BE
189
- else
190
- raise "Sorry, encoding #{encoding} is not supported by WriteExcel."
191
- end
192
- self
193
- end
194
- private :self_with_encoding
195
- end
196
-
197
- unless "".respond_to?(:encoding)
198
- def encoding
199
- @encoding ||= Encoding::UTF_8
200
- end
201
- end
202
-
203
- unless "".respond_to?(:bytesize)
204
- def bytesize # :nodoc:
205
- self.length
206
- end
207
- end
208
-
209
- unless "".respond_to?(:ord)
210
- def ord
211
- self[0]
212
- end
213
- end
214
-
215
- unless "".respond_to?(:force_encoding)
216
- def force_encoding(encoding)
217
- if encoding.respond_to?(:to_str)
218
- @encoding = case encoding
219
- when /ASCII/i
220
- Encoding::ASCII
221
- when /BINARY/i
222
- Encoding::BINARY
223
- when /UTF[-_]8/i
224
- Encoding::UTF_8
225
- when /EUCJP/i
226
- Encoding::EUCJP
227
- when /SJIS/i
228
- Encoding::SJIS
229
- when /UTF[-_]16LE/i
230
- Encoding::UTF_16LE
231
- when /UTF[-_]16BE/i
232
- Encoding::UTF_16BE
233
- end
234
- else
235
- @encoding = case encoding
236
- when Encoding::ASCII
237
- Encoding::ASCII
238
- when Encoding::BINARY
239
- Encoding::BINARY
240
- when Encoding::UTF_8
241
- Encoding::UTF_8
242
- when Encoding::EUCJP
243
- Encoding::EUCJP
244
- when Encoding::SJIS
245
- Encoding::SJIS
246
- when Encoding::UTF_16LE
247
- Encoding::UTF_16LE
248
- when Encoding::UTF_16BE
249
- Encoding::UTF_16BE
250
- end
251
- end
252
- self
253
- end
254
- end
255
-
256
- unless "".respond_to?(:ascii_only?)
257
- def ascii_only?
258
- !!(self =~ /[^!"#\$%&'\(\)\*\+,\-\.\/\:\;<=>\?@0-9A-Za-z_\[\\\]\{\}^` ~\0\n]/)
259
- end
260
- end
261
-
262
- if RUBY_VERSION < "1.9"
263
- unless "".respond_to?(:mbchar?)
264
- PATTERN_SJIS = '[\x81-\x9f\xe0-\xef][\x40-\x7e\x80-\xfc]' # :nodoc:
265
- PATTERN_EUC = '[\xa1-\xfe][\xa1-\xfe]' # :nodoc:
266
- PATTERN_UTF8 = '[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf][\x80-\xbf]' # :nodoc:
267
-
268
- RE_SJIS = Regexp.new(PATTERN_SJIS, 0, 'n') # :nodoc:
269
- RE_EUC = Regexp.new(PATTERN_EUC, 0, 'n') # :nodoc:
270
- RE_UTF8 = Regexp.new(PATTERN_UTF8, 0, 'n') # :nodoc:
271
-
272
- def mbchar?(type = nil)# :nodoc: idea copied from jcode.rb
273
- if (!type.nil? && type =~ /SJIS/i) || $KCODE == 'SJIS'
274
- self =~ RE_SJIS
275
- elsif (!type.nil? && type =~ /EUCJP/i) || $KCODE == 'EUC'
276
- self =~ RE_EUC
277
- elsif (!type.nil? && type =~ /UTF_8/i) || $KCODE == 'UTF8'
278
- self =~ RE_UTF8
279
- else
280
- nil
281
- end
282
- end
283
- end
284
- end
285
- end
286
-
287
- unless File.respond_to?(:binread)
288
- def File.binread(file) #:nodoc:
289
- File.open(file,"rb") { |f| f.read }
290
- end
291
- end
292
-
293
- if RUBY_VERSION < "1.9"
294
-
295
- def ruby_18 #:nodoc:
296
- yield
297
- end
298
-
299
- def ruby_19 #:nodoc:
300
- false
301
- end
302
-
303
- else
304
-
305
- def ruby_18 #:nodoc:
306
- false
307
- end
308
-
309
- def ruby_19 #:nodoc:
310
- yield
311
- end
312
- end
1
+ # coding: utf-8
2
+ #
3
+ # Why would we ever use Ruby 1.8.7 when we can backport with something
4
+ # as simple as this?
5
+ #
6
+ # copied from prawn.
7
+ # modified by Hideo NAKAMURA
8
+ #
9
+ unless defined?(Encoding)
10
+ class Encoding # :nodoc:
11
+ class ConverterNotFoundError < StandardError; end
12
+ class UndefinedConversionError < StandardError; end
13
+
14
+ def self.const_missing (name)
15
+ @looked_for ||= {}
16
+ if !@looked_for.has_key?(name)
17
+ begin
18
+ @looked_for[name] = find(name)
19
+ rescue ArgumentError
20
+ @looked_for[name] = nil
21
+ end
22
+ end
23
+ @looked_for[name]
24
+ end
25
+
26
+ def self.find (name)
27
+ result = try_name name
28
+ result ||= try_name name.gsub(/-/, '_') if name =~ /-/
29
+ raise ArgumentError, "unknown encoding name - #{name}" unless result
30
+ result
31
+ end
32
+ def self.try_name (name)
33
+ dname = name.to_s.downcase
34
+ sel = SupportedEncodings.select{ |se| dname == se.name.downcase }
35
+ return sel.first if 1 <= sel.size
36
+ sel = SupportedEncodings.select{ |se| se.name.downcase =~ Regexp.new(dname) }
37
+ return sel.first if 1 <= sel.size
38
+ nil
39
+ end
40
+
41
+ attr_accessor :name, :value
42
+ def initialize (name, value)
43
+ @name = name
44
+ @value = value
45
+ end
46
+
47
+ def == (other)
48
+ if other.is_a? Encoding
49
+ other.value == @value
50
+ elsif other.is_a? Fixnum
51
+ other == @value
52
+ else
53
+ other == @name
54
+ end
55
+ end
56
+
57
+ SupportedEncodings = [
58
+ Encoding.new('ASCII', 0),
59
+ Encoding.new('US_ASCII', 0),
60
+ Encoding.new('BINARY', 1),
61
+ Encoding.new('ASCII_8BIT', 1),
62
+ Encoding.new('UTF_8', 2),
63
+ Encoding.new('EUCJP', 3),
64
+ Encoding.new('SJIS', 4),
65
+ Encoding.new('UTF_16LE', 5),
66
+ Encoding.new('UTF_16BE', 6)
67
+ ]
68
+ # ASCII = 0
69
+ # BINARY = 1
70
+ # UTF_8 = 2
71
+ # EUCJP = 3
72
+ # SJIS = 4
73
+ # UTF_16LE = 5
74
+ # UTF_16BE = 6
75
+ end
76
+ end
77
+
78
+ class String #:nodoc:
79
+ def first_line
80
+ self.each_line { |line| return line }
81
+ end
82
+ unless "".respond_to?(:lines)
83
+ alias_method :lines, :to_a
84
+ end
85
+ unless "".respond_to?(:each_char)
86
+ def each_char #:nodoc:
87
+ # copied from jcode
88
+ if block_given?
89
+ scan(/./m) { |x| yield x }
90
+ else
91
+ scan(/./m)
92
+ end
93
+ end
94
+ end
95
+
96
+ unless "".respond_to?(:encode)
97
+ @encoding = Encoding::UTF_8
98
+
99
+ def encode(encoding) # :nodoc:
100
+ require 'nkf'
101
+ @encoding ||= Encoding::UTF_8
102
+ if @encoding == Encoding::UTF_8
103
+ # supported only $KCODE = 'u'. so @encoding.nil? means UTF_8.
104
+ case encoding
105
+ when /ASCII$/i
106
+ if self.mbchar?('UTF8')
107
+ raise Encoding::UndefinedConversionError
108
+ else
109
+ str = String.new(self)
110
+ str.force_encoding(encoding)
111
+ str
112
+ end
113
+ when /(BINARY|ASCII[-_]8BIT)/i
114
+ if self.mbchar?('UTF8')
115
+ raise Encoding::UndefinedConversionError
116
+ else
117
+ str = String.new(self)
118
+ str.force_encoding(encoding)
119
+ str
120
+ end
121
+ when /UTF_8/i
122
+ raise Encoding::ConverterNotFoundError
123
+ when /EUCJP/i, /SJIS/i
124
+ enc = encoding =~ /EUCJP/i ? 'e' : 's'
125
+ str = NKF.nkf("-#{enc} -m0 -W", self)
126
+ str.force_encoding(encoding)
127
+ str
128
+ when /UTF_16LE/i, /UTF_16BE/i
129
+ raise Encoding::ConverterNotFoundError
130
+ else
131
+ raise "Sorry, encoding #{encoding} is not supported by WriteExcel."
132
+ end
133
+ elsif @encoding == Encoding::ASCII
134
+ case encoding
135
+ when /ASCII/i, /BINARY/i, /EUCJP/i, /SJIS/i
136
+ str = String.new(self)
137
+ str.force_encoding(encoding)
138
+ str
139
+ when /UTF_8/i, /UTF_16LE/i, /UTF_16BE/i
140
+ raise Encoding::ConverterNotFoundError
141
+ else
142
+ raise "Sorry, encoding #{encoding} is not supported by WriteExcel."
143
+ end
144
+ elsif @encoding == Encoding::BINARY
145
+ case encoding
146
+ when /ASCII$/i, /EUCJP/i, /SJIS/i
147
+ if self.ascii_only? || self.mbchar?('UTF8') || self.mbchar?('EUCJP') || self.mbchar?('SJIS')
148
+ raise Encoding::UndefinedConversionError
149
+ else
150
+ str = String.new(self)
151
+ str.force_encoding(encoding)
152
+ str
153
+ end
154
+ when /(BINARY|ASCII[-_]8BIT)/i
155
+ self
156
+ when /UTF_8/i, /UTF_16LE/i, /UTF_16BE/i
157
+ raise Encoding::ConverterNotFoundError
158
+ else
159
+ raise "Sorry, encoding #{encoding} is not supported by WriteExcel."
160
+ end
161
+ elsif @encoding == Encoding::EUCJP || @encoding == Encoding::SJIS
162
+ type = @encoding == Encoding::EUCJP ? 'EUCJP' : 'SJIS'
163
+ inenc = @encoding == Encoding::EUCJP ? 'e' : 's'
164
+ case encoding
165
+ when /ASCII$/i
166
+ if self.mbchar?(type)
167
+ raise Encoding::UndefinedConversionError
168
+ else
169
+ str = String.new(self)
170
+ str.force_encoding(encoding)
171
+ str
172
+ end
173
+ when /(BINARY|ASCII[-_]8BIT)/i
174
+ if self.mbchar?(type)
175
+ raise Encoding::UndefinedConversionError
176
+ else
177
+ str = String.new(self)
178
+ str.force_encoding(encoding)
179
+ str
180
+ end
181
+ when /UTF_8/i
182
+ raise Encoding::ConverterNotFoundError
183
+ when /EUCJP/i, /SJIS/i
184
+ outenc = encoding =~ /EUCJP/i ? 'E' : 'S'
185
+ str = NKF.nkf("-#{inenc} -#{outenc}", self)
186
+ str.force_encoding(encoding)
187
+ str
188
+ when /UTF_16LE/i, /UTF_16BE/i
189
+ raise Encoding::ConverterNotFoundError
190
+ else
191
+ raise "Sorry, encoding #{encoding} is not supported by WriteExcel."
192
+ end
193
+ elsif @encoding == Encoding::UTF_16LE || @encoding == Encoding::UTF_16BE
194
+ enc = @encoding == Encoding::UTF_16LE ? 'L' : 'B'
195
+ utf8 = NKF.nkf("-w -m0 -W16#{enc}", self)
196
+ case encoding
197
+ when /ASCII$/i
198
+ if utf8.mbchar?
199
+ raise Encoding::UndefinedConversionError
200
+ else
201
+ str = String.new(self)
202
+ str.force_encoding(encoding)
203
+ str
204
+ end
205
+ when /(BINARY|ASCII[-_]8BIT)/i
206
+ if utf8.mbchar?
207
+ raise Encoding::UndefinedConversionError
208
+ else
209
+ str = String.new(self)
210
+ str.force_encoding(encoding)
211
+ str
212
+ end
213
+ when /UTF_8/i
214
+ raise Encoding::ConverterNotFoundError
215
+ when /EUCJP/i, /SJIS/i
216
+ str = String.new(self)
217
+ str.force_encoding(encoding)
218
+ str
219
+ when /UTF_16LE/i, /UTF_16BE/i
220
+ raise Encoding::ConverterNotFoundError
221
+ else
222
+ raise "Sorry, encoding #{encoding} is not supported by WriteExcel."
223
+ end
224
+ else
225
+ end
226
+ end
227
+
228
+ def self_with_encoding(encoding)
229
+ found = Encoding.find(encoding)
230
+ if found
231
+ @encoding = found
232
+ else
233
+ raise "Sorry, encoding #{encoding} is not supported by WriteExcel."
234
+ end
235
+ self
236
+ end
237
+ private :self_with_encoding
238
+ end
239
+
240
+ unless "".respond_to?(:encoding)
241
+ def encoding
242
+ @encoding ||= Encoding::UTF_8
243
+ end
244
+ end
245
+
246
+ unless "".respond_to?(:bytesize)
247
+ def bytesize # :nodoc:
248
+ self.length
249
+ end
250
+ end
251
+
252
+ unless "".respond_to?(:ord)
253
+ def ord
254
+ self[0]
255
+ end
256
+ end
257
+
258
+ unless "".respond_to?(:force_encoding)
259
+ def force_encoding(encoding)
260
+ if encoding.respond_to?(:to_str)
261
+ found = Encoding.find(encoding)
262
+ @encoding = found if found
263
+ else
264
+ @encoding = encoding if encoding.is_a?(Encoding)
265
+ end
266
+ self
267
+ end
268
+ end
269
+
270
+ unless "".respond_to?(:ascii_only?)
271
+ def ascii_only?
272
+ !!(self =~ /[^!"#\$%&'\(\)\*\+,\-\.\/\:\;<=>\?@0-9A-Za-z_\[\\\]\{\}^` ~\0\n]/)
273
+ end
274
+ end
275
+
276
+ if RUBY_VERSION < "1.9"
277
+ unless "".respond_to?(:mbchar?)
278
+ PATTERN_SJIS = '[\x81-\x9f\xe0-\xef][\x40-\x7e\x80-\xfc]' # :nodoc:
279
+ PATTERN_EUC = '[\xa1-\xfe][\xa1-\xfe]' # :nodoc:
280
+ PATTERN_UTF8 = '[\xc0-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf][\x80-\xbf]' # :nodoc:
281
+
282
+ RE_SJIS = Regexp.new(PATTERN_SJIS, 0, 'n') # :nodoc:
283
+ RE_EUC = Regexp.new(PATTERN_EUC, 0, 'n') # :nodoc:
284
+ RE_UTF8 = Regexp.new(PATTERN_UTF8, 0, 'n') # :nodoc:
285
+
286
+ def mbchar?(type = nil)# :nodoc: idea copied from jcode.rb
287
+ if (!type.nil? && type =~ /SJIS/i) || $KCODE == 'SJIS'
288
+ self =~ RE_SJIS
289
+ elsif (!type.nil? && type =~ /EUCJP/i) || $KCODE == 'EUC'
290
+ self =~ RE_EUC
291
+ elsif (!type.nil? && type =~ /UTF_8/i) || $KCODE == 'UTF8'
292
+ self =~ RE_UTF8
293
+ else
294
+ nil
295
+ end
296
+ end
297
+ end
298
+ end
299
+ end
300
+
301
+ unless File.respond_to?(:binread)
302
+ def File.binread(file) #:nodoc:
303
+ File.open(file,"rb") { |f| f.read }
304
+ end
305
+ end
306
+
307
+ if RUBY_VERSION < "1.9"
308
+
309
+ def ruby_18 #:nodoc:
310
+ yield
311
+ end
312
+
313
+ def ruby_19 #:nodoc:
314
+ false
315
+ end
316
+
317
+ else
318
+
319
+ def ruby_18 #:nodoc:
320
+ false
321
+ end
322
+
323
+ def ruby_19 #:nodoc:
324
+ yield
325
+ end
326
+ end
@@ -0,0 +1,205 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'helper'
3
+ require 'nkf'
4
+
5
+ class TC_new_encoding < Test::Unit::TestCase
6
+ def setup
7
+ ruby_18 do
8
+ @kcode = $KCODE
9
+ $KCODE = 'u'
10
+ end
11
+ end
12
+
13
+ def teardown
14
+ ruby_18 { $KCODE = @kcode }
15
+ end
16
+
17
+ def test_finder
18
+ if RUBY_VERSION < '1.9'
19
+ %w{ UTF_8 UTF-8 utf-8 utf }.each do |name|
20
+ e = Encoding.find(name)
21
+ assert_equal 'UTF_8', e.name
22
+ assert_equal 2, e.value
23
+ end
24
+ end
25
+ end
26
+
27
+ def test_comparison
28
+ if RUBY_VERSION < '1.9'
29
+ assert_equal Encoding.find('US-ASCII'), Encoding.find('ASCII')
30
+ assert_equal Encoding.find('ASCII-8BIT'), Encoding.find('BINARY')
31
+ assert_not_equal Encoding.find('ASCII'), Encoding.find('UTF-8')
32
+ end
33
+ end
34
+
35
+
36
+ # TEST WITH ALTERNATE Encoding NAMES
37
+ def test_ascii_str_ascii_to_ascii
38
+ str = ascii_str_enc_us_ascii
39
+ assert_equal(Encoding::ASCII, str.encode('ASCII').encoding)
40
+ end
41
+ def test_ascii_str_ascii_to_us_ascii
42
+ str = ascii_str_enc_us_ascii
43
+ assert_equal(Encoding::US_ASCII, str.encode('US-ASCII').encoding)
44
+ end
45
+
46
+ def test_ascii_str_ascii_to_binary
47
+ str = ascii_str_enc_us_ascii
48
+ assert_equal(Encoding::BINARY, str.encode('BINARY').encoding)
49
+ end
50
+ def test_ascii_str_ascii_to_ascii_8bit
51
+ str = ascii_str_enc_us_ascii
52
+ assert_equal(Encoding::ASCII_8BIT, str.encode('ASCII-8BIT').encoding)
53
+ end
54
+
55
+ def test_ascii_str_utf8_to_ascii
56
+ str = ascii_str_enc_utf8
57
+ assert_equal(Encoding::ASCII, str.encode('US-ASCII').encoding)
58
+ end
59
+
60
+ def test_non_ascii_str_utf8_to_ascii
61
+ str = non_ascii_str_enc_utf8
62
+ assert_raise(Encoding::UndefinedConversionError) { str.encode('US-ASCII') }
63
+ end
64
+
65
+ def test_ascii_str_utf8_to_binary
66
+ str = ascii_str_enc_utf8
67
+ assert_equal(Encoding::BINARY, str.encode('ASCII-8BIT').encoding)
68
+ end
69
+
70
+ def test_non_ascii_str_utf8_to_binary
71
+ str = non_ascii_str_enc_utf8
72
+ assert_raise(Encoding::UndefinedConversionError) { str.encode('ASCII-8BIT') }
73
+ end
74
+
75
+
76
+
77
+ def ascii_str_enc_ascii
78
+ str = ["abc"].pack('a*').encode('ASCII')
79
+ str.encode('ASCII') if RUBY_VERSION < "1.9"
80
+ str
81
+ end
82
+
83
+ def test_ascii_str_enc_ascii_is_ascii_encoding
84
+ assert_equal(Encoding::ASCII, ascii_str_enc_ascii.encoding)
85
+ end
86
+
87
+ def ascii_str_enc_us_ascii
88
+ str = ["abc"].pack('a*').encode('US-ASCII')
89
+ str.encode('US-ASCII') if RUBY_VERSION < "1.9"
90
+ str
91
+ end
92
+
93
+ def test_ascii_str_enc_ascii_is_ascii_encoding
94
+ assert_equal(Encoding::ASCII, ascii_str_enc_us_ascii.encoding)
95
+ end
96
+
97
+ def ascii_str_enc_binary
98
+ str = ["abc"].pack('a*').encode('BINARY')
99
+ str.encode('BINARY') if RUBY_VERSION < "1.9"
100
+ str
101
+ end
102
+
103
+ def test_ascii_str_enc_binary_is_binary_encoding
104
+ assert_equal(Encoding::BINARY, ascii_str_enc_binary.encoding)
105
+ end
106
+
107
+ def non_ascii_str_enc_binary
108
+ str = [0x80].pack('v*')
109
+ str.force_encoding('BINARY')
110
+ str
111
+ end
112
+
113
+ def test_non_ascii_str_enc_binary
114
+ assert_equal(Encoding::BINARY, non_ascii_str_enc_binary.encoding)
115
+ end
116
+
117
+ def ascii_str_enc_utf8
118
+ "abc"
119
+ end
120
+
121
+ def test_ascii_str_enc_utf8_is_utf8_encoding
122
+ assert_equal(Encoding::UTF_8, ascii_str_enc_utf8.encoding)
123
+ end
124
+
125
+ def non_ascii_str_enc_utf8
126
+ 'あいう'
127
+ end
128
+
129
+ def test_non_ascii_str_enc_utf8
130
+ assert_equal(Encoding::UTF_8, non_ascii_str_enc_utf8.encoding)
131
+ end
132
+
133
+ def ascii_str_enc_eucjp
134
+ str = "abc".encode('EUCJP')
135
+ str
136
+ end
137
+
138
+ def test_ascii_str_enc_eucjp_is_eucjp_encoding
139
+ assert_equal(Encoding::EUCJP, ascii_str_enc_eucjp.encoding)
140
+ end
141
+
142
+ def non_ascii_str_enc_eucjp
143
+ str = 'あいう'.encode('EUCJP')
144
+ str
145
+ end
146
+
147
+ def test_non_ascii_str_enc_eucjp
148
+ assert_equal(Encoding::EUCJP, non_ascii_str_enc_eucjp.encoding)
149
+ end
150
+
151
+ def ascii_str_enc_sjis
152
+ str = "abc".encode('SJIS')
153
+ str
154
+ end
155
+
156
+ def test_ascii_str_enc_sjis_is_sjis_encoding
157
+ assert_equal(Encoding::SJIS, ascii_str_enc_sjis.encoding)
158
+ end
159
+
160
+ def non_ascii_str_enc_sjis
161
+ str = 'あいう'.encode('SJIS')
162
+ str
163
+ end
164
+
165
+ def test_non_ascii_str_enc_sjis
166
+ assert_equal(Encoding::SJIS, non_ascii_str_enc_sjis.encoding)
167
+ end
168
+
169
+ def ascii_str_enc_utf16le
170
+ str = NKF.nkf('-w16L0 -m0 -W', "abc")
171
+ str.force_encoding('UTF_16LE') if RUBY_VERSION < "1.9"
172
+ str
173
+ end
174
+
175
+ def test_ascii_str_enc_utf16le_is_utf16le_encoding
176
+ assert_equal(Encoding::UTF_16LE, ascii_str_enc_utf16le.encoding)
177
+ end
178
+
179
+ def non_ascii_str_enc_utf16le
180
+ str = NKF.nkf('-w16L0 -m0 -W', 'あいう')
181
+ str.force_encoding('UTF_16LE') if RUBY_VERSION < "1.9"
182
+ str
183
+ end
184
+
185
+ def test_non_ascii_str_enc_utf16le
186
+ assert_equal(Encoding::UTF_16LE, non_ascii_str_enc_utf16le.encoding)
187
+ end
188
+
189
+ def ascii_str_enc_utf16be
190
+ str = NKF.nkf('-w16B0 -m0 -W', "abc")
191
+ str.force_encoding('UTF_16BE') if RUBY_VERSION < "1.9"
192
+ str
193
+ end
194
+
195
+ def test_ascii_str_enc_utf16be_is_utf16be_encoding
196
+ assert_equal(Encoding::UTF_16BE, ascii_str_enc_utf16be.encoding)
197
+ end
198
+
199
+ def non_ascii_str_enc_utf16be
200
+ str = NKF.nkf('-w16B0 -m0 -W', 'あいう')
201
+ str.force_encoding('UTF_16BE') if RUBY_VERSION < "1.9"
202
+ str
203
+ end
204
+
205
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{writeexcel}
8
- s.version = "0.4.3"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Hideo NAKAMURA"]
12
- s.date = %q{2010-10-13}
12
+ s.date = %q{2010-10-14}
13
13
  s.description = %q{Multiple worksheets can be added to a workbook and formatting can be applied to cells. Text, numbers, formulas, hyperlinks and images can be written to the cells.}
14
14
  s.email = %q{cxn03651@msj.biglobe.ne.jp}
15
15
  s.extra_rdoc_files = [
@@ -219,6 +219,7 @@ Gem::Specification.new do |s|
219
219
  "test/test_example_match.rb",
220
220
  "test/test_format.rb",
221
221
  "test/test_formula.rb",
222
+ "test/test_new_encoding.rb",
222
223
  "test/test_ole.rb",
223
224
  "test/test_storage_lite.rb",
224
225
  "test/test_workbook.rb",
@@ -271,6 +272,7 @@ Gem::Specification.new do |s|
271
272
  "test/test_example_match.rb",
272
273
  "test/test_format.rb",
273
274
  "test/test_formula.rb",
275
+ "test/test_new_encoding.rb",
274
276
  "test/test_ole.rb",
275
277
  "test/test_storage_lite.rb",
276
278
  "test/test_workbook.rb",
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
8
- - 3
9
- version: 0.4.3
7
+ - 5
8
+ - 0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Hideo NAKAMURA
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-13 00:00:00 +09:00
17
+ date: 2010-10-14 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -230,6 +230,7 @@ files:
230
230
  - test/test_example_match.rb
231
231
  - test/test_format.rb
232
232
  - test/test_formula.rb
233
+ - test/test_new_encoding.rb
233
234
  - test/test_ole.rb
234
235
  - test/test_storage_lite.rb
235
236
  - test/test_workbook.rb
@@ -306,6 +307,7 @@ test_files:
306
307
  - test/test_example_match.rb
307
308
  - test/test_format.rb
308
309
  - test/test_formula.rb
310
+ - test/test_new_encoding.rb
309
311
  - test/test_ole.rb
310
312
  - test/test_storage_lite.rb
311
313
  - test/test_workbook.rb