table_print 1.5.7 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +33 -0
- data/LICENSE.txt +1 -1
- data/README.md +291 -0
- data/Rakefile +6 -40
- data/bin/console +237 -0
- data/features/adding_columns.feature +19 -4
- data/features/configuring_output.feature +59 -0
- data/features/printing_array.feature +27 -0
- data/features/sensible_defaults.feature +2 -2
- data/features/support/step_definitions/before.rb +4 -0
- data/features/support/step_definitions/steps.rb +22 -1
- data/lib/table_print/column.rb +7 -2
- data/lib/table_print/config.rb +50 -3
- data/lib/table_print/config_resolver.rb +19 -8
- data/lib/table_print/fingerprinter.rb +1 -1
- data/lib/table_print/formatter.rb +1 -0
- data/lib/table_print/printable.rb +9 -0
- data/lib/table_print/row_group.rb +5 -2
- data/lib/table_print/version.rb +1 -1
- data/lib/table_print.rb +23 -3
- data/spec/column_spec.rb +36 -21
- data/spec/config_resolver_spec.rb +171 -64
- data/spec/config_spec.rb +87 -10
- data/spec/data_shapes_spec.rb +455 -0
- data/spec/fingerprinter_spec.rb +64 -42
- data/spec/formatter_spec.rb +15 -9
- data/spec/hash_extensions_spec.rb +2 -2
- data/spec/printable_spec.rb +16 -6
- data/spec/returnable_spec.rb +5 -5
- data/spec/row_group_spec.rb +73 -55
- data/spec/spec_helper.rb +11 -5
- data/spec/table_print_spec.rb +131 -17
- data/table_print.gemspec +6 -6
- metadata +46 -38
- data/.document +0 -5
- data/.travis.yml +0 -9
- data/README.rdoc +0 -209
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
# One example per kind of input, each showing the data and the table it prints.
|
|
4
|
+
#
|
|
5
|
+
# The rest of the suite covers behaviour thoroughly and input types only in passing.
|
|
6
|
+
#
|
|
7
|
+
# The last group pins types that print badly today, so that fixing one fails
|
|
8
|
+
# here loudly instead of changing behaviour nobody notices.
|
|
9
|
+
describe "printing" do
|
|
10
|
+
def printed(data, *options)
|
|
11
|
+
TablePrint::Printer.new(data, options).table_print.to_s
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Cells are padded, so a table's lines carry trailing spaces - invisible in a
|
|
15
|
+
# heredoc, stripped by most editors on save, and present if you paste real
|
|
16
|
+
# output. Compare line by line without them, on both sides.
|
|
17
|
+
RSpec::Matchers.define :print_as do |expected|
|
|
18
|
+
def lines(table)
|
|
19
|
+
table.to_s.lines.collect(&:rstrip)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
match { |actual| lines(actual) == lines(expected) }
|
|
23
|
+
|
|
24
|
+
failure_message do |actual|
|
|
25
|
+
"expected:\n#{expected}\n got:\n#{actual}"
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# The first row is taken as representative of the whole set, whatever the data
|
|
30
|
+
# is. Every other group below inherits this rule.
|
|
31
|
+
describe "choosing columns" do
|
|
32
|
+
it "takes them from the first row" do
|
|
33
|
+
expect(printed([{ a: 1 }, { b: 2 }])).to print_as <<~TABLE
|
|
34
|
+
A
|
|
35
|
+
--------------
|
|
36
|
+
1
|
|
37
|
+
Method Missing
|
|
38
|
+
TABLE
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "shows nothing for cells past the first row's width" do
|
|
42
|
+
expect(printed([%w[a b], %w[c], %w[d e f]])).to print_as <<~TABLE
|
|
43
|
+
0 | 1
|
|
44
|
+
--|---------------
|
|
45
|
+
a | b
|
|
46
|
+
c | Method Missing
|
|
47
|
+
d | e
|
|
48
|
+
TABLE
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
describe "scalars" do
|
|
53
|
+
it "puts strings in a single VALUE column, untouched" do
|
|
54
|
+
data = ["hello".dup, "world".dup]
|
|
55
|
+
|
|
56
|
+
expect(printed(data)).to print_as <<~TABLE
|
|
57
|
+
VALUE
|
|
58
|
+
-----
|
|
59
|
+
hello
|
|
60
|
+
world
|
|
61
|
+
TABLE
|
|
62
|
+
|
|
63
|
+
expect(data).to eq %w[hello world] # tp used to call String#clear on these
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "treats symbols and numbers the same way" do
|
|
67
|
+
expect(printed([:sym, 42, 3.14])).to print_as <<~TABLE
|
|
68
|
+
VALUE
|
|
69
|
+
-----
|
|
70
|
+
sym
|
|
71
|
+
42
|
|
72
|
+
3.14
|
|
73
|
+
TABLE
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it "treats a range as one value, not as the list it could expand into" do
|
|
77
|
+
expect(printed([(1..3), (4..6)])).to print_as <<~TABLE
|
|
78
|
+
VALUE
|
|
79
|
+
-----
|
|
80
|
+
1..3
|
|
81
|
+
4..6
|
|
82
|
+
TABLE
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it "still expands a range handed over as the whole data set" do
|
|
86
|
+
# a collection at the top level is rows; a range inside one is a value
|
|
87
|
+
expect(printed(1..3)).to print_as <<~TABLE
|
|
88
|
+
VALUE
|
|
89
|
+
-----
|
|
90
|
+
1
|
|
91
|
+
2
|
|
92
|
+
3
|
|
93
|
+
TABLE
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it "treats booleans the same way" do
|
|
97
|
+
expect(printed([true, false])).to print_as <<~TABLE
|
|
98
|
+
VALUE
|
|
99
|
+
-----
|
|
100
|
+
true
|
|
101
|
+
false
|
|
102
|
+
TABLE
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
describe "hashes" do
|
|
107
|
+
it "prints one bare hash as a single row" do
|
|
108
|
+
expect(printed("host" => "db.internal", "user" => "app")).to print_as <<~TABLE
|
|
109
|
+
HOST | USER
|
|
110
|
+
------------|-----
|
|
111
|
+
db.internal | app
|
|
112
|
+
TABLE
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it "uses the keys as columns, blank for a nil" do
|
|
116
|
+
expect(printed([{ a: 1, b: "two" }, { a: 3, b: nil }])).to print_as <<~TABLE
|
|
117
|
+
A | B
|
|
118
|
+
--|----
|
|
119
|
+
1 | two
|
|
120
|
+
3 |
|
|
121
|
+
TABLE
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
it "prints a range in a cell as itself" do
|
|
125
|
+
expect(printed([{ "name" => "alice", "span" => (1..5) }])).to print_as <<~TABLE
|
|
126
|
+
NAME | SPAN
|
|
127
|
+
------|-----
|
|
128
|
+
alice | 1..5
|
|
129
|
+
TABLE
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
it "finds values behind keys that are neither strings nor symbols" do
|
|
133
|
+
expect(printed([{ 1 => "one", 2 => "two" }])).to print_as <<~TABLE
|
|
134
|
+
1 | 2
|
|
135
|
+
----|----
|
|
136
|
+
one | two
|
|
137
|
+
TABLE
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
it "hands a lambda column the row it was given, subclass and all" do
|
|
141
|
+
indifferent = Class.new(Hash) do
|
|
142
|
+
def [](key)
|
|
143
|
+
super(key.to_s)
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
row = indifferent.new
|
|
147
|
+
row["author"] = "ryan"
|
|
148
|
+
|
|
149
|
+
# the row keeps its own lookup, so :author finds the "author" key
|
|
150
|
+
expect(printed([row], include: { two: ->(r) { r[:author] * 2 } })).to print_as <<~TABLE
|
|
151
|
+
AUTHOR | TWO
|
|
152
|
+
-------|---------
|
|
153
|
+
ryan | ryanryan
|
|
154
|
+
TABLE
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
describe "arrays of arrays" do
|
|
159
|
+
it "prints a grid, with each position as a column" do
|
|
160
|
+
expect(printed([[1, "alice"], [2, "bob"]])).to print_as <<~TABLE
|
|
161
|
+
0 | 1
|
|
162
|
+
--|------
|
|
163
|
+
1 | alice
|
|
164
|
+
2 | bob
|
|
165
|
+
TABLE
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
it "lets you address a column by its index" do
|
|
169
|
+
expect(printed([[1, "alice"], [2, "bob"]], "1")).to print_as <<~TABLE
|
|
170
|
+
1
|
|
171
|
+
-----
|
|
172
|
+
alice
|
|
173
|
+
bob
|
|
174
|
+
TABLE
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
describe "structs, Data and OpenStruct" do
|
|
179
|
+
it "uses a struct's members as columns" do
|
|
180
|
+
expect(printed([Struct.new(:title, :author).new("First", "ryan")])).to print_as <<~TABLE
|
|
181
|
+
TITLE | AUTHOR
|
|
182
|
+
------|-------
|
|
183
|
+
First | ryan
|
|
184
|
+
TABLE
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
it "uses a Data object's members" do
|
|
188
|
+
point = Data.define(:x, :y)
|
|
189
|
+
|
|
190
|
+
# columns given explicitly: the default order comes from #methods, which
|
|
191
|
+
# isn't the same on every ruby
|
|
192
|
+
expect(printed([point.new(x: 1, y: 2)], "x", "y")).to print_as <<~TABLE
|
|
193
|
+
X | Y
|
|
194
|
+
--|--
|
|
195
|
+
1 | 2
|
|
196
|
+
TABLE
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
it "uses an OpenStruct's attributes rather than its methods" do
|
|
200
|
+
expect(printed([OpenStruct.new(name: "alice", age: 30)])).to print_as <<~TABLE
|
|
201
|
+
NAME | AGE
|
|
202
|
+
------|----
|
|
203
|
+
alice | 30
|
|
204
|
+
TABLE
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
describe "objects" do
|
|
209
|
+
it "finds the methods defined on the object's own class" do
|
|
210
|
+
post = Class.new do
|
|
211
|
+
def headline
|
|
212
|
+
"once upon a time"
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
expect(printed([post.new])).to print_as <<~TABLE
|
|
217
|
+
HEADLINE
|
|
218
|
+
----------------
|
|
219
|
+
once upon a time
|
|
220
|
+
TABLE
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# Only the object's own class counts, no methods from a superclass or a mixin
|
|
224
|
+
it "leaves out attributes inherited from a superclass" do
|
|
225
|
+
base = Class.new { attr_accessor :title }
|
|
226
|
+
post = Class.new(base) { attr_accessor :subtitle }.new
|
|
227
|
+
post.title = "First"
|
|
228
|
+
post.subtitle = "the worst"
|
|
229
|
+
|
|
230
|
+
expect(printed([post])).to print_as <<~TABLE
|
|
231
|
+
SUBTITLE
|
|
232
|
+
---------
|
|
233
|
+
the worst
|
|
234
|
+
TABLE
|
|
235
|
+
|
|
236
|
+
expect(printed([post], include: :title)).to print_as <<~TABLE
|
|
237
|
+
SUBTITLE | TITLE
|
|
238
|
+
----------|------
|
|
239
|
+
the worst | First
|
|
240
|
+
TABLE
|
|
241
|
+
|
|
242
|
+
# an object whose attributes are all inherited has nothing left to show
|
|
243
|
+
expect(printed([Class.new(base).new])).to print_as "No data."
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
it "leaves out attributes mixed in from a module" do
|
|
247
|
+
titled = Module.new { attr_accessor :title }
|
|
248
|
+
post = Class.new do
|
|
249
|
+
include titled
|
|
250
|
+
attr_accessor :subtitle
|
|
251
|
+
end.new
|
|
252
|
+
post.title = "First"
|
|
253
|
+
post.subtitle = "the worst"
|
|
254
|
+
|
|
255
|
+
expect(printed([post])).to print_as <<~TABLE
|
|
256
|
+
SUBTITLE
|
|
257
|
+
---------
|
|
258
|
+
the worst
|
|
259
|
+
TABLE
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
it "follows a dotted path, printing each parent once" do
|
|
263
|
+
review = Struct.new(:stars)
|
|
264
|
+
book = Struct.new(:title, :reviews)
|
|
265
|
+
author = Struct.new(:name, :books)
|
|
266
|
+
authors = [
|
|
267
|
+
author.new("alice", [book.new("Book A", [review.new(5), review.new(3)]), book.new("Book B", [])]),
|
|
268
|
+
author.new("bob", [book.new("Book C", [review.new(1)])]),
|
|
269
|
+
author.new("carol", [])
|
|
270
|
+
]
|
|
271
|
+
|
|
272
|
+
expect(printed(authors, "name", "books.title", "books.reviews.stars")).to print_as <<~TABLE
|
|
273
|
+
NAME | BOOKS.TITLE | BOOKS.REVIEWS.STARS
|
|
274
|
+
------|-------------|--------------------
|
|
275
|
+
alice | Book A | 5
|
|
276
|
+
| | 3
|
|
277
|
+
| Book B |
|
|
278
|
+
bob | Book C | 1
|
|
279
|
+
carol | |
|
|
280
|
+
TABLE
|
|
281
|
+
end
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
describe "ORM-shaped objects" do
|
|
285
|
+
it "reads column names from an ActiveRecord-style class.columns" do
|
|
286
|
+
post = Class.new do
|
|
287
|
+
def self.columns
|
|
288
|
+
[Struct.new(:name).new("id"), Struct.new(:name).new("title")]
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def id
|
|
292
|
+
1
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def title
|
|
296
|
+
"from .columns"
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def not_a_column
|
|
300
|
+
"hidden"
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
expect(printed([post.new])).to print_as <<~TABLE
|
|
305
|
+
ID | TITLE
|
|
306
|
+
---|--------------
|
|
307
|
+
1 | from .columns
|
|
308
|
+
TABLE
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
it "reads a Sequel-style class.columns of symbols" do
|
|
312
|
+
post = Class.new do
|
|
313
|
+
def self.columns
|
|
314
|
+
[:id, :title]
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
def id
|
|
318
|
+
2
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
def title
|
|
322
|
+
"from .columns"
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
expect(printed([post.new])).to print_as <<~TABLE
|
|
327
|
+
ID | TITLE
|
|
328
|
+
---|--------------
|
|
329
|
+
2 | from .columns
|
|
330
|
+
TABLE
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
it "reads a Mongoid-style #fields hash" do
|
|
334
|
+
post = Class.new do
|
|
335
|
+
def fields
|
|
336
|
+
{ "id" => nil, "title" => nil }
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
def id
|
|
340
|
+
3
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
def title
|
|
344
|
+
"from #fields"
|
|
345
|
+
end
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
expect(printed([post.new])).to print_as <<~TABLE
|
|
349
|
+
ID | TITLE
|
|
350
|
+
---|-------------
|
|
351
|
+
3 | from #fields
|
|
352
|
+
TABLE
|
|
353
|
+
end
|
|
354
|
+
end
|
|
355
|
+
|
|
356
|
+
# Everything below prints badly. The output is pinned so that a fix shows up
|
|
357
|
+
# here as a failure, rather than quietly changing what users see.
|
|
358
|
+
describe "types that don't work yet" do
|
|
359
|
+
it "can't follow a dotted path into a hash" do
|
|
360
|
+
expect { printed([{ a: 1, b: { c: 2 } }], "a", "b.c") }.to raise_error NoMethodError
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
it "drops a column entirely when the association is nil" do
|
|
364
|
+
author = Struct.new(:name, :books)
|
|
365
|
+
|
|
366
|
+
expect(printed([author.new("alice", nil)], "name", "books.title")).to print_as <<~TABLE
|
|
367
|
+
NAME
|
|
368
|
+
-----
|
|
369
|
+
alice
|
|
370
|
+
TABLE
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
it "unwraps an object that defines to_a, losing its attributes" do
|
|
374
|
+
collection = Class.new do
|
|
375
|
+
include Enumerable
|
|
376
|
+
attr_accessor :name
|
|
377
|
+
|
|
378
|
+
def initialize(name, items)
|
|
379
|
+
@name, @items = name, items
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
def each(&block)
|
|
383
|
+
@items.each(&block)
|
|
384
|
+
end
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
expect(printed(collection.new("mine", [1, 2]))).to print_as <<~TABLE
|
|
388
|
+
VALUE
|
|
389
|
+
-----
|
|
390
|
+
1
|
|
391
|
+
2
|
|
392
|
+
TABLE
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
it "calls every zero-argument method, destructive ones included" do
|
|
396
|
+
stateful = Class.new do
|
|
397
|
+
def initialize(items)
|
|
398
|
+
@items = items
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
def items
|
|
402
|
+
@items
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
def size
|
|
406
|
+
@items.size
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
def clear # no bang, so tp has no reason to avoid it
|
|
410
|
+
@items.clear
|
|
411
|
+
end
|
|
412
|
+
end
|
|
413
|
+
items = %w[a b]
|
|
414
|
+
|
|
415
|
+
# items reads as [] even though it's measured before clear runs: the cell
|
|
416
|
+
# holds the same array that clear empties
|
|
417
|
+
expect(printed([stateful.new(items)], "items", "clear", "size")).to print_as <<~TABLE
|
|
418
|
+
ITEMS | CLEAR | SIZE
|
|
419
|
+
------|-------|-----
|
|
420
|
+
[] | [] | 0
|
|
421
|
+
TABLE
|
|
422
|
+
|
|
423
|
+
expect(items).to eq []
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
it "lets a single raising reader take down the whole print" do
|
|
427
|
+
exploder = Class.new do
|
|
428
|
+
def title
|
|
429
|
+
"ok"
|
|
430
|
+
end
|
|
431
|
+
|
|
432
|
+
def boom
|
|
433
|
+
raise ArgumentError, "do not call me"
|
|
434
|
+
end
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
expect { printed([exploder.new], "title", "boom") }.to raise_error ArgumentError
|
|
438
|
+
end
|
|
439
|
+
|
|
440
|
+
it "chokes on an object whose respond_to_missing? says yes to everything" do
|
|
441
|
+
proxy = Class.new do
|
|
442
|
+
def respond_to_missing?(*)
|
|
443
|
+
true
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
def method_missing(name, *)
|
|
447
|
+
"delegated #{name}"
|
|
448
|
+
end
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
# Array(data) asks for to_ary, gets a String, and gives up
|
|
452
|
+
expect { printed([proxy.new]) }.to raise_error TypeError
|
|
453
|
+
end
|
|
454
|
+
end
|
|
455
|
+
end
|
data/spec/fingerprinter_spec.rb
CHANGED
|
@@ -15,52 +15,52 @@ describe Fingerprinter do
|
|
|
15
15
|
describe "#lift" do
|
|
16
16
|
it "turns a single level of columns into a single row" do
|
|
17
17
|
rows = Fingerprinter.new.lift([Column.new(:name => "name")], OpenStruct.new(:name => "dale carnegie"))
|
|
18
|
-
rows.length.
|
|
18
|
+
expect(rows.length).to eq 1
|
|
19
19
|
row = rows.first
|
|
20
|
-
row.children.length.
|
|
21
|
-
row.cells.
|
|
20
|
+
expect(row.children.length).to eq 0
|
|
21
|
+
expect(row.cells).to eq({'name' => "dale carnegie"})
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
it "uses the display_method to get the data" do
|
|
25
25
|
rows = Fingerprinter.new.lift([Column.new(:name => "name of work", :display_method => "title")], OpenStruct.new(:title => "of mice and men"))
|
|
26
|
-
rows.length.
|
|
26
|
+
expect(rows.length).to eq 1
|
|
27
27
|
row = rows.first
|
|
28
|
-
row.children.length.
|
|
29
|
-
row.cells.
|
|
28
|
+
expect(row.children.length).to eq 0
|
|
29
|
+
expect(row.cells).to eq({'name of work' => "of mice and men"})
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
it "turns multiple levels of columns into multiple rows" do
|
|
33
33
|
rows = Fingerprinter.new.lift([Column.new(:name => "name"), Column.new(:name => "books.title")], OpenStruct.new(:name => "dale carnegie", :books => [OpenStruct.new(:title => "how to make influences")]))
|
|
34
|
-
rows.length.
|
|
34
|
+
expect(rows.length).to eq 1
|
|
35
35
|
row = rows.first
|
|
36
|
-
row.children.length.
|
|
37
|
-
row.cells.
|
|
38
|
-
row.children.first.children.first.cells.
|
|
36
|
+
expect(row.children.length).to eq 1
|
|
37
|
+
expect(row.cells).to eq({'name' => "dale carnegie"})
|
|
38
|
+
expect(row.children.first.children.first.cells).to eq({'books.title' => "how to make influences"})
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
it "doesn't choke if an association doesn't exist" do
|
|
42
42
|
rows = Fingerprinter.new.lift([Column.new(:name => "name"), Column.new(:name => "books.title")], OpenStruct.new(:name => "dale carnegie", :books => []))
|
|
43
43
|
|
|
44
|
-
rows.length.
|
|
44
|
+
expect(rows.length).to eq 1
|
|
45
45
|
|
|
46
46
|
row = rows.first
|
|
47
|
-
row.children.length.
|
|
47
|
+
expect(row.children.length).to eq 0
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
it "allows a lambda as the display_method" do
|
|
51
51
|
rows = Fingerprinter.new.lift([Column.new(:name => "name", :display_method => lambda { |row| row.name.gsub(/[aeiou]/, "") })], OpenStruct.new(:name => "dale carnegie"))
|
|
52
|
-
rows.length.
|
|
52
|
+
expect(rows.length).to eq 1
|
|
53
53
|
row = rows.first
|
|
54
|
-
row.children.length.
|
|
55
|
-
row.cells.
|
|
54
|
+
expect(row.children.length).to eq 0
|
|
55
|
+
expect(row.cells).to eq({'name' => "dl crng"})
|
|
56
56
|
end
|
|
57
57
|
|
|
58
58
|
it "doesn't puke if a lambda returns nil" do
|
|
59
59
|
rows = Fingerprinter.new.lift([Column.new(:name => "name", :display_method => lambda { |row| nil })], OpenStruct.new(:name => "dale carnegie"))
|
|
60
|
-
rows.length.
|
|
60
|
+
expect(rows.length).to eq 1
|
|
61
61
|
row = rows.first
|
|
62
|
-
row.children.length.
|
|
63
|
-
row.cells.
|
|
62
|
+
expect(row.children.length).to eq 0
|
|
63
|
+
expect(row.cells).to eq({'name' => nil})
|
|
64
64
|
end
|
|
65
65
|
end
|
|
66
66
|
|
|
@@ -69,25 +69,25 @@ describe Fingerprinter do
|
|
|
69
69
|
f = Fingerprinter.new
|
|
70
70
|
f.instance_variable_set("@column_names_by_display_method", {"name" => "name"})
|
|
71
71
|
rows = f.hash_to_rows("", {'name' => {}}, OpenStruct.new(:name => "dale carnegie"))
|
|
72
|
-
rows.length.
|
|
72
|
+
expect(rows.length).to eq 1
|
|
73
73
|
row = rows.first
|
|
74
|
-
row.children.length.
|
|
75
|
-
row.cells.
|
|
74
|
+
expect(row.children.length).to eq 0
|
|
75
|
+
expect(row.cells).to eq({'name' => 'dale carnegie'})
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
it 'recurses for subsequent levels of hash' do
|
|
79
79
|
f = Fingerprinter.new
|
|
80
80
|
f.instance_variable_set("@column_names_by_display_method", {"name" => "name", "books.title" => "books.title"})
|
|
81
81
|
rows = f.hash_to_rows("", {'name' => {}, 'books' => {'title' => {}}}, [OpenStruct.new(:name => 'dale carnegie', :books => [OpenStruct.new(:title => "hallmark")])])
|
|
82
|
-
rows.length.
|
|
82
|
+
expect(rows.length).to eq 1
|
|
83
83
|
|
|
84
84
|
top_row = rows.first
|
|
85
|
-
top_row.cells.
|
|
86
|
-
top_row.children.length.
|
|
87
|
-
top_row.children.first.child_count.
|
|
85
|
+
expect(top_row.cells).to eq({'name' => 'dale carnegie'})
|
|
86
|
+
expect(top_row.children.length).to eq 1
|
|
87
|
+
expect(top_row.children.first.child_count).to eq 1
|
|
88
88
|
|
|
89
89
|
bottom_row = top_row.children.first.children.first
|
|
90
|
-
bottom_row.cells.
|
|
90
|
+
expect(bottom_row.cells).to eq({'books.title' => 'hallmark'})
|
|
91
91
|
end
|
|
92
92
|
end
|
|
93
93
|
|
|
@@ -96,21 +96,21 @@ describe Fingerprinter do
|
|
|
96
96
|
f = Fingerprinter.new
|
|
97
97
|
f.instance_variable_set("@column_names_by_display_method", {"title" => "title", "author" => "author"})
|
|
98
98
|
row = f.populate_row("", {'title' => {}, 'author' => {}, 'publisher' => {'address' => {}}}, OpenStruct.new(:title => "foobar", :author => "bobby"))
|
|
99
|
-
row.cells.
|
|
99
|
+
expect(row.cells).to eq({'title' => "foobar", 'author' => 'bobby'})
|
|
100
100
|
end
|
|
101
101
|
|
|
102
102
|
it "uses the provided prefix to name the cells" do
|
|
103
103
|
f = Fingerprinter.new
|
|
104
104
|
f.instance_variable_set("@column_names_by_display_method", {"bar.title" => "bar.title", "bar.author" => "bar.author"})
|
|
105
105
|
row = f.populate_row("bar", {'title' => {}, 'author' => {}, 'publisher' => {'address' => {}}}, OpenStruct.new(:title => "foobar", :author => "bobby"))
|
|
106
|
-
row.cells.
|
|
106
|
+
expect(row.cells).to eq({'bar.title' => "foobar", 'bar.author' => 'bobby'})
|
|
107
107
|
end
|
|
108
108
|
|
|
109
109
|
it "uses the column name as the cell name but uses the display method to get the value" do
|
|
110
110
|
f = Fingerprinter.new
|
|
111
111
|
f.instance_variable_set("@column_names_by_display_method", {"bar.title" => "title", "bar.author" => "bar.author"})
|
|
112
112
|
row = f.populate_row("bar", {'title' => {}, 'author' => {}, 'publisher' => {'address' => {}}}, OpenStruct.new(:title => "foobar", :author => "bobby"))
|
|
113
|
-
row.cells.
|
|
113
|
+
expect(row.cells).to eq({'title' => "foobar", 'bar.author' => 'bobby'})
|
|
114
114
|
end
|
|
115
115
|
|
|
116
116
|
context 'using a hash as input_data' do
|
|
@@ -119,7 +119,7 @@ describe Fingerprinter do
|
|
|
119
119
|
f.instance_variable_set('@column_names_by_display_method', {'title' => 'title', 'author' => 'author'})
|
|
120
120
|
input_data = {:title => 'foobar', :author => 'bobby'}
|
|
121
121
|
row = f.populate_row('', {'title' => {}, 'author' => {}, 'publisher' => {'address' => {}}}, input_data)
|
|
122
|
-
row.cells.
|
|
122
|
+
expect(row.cells).to eq({'title' => 'foobar', 'author' => 'bobby'})
|
|
123
123
|
end
|
|
124
124
|
|
|
125
125
|
it "fills a row by calling methods on the target object" do
|
|
@@ -127,7 +127,7 @@ describe Fingerprinter do
|
|
|
127
127
|
f.instance_variable_set('@column_names_by_display_method', {'title' => 'title', 'author' => 'author'})
|
|
128
128
|
input_data = {'title' => 'foobar', 'author' => 'bobby'}
|
|
129
129
|
row = f.populate_row('', {'title' => {}, 'author' => {}, 'publisher' => {'address' => {}}}, input_data)
|
|
130
|
-
row.cells.
|
|
130
|
+
expect(row.cells).to eq({'title' => 'foobar', 'author' => 'bobby'})
|
|
131
131
|
end
|
|
132
132
|
end
|
|
133
133
|
|
|
@@ -136,7 +136,29 @@ describe Fingerprinter do
|
|
|
136
136
|
f = Fingerprinter.new
|
|
137
137
|
f.instance_variable_set('@column_names_by_display_method', {'foo' => 'foo'})
|
|
138
138
|
row = f.populate_row('', {'foo' => {}}, Hash.new)
|
|
139
|
-
row.cells.
|
|
139
|
+
expect(row.cells).to eq({'foo' => 'Method Missing'})
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
it "puts nil in the cell when unreachable_value is nil" do
|
|
143
|
+
TablePrint::Config.set(:unreachable_value, [nil])
|
|
144
|
+
|
|
145
|
+
f = Fingerprinter.new
|
|
146
|
+
f.instance_variable_set('@column_names_by_display_method', {'foo' => 'foo'})
|
|
147
|
+
row = f.populate_row('', {'foo' => {}}, Hash.new)
|
|
148
|
+
expect(row.cells).to eq({'foo' => nil})
|
|
149
|
+
ensure
|
|
150
|
+
TablePrint::Config.clear(:unreachable_value)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
it "uses whatever the unreachable_value config says" do
|
|
154
|
+
TablePrint::Config.set(:unreachable_value, ["-"])
|
|
155
|
+
|
|
156
|
+
f = Fingerprinter.new
|
|
157
|
+
f.instance_variable_set('@column_names_by_display_method', {'foo' => 'foo'})
|
|
158
|
+
row = f.populate_row('', {'foo' => {}}, Hash.new)
|
|
159
|
+
expect(row.cells).to eq({'foo' => '-'})
|
|
160
|
+
ensure
|
|
161
|
+
TablePrint::Config.clear(:unreachable_value)
|
|
140
162
|
end
|
|
141
163
|
end
|
|
142
164
|
end
|
|
@@ -146,38 +168,38 @@ describe Fingerprinter do
|
|
|
146
168
|
f = Fingerprinter.new
|
|
147
169
|
books = []
|
|
148
170
|
|
|
149
|
-
f.
|
|
171
|
+
expect(f).to receive(:hash_to_rows).with("author.books", {'title' => {}}, books).and_return([])
|
|
150
172
|
groups = f.create_child_group("author", {'books' => {'title' => {}}}, OpenStruct.new(:name => "bobby", :books => books))
|
|
151
|
-
groups.length.
|
|
152
|
-
groups.first.
|
|
173
|
+
expect(groups.length).to eq 1
|
|
174
|
+
expect(groups.first).to be_a TablePrint::RowGroup
|
|
153
175
|
end
|
|
154
176
|
end
|
|
155
177
|
|
|
156
178
|
describe "#columns_to_handle" do
|
|
157
179
|
it "returns hash keys that have an empty hash as the value" do
|
|
158
|
-
Fingerprinter.new.handleable_columns({'name' => {}, 'books' => {'title' => {}}}).
|
|
180
|
+
expect(Fingerprinter.new.handleable_columns({'name' => {}, 'books' => {'title' => {}}})).to eq(["name"])
|
|
159
181
|
end
|
|
160
182
|
end
|
|
161
183
|
|
|
162
184
|
describe "#columns_to_pass" do
|
|
163
185
|
it "returns hash keys that do not have an empty hash as the value" do
|
|
164
|
-
Fingerprinter.new.passable_columns({'name' => {}, 'books' => {'title' => {}}}).
|
|
186
|
+
expect(Fingerprinter.new.passable_columns({'name' => {}, 'books' => {'title' => {}}})).to eq(["books"])
|
|
165
187
|
end
|
|
166
188
|
end
|
|
167
189
|
|
|
168
190
|
describe "#chain_to_nested_hash" do
|
|
169
191
|
it "turns a list of methods into a nested hash" do
|
|
170
|
-
Fingerprinter.new.display_method_to_nested_hash("books").
|
|
171
|
-
Fingerprinter.new.display_method_to_nested_hash("reviews.user").
|
|
192
|
+
expect(Fingerprinter.new.display_method_to_nested_hash("books")).to eq({'books' => {}})
|
|
193
|
+
expect(Fingerprinter.new.display_method_to_nested_hash("reviews.user")).to eq({'reviews' => {'user' => {}}})
|
|
172
194
|
end
|
|
173
195
|
end
|
|
174
196
|
|
|
175
197
|
describe "#columns_to_nested_hash" do
|
|
176
198
|
it "splits the column names into a nested hash" do
|
|
177
|
-
Fingerprinter.new.display_methods_to_nested_hash(["books.name"]).
|
|
178
|
-
Fingerprinter.new.display_methods_to_nested_hash(
|
|
199
|
+
expect(Fingerprinter.new.display_methods_to_nested_hash(["books.name"])).to eq({'books' => {'name' => {}}})
|
|
200
|
+
expect(Fingerprinter.new.display_methods_to_nested_hash(
|
|
179
201
|
["books.name", "books.publisher", "reviews.rating", "reviews.user.email", "reviews.user.id"]
|
|
180
|
-
).
|
|
202
|
+
)).to eq({'books' => {'name' => {}, 'publisher' => {}}, 'reviews' => {'rating' => {}, 'user' => {'email' => {}, 'id' => {}}}})
|
|
181
203
|
end
|
|
182
204
|
end
|
|
183
205
|
end
|