-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathchunked_io_test.rb
857 lines (739 loc) · 25.8 KB
/
chunked_io_test.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
require "test_helper"
require "down/chunked_io"
require "timeout"
describe Down::ChunkedIO do
def chunked_io(**options)
Down::ChunkedIO.new(**options)
end
describe "#initialize" do
it "requires :chunks" do
assert_raises(ArgumentError) { chunked_io }
end
it "accepts :size" do
io = chunked_io(chunks: [].each, size: 3)
assert_equal 3, io.size
end
it "accepts :data" do
io = chunked_io(chunks: [].each, data: {foo: "bar"})
assert_equal "bar", io.data[:foo]
end
it "accepts :encoding" do
assert_equal Encoding::BINARY, chunked_io(chunks: [].each).encoding
assert_equal Encoding::UTF_8, chunked_io(chunks: [].each, encoding: "utf-8").encoding
assert_equal Encoding::UTF_8, chunked_io(chunks: [].each, encoding: Encoding::UTF_8).encoding
assert_equal Encoding::BINARY, chunked_io(chunks: [].each, encoding: "unknown").encoding
end
it "retreives the first chunk" do
exceptional_chunks = Enumerator.new { |y| raise Timeout::Error }
assert_raises(Timeout::Error) { chunked_io(chunks: exceptional_chunks) }
end
end
describe "#read" do
describe "without arguments" do
it "reads whole content" do
io = chunked_io(chunks: ["abc"].each)
assert_equal "abc", io.read
end
it "reads across chunks" do
io = chunked_io(chunks: ["ab", "cd", "e"].each)
assert_equal "abcde", io.read
end
it "reads remaining content" do
io = chunked_io(chunks: ["abc"].each)
io.read(1)
assert_equal "bc", io.read
end
it "reads from cache" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read
io.rewind
assert_equal "abc", io.read
end
it "seamlessly switches between reading cached and new content" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read(1)
io.rewind
assert_equal "abc", io.read
end
it "returns empty string on eof" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read
assert_equal "", io.read
end
it "returns empty string on zero chunks" do
io = chunked_io(chunks: [].each)
assert_equal "", io.read
end
it "works when not rewindable" do
io = chunked_io(chunks: ["ab", "c"].each, rewindable: false)
io.read(1)
assert_equal "bc", io.read
end
it "returns content in specified encoding" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal Encoding::BINARY, io.read.encoding
io.rewind
assert_equal Encoding::BINARY, io.read.encoding
io = chunked_io(chunks: ["ab", "c"].each, encoding: "utf-8")
assert_equal Encoding::UTF_8, io.read.encoding
io.rewind
assert_equal Encoding::UTF_8, io.read.encoding
end
end
describe "with length" do
it "reads specified number of bytes" do
io = chunked_io(chunks: ["abc"].each)
assert_equal "a", io.read(1)
assert_equal "bc", io.read(2)
end
it "reads across chunks" do
io = chunked_io(chunks: ["ab", "cd", "e"].each)
assert_equal "a", io.read(1)
assert_equal "bc", io.read(2)
assert_equal "de", io.read(2)
end
it "reads as much as it can read" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal "abc", io.read(4)
end
it "returns empty string when length is zero" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal "", io.read(0)
assert_equal "abc", io.read
end
it "reads from cache" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read(1)
io.rewind
assert_equal "a", io.read(1)
assert_equal "bc", io.read(2)
end
it "seamlessly switches between reading cached and new content" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read(1)
io.rewind
assert_equal "ab", io.read(2)
end
it "returns nil on eof" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read
assert_nil io.read(1)
end
it "returns nil on zero chunks" do
io = chunked_io(chunks: [].each)
assert_nil io.read(1)
end
it "works when not rewindable" do
io = chunked_io(chunks: ["ab", "c"].each, rewindable: false)
assert_equal "a", io.read(1)
assert_equal "bc", io.read(2)
end
it "returns content in binary encoding" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal Encoding::BINARY, io.read(1).encoding
io.rewind
assert_equal Encoding::BINARY, io.read(1).encoding
io = chunked_io(chunks: ["ab", "c"].each, encoding: "utf-8")
assert_equal Encoding::BINARY, io.read(1).encoding
io.rewind
assert_equal Encoding::BINARY, io.read(1).encoding
end
end
describe "with length and buffer" do
it "reads specified number of bytes" do
io = chunked_io(chunks: ["abc"].each)
assert_equal "a", io.read(1, "")
assert_equal "bc", io.read(2, "")
end
it "reads across chunks" do
io = chunked_io(chunks: ["ab", "cd", "e"].each)
assert_equal "a", io.read(1, "")
assert_equal "bc", io.read(2, "")
assert_equal "de", io.read(2, "")
end
it "writes read content into the buffer" do
io = chunked_io(chunks: ["abc"].each)
buffer = ""
io.read(1, buffer)
assert_equal "a", buffer
io.read(2, buffer)
assert_equal "bc", buffer
end
it "returns the buffer" do
io = chunked_io(chunks: ["ab", "c"].each)
buffer = ""
assert_equal buffer.object_id, io.read(1, buffer).object_id
assert_equal buffer.object_id, io.read(2, buffer).object_id
io.rewind
assert_equal buffer.object_id, io.read(1, buffer).object_id
assert_equal buffer.object_id, io.read(2, buffer).object_id
end
it "reads as much as it can read" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal "abc", io.read(4, "")
end
it "returns empty string when length is zero" do
io = chunked_io(chunks: ["ab", "c"].each)
buffer = ""
assert_equal "", io.read(0, buffer)
assert_equal buffer.object_id, io.read(0, buffer).object_id
assert_equal "abc", io.read
end
it "reads from cache" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read(1, "")
io.rewind
assert_equal "a", io.read(1, "")
assert_equal "bc", io.read(2, "")
end
it "seamlessly switches between reading cached and new content" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read(1, "")
io.rewind
assert_equal "abc", io.read(3, "")
end
it "returns nil on eof" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read
buffer = "buffer"
assert_nil io.read(1, buffer)
assert_equal "", buffer
end
it "returns nil on zero chunks" do
io = chunked_io(chunks: [].each)
buffer = "buffer"
assert_nil io.read(1, buffer)
assert_equal "", buffer
end
it "works when not rewindable" do
io = chunked_io(chunks: ["ab", "c"].each, rewindable: false)
assert_equal "a", io.read(1, "")
assert_equal "bc", io.read(2, "")
end
it "returns content in binary encoding" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal Encoding::BINARY, io.read(1, "").encoding
io.rewind
assert_equal Encoding::BINARY, io.read(1, "").encoding
io = chunked_io(chunks: ["ab", "c"].each, encoding: "utf-8")
assert_equal Encoding::BINARY, io.read(1, "").encoding
io.rewind
assert_equal Encoding::BINARY, io.read(1, "").encoding
end
end
it "raises IOError when closed" do
io = chunked_io(chunks: ["ab", "c"].each)
io.close
assert_raises(IOError) { io.read }
end
end
describe "#gets" do
it "retrieves lines" do
io = chunked_io(chunks: ["a\n", "b\nc\nd", "\n"].each)
assert_equal "a\n", io.gets
assert_equal "b\n", io.gets
assert_equal "c\n", io.gets
assert_equal "d\n", io.gets
end
it "retrieves lines when non-rewindable" do
io = chunked_io(chunks: ["a\n", "b\nc\nd", "\n"].each, rewindable: false)
assert_equal "a\n", io.gets
assert_equal "b\n", io.gets
assert_equal "c\n", io.gets
assert_equal "d\n", io.gets
end
it "retrieves lines when non-rewindable with limit" do
io = chunked_io(chunks: ["a\n", "b\nc\nd", "\n"].each, rewindable: false)
assert_equal "a\n", io.gets(3)
assert_equal "b\n", io.gets(3)
assert_equal "c\n", io.gets(3)
assert_equal "d\n", io.gets(3)
end
it "accepts a different separator" do
io = chunked_io(chunks: ["a\r", "b\rc\rd", "\r"].each)
assert_equal "a\r", io.gets("\r")
assert_equal "b\r", io.gets("\r")
assert_equal "c\r", io.gets("\r")
assert_equal "d\r", io.gets("\r")
end
it "reads the whole file when separator and limit are nil" do
io = chunked_io(chunks: ["a\n", "b\nc\nd", "\n"].each)
assert_equal "a\nb\nc\n\d\n", io.gets(nil)
end
it "uses the double-newline separator when separator is empty" do
io = chunked_io(chunks: ["a\n", "b\n\n\nd", "\n"].each)
assert_equal "a\nb\n\n", io.gets("")
assert_equal "\nd\n", io.gets("")
end
it "accepts a limit" do
io = chunked_io(chunks: ["a", "b", "c\n"].each)
assert_equal "a", io.gets(1)
assert_equal "bc", io.gets(2)
assert_equal "\n", io.gets(3)
end
it "accepts a separator and a limit" do
io = chunked_io(chunks: ["a", "bc\r", "d\r"].each)
assert_equal "ab", io.gets("\r", 2)
assert_equal "c\r", io.gets("\r", 3)
assert_equal "d\r", io.gets("\r", 3)
end
it "accepts a nil separator and a limit" do
io = chunked_io(chunks: ["a", "bc\r", "d\r"].each)
assert_equal "ab", io.gets(nil, 2)
assert_equal "c\rd", io.gets(nil, 3)
assert_equal "\r", io.gets(nil, 3)
end
it "returns lines in sepecified encoding" do
io = chunked_io(chunks: ["ab", "c"], encoding: Encoding::UTF_8)
assert_equal Encoding::UTF_8, io.gets.encoding
io = chunked_io(chunks: ["ab", "c"])
assert_equal Encoding::BINARY, io.gets.encoding
end
it "returns nil when on EOF" do
io = chunked_io(chunks: ["a\n"].each)
io.gets
assert_nil io.gets
end
it "raises IOError when closed" do
io = chunked_io(chunks: ["a\n"])
io.close
assert_raises(IOError) { io.gets }
end
end
describe "#readpartial" do
describe "with maxlen" do
it "reads specified number of bytes" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal "a", io.readpartial(1)
assert_equal "b", io.readpartial(1)
assert_equal "c", io.readpartial(1)
end
it "reads maximum of one chunk" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal "ab", io.readpartial(3)
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal "a", io.readpartial(1)
assert_equal "b", io.readpartial(3)
end
it "returns empty string when length is zero" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal "", io.readpartial(0)
assert_equal "abc", io.read
end
it "reads available data from cache" do
io = chunked_io(chunks: ["abc"].each)
io.readpartial(3)
io.rewind
assert_equal "a", io.readpartial(1)
assert_equal "bc", io.readpartial(2)
end
it "reads available data from cache and buffer" do
io = chunked_io(chunks: ["ab", "c"].each)
io.readpartial(1)
io.rewind
assert_equal "ab", io.readpartial(4)
end
it "returns data in binary encoding" do
io = chunked_io(chunks: ["ab", "c"])
assert_equal Encoding::BINARY, io.readpartial(1).encoding
end
it "works with frozen chunks" do
io = chunked_io(chunks: ["ab".freeze, "c".freeze])
assert_equal Encoding::BINARY, io.readpartial(2).encoding
end
it "raises EOFError on eof" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read
assert_raises(EOFError) { io.readpartial(1) }
end
it "raises EOFError on zero chunks" do
io = chunked_io(chunks: [].each)
assert_raises(EOFError) { io.readpartial(1) }
end
end
describe "with maxlen and buffer" do
it "reads specified number of bytes" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal "a", io.readpartial(1, "")
assert_equal "b", io.readpartial(1, "")
assert_equal "c", io.readpartial(1, "")
end
it "reads maximum of one chunk" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal "ab", io.readpartial(3, "")
end
it "writes read content into the buffer" do
io = chunked_io(chunks: ["ab", "c"].each)
buffer = ""
io.readpartial(1, buffer)
assert_equal "a", buffer
io.readpartial(1, buffer)
assert_equal "b", buffer
end
it "returns the buffer" do
io = chunked_io(chunks: ["ab", "c"].each)
buffer = ""
assert_equal buffer.object_id, io.readpartial(1, buffer).object_id
assert_equal buffer.object_id, io.readpartial(2, buffer).object_id
io.rewind
assert_equal buffer.object_id, io.readpartial(1, buffer).object_id
assert_equal buffer.object_id, io.readpartial(2, buffer).object_id
end
it "returns empty string when length is zero" do
io = chunked_io(chunks: ["ab", "c"].each)
buffer = ""
assert_equal "", io.readpartial(0, buffer)
assert_equal buffer.object_id, io.readpartial(0, buffer).object_id
assert_equal "abc", io.read
end
it "reads available data from cache" do
io = chunked_io(chunks: ["abc"].each)
io.readpartial(3, "")
io.rewind
assert_equal "a", io.readpartial(1, "")
assert_equal "bc", io.readpartial(2, "")
end
it "reads available data from cache and buffer" do
io = chunked_io(chunks: ["ab", "c"].each)
io.readpartial(1, "")
io.rewind
assert_equal "ab", io.readpartial(4, "")
end
it "returns data in binary encoding" do
io = chunked_io(chunks: ["ab", "c"])
assert_equal Encoding::BINARY, io.readpartial(1, "").encoding
end
it "works with frozen chunks" do
io = chunked_io(chunks: ["ab".freeze, "c".freeze])
assert_equal Encoding::BINARY, io.readpartial(2, "").encoding
end
it "raises EOFError on eof" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read
buffer = "buffer"
assert_raises(EOFError) { io.readpartial(1, buffer) }
assert_equal "", buffer
end
it "raises EOFError on zero chunks" do
io = chunked_io(chunks: [].each)
buffer = "buffer"
assert_raises(EOFError) { io.readpartial(1, buffer) }
assert_equal "", buffer
end
end
it "raises IOError when closed" do
io = chunked_io(chunks: ["ab", "c"].each)
io.close
assert_raises(IOError) { io.readpartial(4) }
end
end
describe "reading" do
it "downloads only how much it needs" do
chunks = Enumerator.new do |y|
y << "ab"
y << "c"
raise "never reached"
end
io = chunked_io(chunks: chunks)
assert_equal "a", io.read(1)
assert_equal "b", io.read(1)
end
it "updates #pos" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal 0, io.pos
io.read(1)
assert_equal 1, io.pos
io.read
assert_equal 3, io.pos
io.rewind
assert_equal 0, io.pos
io.read(1)
assert_equal 1, io.pos
io.read
assert_equal 3, io.pos
end
it "calls :on_close once whole content has been read" do
on_close_called = nil
io = chunked_io(chunks: ["ab", "c"].each, on_close: -> { on_close_called = true })
refute on_close_called
io.read
assert on_close_called
end
it "calls :on_close only once" do
on_close_called = 0
io = chunked_io(chunks: ["ab", "c"].each, on_close: -> { on_close_called += 1 })
io.read
io.rewind
io.read
assert_equal 1, on_close_called
end
it "propagates exceptions that occur when retrieving chunks" do
exceptional_chunks = Enumerator.new { |y| y << "content"; raise Timeout::Error }
io = chunked_io(chunks: exceptional_chunks)
assert_raises(Timeout::Error) { io.read }
end
it "calls enumerator's ensure block when chunk retrieval fails" do
exceptional_chunks = Enumerator.new do |y|
begin
y << "content"
raise Timeout::Error
ensure
@ensure_called = true
end
end
io = chunked_io(chunks: exceptional_chunks)
io.read rescue nil
assert @ensure_called
end
it "calls :on_close when chunk retrieval fails" do
exceptional_chunks = Enumerator.new do |y|
y << "content"
raise Timeout::Error
end
on_close_called = nil
io = chunked_io(chunks: exceptional_chunks, on_close: -> { on_close_called = true })
io.read rescue nil
assert on_close_called
end
end
describe "#each_chunk" do
it "yields chunks with a block" do
io = chunked_io(chunks: ["ab", "c"].each)
io.each_chunk { |chunk| (@chunks ||= []) << chunk }
assert_equal ["ab", "c"], @chunks
end
it "returns an enumerator without a block" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal ["ab", "c"], io.each_chunk.to_a
end
it "doesn't cache retrieved chunks" do
io = chunked_io(chunks: ["ab", "c"].each)
io.each_chunk {}
io.rewind
assert_equal "", io.read
end
it "raises IOError when closed" do
io = chunked_io(chunks: ["ab", "c"].each)
io.close
assert_raises(IOError) { io.each_chunk {} }
end
end
describe "#seek" do
describe "default" do
it "moves to absolute position" do
io = chunked_io(chunks: ["ab", "c"].each)
io.seek(1)
assert_equal 1, io.tell
assert_equal "bc", io.read
end
end
describe "SET" do
it "moves to absolute position" do
io = chunked_io(chunks: ["ab", "c"].each)
io.seek(2)
assert_equal 2, io.tell
assert_equal "c", io.read
end
end
describe "CUR" do
it "moves to relative position" do
io = chunked_io(chunks: ["ab", "cd"].each)
io.seek(1, IO::SEEK_CUR)
assert_equal 1, io.tell
io.seek(2, IO::SEEK_CUR)
assert_equal 3, io.tell
assert_equal "d", io.read
end
end
describe "END" do
it "moves to position from end" do
io = chunked_io(chunks: ["ab", "cd"].each)
io.seek(-2, IO::SEEK_END)
assert_equal 2, io.tell
assert_equal "cd", io.read
end
end
it "seeks uncached content" do
io = chunked_io(chunks: ["ab", "c"].each)
io.seek(1)
assert_equal 1, io.tell
assert_equal "bc", io.read
end
it "seeks cached content" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read(1)
io.rewind
io.seek(1)
assert_equal 1, io.tell
assert_equal "bc", io.read
end
it "seeks cached and uncached content" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read(1)
io.rewind
io.seek(2)
assert_equal 2, io.tell
assert_equal "c", io.read
end
it "raises error on invalid whence" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read(1)
assert_raises ArgumentError do
io.seek(2, :invalid)
end
end
end
describe "#eof?" do
it "returns false on nonzero chunks" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal false, io.eof?
end
it "returns true on zero chunks" do
io = chunked_io(chunks: [].each)
assert_equal true, io.eof?
end
it "returns true when all data has been read" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read
assert_equal true, io.eof?
end
it "returns false when there is still data to be read" do
io = chunked_io(chunks: ["ab", "cd"].each)
io.read(1)
assert_equal false, io.eof?
io.read(2)
assert_equal false, io.eof?
end
it "returns false when on end of tempfile and there is still data to be read" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read(2)
assert_equal false, io.eof?
end
it "returns false when not on end of tempfile and there is no more data to be read" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read
io.rewind
assert_equal false, io.eof?
end
it "returns false when not on end of tempfile and there is still data to be read" do
io = chunked_io(chunks: ["ab", "c"].each)
io.read(2)
io.rewind
assert_equal false, io.eof?
end
it "raises IOError when closed" do
io = chunked_io(chunks: ["ab", "c"].each)
io.close
assert_raises(IOError) { io.eof? }
end
end
describe "#rewind" do
it "rewinds the IO if it's rewindable" do
io = chunked_io(chunks: ["ab", "c"].each)
assert_equal "abc", io.read
io.rewind
assert_equal "abc", io.read
end
it "raises an error if IO is not rewindable" do
io = chunked_io(chunks: ["ab", "c"].each, rewindable: false)
assert_equal "abc", io.read
assert_raises(IOError) { io.rewind }
end
it "raises IOError when closed" do
io = chunked_io(chunks: ["ab", "c"].each)
io.close
assert_raises(IOError) { io.rewind }
end
end
describe "#close" do
it "calls :on_close" do
on_close_called = nil
io = chunked_io(chunks: ["ab", "c"].each, on_close: -> { on_close_called = true })
io.close
assert on_close_called
end
it "doesn't call :on_close again after whole content has been read" do
on_close_called = 0
io = chunked_io(chunks: ["ab", "c"].each, on_close: -> { on_close_called += 1 })
io.read
io.close
assert_equal 1, on_close_called
end
it "doesn't fail when :on_close wasn't specified" do
io = chunked_io(chunks: ["ab", "c"].each)
io.close
end
it "calls enumerator's ensure block" do
chunks = Enumerator.new do |y|
begin
y << "ab"
y << "c"
ensure
@ensure_called = true
end
end
io = chunked_io(chunks: chunks)
io.close
assert @ensure_called
end
it "doesn't error when called after #each_chunk" do
io = chunked_io(chunks: ["ab", "c"].each)
io.each_chunk {}
io.close
end
it "can be called multiple times" do
on_close_called = 0
io = chunked_io(chunks: ["ab", "c"].each, on_close: -> { on_close_called += 1 })
io.close
io.close
assert_equal 1, on_close_called
end
end
describe "#closed?" do
it "returns false when #close was not called" do
io = chunked_io(chunks: [].each)
assert_equal false, io.closed?
end
it "returns truen when #close was called" do
io = chunked_io(chunks: [].each)
io.close
assert_equal true, io.closed?
end
end
describe "#rewindable?" do
it "returns true by default" do
io = chunked_io(chunks: [].each)
assert_equal true, io.rewindable?
end
it "returns false if :rewindable is false" do
io = chunked_io(chunks: [].each, rewindable: false)
assert_equal false, io.rewindable?
end
end
describe "#inspect" do
it "shows important information" do
assert_equal "#<Down::ChunkedIO chunks=#<Enumerator: [\"\"]:each> size=nil encoding=#<Encoding:ASCII-8BIT> data={} on_close=nil rewindable=true>",
chunked_io(chunks: [""].each).inspect
assert_equal "#<Down::ChunkedIO chunks=#<Enumerator: [\"\"]:each> size=10 encoding=#<Encoding:ASCII-8BIT> data={} on_close=nil rewindable=true>",
chunked_io(chunks: [""].each, size: 10).inspect
assert_equal "#<Down::ChunkedIO chunks=#<Enumerator: [\"\"]:each> size=nil encoding=#<Encoding:UTF-8> data={} on_close=nil rewindable=true>",
chunked_io(chunks: [""].each, encoding: "utf-8").inspect
assert_equal "#<Down::ChunkedIO chunks=#<Enumerator: [\"\"]:each> size=nil encoding=#<Encoding:ASCII-8BIT> data={:foo=>\"bar\"} on_close=nil rewindable=true>",
chunked_io(chunks: [""].each, data: {foo: "bar"}).inspect
assert_equal "#<Down::ChunkedIO chunks=#<Enumerator: [\"\"]:each> size=nil encoding=#<Encoding:ASCII-8BIT> data={} on_close=:callable rewindable=true>",
chunked_io(chunks: [""].each, on_close: :callable).inspect
assert_equal "#<Down::ChunkedIO chunks=#<Enumerator: [\"\"]:each> size=nil encoding=#<Encoding:ASCII-8BIT> data={} on_close=nil rewindable=false>",
chunked_io(chunks: [""].each, rewindable: false).inspect
assert_equal "#<Down::ChunkedIO chunks=#<Enumerator: [\"\"]:each> size=nil encoding=#<Encoding:ASCII-8BIT> data={} on_close=nil rewindable=true (closed)>",
chunked_io(chunks: [""].each).tap(&:close).inspect
end
end
it "can be parsed by the CSV standard library" do
require "csv"
io = chunked_io(chunks: ["h1,h2,h3\na", ",b,c\nd,", "e,f\ng,h,i"])
rows = CSV.parse(io, headers: true)
assert_equal Hash["h1"=>"a", "h2"=>"b", "h3"=>"c"], rows[0].to_h
assert_equal Hash["h1"=>"d", "h2"=>"e", "h3"=>"f"], rows[1].to_h
assert_equal Hash["h1"=>"g", "h2"=>"h", "h3"=>"i"], rows[2].to_h
end
end