Class: RSpecPiccolo::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_piccolo.rb

Overview

RSpecPiccolo Core

Constant Summary collapse

CLASS_TEMPLATE =

RSPec class template

<<-EOS
# encoding: utf-8
require "spec_helper"
require "<%=class_path%>"

describe <%=class_name%> do
<%=reportable_prepare%>
<%=methods_template%>
end
EOS
REPORTABLE_PREPARE =
<<-EOS
  REPORT = "rspec_report"
  DIRS = File.path(__FILE__).gsub(/^.*\\/spec\\//, '').gsub(File.basename(__FILE__), '')
  OUT_DIR = "./#\{REPORT}/#\{DIRS}"
  REPORT_NAME = report_name = File.basename(__FILE__, ".rb")
  REPORT_FILE = "#\{OUT_DIR}#\{REPORT_NAME}.tsv"

  mkspec_report = Proc.new do
Dir.mkdir(REPORT) unless File.exists?(REPORT)
FileUtils.mkdir_p(OUT_DIR) unless File.exists?(OUT_DIR)
File.open(REPORT_FILE, "w") {|f|f.puts "method\\tcase\\ttitle\\tsuccess\\/failure"}
  end.call

  success = Proc.new {|c|File.open(REPORT_FILE, "a") {|f|f.puts "\\tsuccess"}}
  failure = Proc.new {|c|File.open(REPORT_FILE, "a") {|f|f.puts "\\tfailure"}}
EOS
METHOD_TEMPLATE =

RSPec method template

<<-EOS
  context :<%=method_name%> do
    cases = [
      {
        case_no: 1,
        case_title: "case_title",
        expected: "expected",<%=reportable_case%>
      },
    ]

    cases.each do |c|
      it "|case_no=\#{c[:case_no]}|case_title=\#{c[:case_title]}" do
        begin
          case_before c

          # -- given --
          <%=given_src%>

          # -- when --
          # TODO: implement execute code
          <%=when_src%>

          # -- then --
          # TODO: implement assertion code
          # <%=reportable_case_ret%>expect(actual).to eq(c[:expected])
        ensure
          case_after c<%=reportable_case_after%>
        end
      end

      def case_before(c)
        # implement each case before<%=reportable_case_before%>
      end

      def case_after(c)
        # implement each case after
      end
    end
  end
EOS
REPORTABLE_CASE =
<<-EOS

    success_hook: success,
    failure_hook: failure
EOS
REPORTABLE_CASE_BEFORE =
<<-EOS

    File.open(REPORT_FILE, "a") {|f|f.print "method_name\\t\#{c[:case_no]}\\t\#{c[:case_title]}"}
EOS
REPORTABLE_CASE_AFTER =
<<-EOS

      sf_hook = ret ? c[:success_hook] : c[:failure_hook]
      sf_hook.call(c)
EOS
REPORTABLE_CASE_RET =
"ret = "
PRODUCT_CLASS_TEMPLATE =
<<-EOS
# encoding: utf-8
<%=require_rb%>
<%=module_start%>
<%=module_indent%>class <%=class_name%>
<%=fields%><%=methods_template%>
<%=module_indent%>end
<%=module_end%>
EOS

Instance Method Summary collapse

Constructor Details

#initializeCore

initialize



110
111
112
# File 'lib/rspec_piccolo.rb', line 110

def initialize
  @contents = ''
end

Instance Method Details

#generate(class_name, class_path, method_names, options) ⇒ Object

generate rspec test case

params

  • class_name: spec’s module+class full name

  • class_path: spec’s class_path(if you want to create spec/hoge_spec.rb, you should set ‘hoge_spec.rb’)

  • method_names: target class’s method list

  • options: options



120
121
122
123
124
125
126
127
128
# File 'lib/rspec_piccolo.rb', line 120

def generate(class_name, class_path, method_names, options)
  validate_class_name class_name
  validate_class_path class_path
  methods_template = generate_method_template(class_name, method_names, options)
  @contents = generate_class_template(class_name, class_path, methods_template.chop, options)
  create_spec_directory class_path
  File.open("./spec/#{class_path}_spec.rb", 'w:UTF-8') { |f|f.puts @contents }
  output_product_code(class_name, class_path, method_names) if output_product? options
end