File: table_print_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 (113 lines) | stat: -rw-r--r-- 3,219 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require 'spec_helper'
require 'table_print'

include TablePrint

describe TablePrint::Printer do
  before(:each) do
    Sandbox.cleanup!
  end

  describe "printing an empty array" do
    it "returns the string 'no data'" do
      p = Printer.new([])
      p.table_print.should == 'No data.'
    end
  end

  describe "printing an object where there are only association columns with no data" do
    it "returns the string 'no data'" do
      Sandbox.add_class("Blog")
      Sandbox.add_attributes("Blog", :author)
      p = Printer.new(Sandbox::Blog.new, "author.name")
      p.table_print.should == 'No data.'
    end
  end

  describe "message" do
    it "defaults to the time the print took" do
      Printer.new([]).message.should be_a Numeric
    end

    it "shows a warning if the printed objects have config" do
      Sandbox.add_class("User")

      tp.set Sandbox::User, :id, :email
      p = Printer.new(Sandbox::User.new)
      p.message.should == "Printed with config"
    end
  end

  describe "#columns" do

    it "pulls the column names off the data object" do
      Sandbox.add_class("Post")
      Sandbox.add_attributes("Post", :title)

      p = Printer.new(Sandbox::Post.new)
      cols = p.send(:columns)
      cols.length.should == 1
      cols.first.name.should == 'title'
    end

    it 'pull the column names off of the array of Structs' do
      struct = Struct.new(:name, :surname)
      data = [struct.new("User 1", "Familyname 1"), struct.new("User 2", "Familyname 2")]
      p = Printer.new(data)
      cols = p.send(:columns)
      cols.length.should == 2
      cols.collect(&:name).sort.should == ['name', 'surname']
    end
    
    context 'when keys are symbols' do
      it "pulls the column names off the array of hashes" do
        data = [{:name => "User 1",
                  :surname => "Familyname 1"
                },
                {:name => "User 2",
                  :surname => "Familyname 2"}]

        p = Printer.new(data)
        cols = p.send(:columns)
        cols.length.should == 2
        cols.collect(&:name).sort.should == ['name', 'surname']
      end
    end

    context 'when keys are strings' do
      it "pulls the column names off the array of hashes" do
        data = [{'name' => "User 1",
                  'surname' => "Familyname 1"
                },
                {'name' => "User 2",
                  'surname' => "Familyname 2"}]

        p = Printer.new(data)
        cols = p.send(:columns)
        cols.length.should == 2
        cols.collect(&:name).sort.should == ['name', 'surname']
      end
    end

    it "pulls out excepted columns" do
      Sandbox.add_class("Post")
      Sandbox.add_attributes("Post", :title, :author)

      p = Printer.new(Sandbox::Post.new, :except => :title)
      cols = p.send(:columns)
      cols.length.should == 1
      cols.first.name.should == 'author'
    end

    it "adds included columns" do
      Sandbox.add_class("Post")
      Sandbox.add_attributes("Post", :title)

      p = Printer.new(Sandbox::Post.new, :include => :author)
      cols = p.send(:columns)
      cols.length.should == 2
      cols.first.name.should == 'title'
      cols.last.name.should == 'author'
    end
  end
end