Class: Cuprum::Cli::Dependencies::StandardIo::Mock

Inherits:
Cuprum::Cli::Dependencies::StandardIo show all
Defined in:
lib/cuprum/cli/dependencies/standard_io/mock.rb

Overview

Mock implementation of StandardIo for testing purposes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Cuprum::Cli::Dependencies::StandardIo

#color

Constructor Details

#initialize(error_stream: StringIO.new, input_stream: StringIO.new, output_stream: StringIO.new) ⇒ Mock

Returns a new instance of Mock.

Parameters:

  • error_stream (IO) (defaults to: StringIO.new)

    the error stream. Defaults to an instance of StringIO.

  • input_stream (IO) (defaults to: StringIO.new)

    the input stream. Defaults to an instance of StringIO.

  • output_stream (IO) (defaults to: StringIO.new)

    the output stream. Defaulst to an instance of StringIO.



40
41
42
43
44
45
46
47
48
# File 'lib/cuprum/cli/dependencies/standard_io/mock.rb', line 40

def initialize(
  error_stream:  StringIO.new,
  input_stream:  StringIO.new,
  output_stream: StringIO.new
)
  super

  @combined_stream = StringIO.new
end

Instance Attribute Details

#combined_streamIO (readonly)

Returns a combined input/output stream representing all IO activity.

Returns:

  • (IO)

    a combined input/output stream representing all IO activity.



51
52
53
# File 'lib/cuprum/cli/dependencies/standard_io/mock.rb', line 51

def combined_stream
  @combined_stream
end

Class Method Details

.append_for_read(*messages, io:) ⇒ IO

Utility method for appending readable data to a StringIO stream.

Parameters:

  • messages (Array<String>)

    the messages to append. Will be appended to the stream in the provided order.

  • io (IO)

    the io stream to append.

Returns:

  • (IO)

    the io stream.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cuprum/cli/dependencies/standard_io/mock.rb', line 18

def self.append_for_read(*messages, io:)
  total_length = 0

  messages.each do |message|
    message += "\n" unless message.end_with?("\n")

    io.print(message)

    total_length += message.length
  end

  io.pos -= total_length

  io
end

Instance Method Details

#error_streamIO

Returns the error stream.

Returns:

  • (IO)

    the error stream



54
# File 'lib/cuprum/cli/dependencies/standard_io/mock.rb', line 54

def error_stream = super # rubocop:disable Lint/UselessMethodDefinition

#input_streamIO

Returns the input stream.

Returns:

  • (IO)

    the input stream.



57
# File 'lib/cuprum/cli/dependencies/standard_io/mock.rb', line 57

def input_stream = super # rubocop:disable Lint/UselessMethodDefinition

#output_streamIO

Returns the output stream.

Returns:

  • (IO)

    the output stream.



60
# File 'lib/cuprum/cli/dependencies/standard_io/mock.rb', line 60

def output_stream = super # rubocop:disable Lint/UselessMethodDefinition

#read_inputString

Requests a newline-terminated string from the input stream.

Returns:

  • (String)

    the returned input string.



63
64
65
66
67
68
69
# File 'lib/cuprum/cli/dependencies/standard_io/mock.rb', line 63

def read_input
  message = super

  combined_stream.print(message) if message

  message
end

#write_error(message = nil, newline: true) ⇒ nil

Writes the given message to the error stream.

If no error message is given, prints a newline only.

Parameters:

  • message (String, nil) (defaults to: nil)

    the message to write.

  • newline (true, false) (defaults to: true)

    if true, appends a newline to the message if it does not have a newline. Defaults to true.

Returns:

  • (nil)


79
80
81
82
83
# File 'lib/cuprum/cli/dependencies/standard_io/mock.rb', line 79

def write_error(message = nil, newline: true)
  super

  newline ? combined_stream.puts(message) : combined_stream.print(message)
end

#write_output(message = nil, newline: true) ⇒ nil

Writes the given message to the output stream.

If no message is given, prints a newline only.

Parameters:

  • message (String, nil) (defaults to: nil)

    the message to write.

  • newline (true, false) (defaults to: true)

    if true, appends a newline to the message if it does not have a newline. Defaults to true.

Returns:

  • (nil)


72
73
74
75
76
# File 'lib/cuprum/cli/dependencies/standard_io/mock.rb', line 72

def write_output(message = nil, newline: true)
  super

  newline ? combined_stream.puts(message) : combined_stream.print(message)
end