Class: TestBench::Output::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/test_bench/output/writer.rb,
lib/test_bench/output/writer/style.rb,
lib/test_bench/output/writer/buffer.rb,
lib/test_bench/output/writer/defaults.rb,
lib/test_bench/output/writer/substitute.rb,
lib/test_bench/output/writer/buffer/console.rb

Direct Known Subclasses

Substitute::Writer

Defined Under Namespace

Modules: Defaults, Style, Styling, Substitute Classes: Buffer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#alternate_deviceObject



11
12
13
# File 'lib/test_bench/output/writer.rb', line 11

def alternate_device
  @alternate_device ||= Device::Substitute.build
end

#bufferObject



42
43
44
# File 'lib/test_bench/output/writer.rb', line 42

def buffer
  @buffer ||= Buffer.new
end

#column_sequenceObject



32
33
34
# File 'lib/test_bench/output/writer.rb', line 32

def column_sequence
  @column_sequence ||= 0
end

#deviceObject



6
7
8
# File 'lib/test_bench/output/writer.rb', line 6

def device
  @device ||= Device::Substitute.build
end

#digestObject



22
23
24
# File 'lib/test_bench/output/writer.rb', line 22

def digest
  @digest ||= Digest.new
end

#indentation_depthObject



37
38
39
# File 'lib/test_bench/output/writer.rb', line 37

def indentation_depth
  @indentation_depth ||= 0
end

#peerObject

Returns the value of attribute peer.



4
5
6
# File 'lib/test_bench/output/writer.rb', line 4

def peer
  @peer
end

#sequenceObject



27
28
29
# File 'lib/test_bench/output/writer.rb', line 27

def sequence
  @sequence ||= 0
end

#styling_policyObject Also known as: styling



16
17
18
# File 'lib/test_bench/output/writer.rb', line 16

def styling_policy
  @styling_policy ||= Styling.default
end

Class Method Details

.build(device = nil, styling: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/test_bench/output/writer.rb', line 47

def self.build(device=nil, styling: nil)
  device ||= Defaults.device

  instance = new

  instance.device = device
  instance.alternate_device = Device::Null.build

  if device.tty?
    Buffer::Console.configure(instance, device:)
  else
    instance.buffer.limit = 0
  end

  if not styling.nil?
    instance.styling_policy = styling
  end

  instance
end

.configure(receiver, styling: nil, device: nil, attr_name: nil) ⇒ Object



68
69
70
71
72
73
# File 'lib/test_bench/output/writer.rb', line 68

def self.configure(receiver, styling: nil, device: nil, attr_name: nil)
  attr_name ||= :writer

  instance = build(device, styling: styling)
  receiver.public_send(:"#{attr_name}=", instance)
end

.follow(previous_writer) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/test_bench/output/writer.rb', line 79

def self.follow(previous_writer)
  device = previous_writer

  alternate_device = previous_writer.peer
  alternate_device ||= Device::Null.build

  previous_digest = previous_writer.digest
  digest = previous_digest.clone

  writer = new
  writer.sync = false
  writer.device = device
  writer.alternate_device = alternate_device
  writer.styling_policy = previous_writer.styling_policy
  writer.digest = digest
  writer.sequence = previous_writer.sequence
  writer.column_sequence = previous_writer.column_sequence
  writer.indentation_depth = previous_writer.indentation_depth
  writer.digest = previous_writer.digest.clone
  writer
end

Instance Method Details

#branchObject



101
102
103
104
105
106
107
108
# File 'lib/test_bench/output/writer.rb', line 101

def branch
  alternate = self.class.follow(self)
  primary = self.class.follow(self)

  primary.peer = alternate

  return primary, alternate
end

#current?(sequence) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/test_bench/output/writer.rb', line 202

def current?(sequence)
  sequence >= self.sequence
end

#decrease_indentationObject Also known as: deindent!



211
212
213
# File 'lib/test_bench/output/writer.rb', line 211

def decrease_indentation
  self.indentation_depth -= 1
end

#flushObject



182
183
184
# File 'lib/test_bench/output/writer.rb', line 182

def flush
  buffer.flush(device, alternate_device)
end

#follows?(other_writer) ⇒ Boolean

Returns:

  • (Boolean)


216
217
218
219
220
221
222
223
224
225
226
# File 'lib/test_bench/output/writer.rb', line 216

def follows?(other_writer)
  if sequence < other_writer.sequence
    false
  elsif device == other_writer
    true
  elsif device == other_writer.peer
    true
  else
    false
  end
end

#increase_indentationObject Also known as: indent!



206
207
208
# File 'lib/test_bench/output/writer.rb', line 206

def increase_indentation
  self.indentation_depth += 1
end

#indentObject



144
145
146
147
148
# File 'lib/test_bench/output/writer.rb', line 144

def indent
  indentation = '  ' * indentation_depth

  print(indentation)
end


150
151
152
153
154
155
156
157
158
# File 'lib/test_bench/output/writer.rb', line 150

def print(text)
  text = text.dump[1...-1]

  self.column_sequence += text.length

  write(text)

  self
end

#puts(text = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/test_bench/output/writer.rb', line 110

def puts(text=nil)
  if column_sequence.zero?
    indent
  end

  if not text.nil?
    print(text)
  end

  style(:reset)

  if tty?
    write("\e[0K")
  end

  write("\n")

  self.column_sequence = 0
end

#style(style, *additional_styles) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/test_bench/output/writer.rb', line 130

def style(style, *additional_styles)
  styles = [style, *additional_styles]

  control_codes = styles.map do |style|
    Style.control_code(style)
  end

  if styling?
    write("\e[#{control_codes.join(';')}m")
  end

  self
end

#styling?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/test_bench/output/writer.rb', line 228

def styling?
  Styling.styling?(styling_policy, device.tty?)
end

#syncObject



75
76
77
# File 'lib/test_bench/output/writer.rb', line 75

def sync
  @sync.nil? ? @sync = true : @sync
end

#sync=(sync) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/test_bench/output/writer.rb', line 186

def sync=(sync)
  @sync = sync

  if sync
    flush
  end
end

#tty?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/test_bench/output/writer.rb', line 178

def tty?
  device.tty?
end

#write(data) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/test_bench/output/writer.rb', line 160

def write(data)
  if sync
    device.write(data)
    alternate_device.write(data)

    bytes_written = data.bytesize
  else
    bytes_written = buffer.receive(data)
  end

  self.sequence += bytes_written

  data = data[0...bytes_written]
  digest.update(data)

  bytes_written
end

#written?(data = nil) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
197
198
199
200
# File 'lib/test_bench/output/writer.rb', line 194

def written?(data=nil)
  if data.nil?
    sequence > 0
  else
    digest.digest?(data)
  end
end