Sha256: 50c27b43d2ec0a786af6b9222b6cb05aa391a06e3499c3c7dd5bf53dff6c760d

Contents?: true

Size: 1.82 KB

Versions: 9

Compression:

Stored size: 1.82 KB

Contents

# coding: utf-8

require 'spec_helper'

RSpec.describe TTY::Table, '#render' do
  let(:object) { described_class }
  let(:header) { ['h1', 'h2'] }
  let(:rows)   { [['a1', 'a2'], ['b1', 'b2']] }
  let(:color)  { Pastel.new(enabled: true) }
  let(:basic_renderer)   { TTY::Table::Renderer::Basic }
  let(:ascii_renderer)   { TTY::Table::Renderer::ASCII }

  subject(:table) { object.new header, rows }

  before { allow(Pastel).to receive(:new).and_return(color) }

  it { is_expected.to respond_to(:render) }

  context 'with block' do
    it 'allows to modify renderer in a block' do
      expected = nil
      block = lambda { |renderer| expected = renderer }
      table.render(&block)
      expect(expected).to be_kind_of(basic_renderer)
    end

    it 'sets renderer as block parameter' do
      expected = nil
      block = lambda { |renderer| expected = renderer }
      table.render(:ascii, &block)
      expect(expected).to be_kind_of(ascii_renderer)
    end

    it 'sets parameter on renderer' do
      result = table.render :ascii do |renderer|
        renderer.border.style = :red
      end
      expect(result).to eq unindent(<<-EOS)
        \e[31m+--+--+\e[0m
        \e[31m|\e[0mh1\e[31m|\e[0mh2\e[31m|\e[0m
        \e[31m+--+--+\e[0m
        \e[31m|\e[0ma1\e[31m|\e[0ma2\e[31m|\e[0m
        \e[31m|\e[0mb1\e[31m|\e[0mb2\e[31m|\e[0m
        \e[31m+--+--+\e[0m
      EOS
    end
  end

  context 'with params' do
    it 'sets params without renderer' do
      allow(TTY::Table::Renderer).to receive(:render)
      table.render(:basic)
      expect(TTY::Table::Renderer).to have_received(:render).
        with(table, {renderer: :basic})
    end

    it 'sets params with renderer' do
      allow(TTY::Table::Renderer).to receive(:render)
      table.render
      expect(TTY::Table::Renderer).to have_received(:render).with(table, {})
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
tty-table-0.10.0 spec/unit/render_spec.rb
tty-table-0.9.0 spec/unit/render_spec.rb
tty-table-0.8.0 spec/unit/render_spec.rb
tty-table-0.7.0 spec/unit/render_spec.rb
tty-table-0.6.0 spec/unit/render_spec.rb
tty-table-0.5.0 spec/unit/render_spec.rb
tty-table-0.4.0 spec/unit/render_spec.rb
tty-table-0.3.0 spec/unit/render_spec.rb
tty-table-0.2.0 spec/unit/render_spec.rb