Permalink
Browse files

added pager support + improved tests

  • Loading branch information...
banister committed Nov 30, 2012
1 parent 5e39e66 commit 122150fed865ab609c029dde5d85385367f262ee
Showing with 88 additions and 31 deletions.
  1. +36 −20 lib/pry-note/commands.rb
  2. +1 −1 lib/pry-note/hooks.rb
  3. +12 −0 test/helper.rb
  4. +39 −10 test/test_pry_note.rb
View
@@ -56,7 +56,7 @@ def process
add_note(opts.arguments.first, cmd_opts[:message])
elsif opts.command?(:show)
cmd_opts = opts[:show]
- show_note(opts.arguments.first, cmd_opts[:verbose])
+ stagger_output create_note_output(opts.arguments.first, cmd_opts[:verbose])
elsif opts.command?(:list)
cmd_opts = opts[:list]
if cmd_opts.present?(:verbose)
@@ -82,6 +82,7 @@ def process
elsif opts.command?(:load)
PryNote.load_notes(opts.arguments.first)
else
+ output.puts opts.to_s
end
end
@@ -136,8 +137,9 @@ def reedit_note(name, message=nil)
total_notes = notes[co_name].count
note_number = note_number_s.to_i
+ out = ""
if !notes[co_name]
- output.puts "No notes to edit for #{co_name}!"
+ out << "No notes to edit for #{co_name}!\n"
elsif !note_number_s
raise Pry::CommandError, "Must specify a note number. Allowable range is 1-#{total_notes}."
elsif note_number < 1 || note_number > total_notes
@@ -151,77 +153,91 @@ def reedit_note(name, message=nil)
end
notes[co_name][note_number.to_i - 1] = new_content
- output.puts "Updated note #{note_number} for #{co_name}!"
+ out << "Updated note #{note_number} for #{co_name}!\n"
end
end
def delete_note(name)
name, note_number = name.split(/:(\d+)$/)
co_name = code_object_name(retrieve_code_object_safely(name))
+ out = ""
if !notes[co_name]
- output.puts "No notes to delete for #{co_name}!"
+ out << "No notes to delete for #{co_name}!\n"
elsif note_number
notes[co_name].delete_at(note_number.to_i - 1)
notes.delete(co_name) if notes[co_name].empty?
- output.puts "Deleted note #{note_number} for #{co_name}!"
+ out << "Deleted note #{note_number} for #{co_name}!\n"
else
notes.delete(co_name)
- output.puts "Deleted all notes for #{text.bold(co_name)}!"
+ out << "Deleted all notes for #{text.bold(co_name)}!\n"
end
+
+ stagger_output out
end
- def show_note(name, verbose=false)
+ def create_note_output(name, verbose=false)
+ name ||= default_object_name
+ name, _ = name.split(/:(\d+)$/)
code_object = retrieve_code_object_safely(name)
co_name = code_object_name(code_object)
+ raise Pry::CommandError, "Please specify the name of a method or class." if !name
+
if !notes.has_key?(co_name)
output.puts "No notes saved for #{text.bold(co_name)}"
return
end
- output.puts text.bold("#{co_name}:\n--")
+ out = ""
+ out << text.bold("#{co_name}:\n--\n")
if verbose
- output.puts Pry::Code.new(code_object.source, code_object.source_line).with_line_numbers.to_s
+ out << Pry::Code.new(code_object.source, code_object.source_line).with_line_numbers.to_s + "\n"
end
notes[code_object_name(code_object)].each_with_index do |note, index|
- output.puts "\nNote #{text.bold((index + 1).to_s)}: #{note}"
+ out << "\nNote #{text.bold((index + 1).to_s)}: #{note}\n"
end
+
+ out
end
def list_all
if notes.any?
- output.puts text.bold("Showing all available notes:\n\n")
+ out = ""
+ out << text.bold("Showing all available notes:\n\n")
notes.each do |key, content|
begin
- show_note(key, true)
- output.puts "\n"
+ out << create_note_output(key, true) << "\n"
rescue
end
end
- output.puts "\nTo view notes for an item type, e.g: `note show Klass#method`"
- else
- output.puts "No notes available."
+ else
+ out << "No notes available.\n"
end
+
+ stagger_output out
end
def list_notes
if notes.any?
- output.puts text.bold("Showing all available notes:\n\n")
+ out = ""
+ out << text.bold("Showing all available notes:\n\n")
notes.each do |key, content|
begin
if retrieve_code_object_from_string(key, target)
- output.puts "#{text.bold(key)} has #{content.count} notes"
+ out << "#{text.bold(key)} has #{content.count} notes\n"
end
rescue
end
end
- output.puts "\nTo view notes for an item type, e.g: `note show Klass#method`"
+ out << "\nTo view notes for a specific item, e.g: `note show Klass#method`\n"
else
- output.puts "No notes available."
+ out << "No notes available.\n"
end
+
+ stagger_output out
end
end
View
@@ -7,7 +7,7 @@
PryNote.notes[co_name].each_with_index do |note, index|
clipped_note = note.lines.count < 3 ? note : note.lines.to_a[0..2].join +
- text.bold("<...clipped...>") + " Use `note -s #{co_name}` to view unelided notes."
+ text.bold("<...clipped...>") + " Use `note show #{co_name}` to view unelided notes."
amended_note = clipped_note.lines.each_with_index.map do |line, idx|
idx > 0 ? "#{' ' * ((index + 1).to_s.size + 2)}#{line}" : line
end.join
View
@@ -5,10 +5,22 @@
require 'pry-note'
end
+# Ensure file is deleted before and after block
def cleanup_file(file_name)
f = File.expand_path(file_name)
File.unlink(f) if File.exists?(f)
yield
ensure
File.unlink(f) if File.exists?(f)
end
+
+# Return any raised exceptino objects inside the block
+def capture_exception
+ ex = nil
+ begin
+ yield
+ rescue Exception => e
+ ex = e
+ end
+ ex
+end
View
@@ -1,21 +1,12 @@
require 'helper'
+# useful test class
class PryNote::TestClass
def ping
binding
end
end
-def capture_exception
- ex = nil
- begin
- yield
- rescue Exception => e
- ex = e
- end
- ex
-end
-
describe PryNote do
before do
Pad.obj = PryNote::TestClass.new
@@ -183,6 +174,44 @@ def capture_exception
end
end
+ describe "note show" do
+ it 'should display method source when -v flag is used' do
+ @t.process_command "note add PryNote::TestClass -m 'my note1'"
+ @t.process_command "note show PryNote::TestClass -v"
+ @t.last_output.should =~ /ping/
+ end
+
+ it 'should just display number of notes by default' do
+ @t.process_command "note add PryNote::TestClass -m 'my note1'"
+ @t.process_command "note add PryNote::TestClass -m 'my note2'"
+ @t.process_command "note show PryNote::TestClass"
+ @t.last_output.should =~ /2/
+ @t.last_output.should.not =~ /ping/
+ end
+
+ it 'should ignore :number suffix (as used in edit and delete)' do
+ @t.process_command "note add PryNote::TestClass -m 'my note2'"
+ @t.process_command "note show PryNote::TestClass:99"
+ @t.last_output.should =~ /1/
+ end
+
+ it 'should implicitly display notes for current object (class)' do
+ @t.process_command "note add PryNote::TestClass -m 'my note1'"
+ @t.process_command "note add PryNote::TestClass -m 'my note2'"
+ @t.process_command "cd PryNote::TestClass"
+ @t.process_command "note show -v"
+ @t.last_output.should =~ /ping/
+ end
+
+ it 'should implicitly display notes for current object (method)' do
+ t = pry_tester(Pad.obj.ping)
+ t.process_command "note add PryNote::TestClass#ping -m 'my note1'"
+ t.process_command "note add PryNote::TestClass#ping -m 'my note2'"
+ t.process_command "note show -v"
+ t.last_output.should =~ /binding/
+ end
+ end
+
describe "note export" do
it 'should export to Pry.config.notes_file by default' do
cleanup_file("bing.yml") do

0 comments on commit 122150f

Please sign in to comment.