Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveitaly committed Jul 15, 2021
1 parent 36eff10 commit 63a78b8
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 6 deletions.
7 changes: 5 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in pry-nav.gemspec
gemspec

gem "test-unit"

gem "pry", "~> 0.14.1"
gem "pry-remote"
10 changes: 9 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
#!/usr/bin/env rake
require 'bundler/gem_tasks'
require 'rake/testtask'

task :default => :test

Rake::TestTask.new(:test) do |task|
task.libs << 'test'
task.test_files = FileList['test/*_test.rb']
task.verbose = true
end
5 changes: 2 additions & 3 deletions pry-nav.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ Gem::Specification.new do |gem|

gem.required_ruby_version = '>= 2.1.0'

# Dependencies
gem.required_ruby_version = '>= 1.8.7'
gem.add_runtime_dependency 'pry', '>= 0.9.10', '< 0.14.0'
gem.add_development_dependency 'pry-remote', '~> 0.1.6'
gem.add_runtime_dependency 'pry', '>= 0.9.10', '< 0.15'
gem.add_development_dependency 'rake'
end
98 changes: 98 additions & 0 deletions test/pry_nav_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# frozen_string_literal: true

require ::File.expand_path("test_helper", __dir__)

class PryNavTest < Test::Unit::TestCase
# removing color makes string matching easier
def setup
Pry.color = false
end

# lifted from:
# https://github.com/pry/pry-stack_explorer/blob/e3e6bd202e092712900f0d5f239ee21ab2f32b2b/test/support/io_utils.rb
def with_pry_output_captured(new_in, new_out = StringIO.new)
old_in = Pry.input
old_out = Pry.output

# direct stdout so we can test against `puts` in the method we defined above
old_stdout = $stdout
$stdout = new_out

Pry.input = new_in
Pry.output = new_out

begin
yield
ensure
Pry.input = old_in
Pry.output = old_out
$stdout = old_stdout
end

new_out
end

# `step` will step into the frames, while `next` keeps the debugging execution within the frame
def test_step
o = NavSample.new

r = with_pry_output_captured(
InputTester.new(
"step",

"step",
"step",

"continue"
)
){ o.nested_bind_with_call }

# initial binding display
assert(r.string.include?("def nested_bind_with_call"))

# after two steps, we are in the second frame, let's make sure we get there
assert(r.string.include?("def nested_bind_call"))

assert(/nested_puts\n/ =~ r.string)
assert(/root_puts\n/ =~ r.string)
end

def test_next
o = NavSample.new

r = with_pry_output_captured(
InputTester.new(
"next",
"next",
"next",
"continue"
)
){ o.nested_bind_with_call }

assert(r.string.include?("def nested_bind_with_call"))
refute(r.string.include?("def nested_bind_call"))

assert(/nested_puts\n/ =~ r.string)
assert(/root_puts\n/ =~ r.string)
end

def test_continue
o = NavSample.new

r = with_pry_output_captured(
InputTester.new(
"continue",
)
){ o.nested_bind_with_call }

assert(r.string.include?("def nested_bind_with_call"))
refute(r.string.include?("def nested_bind_call"))

assert(/nested_puts\n/ =~ r.string)
assert(/root_puts\n/ =~ r.string)
end

def test_file_context
assert(PryNav.check_file_context(binding))
end
end
39 changes: 39 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'bundler/setup'
require 'test/unit'

# lets make sure it works will all of the pry extensions
Bundler.require

class NavSample
def nested_bind_with_call
Pry.start(binding)
nested_bind_call
puts "root_puts"
end

def nested_bind_call
puts "nested_puts"
end
end

# lifted from:
# https://github.com/pry/pry-stack_explorer/blob/e3e6bd202e092712900f0d5f239ee21ab2f32b2b/test/support/input_tester.rb

class InputTester
def initialize(*actions)
if actions.last.is_a?(Hash) && actions.last.keys == [:history]
@hist = actions.pop[:history]
end

@orig_actions = actions.dup
@actions = actions
end

def readline(*)
@actions.shift.tap{ |line| @hist << line if @hist }
end

def rewind
@actions = @orig_actions.dup
end
end

0 comments on commit 63a78b8

Please sign in to comment.