Skip to content

Commit

Permalink
Merge pull request #21 from BurdetteLamar/classes
Browse files Browse the repository at this point in the history
Classes
  • Loading branch information
BurdetteLamar committed Jul 25, 2018
2 parents fe3f7f2 + 7945c07 commit e976977
Show file tree
Hide file tree
Showing 16 changed files with 197 additions and 3 deletions.
49 changes: 48 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Classes treated in detail:
- [File](#file)
- [Dir](#dir)
- [IO](#io)
- [Exception](#exception)
- [Range](#range)
- [Set](#set)
- [OpenStruct](#openstruct)
Expand Down Expand Up @@ -826,6 +827,52 @@ IO (message='My simple io'):
closed?: false
tty?: false
```
### Exception

#### Simple Exception

This example shows a simple ```Exception```.

```show.rb```:
```ruby
require 'debug_helper'
def foo
exception = nil
begin
raise Exception.new('Boo!')
rescue Exception => exception
# Already saved.
end
DebugHelper.show(exception, 'My simple exception')
end
def bar
foo
end
def baz
bar
end
baz
```

The output shows details of the ```Exception```.

```show.yaml```:
```yaml
---
Exception (message='My simple exception'):
message: Boo!
cause:
backtrace:
- show.rb:6:in `foo'
- show.rb:14:in `bar'
- show.rb:18:in `baz'
- show.rb:21:in `<main>'
```
### Range
#### Inclusive Range
Expand Down Expand Up @@ -1245,7 +1292,7 @@ The output shows details of the datetime.

```show.yaml```:
```yaml
--- 'DateTime (message=''My datetime'') #<DateTime: 2018-07-25T15:20:00-05:00 ((2458325j,73200s,564492000n),-18000s,2299161j)>'
--- 'DateTime (message=''My datetime'') #<DateTime: 2018-07-25T18:07:41-05:00 ((2458325j,83261s,119718000n),-18000s,2299161j)>'
```

#### Regexp
Expand Down
3 changes: 3 additions & 0 deletions lib/debug_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require_relative 'debug_helper/struct_handler'

require_relative 'debug_helper/dir_handler'
require_relative 'debug_helper/exception_handler'
require_relative 'debug_helper/file_handler'
require_relative 'debug_helper/generic_handler'
require_relative 'debug_helper/io_handler'
Expand Down Expand Up @@ -69,6 +70,8 @@ def show(obj, message, info)
EachWithIndexHandler
when obj.kind_of?(Dir)
DirHandler
when obj.kind_of?(Exception)
ExceptionHandler
when obj.kind_of?(File)
FileHandler
when obj.kind_of?(IO)
Expand Down
14 changes: 14 additions & 0 deletions lib/debug_helper/exception_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class DebugHelper

class ExceptionHandler < Handler

def calls_for_instance
[
[:message],
[:cause],
[:backtrace],
]
end
end

end
1 change: 1 addition & 0 deletions markdown/readme/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace :build do
File
Dir
IO
Exception
Range
Set
OpenStruct
Expand Down
1 change: 1 addition & 0 deletions markdown/readme/class_inclusions.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@[:markdown](classes/file/template.md)
@[:markdown](classes/dir/template.md)
@[:markdown](classes/io/template.md)
@[:markdown](classes/exception/template.md)
@[:markdown](classes/range/template.md)
@[:markdown](classes/set/template.md)
@[:markdown](classes/open_struct/template.md)
Expand Down
1 change: 1 addition & 0 deletions markdown/readme/class_links.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [File](#file)
- [Dir](#dir)
- [IO](#io)
- [Exception](#exception)
- [Range](#range)
- [Set](#set)
- [OpenStruct](#openstruct)
44 changes: 44 additions & 0 deletions markdown/readme/classes/exception/simple/show.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#### Simple Exception

This example shows a simple ```Exception```.

```show.rb```:
```ruby
require 'debug_helper'

def foo
exception = nil
begin
raise Exception.new('Boo!')
rescue Exception => exception
# Already saved.
end
DebugHelper.show(exception, 'My simple exception')
end

def bar
foo
end

def baz
bar
end

baz

```

The output shows details of the ```Exception```.

```show.yaml```:
```yaml
---
Exception (message='My simple exception'):
message: Boo!
cause:
backtrace:
- show.rb:6:in `foo'
- show.rb:14:in `bar'
- show.rb:18:in `baz'
- show.rb:21:in `<main>'
```
22 changes: 22 additions & 0 deletions markdown/readme/classes/exception/simple/show.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'debug_helper'

def foo
exception = nil
begin
raise Exception.new('Boo!')
rescue Exception => exception
# Already saved.
end
DebugHelper.show(exception, 'My simple exception')
end

def bar
foo
end

def baz
bar
end

baz

9 changes: 9 additions & 0 deletions markdown/readme/classes/exception/simple/show.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
Exception (message='My simple exception'):
message: Boo!
cause:
backtrace:
- show.rb:6:in `foo'
- show.rb:14:in `bar'
- show.rb:18:in `baz'
- show.rb:21:in `<main>'
9 changes: 9 additions & 0 deletions markdown/readme/classes/exception/simple/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#### Simple Exception

This example shows a simple ```Exception```.

@[ruby](show.rb)

The output shows details of the ```Exception```.

@[yaml](show.yaml)
3 changes: 3 additions & 0 deletions markdown/readme/classes/exception/template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Exception

@[:markdown](simple/show.md)
2 changes: 1 addition & 1 deletion markdown/readme/classes/object/datetime/show.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ The output shows details of the datetime.

```show.yaml```:
```yaml
--- 'DateTime (message=''My datetime'') #<DateTime: 2018-07-25T15:20:00-05:00 ((2458325j,73200s,564492000n),-18000s,2299161j)>'
--- 'DateTime (message=''My datetime'') #<DateTime: 2018-07-25T18:07:41-05:00 ((2458325j,83261s,119718000n),-18000s,2299161j)>'
```
2 changes: 1 addition & 1 deletion markdown/readme/classes/object/datetime/show.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
--- 'DateTime (message=''My datetime'') #<DateTime: 2018-07-25T15:20:00-05:00 ((2458325j,73200s,564492000n),-18000s,2299161j)>'
--- 'DateTime (message=''My datetime'') #<DateTime: 2018-07-25T18:07:41-05:00 ((2458325j,83261s,119718000n),-18000s,2299161j)>'
32 changes: 32 additions & 0 deletions test/debug_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,38 @@ def _test_show_object(test, obj, name, options = {})
end
end

def test_show_exception
def clean_file(actual_file_path)
yaml = YAML.load_file(actual_file_path)
top_key = yaml.keys.first
values = yaml.fetch(top_key)
backtrace = values.delete('backtrace')
assert_match(File.basename(__FILE__), backtrace.first)
yaml.store(top_key, values)
File.write(actual_file_path, YAML.dump(yaml))
end
exception = nil
begin
raise Exception.new('Boo!')
rescue Exception => exception
# It's saved.
end
name = :test_exception
_test_show(self, :show, exception, name) do |expected_file_path,
actual_file_path|
DebugHelperTest.write_stdout(actual_file_path) do
DebugHelper.send(:show, exception, name)
end
clean_file(actual_file_path)
_test_show(self, :putd, exception, name) do |expected_file_path, actual_file_path|
DebugHelperTest.write_stdout(actual_file_path) do
putd(exception, name)
end
clean_file(actual_file_path)
end
end
end

def test_show_object
# To remove volatile values from the captured output.
def clean_file(actual_file_path)
Expand Down
4 changes: 4 additions & 0 deletions test/show/actual/test_exception.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
Exception (message='test_exception'):
message: Boo!
cause:
4 changes: 4 additions & 0 deletions test/show/expected/test_exception.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
Exception (message='test_exception'):
message: Boo!
cause:

0 comments on commit e976977

Please sign in to comment.