diff --git a/Gemfile b/Gemfile index c4dc0af..3c13383 100644 --- a/Gemfile +++ b/Gemfile @@ -10,3 +10,4 @@ gem "rake", "~> 13.0" gem "minitest", "~> 5.0" gem "rubocop", "~> 1.21" +gem "debug" diff --git a/README.md b/README.md index 619d5a8..28d9177 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,12 @@ It's early days and this work is still experimental. Here's what it can do now ### Capabilities - describe -> class +- context -> class - it -> def test_* - expect(actual).to eq(expected) -> assert_equal(expected, actual) +- expect(actual).to be_falsey -> refute(actual) +- expect(actual).to be_truthy -> assert(actual) +- expect(actual).to be_empty -> assert_empty(actual) ## Installation @@ -22,7 +26,11 @@ If bundler is not being used to manage dependencies, install the gem by executin ## Usage ``` -minitestify print FILES # Convert one or more specs to minitest and print to standard out +Usage: minitestify [options] + -v, --version Print version + -h, --help Prints this help + -r, --rails Use more railsy syntax + -s, --save Replace spec with test in path & file name. Write to new file ``` ## Development @@ -31,6 +39,37 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org). +### Testing + +``` +rake test +``` + +### Development Tips + +Use the `stree` cli to output matchers. + +```bash +=> stree expr -e 'expect(false).to be_falsey' +SyntaxTree::CommandCall[ + receiver: SyntaxTree::CallNode[ + receiver: nil, + operator: nil, + message: SyntaxTree::Ident[value: "expect"], + arguments: SyntaxTree::ArgParen[ + arguments: SyntaxTree::Args[ + parts: [SyntaxTree::VarRef[value: SyntaxTree::Kw[value: "false"]]] + ] + ] + ], + operator: SyntaxTree::Period[value: "."], + message: SyntaxTree::Ident[value: "to"], + arguments: SyntaxTree::Args[ + parts: [SyntaxTree::VCall[value: SyntaxTree::Ident[value: "be_falsey"]]] + ] +] +``` + ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/minitestify. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/minitestify/blob/master/CODE_OF_CONDUCT.md). diff --git a/lib/minitestify/cli.rb b/lib/minitestify/cli.rb index 1ae09eb..3d6b808 100644 --- a/lib/minitestify/cli.rb +++ b/lib/minitestify/cli.rb @@ -8,6 +8,7 @@ module Minitestify::CLI module_function def run options = {} + save = false OptionParser .new do |parser| parser.banner = "Usage: minitestify [options] " @@ -21,14 +22,26 @@ module Minitestify::CLI puts(parser) exit end + + parser.on("-r", "--rails", "Use more railsy syntax") do + options[:rails] = true + end + + parser.on("-s", "--save", "Replace spec with test in path & file name. Write to new file") do + save = true + end end .parse! ARGV.each do |file| - spec = Minitestify::Spec.new(file: file) - puts("# #{spec.to_test_filepath}") - puts(spec.to_test_code) - puts + spec = Minitestify::Spec.new(file: file, **options) + if save + puts("Writing to #{spec.to_test_filepath}") + File.write(spec.to_test_filepath, spec.to_test_code) + else + puts(spec.to_test_code) + end end + puts end end diff --git a/lib/minitestify/mutations/base.rb b/lib/minitestify/mutations/base.rb new file mode 100644 index 0000000..0466db4 --- /dev/null +++ b/lib/minitestify/mutations/base.rb @@ -0,0 +1,12 @@ +module Minitestify + module Mutations + class Base + attr_reader :file, :rails + + def initialize file:, rails: false, **args + @file = file + @rails = rails + end + end + end +end diff --git a/lib/minitestify/mutations/be_empty.rb b/lib/minitestify/mutations/be_empty.rb new file mode 100644 index 0000000..63928da --- /dev/null +++ b/lib/minitestify/mutations/be_empty.rb @@ -0,0 +1,64 @@ +require_relative "base" + +module Minitestify + module Mutations + class BeEmpty < Base + def add_mutation!(visitor) + expect_be_empty_search = + 'CommandCall[ + receiver: CallNode[ + message: Ident[value: "expect"], + ], + operator: Period[value: "."], + message: Ident[value: "to"], + arguments: Args[ + parts: [VCall[ + value: Ident[value: "be_empty"] + ]] + ] + ]' + visitor.mutate(expect_be_empty_search) do |node| + node => SyntaxTree::CommandCall[ + receiver: SyntaxTree::CallNode[ + receiver: nil, + operator: nil, + message: SyntaxTree::Ident[value: "expect"], + arguments: SyntaxTree::ArgParen[ + arguments: SyntaxTree::Args[parts: [actual_expr]] + ] + ], + operator: SyntaxTree::Period[value: "."], + message: SyntaxTree::Ident[value: "to"], + arguments: SyntaxTree::Args[ + parts: [ + SyntaxTree::VCall[ + value: SyntaxTree::Ident[value: "be_empty"], + ] + ] + ] + ] + + SyntaxTree::CallNode.new( + message: + SyntaxTree::Ident.new( + value: "assert_empty", + location: node.location + ), + arguments: + SyntaxTree::ArgParen.new( + arguments: + SyntaxTree::Args.new( + parts: [actual_expr], + location: node.location + ), + location: node.location + ), + location: node.location, + receiver: nil, + operator: nil + ) + end + end + end + end +end diff --git a/lib/minitestify/mutations/be_falsey.rb b/lib/minitestify/mutations/be_falsey.rb new file mode 100644 index 0000000..b8c25d4 --- /dev/null +++ b/lib/minitestify/mutations/be_falsey.rb @@ -0,0 +1,64 @@ +require_relative "base" + +module Minitestify + module Mutations + class BeFalsey < Base + def add_mutation!(visitor) + expect_be_falsey_search = + 'CommandCall[ + receiver: CallNode[ + message: Ident[value: "expect"], + ], + operator: Period[value: "."], + message: Ident[value: "to"], + arguments: Args[ + parts: [VCall[ + value: Ident[value: "be_falsey"] + ]] + ] + ]' + visitor.mutate(expect_be_falsey_search) do |node| + node => SyntaxTree::CommandCall[ + receiver: SyntaxTree::CallNode[ + receiver: nil, + operator: nil, + message: SyntaxTree::Ident[value: "expect"], + arguments: SyntaxTree::ArgParen[ + arguments: SyntaxTree::Args[parts: [actual_expr]] + ] + ], + operator: SyntaxTree::Period[value: "."], + message: SyntaxTree::Ident[value: "to"], + arguments: SyntaxTree::Args[ + parts: [ + SyntaxTree::VCall[ + value: SyntaxTree::Ident[value: "be_falsey"], + ] + ] + ] + ] + + SyntaxTree::CallNode.new( + message: + SyntaxTree::Ident.new( + value: "refute", + location: node.location + ), + arguments: + SyntaxTree::ArgParen.new( + arguments: + SyntaxTree::Args.new( + parts: [actual_expr], + location: node.location + ), + location: node.location + ), + location: node.location, + receiver: nil, + operator: nil + ) + end + end + end + end +end diff --git a/lib/minitestify/mutations/be_truthy.rb b/lib/minitestify/mutations/be_truthy.rb new file mode 100644 index 0000000..ecb555e --- /dev/null +++ b/lib/minitestify/mutations/be_truthy.rb @@ -0,0 +1,64 @@ +require_relative "base" + +module Minitestify + module Mutations + class BeTruthy < Base + def add_mutation!(visitor) + expect_be_truthy_search = + 'CommandCall[ + receiver: CallNode[ + message: Ident[value: "expect"], + ], + operator: Period[value: "."], + message: Ident[value: "to"], + arguments: Args[ + parts: [VCall[ + value: Ident[value: "be_truthy"] + ]] + ] + ]' + visitor.mutate(expect_be_truthy_search) do |node| + node => SyntaxTree::CommandCall[ + receiver: SyntaxTree::CallNode[ + receiver: nil, + operator: nil, + message: SyntaxTree::Ident[value: "expect"], + arguments: SyntaxTree::ArgParen[ + arguments: SyntaxTree::Args[parts: [actual_expr]] + ] + ], + operator: SyntaxTree::Period[value: "."], + message: SyntaxTree::Ident[value: "to"], + arguments: SyntaxTree::Args[ + parts: [ + SyntaxTree::VCall[ + value: SyntaxTree::Ident[value: "be_truthy"], + ] + ] + ] + ] + + SyntaxTree::CallNode.new( + message: + SyntaxTree::Ident.new( + value: "assert", + location: node.location + ), + arguments: + SyntaxTree::ArgParen.new( + arguments: + SyntaxTree::Args.new( + parts: [actual_expr], + location: node.location + ), + location: node.location + ), + location: node.location, + receiver: nil, + operator: nil + ) + end + end + end + end +end diff --git a/lib/minitestify/mutations/describe.rb b/lib/minitestify/mutations/describe.rb new file mode 100644 index 0000000..15d8074 --- /dev/null +++ b/lib/minitestify/mutations/describe.rb @@ -0,0 +1,73 @@ +require_relative "base" + +module Minitestify + module Mutations + class Describe < Base + def add_mutation!(visitor) + inflector = Dry::Inflector.new + + # describe|context -> class + describe_node = ->(node) do + case node + in SyntaxTree::Command[ + message: SyntaxTree::Ident[value: "describe"], + arguments: SyntaxTree::Args[ + parts: [SyntaxTree::VarRef[value: SyntaxTree::Const[value: value]]] + ] + ] => node + in SyntaxTree::Command[ + message: SyntaxTree::Ident[value: "describe"], + arguments: SyntaxTree::Args[ + SyntaxTree::StringLiteral[ + parts: [SyntaxTree::TStringContent[value: value]] + ] + ] + ] => node + in SyntaxTree::Command[ + message: SyntaxTree::Ident[value: "context"], + arguments: SyntaxTree::Args[ + SyntaxTree::StringLiteral[ + parts: [SyntaxTree::TStringContent[value: value]] + ] + ] + ] => node + end + + value = value.tr("'", "").gsub(/\W/, "_") + + SyntaxTree::ClassDeclaration.new( + constant: + SyntaxTree::ConstRef.new( + constant: + SyntaxTree::Const.new( + value: "#{inflector.camelize_upper(value)}Test", + location: node.location + ), + location: node.location + ), + superclass: + SyntaxTree::ConstPathRef.new( + parent: + SyntaxTree::VarRef.new( + value: + SyntaxTree::Const.new( + value: "Minitest", + location: node.location + ), + location: node.location + ), + constant: + SyntaxTree::Const.new(value: "Test", location: node.location), + location: node.location + ), + bodystmt: node.child_nodes.last.bodystmt, + location: node.location + ) + end + + visitor.mutate("Command[ message: Ident[value: \"describe\"] ]", &describe_node) + visitor.mutate("Command[ message: Ident[value: \"context\"] ]", &describe_node) + end + end + end +end diff --git a/lib/minitestify/mutations/eq.rb b/lib/minitestify/mutations/eq.rb new file mode 100644 index 0000000..b5eb435 --- /dev/null +++ b/lib/minitestify/mutations/eq.rb @@ -0,0 +1,70 @@ +require_relative "base" + +module Minitestify + module Mutations + class Eq < Base + def add_mutation!(visitor) + expect_eq_search = + 'CommandCall[ + receiver: CallNode[ + message: Ident[value: "expect"] + ], + operator: Period[value: "."], + message: Ident[value: "to"], + arguments: Args[ + parts: [ CallNode[ + message: Ident[value: "eq"] + ] + ] + ] + ]' + visitor.mutate(expect_eq_search) do |node| + node => SyntaxTree::CommandCall[ + receiver: SyntaxTree::CallNode[ + receiver: nil, + operator: nil, + message: SyntaxTree::Ident[value: "expect"], + arguments: SyntaxTree::ArgParen[ + arguments: SyntaxTree::Args[parts: [actual_expr]] + ] + ], + operator: SyntaxTree::Period[value: "."], + message: SyntaxTree::Ident[value: "to"], + arguments: SyntaxTree::Args[ + parts: [ + SyntaxTree::CallNode[ + receiver: nil, + operator: nil, + message: SyntaxTree::Ident[value: "eq"], + arguments: SyntaxTree::ArgParen[ + arguments: SyntaxTree::Args[parts: [expected_expr]] + ] + ] + ] + ] + ] + + SyntaxTree::CallNode.new( + message: + SyntaxTree::Ident.new( + value: "assert_equal", + location: node.location + ), + arguments: + SyntaxTree::ArgParen.new( + arguments: + SyntaxTree::Args.new( + parts: [expected_expr, actual_expr], + location: node.location + ), + location: node.location + ), + location: node.location, + receiver: nil, + operator: nil + ) + end + end + end + end +end diff --git a/lib/minitestify/mutations/it.rb b/lib/minitestify/mutations/it.rb new file mode 100644 index 0000000..c27def2 --- /dev/null +++ b/lib/minitestify/mutations/it.rb @@ -0,0 +1,45 @@ +require_relative "base" + +module Minitestify + module Mutations + class It < Base + def add_mutation!(visitor) + # it -> def + visitor.mutate("Command[message: Ident[value: \"it\"]]") do |node| + node => SyntaxTree::Command[ + message: SyntaxTree::Ident[value: "it"], + arguments: SyntaxTree::Args[ + parts: [ + SyntaxTree::StringLiteral[ + parts: [SyntaxTree::TStringContent[value: value]] + ] + ] + ] + ] + + if rails + SyntaxTree::Command.new( + message: SyntaxTree::Ident.new(value: "test", location: node.message.location), + arguments: node.arguments, + block: node.block, + location: node.location + ) + else + SyntaxTree::DefNode.new( + target: nil, + operator: nil, + name: + SyntaxTree::Ident.new( + value: "test_#{value.tr("'", "").gsub(/\W/, "_").downcase}", + location: node.location + ), + params: SyntaxTree::Params, + bodystmt: node.child_nodes.last.bodystmt, + location: node.location + ) + end + end + end + end + end +end diff --git a/lib/minitestify/spec.rb b/lib/minitestify/spec.rb index bd0b270..3cdb9ea 100644 --- a/lib/minitestify/spec.rb +++ b/lib/minitestify/spec.rb @@ -1,156 +1,57 @@ # frozen_string_literal: true +require "pathname" require "minitestify" +require "minitestify/mutations/base" +require "minitestify/mutations/describe" +require "minitestify/mutations/it" +require "minitestify/mutations/eq" +require "minitestify/mutations/be_truthy" +require "minitestify/mutations/be_falsey" +require "minitestify/mutations/be_empty" module Minitestify class Spec - def initialize(file:) + attr_reader :rails, :file, :source + + def initialize(file:, source: nil, rails: false) @file = file + @source = source || SyntaxTree.read(@file) + @rails = rails end def to_test_filepath - @file.gsub "spec", "test" - # TODO be smarter here and dissect the file path - # replacing only the spec dir (if present) - # and the _spec.rb suffix + path = Pathname.new(file) + parts = path.each_filename.map do |part| + if part == "spec" + "test" + else + part.gsub("_spec.rb", "_test.rb") + end + end + + parts.unshift("/") if path.absolute? + Pathname.new("").join(*parts).to_s end def to_test_code - source = SyntaxTree.read(@file) - program = SyntaxTree.parse(source) + program = SyntaxTree.parse(@source) visitor = SyntaxTree::Visitor::MutationVisitor.new - inflector = Dry::Inflector.new - # describe -> class - visitor.mutate("Command[ message: Ident[value: \"describe\"] ]") do |node| - node => SyntaxTree::Command[ - - message: SyntaxTree::Ident[value: "describe"], - arguments: SyntaxTree::Args[ - parts: [SyntaxTree::VarRef[value: SyntaxTree::Const[value: value]]] - ] - ] - - SyntaxTree::ClassDeclaration.new( - constant: - SyntaxTree::ConstRef.new( - constant: - SyntaxTree::Const.new( - value: "#{inflector.camelize_upper(value)}Test", - location: node.location - ), - location: node.location - ), - superclass: - SyntaxTree::ConstPathRef.new( - parent: - SyntaxTree::VarRef.new( - value: - SyntaxTree::Const.new( - value: "Minitest", - location: node.location - ), - location: node.location - ), - constant: - SyntaxTree::Const.new(value: "Test", location: node.location), - location: node.location - ), - bodystmt: node.child_nodes.last.bodystmt, - location: node.location - ) - end + args = { + file: file, + rails: rails + } - # it -> def - visitor.mutate("Command[message: Ident[value: \"it\"]]") do |node| - node => SyntaxTree::Command[ - message: SyntaxTree::Ident[value: "it"], - arguments: SyntaxTree::Args[ - parts: [ - SyntaxTree::StringLiteral[ - parts: [SyntaxTree::TStringContent[value: value]] - ] - ] - ] - ] - - SyntaxTree::DefNode.new( - target: nil, - operator: nil, - name: - SyntaxTree::Ident.new( - value: "test_#{value.gsub(" ", "_")}", - location: node.location - ), - params: SyntaxTree::Params, - bodystmt: node.child_nodes.last.bodystmt, - location: node.location - ) - end - - expect_eq_search = - 'CommandCall[ - receiver: CallNode[ - message: Ident[value: "expect"] - ], - operator: Period[value: "."], - message: Ident[value: "to"], - arguments: Args[ - parts: [ CallNode[ - message: Ident[value: "eq"] - ] - ] - ] - ]' - visitor.mutate(expect_eq_search) do |node| - node => SyntaxTree::CommandCall[ - receiver: SyntaxTree::CallNode[ - receiver: nil, - operator: nil, - message: SyntaxTree::Ident[value: "expect"], - arguments: SyntaxTree::ArgParen[ - arguments: SyntaxTree::Args[parts: [actual_expr]] - ] - ], - operator: SyntaxTree::Period[value: "."], - message: SyntaxTree::Ident[value: "to"], - arguments: SyntaxTree::Args[ - parts: [ - SyntaxTree::CallNode[ - receiver: nil, - operator: nil, - message: SyntaxTree::Ident[value: "eq"], - arguments: SyntaxTree::ArgParen[ - arguments: SyntaxTree::Args[parts: [expected_expr]] - ] - ] - ] - ] - ] - - SyntaxTree::CallNode.new( - message: - SyntaxTree::Ident.new( - value: "assert_equal", - location: node.location - ), - arguments: - SyntaxTree::ArgParen.new( - arguments: - SyntaxTree::Args.new( - parts: [expected_expr, actual_expr], - location: node.location - ), - location: node.location - ), - location: node.location, - receiver: nil, - operator: nil - ) - end + Mutations::Describe.new(**args).add_mutation!(visitor) + Mutations::It.new(**args).add_mutation!(visitor) + Mutations::Eq.new(**args).add_mutation!(visitor) + Mutations::BeTruthy.new(**args).add_mutation!(visitor) + Mutations::BeFalsey.new(**args).add_mutation!(visitor) + Mutations::BeEmpty.new(**args).add_mutation!(visitor) - SyntaxTree::Formatter.format(source, program.accept(visitor)) + SyntaxTree::Formatter.format(@source, program.accept(visitor)) end end end diff --git a/spec/calculator_spec.rb b/spec/calculator_spec.rb index ffa344b..396cab2 100644 --- a/spec/calculator_spec.rb +++ b/spec/calculator_spec.rb @@ -1,4 +1,4 @@ -describe Calculator do +describe "Calculator" do it "can add" do calculator = Calculator.new expect(calculator.add(1, 1)).to eq(2) diff --git a/test/minitestify/mutations/test_be_empty.rb b/test/minitestify/mutations/test_be_empty.rb new file mode 100644 index 0000000..c6d92f8 --- /dev/null +++ b/test/minitestify/mutations/test_be_empty.rb @@ -0,0 +1,24 @@ +require "test_helper" +require "debug" +require "minitestify/spec" + +class Minitestify::Mutations::TestBeEmpty < Minitest::Test + def test_be_falsey + from = <<~RUBY + class CalculatorTest < Minitest::Test + def test_can_add + expect(user.errors[:invitation_code]).to be_empty + end + end + RUBY + expected = <<~RUBY + class CalculatorTest < Minitest::Test + def test_can_add + assert_empty(user.errors[:invitation_code]) + end + end + RUBY + spec = Minitestify::Spec.new(file: "test", source: from) + assert_equal(expected, spec.to_test_code) + end +end diff --git a/test/minitestify/mutations/test_be_falsey.rb b/test/minitestify/mutations/test_be_falsey.rb new file mode 100644 index 0000000..5c61e41 --- /dev/null +++ b/test/minitestify/mutations/test_be_falsey.rb @@ -0,0 +1,26 @@ +require "test_helper" +require "debug" +require "minitestify/spec" + +class Minitestify::Mutations::TestBeFalsey < Minitest::Test + def test_be_falsey + from = <<~RUBY + class CalculatorTest < Minitest::Test + def test_can_add + testing = false + expect(testing).to be_falsey + end + end + RUBY + expected = <<~RUBY + class CalculatorTest < Minitest::Test + def test_can_add + testing = false + refute(testing) + end + end + RUBY + spec = Minitestify::Spec.new(file: "test", source: from) + assert_equal(expected, spec.to_test_code) + end +end diff --git a/test/minitestify/mutations/test_be_truthy.rb b/test/minitestify/mutations/test_be_truthy.rb new file mode 100644 index 0000000..cc0edd4 --- /dev/null +++ b/test/minitestify/mutations/test_be_truthy.rb @@ -0,0 +1,26 @@ +require "test_helper" +require "debug" +require "minitestify/spec" + +class Minitestify::Mutations::TestBeTruthy < Minitest::Test + def test_be_truthy + from = <<~RUBY + class CalculatorTest < Minitest::Test + def test_can_add + testing = false + expect(testing).to be_truthy + end + end + RUBY + expected = <<~RUBY + class CalculatorTest < Minitest::Test + def test_can_add + testing = false + assert(testing) + end + end + RUBY + spec = Minitestify::Spec.new(file: "test", source: from) + assert_equal(expected, spec.to_test_code) + end +end diff --git a/test/minitestify/mutations/test_describe.rb b/test/minitestify/mutations/test_describe.rb new file mode 100644 index 0000000..21064ba --- /dev/null +++ b/test/minitestify/mutations/test_describe.rb @@ -0,0 +1,48 @@ +require "test_helper" +require "debug" +require "minitestify/spec" + +class Minitestify::Mutations::TestDescribe < Minitest::Test + def test_describe_class + from = <<~RUBY + describe Calculator do + end + RUBY + expected = <<~RUBY + class CalculatorTest < Minitest::Test + end + RUBY + spec = Minitestify::Spec.new(file: "test", source: from) + assert_equal(expected, spec.to_test_code) + end + + def test_describe_string + from = <<~RUBY + describe "my calculator" do + end + RUBY + expected = <<~RUBY + class MyCalculatorTest < Minitest::Test + end + RUBY + spec = Minitestify::Spec.new(file: "test", source: from) + assert_equal(expected, spec.to_test_code) + end + + def test_context_string + from = <<~RUBY + describe Calculator do + context "when adding" do + end + end + RUBY + expected = <<~RUBY + class CalculatorTest < Minitest::Test + class WhenAddingTest < Minitest::Test + end + end + RUBY + spec = Minitestify::Spec.new(file: "test", source: from) + assert_equal(expected, spec.to_test_code) + end +end diff --git a/test/minitestify/mutations/test_eq.rb b/test/minitestify/mutations/test_eq.rb new file mode 100644 index 0000000..8e50ba8 --- /dev/null +++ b/test/minitestify/mutations/test_eq.rb @@ -0,0 +1,24 @@ +require "test_helper" +require "debug" +require "minitestify/spec" + +class Minitestify::Mutations::TestEq < Minitest::Test + def test_eq + from = <<~RUBY + class CalculatorTest < Minitest::Test + def test_can_add + expect(testing).to eq(true) + end + end + RUBY + expected = <<~RUBY + class CalculatorTest < Minitest::Test + def test_can_add + assert_equal(true, testing) + end + end + RUBY + spec = Minitestify::Spec.new(file: "test", source: from) + assert_equal(expected, spec.to_test_code) + end +end diff --git a/test/minitestify/mutations/test_it.rb b/test/minitestify/mutations/test_it.rb new file mode 100644 index 0000000..3123335 --- /dev/null +++ b/test/minitestify/mutations/test_it.rb @@ -0,0 +1,43 @@ +require "test_helper" +require "debug" +require "minitestify/spec" + +class Minitestify::Mutations::TestIt < Minitest::Test + def test_it + from = <<~RUBY + class CalculatorTest < Minitest::Test + it "can add" do + expect(testing).to eq(true) + end + end + RUBY + expected = <<~RUBY + class CalculatorTest < Minitest::Test + def test_can_add + assert_equal(true, testing) + end + end + RUBY + spec = Minitestify::Spec.new(file: "test", source: from) + assert_equal(expected, spec.to_test_code) + end + + def test_it_rails + from = <<~RUBY + class CalculatorTest < Minitest::Test + it "can add" do + expect(testing).to eq(true) + end + end + RUBY + expected = <<~RUBY + class CalculatorTest < Minitest::Test + test "can add" do + assert_equal(true, testing) + end + end + RUBY + spec = Minitestify::Spec.new(file: "test", source: from, rails: true) + assert_equal(expected, spec.to_test_code) + end +end diff --git a/test/minitestify/test_spec.rb b/test/minitestify/test_spec.rb index 83b5c5c..605e7eb 100644 --- a/test/minitestify/test_spec.rb +++ b/test/minitestify/test_spec.rb @@ -1,15 +1,17 @@ # frozen_string_literal: true require "test_helper" +require "debug" require "minitestify/spec" class Minitestify::TestSpec < Minitest::Test - def setup - @spec = Minitestify::Spec.new(file: "spec/calculator_spec.rb") - end - def test_to_test_filepath - assert_equal("test/calculator_test.rb", @spec.to_test_filepath) + assert_equal("test/calculator_test.rb", + Minitestify::Spec.new(file: "spec/calculator_spec.rb", source: "").to_test_filepath) + assert_equal("/test/calculator_test.rb", + Minitestify::Spec.new(file: "/spec/calculator_spec.rb", source: "").to_test_filepath) + assert_equal("test/some_spec_directory/calculator_test.rb", + Minitestify::Spec.new(file: "spec/some_spec_directory/calculator_spec.rb", source: "").to_test_filepath) end def test_to_test_code @@ -21,6 +23,8 @@ def test_can_add end end RUBY - assert_equal(expected, @spec.to_test_code) + + spec = Minitestify::Spec.new(file: "spec/calculator_spec.rb") + assert_equal(expected, spec.to_test_code) end end