File: io_resource_spec.rb

package info (click to toggle)
ruby-inline-svg 1.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 480 kB
  • sloc: ruby: 1,711; makefile: 4
file content (84 lines) | stat: -rw-r--r-- 1,928 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
72
73
74
75
76
77
78
79
80
81
82
83
84
begin
  require_relative '../lib/inline_svg'
rescue LoadError
  require "inline_svg"
end
require "stringio"
require "tempfile"

describe InlineSvg::IOResource do
  it "support api methods" do
    is_expected.to respond_to(:===, :read)
  end

  describe '#===' do
    context 'return true' do
      it "for IO object" do
        read_io, write_io = IO.pipe
        expect(subject === read_io).to be true
        expect(subject === write_io).to be true
      end

      it "for StringIO object" do
        expect(subject === StringIO.new).to be true
      end

      it "for File object" do
        expect(subject === File.new("#{Dir.tmpdir}/testfile", "w")).to be true
      end
    end
    context 'return false' do
      it "for String object" do
        expect(subject === "string/filename").to be false
      end
    end
  end

  describe '#read' do

    tests = proc do
      it "closed raise error" do
        rio.close
        expect do 
          subject.read(rio)
        end.to raise_error(IOError)
      end

      it "empty" do
        rio.read
        expect(subject.read rio).to eq ''
      end

      it "twice" do
        expect(subject.read rio).to eq answer
        expect(subject.read rio).to eq answer
      end

      it "write only raise error" do
        expect do 
          subject.read wio
        end.to raise_error(IOError)
      end
    end

    context 'IO object' do
      let(:answer) { 'read' }
      let(:rio) { StringIO.new(answer, 'r') }
      let(:wio) { StringIO.new('write', 'w') }
      instance_exec &tests
    end

    context 'File object' do
      let(:file_path) { File.expand_path('../files/example.svg', __FILE__) }
      let(:answer) { File.read(file_path) }
      let(:rio) { File.new(file_path, 'r') }
      let(:wio) { File.new('/dev/null', 'w') }
      instance_exec &tests
      it 'has non empty body' do
        expect(answer).to_not eq ''
      end
    end

    
  end
end