Skip to content

Commit

Permalink
Support printer as a Logger object or Logging::Logger (from "loggin…
Browse files Browse the repository at this point in the history
…g" gem). Basically any object that responds to :debug method.
  • Loading branch information
AndyObtiva committed Aug 24, 2020
1 parent 1ccca61 commit 466c854
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
- Display `run_at` run number in printout
- Support `require 'pd`' as a shorter alternative to `require 'puts_debuggerer'`
- Support `return: false` option to return printed String instead of returning printed object
- Support `printer` as a Logger object or Logging::Logger (from "logging" gem). Basically any object that responds to :debug method.

## 0.8.2

Expand Down
1 change: 0 additions & 1 deletion TODO.md
Expand Up @@ -6,7 +6,6 @@ Here are tasks considered for future versions. Once done, they are moved to the

### 0.10.0

- Support `printer` as a Logger object or Logging::Logger (from "logging" gem). Perhaps discover via ducktyping.
- Provide as logging device and/or formatter for Ruby logger API and/or logging gem
- Refactor internals to avoid global method pollution

Expand Down
6 changes: 4 additions & 2 deletions lib/puts_debuggerer.rb
Expand Up @@ -27,7 +27,7 @@ module PutsDebuggerer
Rails.logger.debug(output)
end
PRINT_ENGINE_DEFAULT = :ap
PRINTER_MESSAGE_INVALID = 'printer must be a valid global method symbol (e.g. :puts) or lambda/proc receiving a text arg'
PRINTER_MESSAGE_INVALID = 'printer must be a valid global method symbol (e.g. :puts), a logger, or a lambda/proc receiving a text arg'
PRINT_ENGINE_MESSAGE_INVALID = 'print_engine must be a valid global method symbol (e.g. :p, :ap or :pp) or lambda/proc receiving an object arg'
ANNOUNCER_DEFAULT = '[PD]'
FORMATTER_DEFAULT = -> (data) {
Expand Down Expand Up @@ -182,7 +182,7 @@ def source_line_count=(value)
def printer=(printer)
if printer.nil?
@printer = printer_default
elsif printer.is_a?(Proc)
elsif printer.is_a?(Proc) || printer.respond_to?(:debug) # a logger
@printer = printer
else
@printer = method(printer).name rescue raise(PRINTER_MESSAGE_INVALID)
Expand Down Expand Up @@ -532,6 +532,8 @@ def pd(*objects)
string = sio.string
if PutsDebuggerer.printer.is_a?(Proc)
PutsDebuggerer.printer.call(string)
elsif PutsDebuggerer.printer.respond_to?(:debug)
PutsDebuggerer.printer.debug(string)
else
send(PutsDebuggerer.send(:printer), string)
end
Expand Down
29 changes: 25 additions & 4 deletions spec/lib/puts_debuggerer__with_custom_printer__spec.rb
Expand Up @@ -3,53 +3,73 @@

describe 'PutsDebuggerer' do
let(:puts_debuggerer_invoker_file) {File.expand_path(File.join(__FILE__, '..', '..', 'support', 'puts_debuggerer_invoker.rb'))}

before do
now = Time.new(2000, 1, 1, 1, 1, 1, 1)
Time.stub(:now).and_return(now)
load "awesome_print/core_ext/kernel.rb"
@awesome_print_defaults = AwesomePrint.defaults
AwesomePrint.defaults = {
plain: true
}
PutsDebuggerer.print_engine = :ap
end

context 'with custom printer engine' do
let(:expected_object_printout) {
"[\n [0] 1,\n [1] [\n [0] 2,\n [1] 3\n ]\n]"
}

let(:expected_object_printout_awesome_print) {
"[\\n [0] 1,\\n [1] [\\n [0] 2,\\n [1] 3\\n ]\\n]"
}

before do
Object.send(:remove_const, :Rails) rescue nil
PutsDebuggerer.app_path = nil
PutsDebuggerer.printer = nil
end

after do
Object.send(:remove_const, :Rails) rescue nil
end

it 'prints using passed in custom lambda print engine' do
PutsDebuggerer.printer = lambda {|text| puts "\n#{text}\n"} #intentionally set as :p
name = 'Robert'
PutsDebuggererInvoker.dynamic_greeting(name)
output = $stdout.string
expect(output).to eq("\n[PD] #{puts_debuggerer_invoker_file}:10\n > pd \"Hello \#{name}\"\n => \"Hello Robert\"\n\n")
end
it 'prints file relative to app path, line number, ruby expression, and evaluated string object with default :puts printer' do

it 'prints with default :puts printer (file relative to app path, line number, ruby expression, and evaluated string object)' do
expect(PutsDebuggerer.printer).to eq(:puts)
PutsDebuggererInvoker.static_nested_array
output = $stdout.string
expect(output).to eq("[PD] #{puts_debuggerer_invoker_file}:22\n > pd [1, [2, 3]]\n => #{expected_object_printout}\n")
end
it 'prints file relative to app path, line number, ruby expression, and evaluated string object with specified :print printer' do

it 'prints with specified :print printer (file relative to app path, line number, ruby expression, and evaluated string object)' do
PutsDebuggerer.printer = :ap
expect(PutsDebuggerer.printer).to eq(:ap)
PutsDebuggererInvoker.dynamic_nested_array([1, [2, 3]])
output = $stdout.string
expect(output).to eq("\"[PD] #{puts_debuggerer_invoker_file}:26\\n > pd array, options\\n => #{expected_object_printout_awesome_print}\\n\"\n")
end

it 'prints with specified logger object (file relative to app path, line number, ruby expression, and evaluated string object)' do
logger = Logger.new($stdout)
PutsDebuggerer.printer = logger
expect(PutsDebuggerer.printer).to eq(logger)
PutsDebuggererInvoker.static_nested_array
output = $stdout.string
expect(output).to eq("D, [2000-01-01T01:01:01.000000 ##{Process.pid}] DEBUG -- : [PD] #{puts_debuggerer_invoker_file}:22\n > pd [1, [2, 3]]\n => #{expected_object_printout}\n\n")
end

it 'raises informative error if print_engine was invalid' do
expect {PutsDebuggerer.printer = :invalid}.to raise_error('printer must be a valid global method symbol (e.g. :puts) or lambda/proc receiving a text arg')
expect {PutsDebuggerer.printer = :invalid}.to raise_error('printer must be a valid global method symbol (e.g. :puts), a logger, or a lambda/proc receiving a text arg')
end

it 'prints using Rails non-test env lambda print engine' do
Object.class_eval do
module Rails
Expand Down Expand Up @@ -77,6 +97,7 @@ def self.env
output = $stdout.string
expect(output).to eq("Rails.logger.debug: [PD] /spec/support/puts_debuggerer_invoker.rb:10\n > pd \"Hello \#{name}\"\n => \"Hello Robert\"\n")
end

it 'prints using Rails test env lambda print engine' do
Object.class_eval do
module Rails
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -43,4 +43,5 @@
end

require 'awesome_print'
require 'logger'
require 'pd' # tests both `require 'pd'` and `require 'puts_debuggerer'`

0 comments on commit 466c854

Please sign in to comment.