nbtfile 0.0.2 → 0.0.3

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.
Files changed (5) hide show
  1. data/README.rdoc +2 -0
  2. data/VERSION +1 -1
  3. data/lib/nbtfile.rb +15 -2
  4. data/spec/nbtfile_spec.rb +71 -83
  5. metadata +1 -1
data/README.rdoc CHANGED
@@ -121,6 +121,8 @@ ABNF (see RFC 5234).
121
121
 
122
122
  === NBTFile.tokenize
123
123
 
124
+ === NBTFile.emit
125
+
124
126
  == High-level API
125
127
 
126
128
  === NBTFile.load
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/lib/nbtfile.rb CHANGED
@@ -481,8 +481,21 @@ def self.tokenize(io)
481
481
  end
482
482
  reader = Reader.new(io)
483
483
 
484
- reader.each_token do |token|
485
- yield token
484
+ if block_given?
485
+ reader.each_token { |token| yield token }
486
+ else
487
+ tokens = []
488
+ reader.each_token { |token| tokens << token }
489
+ tokens
490
+ end
491
+ end
492
+
493
+ def self.emit(io)
494
+ writer = Writer.new(io)
495
+ begin
496
+ yield writer
497
+ ensure
498
+ writer.finish
486
499
  end
487
500
  end
488
501
 
data/spec/nbtfile_spec.rb CHANGED
@@ -157,17 +157,84 @@ describe "NBTFile::tokenize" do
157
157
  end
158
158
  end
159
159
 
160
+ describe "NBTFile::tokenize without a block" do
161
+ include ZlibHelpers
162
+
163
+ it_should_behave_like "readers and writers"
164
+
165
+ def check_reader_or_writer(input, tokens)
166
+ io = make_zipped_stream(input)
167
+ actual_tokens = NBTFile.tokenize(io)
168
+ actual_tokens.should be_a_kind_of(Enumerable)
169
+ actual_tokens.to_a.should == tokens
170
+ end
171
+ end
172
+
173
+ describe "NBTFile::emit" do
174
+ include ZlibHelpers
175
+
176
+ it_should_behave_like "readers and writers"
177
+
178
+ def check_reader_or_writer(output, tokens)
179
+ io = StringIO.new()
180
+ NBTFile.emit(io) do |writer|
181
+ for token in tokens
182
+ writer.emit_token(token)
183
+ end
184
+ end
185
+ actual_output = unzip_string(io.string)
186
+ actual_output.should == output
187
+ end
188
+
189
+ def self.emit_shorthand(description, output, &block)
190
+ it description do
191
+ io = StringIO.new()
192
+ NBTFile.emit(io, &block)
193
+ actual_output = unzip_string(io.string)
194
+ actual_output.should == output
195
+ end
196
+ end
197
+
198
+ emit_shorthand "should support shorthand for emitting lists",
199
+ "\x0a\x00\x04test" \
200
+ "\x09\x00\x03foo\x01\x00\x00\x00\x02" \
201
+ "\x0c\x2b" \
202
+ "\x00" do |writer|
203
+ writer.emit_token(Tokens::TAG_Compound["test", nil])
204
+ writer.emit_list("foo", Tokens::TAG_Byte) do
205
+ writer.emit_item(12)
206
+ writer.emit_item(43)
207
+ end
208
+ writer.emit_token(Tokens::TAG_End[nil, nil])
209
+ end
210
+
211
+ emit_shorthand "should support shorthand for emitting compound structures",
212
+ "\x0a\x00\x04test" \
213
+ "\x0a\x00\x03xyz" \
214
+ "\x01\x00\x03foo\x08" \
215
+ "\x01\x00\x03bar\x02" \
216
+ "\x00" \
217
+ "\x00" do |writer|
218
+ writer.emit_token(Tokens::TAG_Compound["test", nil])
219
+ writer.emit_compound("xyz") do
220
+ writer.emit_token(Tokens::TAG_Byte["foo", 0x08])
221
+ writer.emit_token(Tokens::TAG_Byte["bar", 0x02])
222
+ end
223
+ writer.emit_token(Tokens::TAG_End[nil, nil])
224
+ end
225
+ end
226
+
160
227
  describe "NBTFile::load" do
