rspec-kickstarter 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/rspec-kickstarter +14 -0
 - data/lib/rspec_kickstarter.rb +8 -0
 - data/lib/rspec_kickstarter/generator.rb +136 -0
 - data/lib/rspec_kickstarter/version.rb +3 -0
 - metadata +53 -0
 
| 
         @@ -0,0 +1,14 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            #!/usr/bin/env ruby
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require 'rspec_kickstarter/generator'
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            generator = RSpecKickstarter::Generator.new
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            dir_or_file = ARGV.first
         
     | 
| 
      
 8 
     | 
    
         
            +
            unless dir_or_file.match(/.rb$/)
         
     | 
| 
      
 9 
     | 
    
         
            +
              dir_or_file = dir_or_file.gsub(/\/$/, '') + "/**/*.rb"
         
     | 
| 
      
 10 
     | 
    
         
            +
            end
         
     | 
| 
      
 11 
     | 
    
         
            +
            Dir.glob(dir_or_file).each do |file_path|
         
     | 
| 
      
 12 
     | 
    
         
            +
              generator.wite_spec_if_absent(file_path)
         
     | 
| 
      
 13 
     | 
    
         
            +
            end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
         @@ -0,0 +1,136 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # -*- encoding: utf-8 -*-
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require 'rdoc'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'rdoc/parser/ruby'
         
     | 
| 
      
 5 
     | 
    
         
            +
            require 'rdoc/options'
         
     | 
| 
      
 6 
     | 
    
         
            +
            require 'rdoc/stats'
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
            module RSpecKickstarter
         
     | 
| 
      
 9 
     | 
    
         
            +
              class Generator
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                attr_accessor :spec_dir
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                def initialize(spec_dir = './spec')
         
     | 
| 
      
 14 
     | 
    
         
            +
                  @spec_dir = spec_dir.gsub(/\/$/, '')
         
     | 
| 
      
 15 
     | 
    
         
            +
                end
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                def get_target(top_level)
         
     | 
| 
      
 18 
     | 
    
         
            +
                  c = top_level.classes.first
         
     | 
| 
      
 19 
     | 
    
         
            +
                  if c.nil?
         
     | 
| 
      
 20 
     | 
    
         
            +
                    m = top_level.modules.first
         
     | 
| 
      
 21 
     | 
    
         
            +
                    if m.nil?
         
     | 
| 
      
 22 
     | 
    
         
            +
                      top_level.is_a?(RDoc::NormalModule) ? top_level : nil
         
     | 
| 
      
 23 
     | 
    
         
            +
                    else
         
     | 
| 
      
 24 
     | 
    
         
            +
                      get_target(m)
         
     | 
| 
      
 25 
     | 
    
         
            +
                    end
         
     | 
| 
      
 26 
     | 
    
         
            +
                  else
         
     | 
| 
      
 27 
     | 
    
         
            +
                    c
         
     | 
| 
      
 28 
     | 
    
         
            +
                  end
         
     | 
| 
      
 29 
     | 
    
         
            +
                end
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
                def get_complete_class_name(c, name = c.name)
         
     | 
| 
      
 32 
     | 
    
         
            +
                  if !c.parent.name.nil? && c.parent.is_a?(RDoc::NormalModule)
         
     | 
| 
      
 33 
     | 
    
         
            +
                    get_complete_class_name(c.parent, "#{c.parent.name}::#{name}")
         
     | 
| 
      
 34 
     | 
    
         
            +
                  else
         
     | 
| 
      
 35 
     | 
    
         
            +
                    name
         
     | 
| 
      
 36 
     | 
    
         
            +
                  end
         
     | 
| 
      
 37 
     | 
    
         
            +
                end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                def instance_name(c)
         
     | 
| 
      
 40 
     | 
    
         
            +
                  # TODO prefer snake_case
         
     | 
| 
      
 41 
     | 
    
         
            +
                  c.name.downcase
         
     | 
| 
      
 42 
     | 
    
         
            +
                end
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
                def to_param_names_array(params)
         
     | 
| 
      
 45 
     | 
    
         
            +
                  params.split(',').map { |p| p.gsub(/[\(\)\s]/, '').gsub(/=.+$/, '') }.reject { |p| p.nil? || p.empty? }
         
     | 
| 
      
 46 
     | 
    
         
            +
                end
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
                def get_params_initialization_code(method)
         
     | 
| 
      
 49 
     | 
    
         
            +
                  code = to_param_names_array(method.params).map { |p| "      #{p} = stub('#{p}')" }.join("\n")
         
     | 
| 
      
 50 
     | 
    
         
            +
                  code.empty? ? "" : "\n#{code}"
         
     | 
| 
      
 51 
     | 
    
         
            +
                end
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
                def get_instantiation_code(c, method)
         
     | 
| 
      
 54 
     | 
    
         
            +
                  if method.singleton
         
     | 
| 
      
 55 
     | 
    
         
            +
                    ""
         
     | 
| 
      
 56 
     | 
    
         
            +
                  else
         
     | 
| 
      
 57 
     | 
    
         
            +
                    constructor = c.method_list.find { |m| m.name == 'new' }
         
     | 
