Skip to content

Commit b064195

Browse files
authored
Merge pull request #12 from sasamuku/refactor_using_prism
Refactor using prism
2 parents 81d16ab + 4fa2974 commit b064195

File tree

20 files changed

+395
-157
lines changed

20 files changed

+395
-157
lines changed

.github/workflows/main.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ jobs:
1414
strategy:
1515
matrix:
1616
ruby:
17-
- '3.1.2'
17+
- "3.3.1"
1818

1919
steps:
20-
- uses: actions/checkout@v3
21-
- name: Set up Ruby
22-
uses: ruby/setup-ruby@v1
23-
with:
24-
ruby-version: ${{ matrix.ruby }}
25-
bundler-cache: true
26-
- name: Run the default task
27-
run: bundle exec rake
20+
- uses: actions/checkout@v3
21+
- name: Set up Ruby
22+
uses: ruby/setup-ruby@v1
23+
with:
24+
ruby-version: ${{ matrix.ruby }}
25+
bundler-cache: true
26+
- name: Run the default task
27+
run: bundle exec rake

.rubocop.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
AllCops:
2-
TargetRubyVersion: 2.6
2+
TargetRubyVersion: 3.3.1
33

44
Style/StringLiterals:
55
Enabled: true
@@ -11,3 +11,14 @@ Style/StringLiteralsInInterpolation:
1111

1212
Layout/LineLength:
1313
Max: 120
14+
15+
Style/Documentation:
16+
Enabled: false
17+
18+
Metrics/AbcSize:
19+
Max: 30
20+
21+
Metrics/BlockLength:
22+
Exclude:
23+
- "spec/**/*"
24+
- "config/routes.rb"

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.3.1

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ gem "rake", "~> 13.0"
1010
gem "rspec", "~> 3.0"
1111

1212
gem "rubocop", "~> 1.21"
13+
14+
gem "prism", "~> 1.3.0"

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ GEM
1515
parser (3.3.0.5)
1616
ast (~> 2.4.1)
1717
racc
18+
prism (1.3.0)
1819
racc (1.7.3)
1920
rainbow (3.1.1)
2021
rake (13.1.0)
@@ -56,6 +57,7 @@ PLATFORMS
5657
x86_64-linux
5758

5859
DEPENDENCIES
60+
prism (~> 1.3.0)
5961
rake (~> 13.0)
6062
rspec (~> 3.0)
6163
rspec_tree!

README.md

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# RspecTree
22

3-
This is tree command for rspec test files.
3+
`RspecTree` is a command-line tool for displaying the structure of RSpec test files in a tree format. It uses [prism](https://github.com/ruby/prism), the default parser introduced in Ruby 3.4, to parse and traverse the AST.
44

55
## Installation
66

@@ -14,31 +14,42 @@ If bundler is not being used to manage dependencies, install the gem by executin
1414

1515
## Usage
1616

17-
```
17+
### Display All Describes, Contexts, and Examples
18+
19+
Use the `all` option to print all `describe`, `context`, and `example` blocks in a file:
20+
21+
```bash
1822
$ rspec_tree all /path/to/your_spec.rb
19-
desc: Sample
20-
desc: First describe
21-
├─────ctx: First context
22-
├───────it: should do something
23-
├───────ctx: First nested context
24-
├─────────it: should do something
25-
├───────it_behaves_like: shared example
26-
desc: Second describe
27-
├─────ctx: Second context
28-
├───────it: should do something else
23+
desc: SampleClass
24+
├──desc: First describe
25+
├────ctx: First context
26+
├──────ctx: First nested context
27+
├────────it: should do something
28+
├──────it: should do something
29+
├──desc: Second describe
30+
├────ctx: Second context
31+
├──────it: should do something else
2932
```
3033

31-
```
34+
### Display Only Describes and Contexts
35+
36+
Use the `ctx` option to print only `describe` and `context` blocks:
37+
38+
```bash
3239
$ rspec_tree ctx /path/to/your_spec.rb
33-
desc: Sample
34-
desc: First describe
35-
├────ctx: First context
36-
├──────ctx: First nested context
37-
desc: Second describe
38-
├────ctx: Second context
40+
desc: SampleClass
41+
├──desc: First describe
42+
├────ctx: First context
43+
├──────ctx: First nested context
44+
├──desc: Second describe
45+
├────ctx: Second context
3946
```
4047

41-
```
48+
### Display Help Information
49+
50+
Run the `help` command to see all available commands:
51+
52+
```bash
4253
$ rspec_tree help
4354
Commands:
4455
rspec_tree all [file] # Print all (describe, context, it, etc.)

lib/rspec_tree/cli.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,26 @@
22

33
require "thor"
44
require "rspec_tree/tree"
5+
require "rspec_tree/visitor"
56

67
module RspecTree
78
# This class is used to define the CLI
89
class CLI < Thor
910
desc "all [file]", "Print all (describe, context, it, etc.)"
1011
def all(file)
1112
File.open(file, "r") do |f|
12-
Tree.new(f.read, :all).print
13+
visitor = RspecTree::Visitor.new
14+
Prism.parse(f.read).value.accept(visitor)
15+
RspecTree::Tree.new(visitor.root, :all).print
1316
end
1417
end
1518

1619
desc "ctx [file]", "Print only describe and context"
1720
def ctx(file)
1821
File.open(file, "r") do |f|
19-
Tree.new(f.read, :ctx).print
22+
visitor = RspecTree::Visitor.new
23+
Prism.parse(f.read).value.accept(visitor)
24+
RspecTree::Tree.new(visitor.root, :ctx).print
2025
end
2126
end
2227
end

lib/rspec_tree/context.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module RspecTree
4+
class Context
5+
attr_accessor :title, :contexts, :examples
6+
7+
def initialize
8+
@title = nil
9+
@contexts = []
10+
@examples = []
11+
end
12+
end
13+
end

lib/rspec_tree/description.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
module RspecTree
4+
class Description
5+
attr_accessor :title, :contexts
6+
7+
def initialize
8+
@title = nil
9+
@contexts = []
10+
end
11+
end
12+
end

lib/rspec_tree/example.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
module RspecTree
4+
class Example
5+
attr_accessor :title
6+
7+
def initialize
8+
@title = nil
9+
end
10+
end
11+
end

0 commit comments

Comments
 (0)