File: multi_writer_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (71 lines) | stat: -rw-r--r-- 1,474 bytes parent folder | download | duplicates (2)
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
require "spec"
require "../spec_helper"

describe "IO::MultiWriter" do
  describe "#write" do
    it "writes to multiple IOs" do
      io1 = IO::Memory.new
      io2 = IO::Memory.new

      writer = IO::MultiWriter.new(io1, io2)

      writer.puts "foo bar"

      io1.to_s.should eq("foo bar\n")
      io2.to_s.should eq("foo bar\n")
    end
  end

  describe "#read" do
    it "raises" do
      writer = IO::MultiWriter.new(Array(IO).new)

      expect_raises(IO::Error, "Can't read from IO::MultiWriter") do
        writer.read_byte
      end
    end
  end

  describe "#close" do
    it "stops reading" do
      io = IO::Memory.new
      writer = IO::MultiWriter.new(io)

      writer.close

      expect_raises(IO::Error, "Closed") do
        writer.puts "foo"
      end

      io.closed?.should eq(false)
      io.to_s.should eq("")
    end

    it "closes the underlying stream if sync_close is true" do
      io = IO::Memory.new
      writer = IO::MultiWriter.new(io, sync_close: true)

      writer.close

      io.closed?.should eq(true)
    end
  end

  describe "#flush" do
    it "writes to IO and File" do
      with_tempfile("multiple_writer_spec") do |path|
        io = IO::Memory.new

        File.open(path, "w") do |file|
          writer = IO::MultiWriter.new(io, file)

          writer.puts "foo bar"
          writer.flush
        end

        io.to_s.should eq("foo bar\n")
        File.read(path).should eq("foo bar\n")
      end
    end
  end
end