File: cli_spec.rb

package info (click to toggle)
ruby-ascii85 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176 kB
  • sloc: ruby: 660; makefile: 11
file content (211 lines) | stat: -rw-r--r-- 5,047 bytes parent folder | download
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
# frozen_string_literal: true

require 'stringio'
require 'tempfile'

require 'minitest/autorun'

# We can't require the executable file because it doesn't 
# have the '.rb' extension, so we have to load it.
unless defined?(CLI)
  load File.join(__dir__, '..','..', 'bin', 'ascii85')
end

describe 'CLI' do
  it 'should recognize the -h and --help options' do
    [%w[-h], %w[--help]].each do |args|
      cli = CLI.new(args)
      assert_equal :help, cli.options[:action]
    end
  end

  it 'should recognize the -V and --version options' do
    [%w[-V], %w[--version]].each do |args|
      cli = CLI.new(args)
      assert_equal :version, cli.options[:action]
    end
  end

  it 'should complain about superfluous arguments' do
    assert_raises(OptionParser::ParseError) do
      CLI.new(%w[foo bar])
    end
  end

  describe 'wrap' do
    it 'should default to wrapping at 80 characters' do
      cli = CLI.new([])
      assert_equal 80, cli.options[:wrap]
    end

    it 'should recognize the -w and --wrap options' do
      [%w[-w 17], %w[--wrap 17]].each do |args|
        cli = CLI.new(args)
        assert_equal 17, cli.options[:wrap]
      end
    end

    it 'should recognize the no-wrapping setting' do
      cli = CLI.new(%w[-w 0])
      assert_equal false, cli.options[:wrap]
    end

    it 'should raise an error if the wrap option is not an integer' do
      assert_raises(OptionParser::ParseError) do
        CLI.new(%w[-w foo])
      end
    end
  end

  describe 'encoding' do
    it 'should encode from STDIN' do
      stdin = StringIO.new('Ruby')
      stdout = StringIO.new

      CLI.new([], stdin: stdin, stdout: stdout).call

      assert_equal '<~;KZGo~>', stdout.string
    end

    it 'should accept "-" as a file name' do
      stdin = StringIO.new('Ruby')
      stdout = StringIO.new

      CLI.new(['-'], stdin: stdin, stdout: stdout).call

      assert_equal '<~;KZGo~>', stdout.string
    end

    it 'should encode a file' do
      begin
        f = Tempfile.create('ascii85_encode')
        f.write('Ruby')
        f.close

        stdout = StringIO.new
        CLI.new([f.path], stdout: stdout).call

        assert_equal '<~;KZGo~>', stdout.string
      ensure
        File.unlink(f.path) 
      end
    end

    it 'should wrap lines' do
      begin
        f = Tempfile.create('ascii85_wrap')
        f.write('a' * 20)
        f.close

        stdout = StringIO.new
        CLI.new([f.path, '-w2'], stdout: stdout).call

        assert stdout.string.lines.all? { |l| l.chomp.length <= 2 }
      ensure
        File.unlink(f.path) 
      end
    end

    it 'should fail when the input file is not found' do
      assert_raises(StandardError) do
        CLI.new(['./foo/bar/baz']).call
      end
    end

    it 'should fail when the input file is not readable' do
      begin
        f = Tempfile.create('ascii85_encode')
        f.chmod(0o000)

        assert_raises(StandardError) do
          CLI.new([f.path]).call
        end
      ensure
        File.unlink(f.path) 
      end
    end
  end

  describe 'decoding' do
    it 'should decode from STDIN' do
      stdin = StringIO.new('<~;KZGo~>')
      stdout = StringIO.new

      CLI.new(['-d'], stdin: stdin, stdout: stdout).call

      assert_equal 'Ruby', stdout.string
    end

    it 'should accept "-" as a file name' do
      stdin = StringIO.new('<~;KZGo~>')
      stdout = StringIO.new

      CLI.new(['-d','-'], stdin: stdin, stdout: stdout).call

      assert_equal 'Ruby', stdout.string
    end

    it 'should decode a file' do
      begin
        f = Tempfile.create('ascii85_decode')
        f.write('<~;KZGo~>')
        f.close

        stdout = StringIO.new
        CLI.new(['-d', f.path], stdout: stdout).call

        assert_equal 'Ruby', stdout.string
      ensure
        File.unlink(f.path) 
      end
    end

    it 'should fail when the input file is not found' do
      assert_raises(StandardError) do
        CLI.new(['-d', './foo/bar/baz']).call
      end
    end

    it 'should fail when the input file is not readable' do
      begin
        f = Tempfile.create('ascii85_decode')
        f.chmod(0o000)

        assert_raises(StandardError) do
          CLI.new(['-d', f.path]).call
        end
      ensure
        File.unlink(f.path) 
      end
    end

    describe 'invalid input' do
      it 'should return the empty string when the input does not have delimiters' do 
        stdin = StringIO.new('No delimiters')
        stdout = StringIO.new

        CLI.new(['-d'], stdin: stdin, stdout: stdout).call

        assert_equal '', stdout.string
      end

      ERROR_CASES = [
        '<~!!y!!~>',
        '<~!!z!!~>',
        '<~s8W-#~>',
        '<~!~>',
      ]

      it 'should raise an error when invalid input is encountered' do
        ERROR_CASES.each do |input|
          stdin = StringIO.new(input)
          stdout = StringIO.new

          assert_raises(Ascii85::DecodingError) do
            CLI.new(['-d'], stdin: stdin, stdout: stdout).call
          end
        end
      end
    end
  end
end