Skip to content

Commit

Permalink
Merge pull request #199 from kachick/only-support-irb-1.13.0+
Browse files Browse the repository at this point in the history
Update irb integrations with the irb 1.13.0
  • Loading branch information
kachick authored May 2, 2024
2 parents 9d6e61b + 6155e97 commit 33277e6
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 114 deletions.
2 changes: 2 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ updates:
patterns:
- '*rubocop*'
- package-ecosystem: 'bundler'
# For feature dependedncy updates,
# Keep this definition even if the /gemfiles/oldest/Gemfile is completely same as /Gemfile
directory: '/gemfiles/oldest'
schedule:
interval: 'weekly'
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ end

group :test do
gem 'test-unit', '~> 3.6.2'
gem 'test-unit-ruby-core', '~> 1.0'
gem 'warning', '~> 1.3.0'

gem 'irb', '1.13.0'
Expand Down
72 changes: 28 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,21 @@
[![Build Status](https://github.com/kachick/irb-power_assert/actions/workflows/ci-ruby.yml/badge.svg?branch=main)](https://github.com/kachick/irb-power_assert/actions/workflows/ci-ruby.yml?query=branch%3Amain+)
[![Gem Version](https://badge.fury.io/rb/irb-power_assert.svg)](http://badge.fury.io/rb/irb-power_assert)

## Overview

![screenshot - expression](https://user-images.githubusercontent.com/1180335/119386011-efc1bb00-bd01-11eb-80c4-1aea86fa3781.png)

[ruby/power_assert](https://github.com/ruby/power_assert) is a recent my favorites.\
(the author is [@k-tsj](https://github.com/k-tsj), thank you!)

It is super helpful in complex testing.

Don't say [ruby/irb](https://github.com/ruby/irb) is old-fashioned.

I just would get irb version of
[yui-knk/pry-power_assert](https://github.com/yui-knk/pry-power_assert)

Honor should be bestowed upon them.
Use power_assert inspection in irb

## Usage

Require Ruby 3.2 or higher # Tested only in ruby-head and the last 2 stable versions
Tested only in ruby-head and the last 2 stable versions\
So require Ruby 3.2 or higher

```console
$ gem install irb-power_assert
Should be installed!
...installed
```

```console
$ irb -r irb-power_assert
# Then enabled this gem!
# enabled this gem
```

Or specify in your `~/.irbrc` as below
Expand All @@ -41,37 +28,14 @@ require 'irb/power_assert'

```console
$ irb
# Then enabled this gem!
irb(main):004> help pa
Print PowerAssert inspection for the given expression.
```

Then you can use `pa` as an IRB command.

```ruby
irb(main):001:0> pa %q{ "0".class == "3".to_i.times.map {|i| i + 1 }.class }
result: false

"0".class == "3".to_i.times.map {|i| i + 1 }.class
| | | | | |
| | | | | Array
| | | | [1, 2, 3]
| | | #<Enumerator: ...>
| | 3
| false
String
=> nil
```

The `pa` just takes strings of the code.

If you want to directly pass `expression`, [.irbrc](examples/.irbrc) is the hack for single line code.\
if you don't have the file yet, putting the file as one of your `$IRBRC`, `$XDG_CONFIG_HOME/irb/irbrc` or `$HOME/.irbrc`

Then you can use the `pa` as below...

```ruby
irb(main):001:0> pa "0".class == "3".to_i.times.map {|i| i + 1 }.class
result: false

"0".class == "3".to_i.times.map {|i| i + 1 }.class
| | | | | |
| | | | | Array
Expand All @@ -80,11 +44,31 @@ result: false
| | 3
| false
String
=> nil

=> false
```

No hack is needed in your irbrc

## Thanks!

[ruby/power_assert](https://github.com/ruby/power_assert) is a recent my favorites.\
(the author is [@k-tsj](https://github.com/k-tsj), thank you!)

It is super helpful in complex testing.

Don't say [ruby/irb](https://github.com/ruby/irb) is old-fashioned.

I just would get irb version of
[yui-knk/pry-power_assert](https://github.com/yui-knk/pry-power_assert).

Latest IRB is much helpful to [create own command](https://github.com/ruby/irb/pull/886)

Honor should be bestowed upon them.

## References

- [power-assert-js/power-assert](https://github.com/power-assert-js/power-assert)
- [Power Assert in Ruby](https://speakerdeck.com/k_tsj/power-assert-in-ruby)
- [ja - IRB is new than Pry](https://k0kubun.hatenablog.com/entry/2021/04/02/211455)
- [pry-power_assert implementation](https://github.com/yui-knk/pry-power_assert/blob/2d10ee3df8efaf9c448f31d51bff8033a1792739/lib/pry-power_assert.rb#L26-L35)
19 changes: 0 additions & 19 deletions examples/.irbrc

This file was deleted.

3 changes: 2 additions & 1 deletion gemfiles/oldest/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ end

group :test do
gem 'test-unit', '~> 3.6.2'
gem 'test-unit-ruby-core', '~> 1.0'
gem 'warning', '~> 1.3.0'

gem 'irb', '1.12.0'
gem 'irb', '1.13.0'
gem 'power_assert', '2.0.3'
end
2 changes: 1 addition & 1 deletion irb-power_assert.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |gem|
'rubygems_mfa_required' => 'true',
}

gem.add_runtime_dependency 'irb', '>= 1.12.0', '< 2.0'
gem.add_runtime_dependency 'irb', '>= 1.13.0', '< 2.0'
gem.add_runtime_dependency 'power_assert', '>= 2.0.3', '< 3.0'

gem.required_ruby_version = '>= 3.2'
Expand Down
42 changes: 28 additions & 14 deletions lib/irb/cmd/pa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,40 @@

module IRB
class PaCommand < IRB::Command::Base
description 'Show inspections for each result.'
category 'Misc'
help_message 'Print PowerAssert inspection for the given expression.'

def usage
<<~'EOD'
`pa` command will work for expressions that includes method calling.
e.g. `pa (2 * 3 * 7).abs == 1010102.to_s.to_i(2)`
EOD
end

def execute(expression)
# The implementation basically taken from https://github.com/yui-knk/pry-power_assert/blob/2d10ee3df8efaf9c448f31d51bff8033a1792739/lib/pry-power_assert.rb#L26-L35, thank you!
result = +'result: '
if expression == ''
# Avoid warn and raise here, warn does not appear in irb and exception sounds like a IRB bug
puts usage
return
end

result = nil
inspection = nil
::PowerAssert.start(expression, source_binding: irb_context.workspace.binding) do |pa|
result << pa.yield.inspect << "\n\n"
result << pa.message_proc.call
result = pa.yield
inspection = pa.message_proc.call
end

puts result
end
end

if ::IRB::Command.respond_to?(:register)
Command.register(:pa, PaCommand)
else
Command::Pa = PaCommand
if inspection == ''
puts usage
else
puts inspection, "\n"
end

module ExtendCommandBundle
def_extend_command(:irb_pa, :Pa, __FILE__, [:pa, NO_OVERRIDE])
puts "=> #{result.inspect}"
end
end

Command.register(:pa, PaCommand)
end
30 changes: 17 additions & 13 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
require 'warning'
require 'prettyprint'

# How to use => https://test-unit.github.io/test-unit/en/
# https://test-unit.github.io/test-unit/en/
require 'test/unit'

# https://github.com/ruby/test-unit-ruby-core
require 'envutil'
# Test::Unit::TestCase.include Test::Unit::CoreAssertions

Warning[:deprecated] = true
Warning[:experimental] = true

Expand Down Expand Up @@ -52,17 +56,17 @@ def reset
end
end

def execute_lines(*lines, conf: {}, main: self, irb_path: nil)
IRB.init_config(nil)
IRB.conf[:VERBOSE] = false
IRB.conf[:PROMPT_MODE] = :SIMPLE
IRB.conf[:USE_PAGER] = false
IRB.conf.merge!(conf)
input = TestInputMethod.new(lines)
irb = IRB::Irb.new(IRB::WorkSpace.new(main), input)
irb.context.return_format = "=> %s\n"
irb.context.irb_path = irb_path if irb_path
IRB.conf[:MAIN_CONTEXT] = irb.context
irb.eval_input
def save_encodings
@default_encoding = [Encoding.default_external, Encoding.default_internal]
@stdio_encodings = [STDIN, STDOUT, STDERR].map { |io| [io.external_encoding, io.internal_encoding] }
end

def restore_encodings
EnvUtil.suppress_warning do
Encoding.default_external, Encoding.default_internal = *@default_encoding
[STDIN, STDOUT, STDERR].zip(@stdio_encodings) do |io, encs|
io.set_encoding(*encs)
end
end
end
end
Loading

0 comments on commit 33277e6

Please sign in to comment.