| 
      
 58 
     | 
    
         
            +
                    if constructor.nil?
         
     | 
| 
      
 59 
     | 
    
         
            +
                      "\n      #{instance_name(c)} = #{get_complete_class_name(c)}.new"
         
     | 
| 
      
 60 
     | 
    
         
            +
                    else
         
     | 
| 
      
 61 
     | 
    
         
            +
                      get_params_initialization_code(constructor) +
         
     | 
| 
      
 62 
     | 
    
         
            +
                          "\n      #{instance_name(c)} = #{get_complete_class_name(c)}.new#{constructor.params}"
         
     | 
| 
      
 63 
     | 
    
         
            +
                    end
         
     | 
| 
      
 64 
     | 
    
         
            +
                  end
         
     | 
| 
      
 65 
     | 
    
         
            +
                end
         
     | 
| 
      
 66 
     | 
    
         
            +
             
     | 
| 
      
 67 
     | 
    
         
            +
                def get_method_invocation_code(c, method)
         
     | 
| 
      
 68 
     | 
    
         
            +
                  if method.singleton
         
     | 
| 
      
 69 
     | 
    
         
            +
                    "#{get_complete_class_name(c)}.#{method.name}(#{to_param_names_array(method.params).join(', ')})#{get_block_code(method)}"
         
     | 
| 
      
 70 
     | 
    
         
            +
                  else
         
     | 
| 
      
 71 
     | 
    
         
            +
                    "#{instance_name(c)}.#{method.name}(#{to_param_names_array(method.params).join(', ')})#{get_block_code(method)}"
         
     | 
| 
      
 72 
     | 
    
         
            +
                  end
         
     | 
| 
      
 73 
     | 
    
         
            +
                end
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
      
 75 
     | 
    
         
            +
                def get_block_code(method)
         
     | 
| 
      
 76 
     | 
    
         
            +
                  if method.block_params.nil? || method.block_params.empty?
         
     | 
| 
      
 77 
     | 
    
         
            +
                    ""
         
     | 
| 
      
 78 
     | 
    
         
            +
                  else
         
     | 
| 
      
 79 
     | 
    
         
            +
                    " { |#{method.block_params}| }"
         
     | 
| 
      
 80 
     | 
    
         
            +
                  end
         
     | 
| 
      
 81 
     | 
    
         
            +
                end
         
     | 
| 
      
 82 
     | 
    
         
            +
             
     | 
| 
      
 83 
     | 
    
         
            +
                def wite_spec_if_absent(file_path)
         
     | 
| 
      
 84 
     | 
    
         
            +
             
     | 
| 
      
 85 
     | 
    
         
            +
                  body = File.read(file_path)
         
     | 
| 
      
 86 
     | 
    
         
            +
                  RDoc::TopLevel.reset()
         
     | 
| 
      
 87 
     | 
    
         
            +
                  top_level = RDoc::TopLevel.new(file_path)
         
     | 
| 
      
 88 
     | 
    
         
            +
                  parser = RDoc::Parser::Ruby.new(
         
     | 
| 
      
 89 
     | 
    
         
            +
                      top_level,
         
     | 
| 
      
 90 
     | 
    
         
            +
                      file_path,
         
     | 
| 
      
 91 
     | 
    
         
            +
                      body,
         
     | 
| 
      
 92 
     | 
    
         
            +
                      RDoc::Options.new,
         
     | 
| 
      
 93 
     | 
    
         
            +
                      RDoc::Stats.new(1)
         
     | 
| 
      
 94 
     | 
    
         
            +
                  )
         
     | 
| 
      
 95 
     | 
    
         
            +
                  top_level = parser.scan
         
     | 
| 
      
 96 
     | 
    
         
            +
                  c = get_target(top_level)
         
     | 
| 
      
 97 
     | 
    
         
            +
             
     | 
| 
      
 98 
     | 
    
         
            +
                  if c.nil?
         
     | 
| 
      
 99 
     | 
    
         
            +
                    puts "#{file_path} skipped (Class/Module not found)."
         
     | 
| 
      
 100 
     | 
    
         
            +
                  else
         
     | 
| 
      
 101 
     | 
    
         
            +
             
     | 
| 
      
 102 
     | 
    
         
            +
                    self_path = file_path.gsub(/^(lib\/)|(app\/)/, '').gsub(/\.rb$/, '')
         
     | 
| 
      
 103 
     | 
    
         
            +
                    code = <<SPEC
         
     | 
| 
      
 104 
     | 
    
         
            +
            # -*- encoding: utf-8 -*-
         
     | 
| 
      
 105 
     | 
    
         
            +
            require 'spec_helper'
         
     | 
| 
      
 106 
     | 
    
         
            +
            require '#{self_path}'
         
     | 
| 
      
 107 
     | 
    
         
            +
             
     | 
| 
      
 108 
     | 
    
         
            +
            describe #{get_complete_class_name(c)} do
         
     | 
| 
      
 109 
     | 
    
         
            +
             
     | 
| 
      
 110 
     | 
    
         
            +
            #{c.method_list.select { |m| m.visibility == :public }.map { |method|
         
     | 
