Sha256: fb26810f7c12a04c53cdcee2cbcca61624122550d03581f645650098f6dc5172

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

# encoding: utf-8

require 'spec_helper'

describe TTY::Table, 'access' do
  let(:header) { [:head1, :head2] }
  let(:rows)   { [['a1', 'a2'], ['b1', 'b2']] }

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

  it { is_expected.to respond_to(:element) }

  it { should respond_to(:component) }

  it { should respond_to(:at) }

  context 'when array like access' do
    it { expect(table[0,0]).to eq('a1') }

    it { expect(table[0]).to eq(rows[0]) }

    it { expect(table[5]).to eq(nil) }

    it { expect(table[-1]).to eq(rows[-1]) }

    it { expect(table[5,5]).to eq(nil) }

    it 'raises error for negative indices' do
      expect { table[-5,-5] }.to raise_error(IndexError)
    end
  end

  context '#row' do
    it 'returns nil for wrong index' do
      expect(table.row(11)).to be_nil
    end

    it 'gets row at index' do
      expect(table.row(1)).to eq(rows[1])
    end

    it 'yields self for wrong index' do
      block = lambda { |el| [] << el }
      expect(table.row(11, &block)).to eq(table)
    end

    it 'yields row at index' do
      yields = []
      expect { table.row(1).each { |el| yields << el } }.to change { yields }.
        from( [] ).
        to( rows[1] )
    end
  end

  context '#column' do
    it "gets based on header name" do
      expect(table.column(:head1)).to eq(['a1', 'b1'])
    end

    it "yields based on header name" do
      yielded = []
      table.column(:head1) { |el| yielded << el }
      expect(yielded).to eql(['a1', 'b1'])
    end

    it 'returns nil for wrong index' do
      expect(table.column(11)).to be_nil
    end

    it 'gets column at index' do
      expect(table.column(0)).to eq(['a1', 'b1'])
    end

    it 'yields self for wrong index' do
      block = lambda { |el| [] << el }
      expect(table.column(11, &block)).to eq(table)
    end

    it 'yields column at index' do
      yields = []
      expect { table.column(1).each { |el| yields << el } }.to change { yields }.
        from( [] ).
        to( ['a2', 'b2'])
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tty-0.1.2 spec/tty/table/access_spec.rb
tty-0.1.1 spec/tty/table/access_spec.rb
tty-0.1.0 spec/tty/table/access_spec.rb