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

fix rubcop and spec #3

Merged
merged 2 commits into from
Mar 17, 2024
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
1 change: 1 addition & 0 deletions exe/rspec_tree
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "rspec_tree"

Expand Down
3 changes: 3 additions & 0 deletions lib/rspec_tree/cli.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# frozen_string_literal: true

require "thor"
require "rspec_tree/tree"

module RspecTree
# This class is used to define the CLI
class CLI < Thor
desc "all [file]", "Print all (describe, context, it, etc.)"
def all(file)
Expand Down
17 changes: 14 additions & 3 deletions lib/rspec_tree/tree.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# frozen_string_literal: true

module RspecTree
# This module is used to parse the RSpec file
# and print the tree structure of the RSpec file.
module Tree
Class.class_eval do
def const_missing(name)
name.to_s
end
end

# This class is used to override the RSpec module
module RSpec
class << self
def describe(*args, &block)
Expand All @@ -20,12 +23,16 @@ def describe(*args, &block)
class << self
# @param [String] file
def all(file)
# rubocop:disable Security/Eval
eval(file)
# rubocop:enable Security/Eval
end

# TODO: to be implemented
def ctx(file)
# rubocop:disable Security/Eval
eval(file)
# rubocop:enable Security/Eval
end

private
Expand All @@ -39,15 +46,15 @@ def context(*args, &block)
block.call
end

def example(*args, &block)
def example(*args, &_block)
puts "├────ex: #{args.first}"
end

def it(*args, &block)
def it(*args, &_block)
puts "├────it: #{args.first}"
end

def it_behaves_like(*args, &block)
def it_behaves_like(*args, &_block)
puts "├────it_behaves_like: #{args.first}"
end

Expand All @@ -58,6 +65,10 @@ def require(file); end
def include(*args); end

def method_missing(method, *args, &block); end

def respond_to_missing?(method, *)
super
end
end
end
end
50 changes: 50 additions & 0 deletions spec/data/sample.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

# This file is sample RSpec for test.

require "sample"
require "sample/example"

RSpec.describe Sample do # rubocop:disable Metrics/BlockLength
let(:foo) { "foo" }

before do
# Setup code here
end

describe "First describe" do
let(:bar) { "bar" }

context "First context" do
let(:baz) { "baz" }

it "should do something" do
# Test code here
end

context "First nested context" do
it "should do something" do
# Test code here
end
end

it_behaves_like "shared example"
end
end

describe "Second describe" do
let(:qux) { "qux" }

context "Second context" do
it "should do something else" do
# Test code here
end
end
end

shared_examples "shared example" do
it "should behave in a certain way" do
# Test code here
end
end
end
32 changes: 16 additions & 16 deletions spec/rspec_tree_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# frozen_string_literal: true

RSpec.describe RspecTree do
let(:rspec_file) { File.read("spec/data/sample.rb") }

context "when the version is 0.1.0" do
it "has a version number" do
expect(RspecTree::VERSION).not_to be nil
end

it "does something useful" do
expect(false).to eq(true)
end
end

context "when the version is 0.1.1" do
it "has a version number" do
expect(RspecTree::VERSION).not_to be nil
end
describe ".all" do
subject { RspecTree::Tree.all(rspec_file) }

it "does something useful" do
expect(false).to eq(true)
it "prints all (describe, context, it, etc.)" do
expect { subject }.to output(<<~OUTPUT).to_stdout
desc: Sample
desc: First describe
├──ctx: First context
├────it: should do something
├──ctx: First nested context
├────it: should do something
├────it_behaves_like: shared example
desc: Second describe
├──ctx: Second context
├────it: should do something else
OUTPUT
end
end
end
Loading