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 nested context #4

Merged
merged 3 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
4 changes: 2 additions & 2 deletions lib/rspec_tree/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class CLI < Thor
desc "all [file]", "Print all (describe, context, it, etc.)"
def all(file)
File.open(file, "r") do |f|
Tree.all(f.read)
Tree.new(f.read, :all).print
end
end

desc "ctx [file]", "Print only describe and context"
def ctx(file)
File.open(file, "r") do |f|
Tree.ctx(f.read)
Tree.new(f.read, :ctx).print
end
end
end
Expand Down
91 changes: 36 additions & 55 deletions lib/rspec_tree/tree.rb
Original file line number Diff line number Diff line change
@@ -1,74 +1,55 @@
# frozen_string_literal: true

require_relative "tree/rspec"
require_relative "tree/monkey_patch"

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
class Tree
attr_reader :file, :type
attr_accessor :base_depth

def initialize(file, type)
@file = file
@type = type
@base_depth = 0
end

# This class is used to override the RSpec module
module RSpec
class << self
def describe(*args, &block)
puts "desc: #{args.first}"
block.call
end
end
def print
eval(file) # rubocop:disable Security/Eval
end

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

def describe(*args, &block)
RSpec.describe(*args, &block)
end

def context(*args, &block)
puts "├──ctx: #{args.first}"
block.call
end

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

def it(*args, &_block)
puts "├────it: #{args.first}"
end
def describe(*args, &block)
self.base_depth = caller.size
RSpec.describe(*args, &block)
end

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

def require_relative(file); end
def example(*args, &_block)
tree(args.first, "ex")
end

def require(file); end
def it(*args, &_block)
tree(args.first, "it")
end

def include(*args); end
def it_behaves_like(*args, &_block)
tree(args.first, "it_behaves_like")
end

def method_missing(method, *args, &block); end
def dash
"─" * (caller.size - base_depth)
end

def respond_to_missing?(method, *)
super
end
def tree(arg, name)
puts "├#{dash}#{name}: #{arg}"
end
end
end
26 changes: 26 additions & 0 deletions lib/rspec_tree/tree/monkey_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module RspecTree
# This is a monkey patch to override basic behavior
class Tree
Class.class_eval do
def const_missing(name)
name.to_s
end
end

private

def require_relative(file); end

def require(file); end

def include(*args); end

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

def respond_to_missing?(method, *)
super
end
end
end
15 changes: 15 additions & 0 deletions lib/rspec_tree/tree/rspec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module RspecTree
class Tree
# This module is used to override the RSpec module
module RSpec
class << self
def describe(*args, &block)
puts "desc: #{args.first}"
block.call
end
end
end
end
end
16 changes: 8 additions & 8 deletions spec/rspec_tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
let(:rspec_file) { File.read("spec/data/sample.rb") }

describe ".all" do
subject { RspecTree::Tree.all(rspec_file) }
subject { RspecTree::Tree.new(rspec_file, :all).print }

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
├─────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
├─────ctx: Second context
├───────it: should do something else
OUTPUT
end
end
Expand Down
Loading