Skip to content

Commit

Permalink
Merge pull request #18 from akabiru/chore/touch-up-tests
Browse files Browse the repository at this point in the history
Touch Up Tests
  • Loading branch information
akabiru authored Jan 4, 2019
2 parents a74a44b + 3ad0241 commit 1ff74a8
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 23 deletions.
53 changes: 30 additions & 23 deletions lib/fakerbot/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,52 @@

module FakerBot
class Renderer
def self.call(hash, options, output)
new(hash, options, output).call
attr_reader :crayon, :hash, :options, :output, :pager

def self.call(*args)
new(*args).call
end

def initialize(hash, options, output)
@hash = hash
@options = options
@options = options.deep_symbolize_keys
@output = output
@crayon = Pastel.new(enabled: output.tty?)
@pager = TTY::Pager.new(command: 'less -R')
@screen = TTY::Screen
@tree = TTY::Tree
end

def call
data_tree = tree.new(build_tree)
view = data_tree.render
if gt_screen_height?(data_tree)
output.tty? ? pager.page(view) : output.puts(view)
if paginable?
pager.page(render)
else
output.puts view
output.puts(render)
end
end

private
def render
tree.render
end

def tree
@tree ||= TTY::Tree.new(build_tree)
end

def paginable?
gt_screen_height? && output.tty?
end

attr_reader :crayon, :hash, :options, :output, :pager, :screen, :tree
def gt_screen_height?
tree.nodes.size > TTY::Screen.height
end

private

def build_tree
data_tree = hash.reduce({}) do |h, (faker, methods)|
result = hash.reduce({}) do |h, (faker, methods)|
h.merge! node(faker, methods&.sort)
end
data_tree.sort_by(&:to_s).to_h

result.sort_by(&:to_s).to_h
end

def node(const, methods)
Expand All @@ -49,21 +62,15 @@ def node(const, methods)
end

def leaf(const, methods)
(methods || []).map do |m|
crayon.cyan(*leaf_args(m, const))
end
(methods || []).map { |m| crayon.cyan(*leaf_args(m, const)) }
end

def leaf_args(method, const)
[method.to_s].tap { |arr| verbose_output(method, const, arr) if verbose? }
end

def gt_screen_height?(data_tree)
data_tree.nodes.size > screen.height
end

def verbose?
options[:verbose]
options[:verbose] == true
end

def verbose_output(method, const, arr)
Expand All @@ -73,7 +80,7 @@ def verbose_output(method, const, arr)

def faker_method(method, const)
[const.public_send(method), ensure_method_is_supported(method, const)]
rescue ArgumentError
rescue ArgumentError => _exception
['N/A', '']
end

Expand Down
File renamed without changes.
File renamed without changes.
171 changes: 171 additions & 0 deletions spec/fakerbot/renderer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
require 'spec_helper'

module MockFaker
class Foo
def self.bar
'bar'
end
end
end

RSpec.describe FakerBot::Renderer do
describe '#call' do
# FIXME: work out how to capture less
xcontext 'when paginable interface' do
before do
expect_any_instance_of(described_class).to receive(:paginable?).and_return(true)
end

it 'renders paginated content' do
hash = { MockFaker::Foo => [:bar] }
options = {}
output = StringIO.new

renderer = described_class.new(hash, options, output)
renderer.call

expect(renderer.pager.page_height).to be_positive
expect(output.string).to be_empty
end
end

context 'when non-paginable interface' do
before do
expect_any_instance_of(described_class).to receive(:paginable?).and_return(false)
end

it 'renders the full content' do
hash = { MockFaker::Foo => [:bar] }
options = {}
output = StringIO.new

renderer = described_class.new(hash, options, output)
renderer.call

expect(output.string).to eq("MockFaker::Foo\n└── bar\n")
end
end
end

describe '#render' do
context 'when the verbose option is enabled' do
it 'renders methods with data' do
hash = { MockFaker::Foo => [:bar] }
options = { 'verbose' => true }
output = StringIO.new

renderer = described_class.new(hash, options, output)
result = renderer.render

expect(result).to eq("MockFaker::Foo\n└── bar=> bar\n")
expect(output.string).to be_empty
end
end

context 'when the verbose option is disabled' do
it 'renders methods only' do
hash = { MockFaker::Foo => [:bar] }
options = { 'verbose' => false }
output = StringIO.new

renderer = described_class.new(hash, options, output)
result = renderer.render

expect(result).to eq("MockFaker::Foo\n└── bar\n")
expect(output.string).to be_empty
end
end
end

describe '#tree' do
it 'returns the tree object' do
hash = { MockFaker::Foo => [:bar] }
options = {}
output = StringIO.new

renderer = described_class.new(hash, options, output)
result = renderer.tree

expect(result.nodes.size).to be_positive
expect(result).to be_a(TTY::Tree)
expect(output.string).to be_empty
end

it 'memoizes the tree object' do
hash = { MockFaker::Foo => [:bar] }
options = {}
output = StringIO.new

renderer = described_class.new(hash, options, output)
first_result = renderer.tree
second_result = renderer.tree

expect(first_result).to be(second_result)
end
end

describe '#gt_screen_height?' do
context 'when tree size is greater than the screen size' do
before do
expect(TTY::Screen).to receive(:height).and_return(1)
end

it 'returns true' do
hash = { MockFaker::Foo => [:bar] }
options = {}
output = StringIO.new

renderer = described_class.new(hash, options, output)
result = renderer.gt_screen_height?

expect(result).to be(true)
end
end

context 'when the tree size is less than the screen size' do
it 'returns false' do
hash = { MockFaker::Foo => [:bar] }
options = {}
output = StringIO.new

renderer = described_class.new(hash, options, output)
result = renderer.gt_screen_height?

expect(result).to be(false)
end
end
end

describe '#paginable?' do
context 'when interactive terminal' do
before do
expect(TTY::Screen).to receive(:height).and_return(1)
allow_any_instance_of(StringIO).to receive(:tty?).and_return(true)
end

it 'returns true' do
hash = { MockFaker::Foo => [:bar] }
options = {}
output = StringIO.new

renderer = described_class.new(hash, options, output)
result = renderer.paginable?

expect(result).to be(true)
end
end

context 'when non-interactive terminal' do
it 'returns false' do
hash = { MockFaker::Foo => [:bar] }
options = {}
output = StringIO.new

renderer = described_class.new(hash, options, output)
result = renderer.paginable?

expect(result).to be(false)
end
end
end
end

0 comments on commit 1ff74a8

Please sign in to comment.