File: printer_spec.rb

package info (click to toggle)
ruby-tty-command 0.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 452 kB
  • sloc: ruby: 1,990; makefile: 4; sh: 4
file content (50 lines) | stat: -rw-r--r-- 1,575 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
# frozen_string_literal: true

RSpec.describe TTY::Command, ":printer" do
  it "fails to find printer for nil" do
    expect {
      TTY::Command.new(printer: nil)
    }.to raise_error(ArgumentError, /Unknown printer type ""/)
  end

  it "fails to find printer based on name" do
    expect {
      TTY::Command.new(printer: :unknown)
    }.to raise_error(ArgumentError, /Unknown printer type "unknown"/)
  end

  it "detects null printer" do
    cmd = TTY::Command.new(printer: :null)
    expect(cmd.printer).to be_an_instance_of(TTY::Command::Printers::Null)
  end

  it "detects printer based on name" do
    cmd = TTY::Command.new(printer: :progress)
    expect(cmd.printer).to be_an_instance_of(TTY::Command::Printers::Progress)
  end

  it "uses printer based on class name" do
    output = StringIO.new
    printer = TTY::Command::Printers::Pretty
    cmd = TTY::Command.new(output: output, printer: printer)
    expect(cmd.printer).to be_an_instance_of(TTY::Command::Printers::Pretty)
  end

  it "uses printer based on instance" do
    output = StringIO.new
    printer = TTY::Command::Printers::Pretty.new(output)
    cmd = TTY::Command.new(printer: printer)
    expect(cmd.printer).to be_an_instance_of(TTY::Command::Printers::Pretty)
  end

  it "uses custom printer" do
    stub_const("CustomPrinter", Class.new(TTY::Command::Printers::Abstract) do
      def write(message)
        output << message
      end
    end)
    printer = CustomPrinter
    cmd = TTY::Command.new(printer: printer)
    expect(cmd.printer).to be_an_instance_of(CustomPrinter)
  end
end