Sha256: 6466e2d638f0ed66691cdbdfba91299154abe2cb5242eed5c648525e4fc3d8f7

Contents?: true

Size: 1.88 KB

Versions: 4

Compression:

Stored size: 1.88 KB

Contents

# -*- encoding: utf-8 -*-

require 'spec_helper'

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

  it { should respond_to(:element) }

  it { should respond_to(:component) }

  it { should respond_to(:at) }

  its([0,0]) { should == 'a1'}

  its([0]) { should == rows[0] }

  its([5]) { should be_nil }

  its([-1]) { should == rows[-1] }

  its([5,5]) { should be_nil }

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

  context '#row' do
    it 'returns nil for wrong index' do
      subject.row(11).should be_nil
    end

    it 'gets row at index' do
      subject.row(1).should == rows[1]
    end

    it 'yields self for wrong index' do
      block = lambda { |el| [] << el }
      subject.row(11, &block).should eql subject
    end

    it 'yields row at index' do
      yields = []
      expect { subject.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
      subject.column(:head1).should eql ['a1', 'b1']
    end

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

    it 'returns nil for wrong index' do
      subject.column(11).should be_nil
    end

    it 'gets column at index' do
      subject.column(0).should == ['a1', 'b1']
    end

    it 'yields self for wrong index' do
      block = lambda { |el| [] << el }
      subject.column(11, &block).should eql subject
    end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tty-0.0.11 spec/tty/table/access_spec.rb
tty-0.0.10 spec/tty/table/access_spec.rb
tty-0.0.9 spec/tty/table/access_spec.rb
tty-0.0.8 spec/tty/table/access_spec.rb