-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from akabiru/feature/list
Add `fakerbot list` Command
- Loading branch information
Showing
12 changed files
with
230 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
fakerbot (0.2.4) | ||
fakerbot (0.3.0) | ||
faker | ||
pastel (~> 0.7.2) | ||
thor (~> 0.20.0) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'forwardable' | ||
require 'pastel' | ||
require 'tty/pager' | ||
require 'tty/tree' | ||
|
||
module FakerBot | ||
class Command | ||
extend Forwardable | ||
|
||
def_delegators :command, :run | ||
|
||
def pager | ||
TTY::Pager.new(command: 'less -R') | ||
end | ||
|
||
def screen | ||
TTY::Screen | ||
end | ||
|
||
def tree(input) | ||
TTY::Tree.new do | ||
input.each do |faker, methods| | ||
node Pastel.new.green(faker.to_s) do | ||
methods&.each { |m| leaf Pastel.new.cyan(m.to_s) } | ||
end | ||
end | ||
end | ||
end | ||
|
||
def render(result, output = $stdout) | ||
result_tree = tree(result) | ||
view = result_tree.render | ||
if screen.height < result_tree.nodes.size | ||
# paginate when attached to terminal | ||
output.tty? ? pager.page(view) : output.puts(view) | ||
else | ||
output.puts view | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../command' | ||
|
||
module FakerBot | ||
module Commands | ||
class List < FakerBot::Command | ||
def initialize(options) | ||
@options = options | ||
end | ||
|
||
def execute(output: $stdout) | ||
render FakerBot::Bot.list(verbose: @options[:verbose]), output | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module FakerBot | ||
VERSION = '0.2.4'.freeze | ||
VERSION = '0.3.0'.freeze | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe '`fakerbot list` command', type: :cli do | ||
it 'executes `fakerbot help list` command successfully' do | ||
output = `fakerbot help list` | ||
expected_output = <<~OUT | ||
Usage: | ||
fakerbot list | ||
Options: | ||
-h, [--help], [--no-help] # Display usage information | ||
-v, [--verbose], [--no-verbose] # Display Faker constants with methods | ||
List all Faker constants | ||
OUT | ||
|
||
expect(output).to eq(expected_output) | ||
end | ||
|
||
it 'executes `fakerbot list` command successfully' do | ||
output = `fakerbot list` | ||
expect(output).to match(/Faker::/) | ||
end | ||
|
||
it 'executes `fakerbot list -v` command successfully' do | ||
output = `fakerbot list -v` | ||
expect(output).to match(/Faker::/) | ||
expect(output).to match(/└──/) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe '`fakerbot search` command', type: :cli do | ||
it 'executes `fakerbot help search` command successfully' do | ||
output = `fakerbot help search` | ||
expected_output = <<~OUT | ||
Usage: | ||
fakerbot search [Faker] | ||
Options: | ||
-h, [--help], [--no-help] # Display usage information | ||
-v, [--verbose], [--no-verbose] # Display Faker constants methods with examples | ||
Search Faker method(s) | ||
OUT | ||
|
||
expect(output).to eq(expected_output) | ||
end | ||
|
||
context 'when search query exists' do | ||
it 'returns results' do | ||
output = `fakerbot search name` | ||
expect(output).to match(/Faker::/) | ||
expect(output).to match(/└──/) | ||
end | ||
end | ||
|
||
context 'when search query does not exist' do | ||
it 'returns a not found message' do | ||
output = `fakerbot search asdasdhk` | ||
expect(output).to match(/Sorry, we couldn't find a match/) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'fakerbot/commands/list' | ||
|
||
RSpec.describe FakerBot::Commands::List do | ||
let(:output) { StringIO.new } | ||
let(:options) { {} } | ||
let(:command) { FakerBot::Commands::List.new(options) } | ||
|
||
before do | ||
command.execute(output: output) | ||
end | ||
|
||
context 'when single `list` command' do | ||
it 'executes successfully' do | ||
expect(output.string).to match(/Faker/) | ||
expect(output.string.lines.size).to be_positive | ||
end | ||
end | ||
|
||
context 'when `list -v` verbose command' do | ||
let(:options) { { verbose: true } } | ||
|
||
it 'executes successfully' do | ||
constant = output.string.lines[0] | ||
method = output.string.lines[1] | ||
|
||
expect(constant).to match(/Faker::/) | ||
expect(method).not_to match(/Faker::/) | ||
expect(method).to match(/└──/) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'fakerbot/commands/search' | ||
|
||
RSpec.describe FakerBot::Commands::Search do | ||
let(:output) { StringIO.new } | ||
let(:options) { {} } | ||
let(:command) { described_class.new(options) } | ||
|
||
context 'when query object exists' do | ||
before do | ||
command.execute('image', output: output) | ||
end | ||
|
||
it 'returns results' do | ||
expect(output.string).to match(/Faker/) | ||
expect(output.string.lines.size).to be_positive | ||
end | ||
end | ||
|
||
context 'when query object does not exist' do | ||
before do | ||
command.execute('hasjdhaksjd', output: output) | ||
end | ||
|
||
it 'returns nil' do | ||
expect(output.string).to be_empty | ||
end | ||
end | ||
end |