Sha256: b3070a40b76f5db0c0305a011d05a5f7f0db9f26fca95657b41be2480bb6f142

Contents?: true

Size: 1.8 KB

Versions: 14

Compression:

Stored size: 1.8 KB

Contents

require 'helper'

class Nanoc::CLI::CleaningStreamTest < Nanoc::TestCase
  class Stream
    attr_accessor :called_methods

    def initialize
      @called_methods = []
    end

    # rubocop:disable Style/MethodMissing
    def method_missing(symbol, *_args)
      @called_methods << symbol
    end
    # rubocop:enable Style/MethodMissing

    def respond_to_missing?(*_args)
      true
    end
  end

  def test_forward
    methods = [:write, :<<, :tty?, :tty?, :flush, :tell, :print, :puts, :string, :reopen, :exist?, :exists?, :close]

    s = Stream.new
    cs = Nanoc::CLI::CleaningStream.new(s)

    cs.write('aaa')
    cs << 'bb'
    cs.tty?
    cs.isatty
    cs.flush
    cs.tell
    cs.print('cc')
    cs.puts('dd')
    cs.string
    cs.reopen('/dev/null', 'r')
    cs.exist?
    cs.exists?
    cs.close

    methods.each do |m|
      assert s.called_methods.include?(m), "expected #{m} to be called"
    end
  end

  def test_forward_tty_cached
    s = Stream.new
    cs = Nanoc::CLI::CleaningStream.new(s)

    cs.tty?
    cs.isatty

    assert_equal [:tty?], s.called_methods
  end

  def test_works_with_logger
    require 'logger'
    stream = StringIO.new
    cleaning_stream = Nanoc::CLI::CleaningStream.new(stream)
    logger = Logger.new(cleaning_stream)
    logger.info('Some info')
    logger.warn('Something could start going wrong!')
  end

  def test_broken_pipe
    stream = StringIO.new
    def stream.write(_s)
      raise Errno::EPIPE.new
    end

    cleaning_stream = Nanoc::CLI::CleaningStream.new(stream)
    cleaning_stream.write('lol')
  end

  def test_non_string
    obj = Object.new
    def obj.to_s
      'Hello… world!'
    end

    stream = StringIO.new
    cleaning_stream = Nanoc::CLI::CleaningStream.new(stream)
    cleaning_stream << obj
    assert_equal 'Hello… world!', stream.string
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
nanoc-4.7.1 test/cli/test_cleaning_stream.rb
nanoc-4.7.0 test/cli/test_cleaning_stream.rb
nanoc-4.6.4 test/cli/test_cleaning_stream.rb
nanoc-4.6.3 test/cli/test_cleaning_stream.rb
nanoc-4.6.2 test/cli/test_cleaning_stream.rb
nanoc-4.6.1 test/cli/test_cleaning_stream.rb
nanoc-4.6.0 test/cli/test_cleaning_stream.rb
nanoc-4.5.4 test/cli/test_cleaning_stream.rb
nanoc-4.5.3 test/cli/test_cleaning_stream.rb
nanoc-4.5.2 test/cli/test_cleaning_stream.rb
nanoc-4.5.1 test/cli/test_cleaning_stream.rb
nanoc-4.5.0 test/cli/test_cleaning_stream.rb
nanoc-4.4.7 test/cli/test_cleaning_stream.rb
nanoc-4.4.6 test/cli/test_cleaning_stream.rb