File: column_spec.rb

package info (click to toggle)
ruby-table-print 1.5.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 292 kB
  • sloc: ruby: 1,821; makefile: 2
file content (93 lines) | stat: -rw-r--r-- 2,343 bytes parent folder | download | duplicates (3)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require 'spec_helper'

include TablePrint

describe Column do
  let(:c) {Column.new(:data => ["Once upon a time", "there was a dark and stormy night"], :name => :tagline)}

  it "remembers its name as a string" do
    c.name.should == "tagline"
  end

  it "exposes the array of data representing the column" do
    c.data.should == ["Once upon a time", "there was a dark and stormy night"]
  end

  describe "#add_formatter" do
    it "stores the formatter" do
      f = {}
      c.add_formatter(f)
      c.formatters.should == [f]
    end
  end

  describe "#formatter=" do
    it "adds the formatters individually" do
      c.should_receive(:add_formatter).twice
      c.formatters = [{}, {}]
    end
  end

  describe "#display_method" do
    it "returns the column's display method as a string" do
      c = Column.new(:display_method => :boofar)
      c.display_method.should == "boofar"
    end

    it "doesn't turn a lambda display method into a string" do
      lam = lambda{}
      c = Column.new(:display_method => lam)
      c.display_method.should == lam
    end

    it "defaults to the column name" do
      c = Column.new(:name => :boofar)
      c.display_method.should == "boofar"
    end
  end

  describe "#data_width" do
    it "reflects the width of the data set" do
      c.data_width.should == 33
    end

    it "includes the title in the calculation" do
      c.name = "a horse is a horse of course of course"
      c.data_width.should == 38
    end
  end

  describe "#width" do
    context "when default width is specified" do
      it "uses the default width" do
        c.default_width = 10
        c.stub(:data_width => 15)
        c.stub(:max_width => 20)
        c.width.should == 10
      end

      it "isn't limited by the config width" do
        c.default_width = 40
        c.stub(:data_width => 50)
        c.stub(:max_width => 20)
        c.width.should == 40
      end
    end

    context "When default width is not specified" do
      it "uses the data width" do
        c.default_width = nil
        c.stub(:data_width => 10)
        c.stub(:max_width => 20)
        c.width.should == 10
      end

      it "is limited by the config width" do
        c.default_width = nil
        c.stub(:data_width => 30)
        c.stub(:max_width => 20)
        c.width.should == 20
      end
    end
  end
end