Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

started tests, moved from storing notes on state to PryNote instance var

  • Loading branch information...
commit b29d6b8a539db20028122a6869d00d75a4f8e67e 1 parent 2cadc58
@banister authored
Showing with 256 additions and 36 deletions.
  1. +12 −2 Rakefile
  2. +84 −34 lib/pry-note.rb
  3. +14 −0 test/helper.rb
  4. +146 −0 test/test_pry_note.rb
View
14 Rakefile
@@ -6,7 +6,7 @@ direc = File.dirname(__FILE__)
PROJECT_NAME = "pry-note"
require 'rake/clean'
-require 'rake/gempackagetask'
+require 'rubygems/package_task'
require "#{PROJECT_NAME}/version"
CLOBBER.include("**/*~", "**/*#*", "**/*.log")
@@ -28,11 +28,21 @@ def apply_spec_defaults(s)
s.test_files = `git ls-files -- test/*`.split("\n")
end
+desc "Set up and run tests"
+task :default => [:test]
+
+
desc "run pry with plugin enabled"
task :pry do
exec("pry -rubygems -I#{direc}/lib/ -r #{direc}/lib/#{PROJECT_NAME}")
end
+desc "Run tests"
+task :test do
+ sh "bacon -Itest -rubygems -a -q"
+end
+task :spec => :test
+
desc "Show version"
task :version do
puts "Pry-note version: #{PryNote::VERSION}"
@@ -47,7 +57,7 @@ namespace :ruby do
s.platform = Gem::Platform::RUBY
end
- Rake::GemPackageTask.new(spec) do |pkg|
+ Gem::PackageTask.new(spec) do |pkg|
pkg.need_zip = false
pkg.need_tar = false
end
View
118 lib/pry-note.rb
@@ -1,6 +1,21 @@
+require 'pry-note/version'
+
Pry.config.notes_file = "./notes.yml"
+module PryNote
+ Note = Struct.new(:line_number, :content)
+
+ class << self
+ attr_accessor :notes
+ attr_accessor :initial_setup_complete
+ end
+end
+
Pry::Commands.create_command "note" do
+ class Note
+ attr_accessor :line_number
+
+ end
description "Note stuff."
banner <<-USAGE
@@ -14,14 +29,28 @@
e.g note -l #=> list all notes
USAGE
+
+ def subcommands(cmd)
+ cmd.on :add do |opt|
+ opt.on :m, "message", "Show the list of all available plugins", :argument => true
+ end
+
+ cmd.on :show
+ cmd.on :list do |opt|
+ opt.on :v, :verbose, "List all notes and content with source code"
+ end
+ cmd.on :export
+ cmd.on :load
+ cmd.on :delete do |opt|
+ opt.on :all, "Delete all notes."
+ end
+ end
+
def options(opt)
opt.on :a, :add, "Add a note to a method or class.", :argument => true
- opt.on :s, :show, "Show any notes associated with the given method or class.", :argument => true
+# opt.on :s, :show, "Show any notes associated with the given method or class.", :argument => true
opt.on :d, :delete, "Delete notes for a method or class.", :argument => true
opt.on "delete-all", "Delete all notes."
- opt.on :e, :export, "Export notes to a file.", :argument => :optional
- opt.on :load, "Load notes from a file.", :argument => :optional
- opt.on :l, :list, "List all notes."
opt.on "list-all", "List all notes with content."
end
@@ -30,12 +59,14 @@ def setup
add_reminders
load_notes
state.initial_setup_complete = true
- _pry_.hooks.add_hook(:after_session, :export_notes) { export_notes }
+ if !_pry_.hooks.hook_exists?(:after_session, :export_notes)
+ _pry_.hooks.add_hook(:after_session, :export_notes) { export_notes }
+ end
end
end
def notes
- state.notes ||= {}
+ PryNote.notes ||= {}
end
# edit a note in a temporary file and return note content
@@ -50,31 +81,31 @@ def edit_note(obj_name)
end
def process
- if opts.present?(:add)
- add_note(opts[:a])
- elsif opts.present?(:show)
- show_note(opts[:s])
- elsif opts.present?(:list)
- list_notes
- elsif opts.present?(:export)
- export_notes(opts[:e])
- elsif opts.present?(:delete)
- delete_note(opts[:d])
- elsif opts.present?(:"delete-all")
- notes.replace({})
- elsif opts.present?(:"list-all")
- list_all
- elsif opts.present?(:load)
- load_notes(opts[:load])
- else
- meth = Pry::Method.from_binding(target)
- if internal_binding?(target) || !meth
- obj = target.eval("self")
- obj_name = obj.is_a?(Module) ? obj.name : obj.class.name
- add_note(obj_name)
+ if opts.command?(:add)
+ cmd_opts = opts[:add]
+ add_note(opts.arguments.first, cmd_opts[:message])
+ elsif opts.command?(:show)
+ show_note(opts.arguments.first)
+ elsif opts.command?(:list)
+ cmd_opts = opts[:list]
+ if cmd_opts.present?(:verbose)
+ list_all
+ else
+ list_notes
+ end
+ elsif opts.command?(:export)
+ export_notes(opts.arguments.first)
+ elsif opts.command?(:delete)
+ cmd_opts = opts[:delete]
+ if cmd_opts.present?(:all)
+ notes.replace({})
+ output.puts "Deleted all notes!"
else
- add_note(meth.name_with_owner)
+ delete_note(opts.arguments.first)
end
+ elsif opts.command?(:load)
+ load_notes(opts.arguments.first)
+ else
end
end
@@ -90,15 +121,30 @@ def retrieve_code_object_safely(name)
code_object
end
+ def default_object_name
+ meth = Pry::Method.from_binding(target)
+ if internal_binding?(target) || !meth
+ obj = target.eval("self")
+ obj_name = obj.is_a?(Module) ? obj.name : obj.class.name
+ obj_name
+ else
+ meth.name_with_owner
+ end
+ end
+
def code_object_name(co)
co.is_a?(Pry::Method) ? co.name_with_owner : co.name
end
- def add_note(name)
+ def add_note(name, message=nil)
+ name ||= default_object_name
+ name, line_number = name.split(/@(\d+)$/)
+
+
co_name = code_object_name(retrieve_code_object_safely(name))
- if args.any?
- note = args.join(" ")
+ if message
+ note = message
else
note = edit_note(co_name)
end
@@ -110,7 +156,7 @@ def add_note(name)
end
def delete_note(name)
- name, note_number = name.split(":")
+ name, note_number = name.split(/:(\d+)$/)
co_name = code_object_name(retrieve_code_object_safely(name))
if !notes[co_name]
@@ -136,13 +182,15 @@ def show_note(name)
end
output.puts text.bold("#{co_name}:\n--")
-
+ output.puts Pry::Code.new(code_object.source, code_object.source_line).with_line_numbers.to_s
notes[code_object_name(code_object)].each_with_index do |note, index|
output.puts "\nNote #{text.bold((index + 1).to_s)}: #{note}"
end
end
def export_notes(file_name=nil)
+ return if !file_name && !Pry.config.notes_file
+
require 'yaml'
file_name ||= Pry.config.notes_file
@@ -152,6 +200,8 @@ def export_notes(file_name=nil)
end
def load_notes(file_name=nil)
+ return if !file_name && !Pry.config.notes_file
+
require 'yaml'
file_name ||= Pry.config.notes_file
View
14 test/helper.rb
@@ -0,0 +1,14 @@
+require 'pry/test/helper'
+
+unless Object.const_defined? 'PryNote'
+ $:.unshift File.expand_path '../../lib', __FILE__
+ require 'pry-note'
+end
+
+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
View
146 test/test_pry_note.rb
@@ -0,0 +1,146 @@
+require 'helper'
+
+class PryNote::TestClass
+ def ping
+ binding
+ end
+end
+
+describe PryNote do
+ before do
+ Pad.obj = PryNote::TestClass.new
+ @t = pry_tester
+ PryNote.notes = {}
+ Pry.config.notes_file = nil
+ end
+
+ after do
+ Pad.clear
+ end
+
+ describe "note add" do
+ describe "explicit object" do
+ it 'should add a new note for a method (bound method)' do
+ @t.process_command "note add Pad.obj.ping -m 'my note'"
+ @t.last_output.should =~ /Added note to PryNote::TestClass#ping/
+ PryNote.notes["PryNote::TestClass#ping"].first.should =~ /my note/
+ end
+
+ it 'should add a new note for a method (unbound method)' do
+ @t.process_command "note add PryNote::TestClass#ping -m 'my note'"
+ @t.last_output.should =~ /Added note to PryNote::TestClass#ping/
+ PryNote.notes["PryNote::TestClass#ping"].first.should =~ /my note/
+ end
+
+ it 'should add a new note for a class' do
+ @t.process_command "note add PryNote::TestClass -m 'my note'"
+ @t.last_output.should =~ /Added note to PryNote::TestClass/
+ PryNote.notes["PryNote::TestClass"].first.should =~ /my note/
+ end
+ end
+
+ describe "implicit object" do
+ it 'should add a new note for class of object implicitly (without specifying object)' do
+ @t.process_command "cd 0"
+ @t.process_command "note add -m 'my note'"
+ @t.last_output.should =~ /Added note to Fixnum/
+ PryNote.notes["Fixnum"].first.should =~ /my note/
+ end
+
+ it 'should add a new note for a method implicitly (without specifying object)' do
+ o = PryNote::TestClass.new
+ t = pry_tester(o.ping)
+ t.process_command "note add -m 'my note'"
+ t.last_output.should =~ /Added note to PryNote::TestClass#ping/
+ PryNote.notes["PryNote::TestClass#ping"].first.should =~ /my note/
+ end
+ end
+
+ describe "multiple notes can be added" do
+ it 'should add multiple notes' do
+ @t.process_command "note add PryNote::TestClass -m 'my note1'"
+ @t.process_command "note add PryNote::TestClass -m 'my note2'"
+ PryNote.notes["PryNote::TestClass"].count.should == 2
+ PryNote.notes["PryNote::TestClass"].first.should =~ /my note1/
+ PryNote.notes["PryNote::TestClass"].last.should =~ /my note2/
+ end
+ end
+ end
+
+ describe "note delete" do
+ it 'should delete all notes for an object' do
+ @t.process_command "note add PryNote::TestClass -m 'my note'"
+ PryNote.notes["PryNote::TestClass"].count.should == 1
+ @t.process_command "note delete PryNote::TestClass"
+ @t.last_output.should =~ /Deleted all notes for PryNote::TestClass/
+ PryNote.notes["PryNote::TestClass"].should == nil
+ end
+
+ it 'should NOT delete notes for unspecified object' do
+ @t.process_command "note add PryNote::TestClass -m 'my note'"
+ @t.process_command "note add PryNote::TestClass#ping -m 'my note'"
+ @t.process_command "note delete PryNote::TestClass"
+ PryNote.notes["PryNote::TestClass#ping"].count.should == 1
+ end
+
+ it 'should delete all notes for all objects' do
+ @t.process_command "note add PryNote::TestClass -m 'my note'"
+ @t.process_command "note add PryNote::TestClass#ping -m 'my note'"
+ PryNote.notes.keys.count.should == 2
+ @t.process_command "note delete --all"
+ @t.last_output.should =~ /Deleted all notes/
+ PryNote.notes.empty?.should == true
+ end
+
+ describe "deleting specific notes for an object" do
+ it 'should delete first note for an object' do
+ @t.process_command "note add PryNote::TestClass -m 'my note1'"
+ @t.process_command "note add PryNote::TestClass -m 'my note2'"
+ PryNote.notes["PryNote::TestClass"].count.should == 2
+ @t.process_command "note delete PryNote::TestClass:1"
+ @t.last_output.should =~ /Deleted note 1 for PryNote::TestClass/
+ PryNote.notes["PryNote::TestClass"].count.should == 1
+ PryNote.notes["PryNote::TestClass"].first.should =~ /my note2/
+ end
+
+ it 'should delete middle note for an object' 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 add PryNote::TestClass -m 'my note3'"
+ PryNote.notes["PryNote::TestClass"].count.should == 3
+ @t.process_command "note delete PryNote::TestClass:2"
+ @t.last_output.should =~ /Deleted note 2 for PryNote::TestClass/
+ PryNote.notes["PryNote::TestClass"].count.should == 2
+ PryNote.notes["PryNote::TestClass"].first.should =~ /my note1/
+ PryNote.notes["PryNote::TestClass"].last.should =~ /my note3/
+ end
+
+ end
+ end
+
+ describe "note export" do
+ it 'should export to Pry.config.notes_file by default' do
+ cleanup_file("bing.yml") do
+ Pry.config.notes_file = "bing.yml"
+ @t.process_command "note add PryNote::TestClass -m 'my note1'"
+ @t.process_command "note add PryNote::TestClass -m 'my note2'"
+ @t.process_command "note export"
+
+ o = YAML.load(File.read("bing.yml"))
+ o["PryNote::TestClass"].should == ['my note1', 'my note2']
+
+ Pry.config.notes_file = nil
+ end
+ end
+
+ it 'should export to specified file' do
+ cleanup_file("blah.yml") 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 export blah.yml"
+ o = YAML.load(File.read("blah.yml"))
+ o["PryNote::TestClass"].should == ['my note1', 'my note2']
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.