diff --git a/lib/rspec_tree/cli.rb b/lib/rspec_tree/cli.rb index 90427fb..5b64946 100644 --- a/lib/rspec_tree/cli.rb +++ b/lib/rspec_tree/cli.rb @@ -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 diff --git a/lib/rspec_tree/tree.rb b/lib/rspec_tree/tree.rb index 9427721..652d5fb 100644 --- a/lib/rspec_tree/tree.rb +++ b/lib/rspec_tree/tree.rb @@ -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 diff --git a/lib/rspec_tree/tree/monkey_patch.rb b/lib/rspec_tree/tree/monkey_patch.rb new file mode 100644 index 0000000..76b4e93 --- /dev/null +++ b/lib/rspec_tree/tree/monkey_patch.rb @@ -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 diff --git a/lib/rspec_tree/tree/rspec.rb b/lib/rspec_tree/tree/rspec.rb new file mode 100644 index 0000000..2678780 --- /dev/null +++ b/lib/rspec_tree/tree/rspec.rb @@ -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 diff --git a/spec/rspec_tree_spec.rb b/spec/rspec_tree_spec.rb index 96e4c1e..4ca4fc2 100644 --- a/spec/rspec_tree_spec.rb +++ b/spec/rspec_tree_spec.rb @@ -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