Class: TestBench::Output::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/test_bench/output/writer.rb,
lib/test_bench/output/writer/assertions.rb,
lib/test_bench/output/writer/assertions/line.rb

Defined Under Namespace

Modules: Assertions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = nil) ⇒ Writer

Returns a new instance of Writer.



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

def initialize level=nil
  @level = level
  @indentation = 0
  @mutex = Mutex.new
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



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

def color
  @color
end

#deviceObject



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

def device
  @device ||= StringIO.new
end

#indentationObject

Returns the value of attribute indentation.



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

def indentation
  @indentation
end

#levelObject



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

def level
  @level ||= :normal
end

#mutexObject (readonly)

Returns the value of attribute mutex.



8
9
10
# File 'lib/test_bench/output/writer.rb', line 8

def mutex
  @mutex
end

Class Method Details

.build(device) ⇒ Object



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

def self.build device
  instance = new
  instance.device = device
  instance
end

Instance Method Details

#color?Boolean

Returns:

  • (Boolean)


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

def color?
  return color unless color.nil?
  device.tty?
end

#decrease_indentationObject



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

def decrease_indentation
  mutex.synchronize do
    self.indentation -= 1
  end
end

#increase_indentationObject



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

def increase_indentation
  mutex.synchronize do
    self.indentation += 1
  end
end

#lower_verbosityObject



47
48
49
50
51
52
53
# File 'lib/test_bench/output/writer.rb', line 47

def lower_verbosity
  if level == :verbose
    self.level = :normal
  elsif level == :normal
    self.level = :quiet
  end
end

#normal(prose, **arguments) ⇒ Object



55
56
57
58
# File 'lib/test_bench/output/writer.rb', line 55

def normal prose, **arguments
  arguments[:render] = false if level == :quiet
  write prose, **arguments
end

#quiet(prose, **arguments) ⇒ Object



60
61
62
# File 'lib/test_bench/output/writer.rb', line 60

def quiet prose, **arguments
  write prose, **arguments
end

#raise_verbosityObject



64
65
66
67
68
69
70
# File 'lib/test_bench/output/writer.rb', line 64

def raise_verbosity
  if level == :quiet
    self.level = :normal
  elsif level == :normal
    self.level = :verbose
  end
end

#verbose(prose, **arguments) ⇒ Object



72
73
74
75
# File 'lib/test_bench/output/writer.rb', line 72

def verbose prose, **arguments
  arguments[:render] = false unless level == :verbose
  write prose, **arguments
end

#write(prose, bg: nil, fg: nil, render: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/test_bench/output/writer.rb', line 77

def write prose, bg: nil, fg: nil, render: nil
  render = true if render.nil?

  if render
    text = String.new prose

    indentation = '  ' * self.indentation

    prose = Palette.apply prose, bg: bg, fg: fg if color?

    text = "#{indentation}#{prose}#{$INPUT_RECORD_SEPARATOR}"

    device.write text
  end
end