| 
      
 111 
     | 
    
         
            +
                      <<EACH_SPEC
         
     | 
| 
      
 112 
     | 
    
         
            +
              describe '#{method.name}' do
         
     | 
| 
      
 113 
     | 
    
         
            +
                it 'should work' do#{get_instantiation_code(c, method)}#{get_params_initialization_code(method)}
         
     | 
| 
      
 114 
     | 
    
         
            +
                  result = #{get_method_invocation_code(c, method)}
         
     | 
| 
      
 115 
     | 
    
         
            +
                  # result.should_not be_nil
         
     | 
| 
      
 116 
     | 
    
         
            +
                end
         
     | 
| 
      
 117 
     | 
    
         
            +
              end
         
     | 
| 
      
 118 
     | 
    
         
            +
            EACH_SPEC
         
     | 
| 
      
 119 
     | 
    
         
            +
            }.join("\n")}
         
     | 
| 
      
 120 
     | 
    
         
            +
            end
         
     | 
| 
      
 121 
     | 
    
         
            +
            SPEC
         
     | 
| 
      
 122 
     | 
    
         
            +
             
     | 
| 
      
 123 
     | 
    
         
            +
                    # TODO improve the logic
         
     | 
| 
      
 124 
     | 
    
         
            +
                    spec_path = spec_dir + '/' + file_path.gsub(/^(lib\/)|(app\/)/, '').gsub(/\.rb$/, '_spec.rb')
         
     | 
| 
      
 125 
     | 
    
         
            +
                    if File.exist?(spec_path)
         
     | 
| 
      
 126 
     | 
    
         
            +
                      puts "#{spec_path} already exists."
         
     | 
| 
      
 127 
     | 
    
         
            +
                    else
         
     | 
| 
      
 128 
     | 
    
         
            +
                      FileUtils.mkdir_p(File.dirname(spec_path))
         
     | 
| 
      
 129 
     | 
    
         
            +
                      File.open(spec_path, 'w') { |f| f.write(code) }
         
     | 
| 
      
 130 
     | 
    
         
            +
                      puts "#{spec_path} created."
         
     | 
| 
      
 131 
     | 
    
         
            +
                    end
         
     | 
| 
      
 132 
     | 
    
         
            +
                  end
         
     | 
| 
      
 133 
     | 
    
         
            +
                end
         
     | 
| 
      
 134 
     | 
    
         
            +
              end
         
     | 
| 
      
 135 
     | 
    
         
            +
            end
         
     | 
| 
      
 136 
     | 
    
         
            +
             
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,53 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: rspec-kickstarter
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.6
         
     | 
| 
      
 5 
     | 
    
         
            +
              prerelease: 
         
     | 
| 
      
 6 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 7 
     | 
    
         
            +
            authors:
         
     | 
| 
      
 8 
     | 
    
         
            +
            - Kazuhiro Sera
         
     | 
| 
      
 9 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 10 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 11 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2013-04-25 00:00:00.000000000 Z
         
     | 
| 
      
 13 
     | 
    
         
            +
            dependencies: []
         
     | 
| 
      
 14 
     | 
    
         
            +
            description: RSpec kickstarter generates spec files for existing code.
         
     | 
| 
      
 15 
     | 
    
         
            +
            email:
         
     | 
| 
      
 16 
     | 
    
         
            +
            - - seratch@gmail.com
         
     | 
| 
      
 17 
     | 
    
         
            +
            - - MIT
         
     | 
| 
      
 18 
     | 
    
         
            +
            - RSpec kickstarter generates spec files for existing code.
         
     | 
| 
      
 19 
     | 
    
         
            +
            executables:
         
     | 
| 
      
 20 
     | 
    
         
            +
            - rspec-kickstarter
         
     | 
| 
      
 21 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 22 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 23 
     | 
    
         
            +
            files:
         
     | 
| 
      
 24 
     | 
    
         
            +
            - bin/rspec-kickstarter
         
     | 
| 
      
 25 
     | 
    
         
            +
            - lib/rspec_kickstarter/generator.rb
         
     | 
| 
      
 26 
     | 
    
         
            +
            - lib/rspec_kickstarter/version.rb
         
     | 
| 
      
 27 
     | 
    
         
            +
            - lib/rspec_kickstarter.rb
         
     | 
| 
      
 28 
     | 
    
         
            +
            homepage: https://github.com/seratch/rspec-kickstarter
         
     | 
| 
      
 29 
     | 
    
         
            +
            licenses:
         
     | 
| 
      
 30 
     | 
    
         
            +
            - MIT
         
     | 
| 
      
 31 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 32 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 33 
     | 
    
         
            +
            require_paths:
         
     | 
| 
      
 34 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 35 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 36 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 37 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 38 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 39 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 40 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 41 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 42 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 43 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 44 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 45 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 46 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 47 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 48 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 49 
     | 
    
         
            +
            rubygems_version: 1.8.23
         
     | 
| 
      
 50 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 51 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 52 
     | 
    
         
            +
            summary: RSpec kickstarter generates spec files for existing code.
         
     | 
| 
      
 53 
     | 
    
         
            +
            test_files: []
         
     |