fixed-layout-mapper 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
- module FixedLayoutMapper
2
- VERSION = "0.0.2"
3
- end
1
+ module FixedLayoutMapper
2
+ VERSION = "0.0.3"
3
+ end
@@ -1,241 +1,241 @@
1
- #coding: Windows-31J
2
-
3
- $LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
4
- require File.expand_path(File.dirname(__FILE__) + '/../lib/fixed-layout-mapper')
5
- require 'rspec'
6
-
7
- describe FixedLayoutMapper::Mapper do
8
- def define_layout(&block)
9
- FixedLayoutMapper::Mapper.define_layout(&block)
10
- end
11
-
12
- def genstr(ary)
13
- ary.join
14
- end
15
-
16
- it "�T�C�Y�w��ŃJ�������`�ł��邱��" do
17
- mapper = define_layout do
18
- col :field1, 2
19
- col :field2, 1
20
- end
21
- rt = mapper.map(%w(12 3).join)
22
- rt.field1.should == "12"
23
- rt.field2.should == "3"
24
- end
25
-
26
- it "�T�C�Y�̔z��ŃJ�������`����Ƃ��̌��ŕ������ꂽ�z��ɂȂ邱��" do
27
- mapper = define_layout do
28
- col :f1, 2
29
- col :f2, [1, 2, 3]
30
- col :f3, [4] * 2
31
- end
32
-
33
- rt = mapper.map(%w(12 a bb ccc 1234 5678).join)
34
- rt.f1.should == "12"
35
- rt.f2.should == %w(a bb ccc)
36
- rt.f3.should == %w(1234 5678)
37
- end
38
-
39
- it "���C�A�E�g�ɖ��O���‚����邱��" do
40
- mapper = define_layout do
41
- layout :layout1 do
42
- col :f1, 2
43
- col :f2, 1
44
- end
45
- end
46
- rt = mapper.map(%w(12 3).join, :layout1)
47
- rt.f1.should == "12"
48
- rt.f2.should == "3"
49
- end
50
-
51
- it "���C�A�E�g�����w�肵�Ă��Ȃ��ꍇ:default_layout�Œ�`����Ă��邱��" do
52
- mapper = define_layout do
53
- col :f1, 2
54
- end
55
-
56
- rt = mapper.map(%w(12).join, :default_layout)
57
- rt.f1.should == "12"
58
- end
59
-
60
- it "���郌�C�A�E�g�ŕʂ̃��C�A�E�g(�ȉ��T�u���C�A�E�g)���g���邱��" do
61
- mapper = define_layout do
62
- layout :layout1 do
63
- col :f1, 2
64
- col :f2, 1
65
- end
66
-
67
- col :f, :layout1
68
- end
69
- rt = mapper.map(%w(12 3).join)
70
- rt.f.f1.should == "12"
71
- rt.f.f2.should == "3"
72
- end
73
-
74
- it "�T�u���C�A�E�g�̔z�����`�ł��邱��" do
75
- mapper = define_layout do
76
- layout :layout1 do
77
- col :f1, 2
78
- col :f2, 1
79
- end
80
-
81
- col :f, [:layout1, :layout1]
82
- end
83
- rt = mapper.map(%w(12 3 ab c).join)
84
- rt.f[0].f1.should == "12"
85
- rt.f[0].f2.should == "3"
86
- rt.f[1].f1.should == "ab"
87
- rt.f[1].f2.should == "c"
88
- end
89
-
90
- it "�z��ɂ̓T�C�Y�ł��T�u���C�A�E�g�ł��w��ł��邱��" do
91
- mapper = define_layout do
92
- layout :layout1 do
93
- col :f1, 2
94
- col :f2, 1
95
- end
96
-
97
- col :f, [:layout1, 1, 2]
98
- end
99
- rt = mapper.map(%w(12 3 a bc).join)
100
- rt.f[0].f1.should == "12"
101
- rt.f[0].f2.should == "3"
102
- rt.f[1].should == "a"
103
- rt.f[2].should == "bc"
104
- end
105
-
106
- it "�����̃��C�A�E�g���`�ł��邱��" do
107
- mapper = define_layout do
108
- layout :layout1 do
109
- col :f1, 1
110
- end
111
-
112
- layout :layout2 do
113
- col :f2, 2
114
- end
115
- end
116
- rt = mapper.map(%w(1).join, :layout1)
117
- rt.f1.should == "1"
118
-
119
- rt = mapper.map(%w(ab).join, :layout2)
120
- rt.f2.should == "ab"
121
- end
122
-
123
- it "�ϊ����ɃA�N�V�������`�����ꍇ���̖߂�l�Ń}�b�s���O����邱��" do
124
- mapper = define_layout do
125
- col :f1, 2 do |v|
126
- v + v
127
- end
128
- end
129
-
130
- rt = mapper.map(%w(12).join, :default_layout)
131
- rt.f1.should == "1212"
132
- end
133
-
134
- it "�����O�X�w��̃��C�A�E�g�̃����O�X���擾�ł��邱��" do
135
- mapper = define_layout do
136
- col :f1, 2
137
- end
138
-
139
- mapper.get_layout.length.should == 2
140
- end
141
-
142
- it "�z��w��̃��C�A�E�g�̃����O�X���擾�ł��邱��" do
143
- mapper = define_layout do
144
- col :f1, [1, 2, 3]
145
- end
146
-
147
- mapper.get_layout.length.should == 6
148
- end
149
-
150
- it "�T�u���C�A�E�g�w��̃��C�A�E�g�̃����O�X���擾�ł��邱��" do
151
- mapper = define_layout do
152
- layout :layout1 do
153
- col :f, 1
154
- end
155
-
156
- layout :layout2 do
157
- col :f ,3
158
- end
159
-
160
- col :f1, :layout1
161
- col :f2, :layout2
162
- end
163
-
164
- mapper.get_layout.length.should == 4
165
- end
166
-
167
- it "�z��+�T�u���C�A�E�g�w��̃��C�A�E�g�̃����O�X���擾�ł��邱��" do
168
- mapper = define_layout do
169
- layout :layout1 do
170
- col :f, 1
171
- end
172
- col :f1, [10, :layout1, 1]
173
- end
174
- mapper.get_layout.length.should == 12
175
- end
176
-
177
- it "���C�A�E�g�Ƀ����O�X����(���Ȃ��Ƃ��f�[�^�̂ق�������)���w�肳�ꂽ�ꍇ�f�[�^�̃����O�X�̕��������Ă��G���[�ɂȂ�Ȃ�����" do
178
- mapper = define_layout do
179
- layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_ALLOW_LONG do
180
- col :f, 2
181
- end
182
- end
183
- rt = mapper.map("12345", :layout1)
184
- rt.f.should == "12"
185
- end
186
-
187
- it "���C�A�E�g�Ƀ����O�X����(���Ȃ��Ƃ��f�[�^�̂ق�������)���w�肳�ꂽ�ꍇ�f�[�^�̃����O�X�̕����Z���ꍇ��O���������邱��" do
188
- mapper = define_layout do
189
- layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_ALLOW_LONG do
190
- col :f, 2
191
- end
192
- end
193
- proc {
194
- mapper.map("1", :layout1)
195
- }.should raise_error
196
- end
197
-
198
- it "���C�A�E�g�Ƀ����O�X����(�f�[�^�����Ɠ���)���w�肳�ꂽ�ꍇ�f�[�^�̃����O�X�̕��������ƃG���[�ɂȂ邱��" do
199
- mapper = define_layout do
200
- layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_STRICT do
201
- col :f, 2
202
- end
203
- end
204
- proc {
205
- mapper.map("12345", :layout1)
206
- }.should raise_error
207
- end
208
-
209
- it "���C�A�E�g�Ƀ����O�X����(�f�[�^�����Ɠ���)���w�肳�ꂽ�ꍇ�f�[�^�̃����O�X�̕����Z���ƃG���[�ɂȂ邱��" do
210
- mapper = define_layout do
211
- layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_STRICT do
212
- col :f, 2
213
- end
214
- end
215
- proc {
216
- mapper.map("1", :layout1)
217
- }.should raise_error
218
- end
219
-
220
- it "���C�A�E�g�Ƀ����O�X����(�f�[�^�����Ɠ���)���w�肳�ꂽ�ꍇ�f�[�^�̃����O�X�Ɠ����ꍇ�̓G���[���������Ȃ�����" do
221
- mapper = define_layout do
222
- layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_STRICT do
223
- col :f, 2
224
- end
225
- end
226
-
227
- rt = mapper.map("12", :layout1)
228
- rt.f.should == "12"
229
- end
230
-
231
- it "���C�A�E�g�Ƀ����O�X����(�f�[�^�������͒���)���w�肳�ꂽ�ꍇ�f�[�^�̃����O�X�̕����Z���ƃG���[�ɂȂ邱��" do
232
- mapper = define_layout do
233
- layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_ALLOW_LONG do
234
- col :f, 2
235
- end
236
- end
237
- proc {
238
- mapper.map("1", :layout1)
239
- }.should raise_error
240
- end
241
- end
1
+
2
+ $LOAD_PATH << File.expand_path(File.dirname(__FILE__) + '/../lib')
3
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/fixed-layout-mapper')
4
+ require 'rspec'
5
+
6
+ describe FixedLayoutMapper::Mapper do
7
+ def define_layout(&block)
8
+ FixedLayoutMapper::Mapper.define_layout(&block)
9
+ end
10
+
11
+ def genstr(ary)
12
+ ary.join
13
+ end
14
+
15
+ it "サイズ指定でカラムを定義できること" do
16
+ mapper = define_layout do
17
+ col :field1, 2
18
+ col :field2, 1
19
+ end
20
+ rt = mapper.map(%w(12 3).join)
21
+ expect(rt.field1).to eq "12"
22
+ expect(rt.field2).to eq "3"
23
+ end
24
+
25
+ it "サイズの配列でカラムを定義するとその桁で分割された配列になること" do
26
+ mapper = define_layout do
27
+ col :f1, 2
28
+ col :f2, [1, 2, 3]
29
+ col :f3, [4] * 2
30
+ end
31
+
32
+ rt = mapper.map(%w(12 a bb ccc 1234 5678).join)
33
+ expect(rt.f1).to eq "12"
34
+ expect(rt.f2).to eq %w(a bb ccc)
35
+ expect(rt.f3).to eq %w(1234 5678)
36
+ end
37
+
38
+ it "レイアウトに名前をつけられること" do
39
+ mapper = define_layout do
40
+ layout :layout1 do
41
+ col :f1, 2
42
+ col :f2, 1
43
+ end
44
+ end
45
+ rt = mapper.map(%w(12 3).join, :layout1)
46
+ expect(rt.f1).to eq "12"
47
+ expect(rt.f2).to eq "3"
48
+ end
49
+
50
+ it "レイアウト名を指定していない場合:default_layoutで定義されていること" do
51
+ mapper = define_layout do
52
+ col :f1, 2
53
+ end
54
+
55
+ rt = mapper.map(%w(12).join, :default_layout)
56
+ expect(rt.f1).to eq "12"
57
+ end
58
+
59
+ it "あるレイアウトで別のレイアウト(以下サブレイアウト)を使えること" do
60
+ mapper = define_layout do
61
+ layout :layout1 do
62
+ col :f1, 2
63
+ col :f2, 1
64
+ end
65
+
66
+ col :f, :layout1
67
+ end
68
+ rt = mapper.map(%w(12 3).join)
69
+ expect(rt.f.f1).to eq "12"
70
+ expect(rt.f.f2).to eq "3"
71
+ end
72
+
73
+ it "サブレイアウトの配列も定義できること" do
74
+ mapper = define_layout do
75
+ layout :layout1 do
76
+ col :f1, 2
77
+ col :f2, 1
78
+ end
79
+
80
+ col :f, [:layout1, :layout1]
81
+ end
82
+ rt = mapper.map(%w(12 3 ab c).join)
83
+ expect(rt.f[0].f1).to eq "12"
84
+ expect(rt.f[0].f2).to eq "3"
85
+ expect(rt.f[1].f1).to eq "ab"
86
+ expect(rt.f[1].f2).to eq "c"
87
+ end
88
+
89
+ it "配列にはサイズでもサブレイアウトでも指定できること" do
90
+ mapper = define_layout do
91
+ layout :layout1 do
92
+ col :f1, 2
93
+ col :f2, 1
94
+ end
95
+
96
+ col :f, [:layout1, 1, 2]
97
+ end
98
+ rt = mapper.map(%w(12 3 a bc).join)
99
+ expect(rt.f[0].f1).to eq "12"
100
+ expect(rt.f[0].f2).to eq "3"
101
+ expect(rt.f[1]).to eq "a"
102
+ expect(rt.f[2]).to eq "bc"
103
+ end
104
+
105
+ it "複数のレイアウトを定義できること" do
106
+ mapper = define_layout do
107
+ layout :layout1 do
108
+ col :f1, 1
109
+ end
110
+
111
+ layout :layout2 do
112
+ col :f2, 2
113
+ end
114
+ end
115
+ rt = mapper.map(%w(1).join, :layout1)
116
+ expect(rt.f1).to eq "1"
117
+
118
+ rt = mapper.map(%w(ab).join, :layout2)
119
+ expect(rt.f2).to eq "ab"
120
+ end
121
+
122
+ it "変換時にアクションを定義した場合その戻り値でマッピングされること" do
123
+ mapper = define_layout do
124
+ col :f1, 2 do |v|
125
+ v + v
126
+ end
127
+ end
128
+
129
+ rt = mapper.map(%w(12).join, :default_layout)
130
+ expect(rt.f1).to eq "1212"
131
+ end
132
+
133
+ it "レングス指定のレイアウトのレングスが取得できること" do
134
+ mapper = define_layout do
135
+ col :f1, 2
136
+ end
137
+
138
+ expect(mapper.get_layout.length).to eq 2
139
+ end
140
+
141
+ it "配列指定のレイアウトのレングスが取得できること" do
142
+ mapper = define_layout do
143
+ col :f1, [1, 2, 3]
144
+ end
145
+
146
+ expect(mapper.get_layout.length).to eq 6
147
+ end
148
+
149
+ it "サブレイアウト指定のレイアウトのレングスが取得できること" do
150
+ mapper = define_layout do
151
+ layout :layout1 do
152
+ col :f, 1
153
+ end
154
+
155
+ layout :layout2 do
156
+ col :f ,3
157
+ end
158
+
159
+ col :f1, :layout1
160
+ col :f2, :layout2
161
+ end
162
+
163
+ expect(mapper.get_layout.length).to eq 4
164
+ end
165
+
166
+ it "配列+サブレイアウト指定のレイアウトのレングスが取得できること" do
167
+ mapper = define_layout do
168
+ layout :layout1 do
169
+ col :f, 1
170
+ end
171
+ col :f1, [10, :layout1, 1]
172
+ end
173
+ expect(mapper.get_layout.length).to eq 12
174
+ end
175
+
176
+ it "レイアウトにレングス制約(少なくともデータのほうが長い)を指定された場合データのレングスの方が長くてもエラーにならないこと" do
177
+ mapper = define_layout do
178
+ layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_ALLOW_LONG do
179
+ col :f, 2
180
+ end
181
+ end
182
+ rt = mapper.map("12345", :layout1)
183
+ expect(rt.f).to eq "12"
184
+ end
185
+
186
+ it "レイアウトにレングス制約(少なくともデータのほうが長い)を指定された場合データのレングスの方が短い場合例外が発生すること" do
187
+ mapper = define_layout do
188
+ layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_ALLOW_LONG do
189
+ col :f, 2
190
+ end
191
+ end
192
+ expect {
193
+ mapper.map("1", :layout1)
194
+ }.to raise_error("byte length is too short")
195
+ end
196
+
197
+ it "レイアウトにレングス制約(データ長さと同じ)を指定された場合データのレングスの方が長いとエラーになること" do
198
+ mapper = define_layout do
199
+ layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_STRICT do
200
+ col :f, 2
201
+ end
202
+ end
203
+
204
+ expect {
205
+ mapper.map("12345", :layout1)
206
+ }.to raise_error("byte length is invalid")
207
+ end
208
+
209
+ it "レイアウトにレングス制約(データ長さと同じ)を指定された場合データのレングスの方が短いとエラーになること" do
210
+ mapper = define_layout do
211
+ layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_STRICT do
212
+ col :f, 2
213
+ end
214
+ end
215
+ expect {
216
+ mapper.map("1", :layout1)
217
+ }.to raise_error("byte length is invalid")
218
+ end
219
+
220
+ it "レイアウトにレングス制約(データ長さと同じ)を指定された場合データのレングスと同じ場合はエラーが発生しないこと" do
221
+ mapper = define_layout do
222
+ layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_STRICT do
223
+ col :f, 2
224
+ end
225
+ end
226
+
227
+ rt = mapper.map("12", :layout1)
228
+ expect(rt.f).to eq "12"
229
+ end
230
+
231
+ it "レイアウトにレングス制約(データ長さよりは長い)を指定された場合データのレングスの方が短いとエラーになること" do
232
+ mapper = define_layout do
233
+ layout :layout1, FixedLayoutMapper::LENGTH_CONDITION_ALLOW_LONG do
234
+ col :f, 2
235
+ end
236
+ end
237
+ expect {
238
+ mapper.map("1", :layout1)
239
+ }.to raise_error("byte length is too short")
240
+ end
241
+ end