mymatrix 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ class MyMatrix
2
+ VERSION = "0.0.2"
3
+ end
data/mymatrix.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mymatrix/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mymatrix"
7
+ s.version = MyMatrix::VERSION
8
+ s.authors = ["yukihico"]
9
+ s.email = ["yukihico@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{MS Excel and csv/tsv text handling library}
12
+ s.description = %q{mymatrix is a handling library for MS Excel and csv/tsv text.}
13
+
14
+ s.rubyforge_project = "mymatrix"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
data/spec/csv.csv ADDED
@@ -0,0 +1,3 @@
1
+ a,b,c
2
+ ,�ޗ�,���
3
+ "�_�u��""�N�I�[�e�[�V����","�J��,�}",aaa
data/spec/csv.csv.txt ADDED
@@ -0,0 +1,3 @@
1
+ a, b, c
2
+ , �ޗ�, ���
3
+ �_�u���N�I�[�e�[�V����, "�J��,�}", �Ȃ�
data/spec/csv_test.csv ADDED
@@ -0,0 +1,3 @@
1
+ a,b,c
2
+ ,�ޗ�,���
3
+ "�_�u��""�N�I�[�e�[�V����","�J��,�}",aaa
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,4 @@
1
+ myid val FAX
2
+ 001 aaa 0120-12-3456
3
+ 002 bbb 0120-12-3457
4
+ 003 ccc 0120-12-3458
data/spec/line4.txt ADDED
@@ -0,0 +1,9 @@
1
+ A B C D
2
+ a1 b1 c1 d1
3
+ a2 b2 c2 d2
4
+ a3 b3 c3 d3
5
+ a4 b4 c4 d4
6
+
7
+
8
+
9
+
data/spec/line4.xls ADDED
Binary file
@@ -0,0 +1,337 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'mymatrix'
3
+ require 'rspec'
4
+
5
+ describe MyMatrix do
6
+ before :all do
7
+ if(RUBY_VERSION =~ /1\.[^9]/)
8
+ else
9
+ Encoding.default_external = 'Windows-31J'
10
+ end
11
+ end
12
+
13
+ it '4行と4行のデータをconcatFileで結合すると8行になること' do
14
+ @mx = MyMatrix.new('spec/line4.xls')
15
+ @mx.concatFile('spec/line4.xls')
16
+ @mx.size.should == 8
17
+ end
18
+ it 'concatが正しく出来ていること' do
19
+ @mx = makeSample
20
+ @mx.concat(makeSample)
21
+ @mx[4].should == ['4', '6', '8']
22
+ @mx.size.should == 8
23
+ end
24
+
25
+ it 'newして保存すると、適切な値が保存されていること' do
26
+ @mx = MyMatrix.new
27
+ @mx.addHeaders(%w[genre name])
28
+ row = %w[furuit banana]
29
+ @mx << row
30
+ @mx.size.should == 1
31
+ @mx[0].should == row
32
+ end
33
+ =begin
34
+ it 'sortされること' do
35
+ @mx = MyMatrix.new('spec/110619.xls')
36
+ @mx = @mx.sortBy('要確認').reverse
37
+ @mx.each do |row|
38
+ #p @mx.val(row, '要確認')
39
+ end
40
+ @mx.val(@mx[0], '要確認').should == '★'
41
+ @mx.val(@mx[15], '要確認').should == '★'
42
+ @mx.val(@mx[16], '要確認').should == ''
43
+ @mx.val(@mx[17], '要確認').should == ''
44
+ end
45
+ =end
46
+
47
+ it 'concatFileに存在しないファイルを指定したら例外発生すること' do
48
+ @mx = MyMatrix.new('spec/line4.xls')
49
+ proc{
50
+ @mx.concatFile('hogehogehogefhoeg.xls')
51
+ }.should raise_error
52
+ end
53
+
54
+ it '<<rowした場合、各要素にnilが入らないこと' do
55
+ @mx = MyMatrix.new
56
+ @mx.addHeaders(%w[a b c d])
57
+ row = []
58
+ @mx.setValue(row, 'c', 'hoge')
59
+ @mx << row
60
+ @mx[0][0].should == ''
61
+ @mx[0][2].should == 'hoge'
62
+ @mx[0][3].should == ''
63
+ end
64
+
65
+ it 'XLSで表示されている通りの値がStringとして取得できること' do
66
+ @mx = MyMatrix.new('spec/std_shoshiki.xls')
67
+ @mx[0][1].should == '1'
68
+ @mx[0][1].class.should == String
69
+
70
+ @mx[0][2].should == '2'
71
+ @mx[0][2].class.should == String
72
+
73
+ @mx[0][3].should == '3.3'
74
+ @mx[0][3].class.should == String
75
+ end
76
+
77
+ it 'ファイルを読み込む際に、末尾の空行を削除して読み込むこと' do
78
+ out = MyMatrix.new('spec/line4.txt')
79
+ out.size.should == 4
80
+ end
81
+ it '日本語ファイル名に対応していること(Mac環境を想定)' do
82
+ filename = 'spec/ダクテン(だくてん)つきファイル.txt'
83
+ mx = makeSample()
84
+ mx.to_t(filename)
85
+
86
+ mx = MyMatrix.new(filename)
87
+ mx.val(mx[0], 'b').should == '6'
88
+
89
+ end
90
+ it 'csvファイルが読めること' do
91
+ s = MyMatrix.new
92
+ mx = makecsv
93
+
94
+ mx[0][0].should == ''
95
+ mx[1][0].should == 'ダブル"クオーテーション'
96
+ mx[1][1].should == 'カン,マ'
97
+ mx[1][2].should == 'aaa'
98
+
99
+
100
+ end
101
+
102
+ it 'concatDirでフォルダ内ファイルを全て結合できること' do
103
+ mx = MyMatrix.new
104
+ mx.concatDir('spec/for_concat')
105
+ mx.size.should == 16
106
+ end
107
+
108
+ it '存在しないカラムを取得しようとしたら例外が発生すること' do
109
+ mx = MyMatrix.new
110
+ row = []
111
+ proc{
112
+ mx.val(row, 'foobar')
113
+ }.should raise_error
114
+ end
115
+
116
+ it 'ヘッダが先頭にないファイルも読み込めること' do
117
+ mx = MyMatrix.new('spec/offset.txt', {:offset=>2})
118
+ mx.getHeaders.should == %w[A B C D]
119
+ mx[0][0].should == 'a1'
120
+ mx.val(mx[1], 'D').should == 'd2'
121
+ end
122
+ it 'ヘッダが先頭にないファイルに書き出せること' do
123
+ pending('必要になったら実装する')
124
+ mx = makeSample()
125
+ mx.to_xls({:template => 'spec/template.xls', :out=>'spec/out.xls', :offset_r => 3, :offset_c =>1})
126
+ mx = MyMatrix.new('spec/out.xls')
127
+ mx.val(mx[0], 'P-a').should == 'iti'
128
+ mx[3][1].should == 'a'
129
+ mx[4][1].should == '4'
130
+ end
131
+ it '同じシーケンスIDに同一の値をsetできること' do
132
+ mx = MyMatrix.new()
133
+ mx.addHeaders(%w[シーケンス 名称 情報])
134
+ mx << ['1', 'apple', 'begi']
135
+ mx << ['11', 'tomato' ,'begi']
136
+ mx << ['1', 'greenapple', 'begi']
137
+ mx.setSame('シーケンス', '1', {'情報'=>'fruit'})
138
+ mx[0][0].should == '1'
139
+ mx[0][2].should == 'fruit'
140
+ mx[1][2].should == 'begi'
141
+ mx[2][2].should == 'fruit'
142
+ end
143
+
144
+ it 'ただしくハッシュオブジェクトが作成されること' do
145
+ mx = MyMatrix.new()
146
+ mx.addHeaders(%w[シーケンス 名称 情報])
147
+ mx << ['1', 'apple', 'begi']
148
+ mx << ['11', 'tomato' ,'begi']
149
+ mx << ['1', 'greenapple', 'begi']
150
+
151
+ hash = mx.makeKey('シーケンス')
152
+ hash['1'].size.should == 2
153
+ hash['1'][1].should == ['1', 'greenapple', 'begi']
154
+ hash['11'][0].should == ['11', 'tomato' ,'begi']
155
+ end
156
+
157
+ it 'ヘッダの名称変更が正しく行えること' do
158
+ mx = makeSample
159
+ mx.replaceHeader('a', 'aaa')
160
+ mx.replaceHeader('b', 'bbb')
161
+ mx.getHeaders[0].should == 'aaa'
162
+ mx.getHeaders[1].should == 'bbb'
163
+ mx.getHeaders.size.should == 3
164
+ end
165
+ it 'カンマをエスケープしてcsvファイルを出力できること' do
166
+ mx = makecsv
167
+ mx.to_csv('spec/csv_test.csv')
168
+ fi = open('spec/csv_test.csv')
169
+ str = fi.gets
170
+ str.should == MyMatrix.tosjis("a,b,c\r\n")
171
+ str = fi.gets
172
+ str.should == MyMatrix.tosjis(",奈良,大阪\r\n")
173
+ str = fi.gets
174
+ str.should == MyMatrix.tosjis("\"ダブル\"\"クオーテーション\",\"カン,マ\",aaa\r\n")
175
+ fi.close
176
+ end
177
+ it '市町村コードの桁が揃えられること' do
178
+ mx = MyMatrix.new
179
+ mx.addHeaders(['都道府県市区町村コード'])
180
+ mx << ['1100']
181
+ mx.correctCityCodes!
182
+ mx[0][0].should == '01100'
183
+ end
184
+ it '開き直してもCP932範囲内の文字コードは変わらないこと' do
185
+ testcases = [
186
+ ['-', '-'], #変更なし:FULLWIDTH HYPHEN-MINUS(U+FF0D)
187
+ ['~', '~'], #変更なし:FULLWIDTH TILDE(U+FF5E)
188
+ ['ア', 'ア'], #1byte kana
189
+ ['①', '①'] #windows CP932 only
190
+ ]
191
+ translationCheck(testcases)
192
+ end
193
+ it 'CP932範囲外の記号は、CP932範囲の記号に変換されること' do
194
+ testcases = [
195
+ #['1−', '1―'], #MINUS SIGN(U+2212) to FULLWIDTH HYPHEN-MINUS(U+2015)(windows)
196
+ #↑仕様変更のためコメントアウト
197
+
198
+ ['2〜','2~'], #WAVE DASH (U+301C) to FULLWIDTH TILDE(U+FF5E)(windows)
199
+ ['3‖','3∥'], #DOUBLE VERTICAL LINE (U+2016, "‖") を PARALLEL TO (U+2225, "∥") に
200
+ ['4—', '4―'], #EM DASH (U+2014, "—") を HORIZONTAL BAR (U+2015, "―") に
201
+ #キー入力を想定した変換。
202
+ ['5ー', '5ー'], #MacのハイフンF7(google ime)→Windows(googleime):同じ
203
+ ['6ー', '6ー'], #MacのハイフンF8(google ime)→Windows(googleime):同じ
204
+ ['7−', '7-'], #MacのハイフンF9(google ime)→Windows(googleime):違う。MINUS SIGN(U+2212) to FULLWIDTH HYPHEN-MINUS(U+FF0D)
205
+ ['8-', '8-'], #MacのハイフンF10(google ime)→Windows(googleime):同じ
206
+ ]
207
+ if(RUBY_VERSION =~ /1\.[^9]/)
208
+ pending
209
+ else
210
+ translationCheck(testcases)
211
+ end
212
+ end
213
+
214
+ it '半角カナを全角に出来ること' do
215
+ testcases = [
216
+ ['-', '-'], #hyphen
217
+ ['ファミリーマート', 'ファミリーマート'], #ハイフンを長母音に変換
218
+ ['03-3352-7334', '03-3352-7334'],
219
+ ['abc0', 'abc0'], #全角英数はそのまま
220
+ ['abc', 'abc'] #半角英数もはそのまま
221
+ ]
222
+ translationCheck(testcases) do |mx|
223
+ mx.twoByteKana!
224
+ end
225
+ end
226
+ it '拡張子によって保存形式が変わること' do
227
+ mx = makeSample()
228
+ mx.to_t('spec/test.csv')
229
+ fi = open('spec/test.csv')
230
+ str = fi.gets
231
+ str.should == MyMatrix.tosjis("a,b,c\r\n")
232
+
233
+ mx.to_t('spec/test.tsv')
234
+ fi = open('spec/test.tsv')
235
+ str = fi.gets
236
+ str.should == MyMatrix.tosjis("a\tb\tc\r\n")
237
+
238
+ mx.to_t('spec/test.txt')
239
+ fi = open('spec/test.txt')
240
+ str = fi.gets
241
+ str.should == MyMatrix.tosjis("a\tb\tc\r\n")
242
+
243
+ end
244
+ it 'セルの中に改行コードが入っていた場合、to_tしたら削除されること' do
245
+ mx = makeSample
246
+ mx[0][1] = mx[0][1] + "\r"
247
+ mx.to_t('spec/test.txt')
248
+ fi = open('spec/test.txt')
249
+ str = fi.gets
250
+ str = fi.gets
251
+ str.should == MyMatrix.tosjis("4\t6\t8\r\n")
252
+ end
253
+ it '長すぎる文字列があったらcutOffで短くできること' do
254
+ mx = makeSample
255
+ mx[0][0] = '01234567890'
256
+ mx[0][1] = ''
257
+ str = mx[0][2].dup
258
+ mx.cutOff('a', 4)
259
+ mx[0][0].should == '0123'
260
+ mx[0][1].should == ''
261
+ mx[0][2].should == str
262
+ end
263
+
264
+ it 'SJIS範囲外の漢字が含まれるデータをテキスト出力する時は例外を発生させること' do
265
+ pending('実装が難しいためペンディング')
266
+ mx = MyMatrix.new
267
+ mx.addHeaders(['str'])
268
+ mx << ['盌']
269
+ Proc {
270
+ mx.to_t('test.txt')
271
+ }.should raise_error
272
+ end
273
+
274
+ end
275
+ def translationCheck(testcases)
276
+ mx = MyMatrix.new
277
+ mx.addHeaders(['カラム'])
278
+ testcases.each do |mycase|
279
+ mx << [mycase[0]]
280
+ end
281
+ if(block_given?)
282
+ yield(mx)
283
+ end
284
+ mx.to_t('spec/test.txt')
285
+ mx = MyMatrix.new('spec/test.txt')
286
+
287
+ testcases.each_with_index do |mycase, i|
288
+ mx[i][0].should == mycase[1]
289
+ end
290
+ end
291
+
292
+ def makeSample
293
+ out = MyMatrix.new()
294
+ out.addHeaders(['a', 'b', 'c'])
295
+ out << ['4', '6', '8']
296
+ out << ['1', '3', '5']
297
+ out << ['3', '5', '7']
298
+ out << ['2', '4', '6']
299
+ return out
300
+ end
301
+ def makeSample2
302
+ out = MyMatrix.new()
303
+ out.addHeaders(['a', 'b', 'c'])
304
+ out << ['4', '5', '8']
305
+ out << ['', '3', '5']
306
+ out << ['3', '5', '7']
307
+ out << ['4', '6', '8']
308
+ out << ['2', '4', '6']
309
+ return out
310
+ end
311
+ def makeEmptySample
312
+ out = MyMatrix.new()
313
+ out.addHeaders(['a', 'b', 'c'])
314
+ out << ['4', '5', '8']
315
+ out << ['', '', '']
316
+ out << ['', '', '']
317
+ return out
318
+ end
319
+
320
+ def makecsv
321
+ open('spec/csv.csv', 'w') do |fo|
322
+ fo.write("a,b,c\r\n")
323
+ fo.write(MyMatrix.tosjis(',奈良,大阪'))
324
+ fo.write("\r\n")
325
+ fo.write(MyMatrix.tosjis('"ダブル""クオーテーション","カン,マ",aaa'))
326
+ fo.write("\r\n")
327
+ end
328
+ mx = MyMatrix.new('spec/csv.csv')
329
+ return mx
330
+ end
331
+ def makecsv_norow
332
+ open('spec/csv.csv', 'w') do |fo|
333
+ fo.write("a,b,c\r\n")
334
+ end
335
+ mx = MyMatrix.new('spec/csv.csv')
336
+ return mx
337
+ end
data/spec/offset.txt ADDED
@@ -0,0 +1,11 @@
1
+ list
2
+ name
3
+ A B C D
4
+ a1 b1 c1 d1
5
+ a2 b2 c2 d2
6
+ a3 b3 c3 d3
7
+ a4 b4 c4 d4
8
+
9
+
10
+
11
+
data/spec/out.xls ADDED
Binary file
Binary file
data/spec/template.xls ADDED
Binary file
data/spec/test.csv ADDED
@@ -0,0 +1,5 @@
1
+ a,b,c
2
+ 4,6,8
3
+ 1,3,5
4
+ 3,5,7
5
+ 2,4,6
data/spec/test.txt ADDED
@@ -0,0 +1,5 @@
1
+ a b c
2
+ 4 6 8
3
+ 1 3 5
4
+ 3 5 7
5
+ 2 4 6
@@ -1,3 +1,4 @@
1
1
  require 'stringio'
2
2
  require 'test/unit'
3
+ require 'rspec'
3
4
  require File.dirname(__FILE__) + '/../lib/mymatrix'