├── .github └── workflows │ └── main.yml ├── .gitignore ├── .rubocop.yml ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── bin ├── console └── setup ├── exe └── minitestify ├── lib ├── minitestify.rb └── minitestify │ ├── cli.rb │ ├── spec.rb │ └── version.rb ├── minitestify.gemspec ├── scripts └── transform.md ├── sig └── minitestify.rbs ├── spec └── calculator_spec.rb └── test ├── minitestify └── test_spec.rb ├── test_helper.rb └── test_minitestify.rb /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Ruby 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | pull_request: 9 | 10 | jobs: 11 | build: 12 | runs-on: ubuntu-latest 13 | name: Ruby ${{ matrix.ruby }} 14 | strategy: 15 | matrix: 16 | ruby: 17 | - '3.1.2' 18 | 19 | steps: 20 | - uses: actions/checkout@v3 21 | - name: Set up Ruby 22 | uses: ruby/setup-ruby@v1 23 | with: 24 | ruby-version: ${{ matrix.ruby }} 25 | bundler-cache: true 26 | - name: Run the default task 27 | run: bundle exec rake 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /_yardoc/ 4 | /coverage/ 5 | /doc/ 6 | /pkg/ 7 | /spec/reports/ 8 | /tmp/ 9 | Gemfile.lock 10 | scratch.rb 11 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | AllCops: 2 | TargetRubyVersion: 2.6 3 | 4 | Style/StringLiterals: 5 | Enabled: true 6 | EnforcedStyle: double_quotes 7 | 8 | Style/StringLiteralsInInterpolation: 9 | Enabled: true 10 | EnforcedStyle: double_quotes 11 | 12 | Layout/LineLength: 13 | Max: 120 14 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [Unreleased] 2 | 3 | ## [0.1.0] - 2022-11-22 4 | 5 | - Initial release 6 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. 8 | 9 | ## Our Standards 10 | 11 | Examples of behavior that contributes to a positive environment for our community include: 12 | 13 | * Demonstrating empathy and kindness toward other people 14 | * Being respectful of differing opinions, viewpoints, and experiences 15 | * Giving and gracefully accepting constructive feedback 16 | * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience 17 | * Focusing on what is best not just for us as individuals, but for the overall community 18 | 19 | Examples of unacceptable behavior include: 20 | 21 | * The use of sexualized language or imagery, and sexual attention or 22 | advances of any kind 23 | * Trolling, insulting or derogatory comments, and personal or political attacks 24 | * Public or private harassment 25 | * Publishing others' private information, such as a physical or email 26 | address, without their explicit permission 27 | * Other conduct which could reasonably be considered inappropriate in a 28 | professional setting 29 | 30 | ## Enforcement Responsibilities 31 | 32 | Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. 33 | 34 | Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. 35 | 36 | ## Scope 37 | 38 | This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. 39 | 40 | ## Enforcement 41 | 42 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at rafe@existentialmutt.com. All complaints will be reviewed and investigated promptly and fairly. 43 | 44 | All community leaders are obligated to respect the privacy and security of the reporter of any incident. 45 | 46 | ## Enforcement Guidelines 47 | 48 | Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: 49 | 50 | ### 1. Correction 51 | 52 | **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. 53 | 54 | **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. 55 | 56 | ### 2. Warning 57 | 58 | **Community Impact**: A violation through a single incident or series of actions. 59 | 60 | **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. 61 | 62 | ### 3. Temporary Ban 63 | 64 | **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. 65 | 66 | **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. 67 | 68 | ### 4. Permanent Ban 69 | 70 | **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. 71 | 72 | **Consequence**: A permanent ban from any sort of public interaction within the community. 73 | 74 | ## Attribution 75 | 76 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, 77 | available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 78 | 79 | Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). 80 | 81 | [homepage]: https://www.contributor-covenant.org 82 | 83 | For answers to common questions about this code of conduct, see the FAQ at 84 | https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations. 85 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source "https://rubygems.org" 4 | 5 | # Specify your gem's dependencies in minitestify.gemspec 6 | gemspec 7 | 8 | gem "rake", "~> 13.0" 9 | 10 | gem "minitest", "~> 5.0" 11 | 12 | gem "rubocop", "~> 1.21" 13 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2022 Rafe Rosen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Minitestify 2 | 3 | minitestify is a tool to convert Rspec specs to equivalent minitest tests by parsing and transforming the code using the [syntax_tree](https://github.com/ruby-syntax-tree/syntax_tree) gem. 4 | 5 | It's early days and this work is still experimental. Here's what it can do now 6 | 7 | ### Capabilities 8 | - describe -> class 9 | - it -> def test_* 10 | - expect(actual).to eq(expected) -> assert_equal(expected, actual) 11 | 12 | ## Installation 13 | 14 | Install the gem and add to the application's Gemfile by executing: 15 | 16 | $ bundle add minitestify 17 | 18 | If bundler is not being used to manage dependencies, install the gem by executing: 19 | 20 | $ gem install minitestify 21 | 22 | ## Usage 23 | 24 | ``` 25 | minitestify print FILES # Convert one or more specs to minitest and print to standard out 26 | ``` 27 | 28 | ## Development 29 | 30 | After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. 31 | 32 | 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). 33 | 34 | ## Contributing 35 | 36 | 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). 37 | 38 | ## License 39 | 40 | The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). 41 | 42 | ## Code of Conduct 43 | 44 | Everyone interacting in the Minitestify project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/minitestify/blob/master/CODE_OF_CONDUCT.md). 45 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "bundler/gem_tasks" 4 | require "rake/testtask" 5 | 6 | Rake::TestTask.new(:test) do |t| 7 | t.libs << "test" 8 | t.libs << "lib" 9 | t.test_files = FileList["test/**/test_*.rb"] 10 | end 11 | 12 | require "rubocop/rake_task" 13 | 14 | RuboCop::RakeTask.new 15 | 16 | task default: %i[test rubocop] 17 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | require "bundler/setup" 5 | require "minitestify" 6 | 7 | # You can add fixtures and/or initialization code here to make experimenting 8 | # with your gem easier. You can also use a different console, if you like. 9 | 10 | # (If you use this, don't forget to add pry to your Gemfile!) 11 | # require "pry" 12 | # Pry.start 13 | 14 | require "irb" 15 | IRB.start(__FILE__) 16 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | 8 | # Do any other automated setup that you need to do here 9 | -------------------------------------------------------------------------------- /exe/minitestify: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | require "bundler/setup" 5 | require "minitestify/cli" 6 | 7 | Minitestify::CLI.run 8 | -------------------------------------------------------------------------------- /lib/minitestify.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative "minitestify/version" 4 | require "syntax_tree" 5 | require "dry/inflector" 6 | 7 | module Minitestify 8 | class Error < StandardError 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /lib/minitestify/cli.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "minitestify" 4 | require "minitestify/version" 5 | require "minitestify/spec" 6 | require "optparse" 7 | 8 | module Minitestify::CLI 9 | module_function def run 10 | options = {} 11 | OptionParser 12 | .new do |parser| 13 | parser.banner = "Usage: minitestify [options] " 14 | 15 | parser.on("-v", "--version", "Print version") do |v| 16 | puts(Minitestify::VERSION) 17 | exit 18 | end 19 | 20 | parser.on("-h", "--help", "Prints this help") do 21 | puts(parser) 22 | exit 23 | end 24 | end 25 | .parse! 26 | 27 | ARGV.each do |file| 28 | spec = Minitestify::Spec.new(file: file) 29 | puts("# #{spec.to_test_filepath}") 30 | puts(spec.to_test_code) 31 | puts 32 | end 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /lib/minitestify/spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "minitestify" 4 | 5 | module Minitestify 6 | class Spec 7 | def initialize(file:) 8 | @file = file 9 | end 10 | 11 | def to_test_filepath 12 | @file.gsub "spec", "test" 13 | # TODO be smarter here and dissect the file path 14 | # replacing only the spec dir (if present) 15 | # and the _spec.rb suffix 16 | end 17 | 18 | def to_test_code 19 | source = SyntaxTree.read(@file) 20 | program = SyntaxTree.parse(source) 21 | 22 | visitor = SyntaxTree::Visitor::MutationVisitor.new 23 | inflector = Dry::Inflector.new 24 | 25 | # describe -> class 26 | visitor.mutate("Command[ message: Ident[value: \"describe\"] ]") do |node| 27 | node => SyntaxTree::Command[ 28 | 29 | message: SyntaxTree::Ident[value: "describe"], 30 | arguments: SyntaxTree::Args[ 31 | parts: [SyntaxTree::VarRef[value: SyntaxTree::Const[value: value]]] 32 | ] 33 | ] 34 | 35 | SyntaxTree::ClassDeclaration.new( 36 | constant: 37 | SyntaxTree::ConstRef.new( 38 | constant: 39 | SyntaxTree::Const.new( 40 | value: "#{inflector.camelize_upper(value)}Test", 41 | location: node.location 42 | ), 43 | location: node.location 44 | ), 45 | superclass: 46 | SyntaxTree::ConstPathRef.new( 47 | parent: 48 | SyntaxTree::VarRef.new( 49 | value: 50 | SyntaxTree::Const.new( 51 | value: "Minitest", 52 | location: node.location 53 | ), 54 | location: node.location 55 | ), 56 | constant: 57 | SyntaxTree::Const.new(value: "Test", location: node.location), 58 | location: node.location 59 | ), 60 | bodystmt: node.child_nodes.last.bodystmt, 61 | location: node.location 62 | ) 63 | end 64 | 65 | # it -> def 66 | visitor.mutate("Command[message: Ident[value: \"it\"]]") do |node| 67 | node => SyntaxTree::Command[ 68 | message: SyntaxTree::Ident[value: "it"], 69 | arguments: SyntaxTree::Args[ 70 | parts: [ 71 | SyntaxTree::StringLiteral[ 72 | parts: [SyntaxTree::TStringContent[value: value]] 73 | ] 74 | ] 75 | ] 76 | ] 77 | 78 | SyntaxTree::DefNode.new( 79 | target: nil, 80 | operator: nil, 81 | name: 82 | SyntaxTree::Ident.new( 83 | value: "test_#{value.gsub(" ", "_")}", 84 | location: node.location 85 | ), 86 | params: SyntaxTree::Params, 87 | bodystmt: node.child_nodes.last.bodystmt, 88 | location: node.location 89 | ) 90 | end 91 | 92 | expect_eq_search = 93 | 'CommandCall[ 94 | receiver: CallNode[ 95 | message: Ident[value: "expect"] 96 | ], 97 | operator: Period[value: "."], 98 | message: Ident[value: "to"], 99 | arguments: Args[ 100 | parts: [ CallNode[ 101 | message: Ident[value: "eq"] 102 | ] 103 | ] 104 | ] 105 | ]' 106 | visitor.mutate(expect_eq_search) do |node| 107 | node => SyntaxTree::CommandCall[ 108 | receiver: SyntaxTree::CallNode[ 109 | receiver: nil, 110 | operator: nil, 111 | message: SyntaxTree::Ident[value: "expect"], 112 | arguments: SyntaxTree::ArgParen[ 113 | arguments: SyntaxTree::Args[parts: [actual_expr]] 114 | ] 115 | ], 116 | operator: SyntaxTree::Period[value: "."], 117 | message: SyntaxTree::Ident[value: "to"], 118 | arguments: SyntaxTree::Args[ 119 | parts: [ 120 | SyntaxTree::CallNode[ 121 | receiver: nil, 122 | operator: nil, 123 | message: SyntaxTree::Ident[value: "eq"], 124 | arguments: SyntaxTree::ArgParen[ 125 | arguments: SyntaxTree::Args[parts: [expected_expr]] 126 | ] 127 | ] 128 | ] 129 | ] 130 | ] 131 | 132 | SyntaxTree::CallNode.new( 133 | message: 134 | SyntaxTree::Ident.new( 135 | value: "assert_equal", 136 | location: node.location 137 | ), 138 | arguments: 139 | SyntaxTree::ArgParen.new( 140 | arguments: 141 | SyntaxTree::Args.new( 142 | parts: [expected_expr, actual_expr], 143 | location: node.location 144 | ), 145 | location: node.location 146 | ), 147 | location: node.location, 148 | receiver: nil, 149 | operator: nil 150 | ) 151 | end 152 | 153 | SyntaxTree::Formatter.format(source, program.accept(visitor)) 154 | end 155 | end 156 | end 157 | -------------------------------------------------------------------------------- /lib/minitestify/version.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module Minitestify 4 | VERSION = "0.3.0" 5 | end 6 | -------------------------------------------------------------------------------- /minitestify.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require_relative "lib/minitestify/version" 4 | 5 | Gem::Specification.new do |spec| 6 | spec.name = "minitestify" 7 | spec.version = Minitestify::VERSION 8 | spec.authors = ["Rafe Rosen"] 9 | spec.email = ["rafe@existentialmutt.com"] 10 | 11 | spec.summary = "Convert rspec to minitest" 12 | spec.homepage = "https://github.com/existentialmutt/minitestify" 13 | spec.license = "MIT" 14 | spec.required_ruby_version = ">= 2.7.0" 15 | 16 | spec.metadata["homepage_uri"] = spec.homepage 17 | spec.metadata["source_code_uri"] = spec.homepage 18 | spec.metadata["changelog_uri"] = spec.homepage + "/CHANGELOG.md" 19 | 20 | # Specify which files should be added to the gem when it is released. 21 | # The `git ls-files -z` loads the files in the RubyGem that have been added into git. 22 | spec.files = Dir.chdir(__dir__) do 23 | `git ls-files -z`.split("\x0").reject do |f| 24 | (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)}) 25 | end 26 | end 27 | spec.bindir = "exe" 28 | spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) } 29 | spec.require_paths = ["lib"] 30 | 31 | # Uncomment to register a new dependency of your gem 32 | spec.add_dependency "syntax_tree", "~> 5.0" 33 | spec.add_dependency "dry-inflector", "~> 1.0" 34 | 35 | # For more information and examples about making a new gem, check out our 36 | # guide at: https://bundler.io/guides/creating_gem.html 37 | end 38 | -------------------------------------------------------------------------------- /scripts/transform.md: -------------------------------------------------------------------------------- 1 | # How to add a transformation 2 | 3 | Run the following in an irb session to determine the search pattern 4 | 5 | ```ruby 6 | require "syntax_tree" 7 | 8 | code = <<~RUBY 9 | expect(calculator.add(1, 1)).to eq(2) 10 | RUBY 11 | 12 | program = SyntaxTree.parse code 13 | puts program.construct_keys 14 | ``` 15 | 16 | You'll get a bunch of output like this 17 | ```ruby 18 | SyntaxTree::Program[ 19 | statements: SyntaxTree::Statements[ 20 | body: [ 21 | SyntaxTree::CommandCall[ 22 | receiver: SyntaxTree::CallNode[ 23 | receiver: nil, 24 | operator: nil, 25 | message: SyntaxTree::Ident[value: "expect"], 26 | arguments: SyntaxTree::ArgParen[ 27 | arguments: SyntaxTree::Args[ 28 | parts: [ 29 | SyntaxTree::CallNode[ 30 | receiver: SyntaxTree::VCall[ 31 | value: SyntaxTree::Ident[value: "calculator"] 32 | ], 33 | operator: SyntaxTree::Period[value: "."], 34 | message: SyntaxTree::Ident[value: "add"], 35 | arguments: SyntaxTree::ArgParen[ 36 | arguments: SyntaxTree::Args[ 37 | parts: [ 38 | SyntaxTree::Int[value: "1"], 39 | SyntaxTree::Int[value: "1"] 40 | ] 41 | ] 42 | ] 43 | ] 44 | ] 45 | ] 46 | ] 47 | ], 48 | operator: SyntaxTree::Period[value: "."], 49 | message: SyntaxTree::Ident[value: "to"], 50 | arguments: SyntaxTree::Args[ 51 | parts: [ 52 | SyntaxTree::CallNode[ 53 | receiver: nil, 54 | operator: nil, 55 | message: SyntaxTree::Ident[value: "eq"], 56 | arguments: SyntaxTree::ArgParen[ 57 | arguments: SyntaxTree::Args[ 58 | parts: [SyntaxTree::Int[value: "2"]] 59 | ] 60 | ] 61 | ] 62 | ] 63 | ] 64 | ] 65 | ] 66 | ] 67 | ] 68 | ``` 69 | 70 | We want the top level node of the body. Remove the SyntaxTree namespaces and take just enough to identify the top node 71 | ```ruby 72 | expect_eq_search = 73 | 'CommandCall[ 74 | receiver: CallNode[ 75 | message: Ident[value: "expect"] 76 | ], 77 | operator: Period[value: "."], 78 | message: Ident[value: "to"], 79 | arguments: Args[ 80 | parts: [ CallNode[ 81 | message: Ident[value: "eq"] 82 | ] 83 | ] 84 | ] 85 | ]' 86 | ``` 87 | 88 | And use that as our argument to `visitor.mutate` 89 | ```ruby 90 | visitor.mutate(expect_eq_search) do 91 | # ... 92 | end 93 | ``` 94 | 95 | For the block body, start by pulling out relevant information with a pattern matching statement from construct_keys 96 | TODO pare down the pattern match and extract variables 97 | ```ruby 98 | node => SyntaxTree::CommandCall[ 99 | receiver: SyntaxTree::CallNode[ 100 | receiver: nil, 101 | operator: nil, 102 | message: SyntaxTree::Ident[value: "expect"], 103 | arguments: SyntaxTree::ArgParen[ 104 | arguments: SyntaxTree::Args[parts: [actual_expr]] 105 | ] 106 | ], 107 | operator: SyntaxTree::Period[value: "."], 108 | message: SyntaxTree::Ident[value: "to"], 109 | arguments: SyntaxTree::Args[ 110 | parts: [ 111 | SyntaxTree::CallNode[ 112 | receiver: nil, 113 | operator: nil, 114 | message: SyntaxTree::Ident[value: "eq"], 115 | arguments: SyntaxTree::ArgParen[ 116 | arguments: SyntaxTree::Args[parts: [expected_expr]] 117 | ] 118 | ] 119 | ] 120 | ] 121 | ] 122 | ``` 123 | 124 | Now write the code you want to transform it to and construct the nodes 125 | 126 | ```ruby 127 | require "syntax_tree" 128 | 129 | code = <<~RUBY 130 | assert_equal(2, calculator.add(1, 1)) 131 | RUBY 132 | 133 | program = SyntaxTree.parse code 134 | puts program.construct_keys 135 | ``` 136 | 137 | Again you get a bunch of output 138 | ```ruby 139 | SyntaxTree::Program[ 140 | statements: SyntaxTree::Statements[ 141 | body: [ 142 | SyntaxTree::CallNode[ 143 | receiver: nil, 144 | operator: nil, 145 | message: SyntaxTree::Ident[value: "assert_equal"], 146 | arguments: SyntaxTree::ArgParen[ 147 | arguments: SyntaxTree::Args[ 148 | parts: [ 149 | SyntaxTree::Int[value: "2"], 150 | SyntaxTree::CallNode[ 151 | receiver: SyntaxTree::VCall[ 152 | value: SyntaxTree::Ident[value: "calculator"] 153 | ], 154 | operator: SyntaxTree::Period[value: "."], 155 | message: SyntaxTree::Ident[value: "add"], 156 | arguments: SyntaxTree::ArgParen[ 157 | arguments: SyntaxTree::Args[ 158 | parts: [ 159 | SyntaxTree::Int[value: "1"], 160 | SyntaxTree::Int[value: "1"] 161 | ] 162 | ] 163 | ] 164 | ] 165 | ] 166 | ] 167 | ] 168 | ] 169 | ] 170 | ] 171 | ] 172 | ``` 173 | 174 | Add `.new` and convert parentheses. You'll have to add `location: node.location` where required (trial and error). Make this node tree the return value of the mutate block. 175 | 176 | ```ruby 177 | expect_eq_search = 178 | 'CommandCall[ 179 | receiver: CallNode[ 180 | message: Ident[value: "expect"] 181 | ], 182 | operator: Period[value: "."], 183 | message: Ident[value: "to"], 184 | arguments: Args[ 185 | parts: [ CallNode[ 186 | message: Ident[value: "eq"] 187 | ] 188 | ] 189 | ] 190 | ]' 191 | visitor.mutate(expect_eq_search) do |node| 192 | node => SyntaxTree::CommandCall[ 193 | receiver: SyntaxTree::CallNode[ 194 | receiver: nil, 195 | operator: nil, 196 | message: SyntaxTree::Ident[value: "expect"], 197 | arguments: SyntaxTree::ArgParen[ 198 | arguments: SyntaxTree::Args[parts: [actual_expr]] 199 | ] 200 | ], 201 | operator: SyntaxTree::Period[value: "."], 202 | message: SyntaxTree::Ident[value: "to"], 203 | arguments: SyntaxTree::Args[ 204 | parts: [ 205 | SyntaxTree::CallNode[ 206 | receiver: nil, 207 | operator: nil, 208 | message: SyntaxTree::Ident[value: "eq"], 209 | arguments: SyntaxTree::ArgParen[ 210 | arguments: SyntaxTree::Args[parts: [expected_expr]] 211 | ] 212 | ] 213 | ] 214 | ] 215 | ] 216 | 217 | SyntaxTree::CallNode.new( 218 | message: 219 | SyntaxTree::Ident.new( 220 | value: "assert_equal", 221 | location: node.location 222 | ), 223 | arguments: 224 | SyntaxTree::ArgParen.new( 225 | arguments: 226 | SyntaxTree::Args.new( 227 | parts: [expected_expr, actual_expr], 228 | location: node.location 229 | ), 230 | location: node.location 231 | ), 232 | location: node.location, 233 | receiver: nil, 234 | operator: nil 235 | ) 236 | end 237 | ``` 238 | -------------------------------------------------------------------------------- /sig/minitestify.rbs: -------------------------------------------------------------------------------- 1 | module Minitestify 2 | VERSION: String 3 | # See the writing guide of rbs: https://github.com/ruby/rbs#guides 4 | end 5 | -------------------------------------------------------------------------------- /spec/calculator_spec.rb: -------------------------------------------------------------------------------- 1 | describe Calculator do 2 | it "can add" do 3 | calculator = Calculator.new 4 | expect(calculator.add(1, 1)).to eq(2) 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /test/minitestify/test_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "test_helper" 4 | require "minitestify/spec" 5 | 6 | class Minitestify::TestSpec < Minitest::Test 7 | def setup 8 | @spec = Minitestify::Spec.new(file: "spec/calculator_spec.rb") 9 | end 10 | 11 | def test_to_test_filepath 12 | assert_equal("test/calculator_test.rb", @spec.to_test_filepath) 13 | end 14 | 15 | def test_to_test_code 16 | expected = <<~RUBY 17 | class CalculatorTest < Minitest::Test 18 | def test_can_add 19 | calculator = Calculator.new 20 | assert_equal(2, calculator.add(1, 1)) 21 | end 22 | end 23 | RUBY 24 | assert_equal(expected, @spec.to_test_code) 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | $LOAD_PATH.unshift File.expand_path("../lib", __dir__) 4 | require "minitestify" 5 | 6 | require "minitest/autorun" 7 | -------------------------------------------------------------------------------- /test/test_minitestify.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require "test_helper" 4 | 5 | class TestMinitestify < Minitest::Test 6 | def test_that_it_has_a_version_number 7 | refute_nil ::Minitestify::VERSION 8 | end 9 | end 10 | --------------------------------------------------------------------------------