161
228
  include ZlibHelpers
162
229
 
163
230
  def self.nbtfile_load(description, tokens, result)
164
231
  it description do
165
232
  io = StringIO.new
166
- writer = NBTFile::Writer.new(io)
167
- for token in tokens
168
- writer.emit_token(token)
233
+ NBTFile.emit(io) do |writer|
234
+ for token in tokens
235
+ writer.emit_token(token)
236
+ end
169
237
  end
170
- writer.finish
171
238
  actual_result = NBTFile.load(StringIO.new(io.string))
172
239
  actual_result.should == result
173
240
  end
@@ -198,82 +265,3 @@ describe "NBTFile::load" do
198
265
  Tokens::TAG_End["", nil]],
199
266
  ["foo", {"bar" => [32, 45]}]
200
267
  end
201
-
202
- describe NBTFile::Reader do
203
- include ZlibHelpers
204
-
205
- it_should_behave_like "readers and writers"
206
-
207
- def check_reader_or_writer(input, tokens)
208
- io = make_zipped_stream(input)
209
- reader = NBTFile::Reader.new(io)
210
- actual_tokens = []
211
- reader.each_token do |token|
212
- actual_tokens << token
213
- end
214
- actual_tokens.should == tokens
215
- end
216
- end
217
-
218
- describe NBTFile::Writer do
219
- include ZlibHelpers
220
-
221
- it_should_behave_like "readers and writers"
222
-
223
- def check_reader_or_writer(output, tokens)
224
- stream = StringIO.new()
225
- writer = NBTFile::Writer.new(stream)
226
- begin
227
- for token in tokens
228
- writer.emit_token(token)
229
- end
230
- ensure
231
- writer.finish
232
- end
233
- actual_output = unzip_string(stream.string)
234
- actual_output.should == output
235
- end
236
-
237
- it "should support shorthand for emitting lists" do
238
- output = StringIO.new()
239
- writer = NBTFile::Writer.new(output)
240
- begin
241
- writer.emit_token(Tokens::TAG_Compound["test", nil])
242
- writer.emit_list("foo", Tokens::TAG_Byte) do
243
- writer.emit_item(12)
244
- writer.emit_item(43)
245
- end
246
- writer.emit_token(Tokens::TAG_End[nil, nil])
247
- ensure
248
- writer.finish
249
- end
250
-
251
- actual_output = unzip_string(output.string)
252
- actual_output.should == "\x0a\x00\x04test" \
253
- "\x09\x00\x03foo\x01\x00\x00\x00\x02" \
254
- "\x0c\x2b" \
255
- "\x00"
256
- end
257
-
258
- it "should support shorthand for emitting compound structures" do
259
- output = StringIO.new()
260
- writer = NBTFile::Writer.new(output)
261
- begin
262
- writer.emit_token(Tokens::TAG_Compound["test", nil])
263
- writer.emit_compound("xyz") do
264
- writer.emit_token(Tokens::TAG_Byte["foo", 0x08])
265
- writer.emit_token(Tokens::TAG_Byte["bar", 0x02])
266
- end
267
- writer.emit_token(Tokens::TAG_End[nil, nil])
268
- ensure
269
- writer.finish
270
- end
271
- actual_output = unzip_string(output.string)
272
- actual_output.should == "\x0a\x00\x04test" \
273
- "\x0a\x00\x03xyz" \
274
- "\x01\x00\x03foo\x08" \
275
- "\x01\x00\x03bar\x02" \
276
- "\x00" \
277
- "\x00"
278
- end
279
- end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nbtfile
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - MenTaLguY