Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test builder #111

Merged
merged 4 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 193 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ If you spot any problem, please open an issue.

```bash
$ rake test
# or
$ ruby bin/test-unit.rb
```

### Run specific test(s)
Expand All @@ -27,6 +25,199 @@ $ ruby test/debug/bp_test.rb # run all tests in the specified file
$ ruby test/debug/bp_test.rb -h # to see all the test options
```

## Generate Tests
There is a test generator in `debug.rb` project to make it easier to write tests.
### Quickstart
This section shows you how to create test file by test generator. For more advanced informations on creating tests, please take a look at [gentest options](#gentest-options). (You can also check by `$bin/gentest -h`)
#### 1. Create a target file for debuggee.
Let's say, we created `target.rb` which is located in top level directory of debugger.
```ruby
module Foo
class Bar
def self.a
"hello"
end
end
Bar.a
bar = Bar.new
end
```
#### 2. Run `gentest` as shown in the example below.
```shell
$ bin/gentest target.rb
```
#### 3. Debugger will be executed. You can type any debug commands.
```shell
$ bin/gentest target.rb
[1, 9] in ~/workspace/debug/target.rb
=> 1| module Foo
2| class Bar
3| def self.a
4| "hello"
5| end
6| end
7| Bar.a
8| bar = Bar.new
9| end
=>#0 <main> at ~/workspace/debug/target.rb:1
INTERNAL_INFO: {"location":"~/workspace/debug/target.rb:1","line":1}

(rdbg)s
s
[1, 9] in ~/workspace/debug/target.rb
1| module Foo
=> 2| class Bar
3| def self.a
4| "hello"
5| end
6| end
7| Bar.a
8| bar = Bar.new
9| end
=>#0 <module:Foo> at ~/workspace/debug/target.rb:2
#1 <main> at ~/workspace/debug/target.rb:1
INTERNAL_INFO: {"location":"~/workspace/debug/target.rb:2","line":2}

(rdbg)n
n
[1, 9] in ~/workspace/debug/target.rb
1| module Foo
2| class Bar
=> 3| def self.a
4| "hello"
5| end
6| end
7| Bar.a
8| bar = Bar.new
9| end
=>#0 <class:Bar> at ~/workspace/debug/target.rb:3
#1 <module:Foo> at ~/workspace/debug/target.rb:2
#2 <main> at ~/workspace/debug/target.rb:1
INTERNAL_INFO: {"location":"~/workspace/debug/target.rb:3","line":3}

(rdbg)b 7
b 7
INTERNAL_INFO: {"location":"~/workspace/debug/target.rb:3","line":3}

(rdbg)c
c
[2, 9] in ~/workspace/debug/target.rb
2| class Bar
3| def self.a
4| "hello"
5| end
6| end
=> 7| Bar.a
8| bar = Bar.new
9| end
=>#0 <module:Foo> at ~/workspace/debug/target.rb:7
#1 <main> at ~/workspace/debug/target.rb:1

Stop by #0 BP - Line /Users/naotto/workspace/debug/target.rb:7 (line)
INTERNAL_INFO: {"location":"~/workspace/debug/target.rb:7","line":7}

(rdbg)q!
q!
```
#### 4. The test file will be created as `test/debug/foo_test.rb`.
If the file already exists, **only method** will be added to it.
```ruby
# frozen_string_literal: true

require_relative '../support/test_case'

module DEBUGGER__
class FooTest < TestCase
def program
<<~RUBY
1| module Foo
1| class Bar
2| def self.a
3| "hello"
4| end
5| end
6| Bar.a
7| bar = Bar.new
8| end
RUBY
end

def test_foo
debug_code(program) do
type 's'
assert_line_num 2
assert_line_text([
/[1, 9] in .*/,
/ 1| module Foo/,
/=> 2| class Bar/,
/ 3| def self.a/,
/ 4| "hello"/,
/ 5| end/,
/ 6| end/,
/ 7| Bar.a/,
/ 8| bar = Bar.new/,
/ 9| end/,
/=>#0 <module:Foo> at .*/,
/ #1 <main> at .*/
])
type 'n'
assert_line_num 3
assert_line_text([
/[1, 9] in .*/,
/ 1| module Foo/,
/ 2| class Bar/,
/=> 3| def self.a/,
/ 4| "hello"/,
/ 5| end/,
/ 6| end/,
/ 7| Bar.a/,
/ 8| bar = Bar.new/,
/ 9| end/,
/=>#0 <class:Bar> at .*/,
/ #1 <module:Foo> at .*/,
/ #2 <main> at .*/
])
type 'b 7'
assert_line_text(//)
type 'c'
assert_line_num 7
assert_line_text([
/[2, 9] in .*/,
/ 2| class Bar/,
/ 3| def self.a/,
/ 4| "hello"/,
/ 5| end/,
/ 6| end/,
/=> 7| Bar.a/,
/ 8| bar = Bar.new/,
/ 9| end/,
/=>#0 <module:Foo> at .*/,
/ #1 <main> at .*/,
//,
/Stop by #0 BP - Line .*/
])
type 'q!'
end
end
end
end
```

#### gentest options
You can get more information about `gentest` here.

The default method name is `test_foo` and the class name is `FooTest`. The file name will be `[Lowercase letters with "Test" removed from the class name]_test.rb`.
```shell
# run without any options(test method name will be `test_foo`, class name will be `FooTest`, file name will be `foo_test.rb`)
$ bin/gentest target.rb
# specify the class name(test method name will be `test_foo`, class name will be `StepTest`, file name will be `step_test.rb`)
$ bin/gentest target.rb -c StepTest
# specify the method name(test method name will be `test_step`, class name will be `FooTest`, file name will be `foo_test.rb`)
$ bin/gentest target.rb -m test_step
# specify class name and method name(test method name will be `test_step`, class name will be `StepTest`, file name will be `step_test.rb`.)
$ bin/gentest target.rb -c StepTest -m test_step
```

## To Update README

This project generates `README.md` from the template `misc/README.md.erb`
Expand Down
22 changes: 22 additions & 0 deletions bin/gentest
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env ruby

require 'optparse'

require_relative '../test/tool/test_builder'

file_info = {}

OptionParser.new do |opt|
opt.banner = 'Usage: bin/gentest [file] [option]'
opt.on('-m METHOD', 'Method name in the test file') do |m|
file_info[:method] = m
end
opt.on('-c CLASS', 'Class name in the test file') do |c|
file_info[:class] = c
end
opt.parse!(ARGV)
end

exit if ARGV.empty?

DEBUGGER__::TestBuilder.new(ARGV, file_info[:method], file_info[:class]).start
Loading