to_factory 0.0.3 → 0.1.0
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/.gitignore +6 -0
- data/.rspec +2 -1
- data/.travis.yml +23 -0
- data/Gemfile +3 -13
- data/README.md +59 -20
- data/Rakefile +20 -14
- data/bin/ci +2 -0
- data/bin/spec +2 -0
- data/lib/to_factory/collation.rb +34 -0
- data/lib/to_factory/config.rb +18 -0
- data/lib/to_factory/definition_group.rb +47 -0
- data/lib/to_factory/file_sync.rb +48 -0
- data/lib/to_factory/file_writer.rb +39 -0
- data/lib/to_factory/finders/factory.rb +17 -0
- data/lib/to_factory/finders/model.rb +47 -0
- data/lib/to_factory/generation/attribute.rb +52 -0
- data/lib/to_factory/generation/factory.rb +74 -0
- data/lib/to_factory/parsing/file.rb +38 -0
- data/lib/to_factory/parsing/new_syntax.rb +9 -0
- data/lib/to_factory/parsing/old_syntax.rb +9 -0
- data/lib/to_factory/parsing/syntax.rb +86 -0
- data/lib/to_factory/version.rb +1 -1
- data/lib/to_factory.rb +55 -1
- data/spec/db/migrate/{201108201012010100_create_users.rb → 1_create_users.rb} +0 -0
- data/spec/db/migrate/2_create_projects.rb +13 -0
- data/spec/db/migrate/3_create_not_namespaced.rb +11 -0
- data/spec/db/migrate/4_add_birthday_to_users.rb +9 -0
- data/spec/example_factories/new_syntax/admin.rb +6 -0
- data/spec/example_factories/new_syntax/admin_with_header.rb +8 -0
- data/spec/example_factories/new_syntax/project_with_header.rb +7 -0
- data/spec/example_factories/new_syntax/user.rb +6 -0
- data/spec/example_factories/new_syntax/user_admin.rb +13 -0
- data/spec/example_factories/new_syntax/user_admin_with_header.rb +15 -0
- data/spec/example_factories/new_syntax/user_with_header.rb +8 -0
- data/spec/example_factories/old_syntax/admin.rb +6 -0
- data/spec/example_factories/old_syntax/project_with_header.rb +5 -0
- data/spec/example_factories/old_syntax/user.rb +6 -0
- data/spec/example_factories/old_syntax/user_admin.rb +13 -0
- data/spec/example_factories/old_syntax/user_admin_with_header.rb +13 -0
- data/spec/example_factories/old_syntax/user_with_header.rb +6 -0
- data/spec/integration/config_spec.rb +19 -0
- data/spec/integration/file_sync_spec.rb +77 -0
- data/spec/integration/file_writer_spec.rb +16 -0
- data/spec/integration/generate_class_method_spec.rb +107 -0
- data/spec/integration/lint_spec.rb +16 -0
- data/spec/spec_helper.rb +90 -14
- data/spec/support/match_sexp.rb +24 -0
- data/spec/support/models/not_active_record.rb +2 -0
- data/spec/support/models/project.rb +3 -0
- data/spec/support/models/user.rb +3 -0
- data/spec/support/terse_expect_syntax.rb +23 -0
- data/spec/unit/collation_spec.rb +35 -0
- data/spec/unit/definition_group_spec.rb +19 -0
- data/spec/unit/file_writer_spec.rb +28 -0
- data/spec/unit/finders/factory_spec.rb +27 -0
- data/spec/unit/finders/model_spec.rb +28 -0
- data/spec/unit/generator_spec.rb +71 -0
- data/spec/unit/parsing/file_spec.rb +83 -0
- data/tmp/.keep +0 -0
- data/to_factory.gemspec +49 -22
- metadata +225 -25
- data/.rvmrc +0 -1
- data/Gemfile.lock +0 -113
- data/lib/to_factory/generator.rb +0 -115
- data/spec/config/database.yml +0 -2
- data/spec/db/test.sqlite +0 -0
- data/spec/generator_spec.rb +0 -166
| @@ -0,0 +1,86 @@ | |
| 1 | 
            +
            module ToFactory
         | 
| 2 | 
            +
              module Parsing
         | 
| 3 | 
            +
                class Syntax
         | 
| 4 | 
            +
                  attr_accessor :contents
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                  def initialize(contents)
         | 
| 7 | 
            +
                    @contents = contents
         | 
| 8 | 
            +
                  end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  def multiple_factories?
         | 
| 11 | 
            +
                    factories_sexp[0] == :block
         | 
| 12 | 
            +
                  end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                  def parse
         | 
| 15 | 
            +
                    result = {}
         | 
| 16 | 
            +
                    @klass_inference = KlassInference.new
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                    parse_multiple do |factory_name, parent_name, ruby|
         | 
| 19 | 
            +
                      klass = @klass_inference.infer(factory_name, parent_name)
         | 
| 20 | 
            +
                      result[klass] ||= {}
         | 
| 21 | 
            +
                      result[klass][factory_name] = ruby
         | 
| 22 | 
            +
                    end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                    result
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                  def factories
         | 
| 28 | 
            +
                    if multiple_factories?
         | 
| 29 | 
            +
                      factories_sexp[1..-1]
         | 
| 30 | 
            +
                    else
         | 
| 31 | 
            +
                      [factories_sexp]
         | 
| 32 | 
            +
                    end
         | 
| 33 | 
            +
                  end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                  def factories_sexp
         | 
| 36 | 
            +
                    header? ?  sexp[3] : sexp
         | 
| 37 | 
            +
                  end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                  def parse_multiple(&block)
         | 
| 40 | 
            +
                    factories.each do |x|
         | 
| 41 | 
            +
                      yield name_from(x), parent_from(x), to_ruby(x)
         | 
| 42 | 
            +
                    end
         | 
| 43 | 
            +
                  end
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                  def parent_from(x)
         | 
| 46 | 
            +
                    x[1][-1][-1][-1] rescue name_from(x)
         | 
| 47 | 
            +
                  end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                  def name_from(sexp)
         | 
| 50 | 
            +
                    sexp[1][3][1]
         | 
| 51 | 
            +
                  end
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                  private
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                  def to_ruby(sexp)
         | 
| 56 | 
            +
                    ruby2ruby.process sexp.deep_clone
         | 
| 57 | 
            +
                  end
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                  def sexp
         | 
| 60 | 
            +
                    @sexp ||= ruby_parser.process(@contents)
         | 
| 61 | 
            +
                  end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                  def ruby2ruby
         | 
| 64 | 
            +
                    @ruby2ruby ||= Ruby2Ruby.new
         | 
| 65 | 
            +
                  end
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                  def ruby_parser
         | 
| 68 | 
            +
                    @ruby_parseer ||= RubyParser.new
         | 
| 69 | 
            +
                  end
         | 
| 70 | 
            +
             | 
| 71 | 
            +
                  class KlassInference
         | 
| 72 | 
            +
                    def initialize
         | 
| 73 | 
            +
                      @mapping = {}
         | 
| 74 | 
            +
                    end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
                    def infer(klass, parent_klass=nil)
         | 
| 77 | 
            +
                      @mapping[klass] ||= klass.to_s.camelize.constantize
         | 
| 78 | 
            +
                    rescue
         | 
| 79 | 
            +
                      infer(parent_klass)
         | 
| 80 | 
            +
                    end
         | 
| 81 | 
            +
                  end
         | 
| 82 | 
            +
             | 
| 83 | 
            +
             | 
| 84 | 
            +
                end
         | 
| 85 | 
            +
              end
         | 
| 86 | 
            +
            end
         | 
    
        data/lib/to_factory/version.rb
    CHANGED
    
    
    
        data/lib/to_factory.rb
    CHANGED
    
    | @@ -1,2 +1,56 @@ | |
| 1 | 
            +
            require "deep_merge"
         | 
| 2 | 
            +
             | 
| 1 3 | 
             
            require "to_factory/version"
         | 
| 2 | 
            -
            require "to_factory/ | 
| 4 | 
            +
            require "to_factory/config"
         | 
| 5 | 
            +
            require "to_factory/generation/factory"
         | 
| 6 | 
            +
            require "to_factory/generation/attribute"
         | 
| 7 | 
            +
            require "to_factory/collation"
         | 
| 8 | 
            +
            require "to_factory/file_writer"
         | 
| 9 | 
            +
            require "to_factory/finders/model"
         | 
| 10 | 
            +
            require "to_factory/finders/factory"
         | 
| 11 | 
            +
            require "to_factory/definition_group"
         | 
| 12 | 
            +
            require "to_factory/file_sync"
         | 
| 13 | 
            +
            require "to_factory/parsing/file"
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            module ToFactory
         | 
| 16 | 
            +
              class MissingActiveRecordInstanceException < Exception;end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              class << self
         | 
| 19 | 
            +
                def new_syntax?
         | 
| 20 | 
            +
                  require "factory_girl"
         | 
| 21 | 
            +
                  if FactoryGirl::VERSION.to_s[0..0].to_i > 1
         | 
| 22 | 
            +
                    true
         | 
| 23 | 
            +
                  else
         | 
| 24 | 
            +
                    false
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
                rescue NameError, ArgumentError
         | 
| 27 | 
            +
                  false
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
            end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            public
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            def ToFactory(args=nil)
         | 
| 35 | 
            +
              exclusions = if args.is_a?(Hash)
         | 
| 36 | 
            +
                             exclusions = Array(args.delete(:exclude) || [])
         | 
| 37 | 
            +
                             args = nil if args.keys.length == 0
         | 
| 38 | 
            +
                             exclusions
         | 
| 39 | 
            +
                           else
         | 
| 40 | 
            +
                             []
         | 
| 41 | 
            +
                           end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
              meth = ToFactory::FileSync.method(:new)
         | 
| 44 | 
            +
              sync = args ? meth.call(args) : meth.call
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              sync.perform(exclusions)
         | 
| 47 | 
            +
            end
         | 
| 48 | 
            +
             | 
| 49 | 
            +
            if defined?(Rails)
         | 
| 50 | 
            +
              unless Rails.respond_to?(:configuration)
         | 
| 51 | 
            +
                #FactoryGirl 1.3.x expects this method, but it isn't defined in Rails 2.0.2
         | 
| 52 | 
            +
                def Rails.configuration
         | 
| 53 | 
            +
                  OpenStruct.new
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
              end
         | 
| 56 | 
            +
            end
         | 
| 
            File without changes
         | 
| @@ -0,0 +1,13 @@ | |
| 1 | 
            +
            class CreateProjects < ActiveRecord::Migration
         | 
| 2 | 
            +
              def self.up
         | 
| 3 | 
            +
                create_table :projects do |t|
         | 
| 4 | 
            +
                  t.column :name, :string, :null => false
         | 
| 5 | 
            +
                  t.column :objective, :string
         | 
| 6 | 
            +
                  t.integer :some_id
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
              end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              def self.down
         | 
| 11 | 
            +
                drop_table :projects
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
            end
         | 
| @@ -0,0 +1,11 @@ | |
| 1 | 
            +
            class CreateNotNamespaced < ActiveRecord::Migration
         | 
| 2 | 
            +
              def self.up
         | 
| 3 | 
            +
                create_table :not_namespaced_active_record_class_but_long_enough_it_shouldnt_cause_conflicts do |t|
         | 
| 4 | 
            +
                  t.column :name, :string, :null => false
         | 
| 5 | 
            +
                end
         | 
| 6 | 
            +
              end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              def self.down
         | 
| 9 | 
            +
                drop_table :not_namespaced_active_record_class_but_long_enough_it_shouldnt_cause_conflicts
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
            end
         | 
| @@ -0,0 +1,13 @@ | |
| 1 | 
            +
            factory(:"to_factory/user") do
         | 
| 2 | 
            +
              birthday "2014-07-08T15:30Z"
         | 
| 3 | 
            +
              email "test@example.com"
         | 
| 4 | 
            +
              name "Jeff"
         | 
| 5 | 
            +
              some_id 8
         | 
| 6 | 
            +
            end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            factory(:admin, :parent => :"to_factory/user") do
         | 
| 9 | 
            +
              birthday "2014-07-08T15:30Z"
         | 
| 10 | 
            +
              email "admin@example.com"
         | 
| 11 | 
            +
              name "Admin"
         | 
| 12 | 
            +
              some_id 9
         | 
| 13 | 
            +
            end
         | 
| @@ -0,0 +1,15 @@ | |
| 1 | 
            +
            FactoryGirl.define do
         | 
| 2 | 
            +
              factory(:"to_factory/user") do
         | 
| 3 | 
            +
                birthday "2014-07-08T15:30Z"
         | 
| 4 | 
            +
                email "test@example.com"
         | 
| 5 | 
            +
                name "Jeff"
         | 
| 6 | 
            +
                some_id 8
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              factory(:admin, :parent => :"to_factory/user") do
         | 
| 10 | 
            +
                birthday "2014-07-08T15:30Z"
         | 
| 11 | 
            +
                email "admin@example.com"
         | 
| 12 | 
            +
                name "Admin"
         | 
| 13 | 
            +
                some_id 9
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
            end
         | 
| @@ -0,0 +1,13 @@ | |
| 1 | 
            +
            Factory.define(:"to_factory/user") do |o|
         | 
| 2 | 
            +
              o.birthday "2014-07-08T15:30Z"
         | 
| 3 | 
            +
              o.email "test@example.com"
         | 
| 4 | 
            +
              o.name "Jeff"
         | 
| 5 | 
            +
              o.some_id 8
         | 
| 6 | 
            +
            end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            Factory.define(:admin, :parent => :"to_factory/user") do |o|
         | 
| 9 | 
            +
              o.birthday "2014-07-08T15:30Z"
         | 
| 10 | 
            +
              o.email "admin@example.com"
         | 
| 11 | 
            +
              o.name "Admin"
         | 
| 12 | 
            +
              o.some_id 9
         | 
| 13 | 
            +
            end
         | 
| @@ -0,0 +1,13 @@ | |
| 1 | 
            +
            Factory.define(:"to_factory/user") do |o|
         | 
| 2 | 
            +
              o.birthday "2014-07-08T15:30Z"
         | 
| 3 | 
            +
              o.email "test@example.com"
         | 
| 4 | 
            +
              o.name "Jeff"
         | 
| 5 | 
            +
              o.some_id 8
         | 
| 6 | 
            +
            end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            Factory.define(:admin, :parent => :"to_factory/user") do |o|
         | 
| 9 | 
            +
              o.birthday "2014-07-08T15:30Z"
         | 
| 10 | 
            +
              o.email "admin@example.com"
         | 
| 11 | 
            +
              o.name "Admin"
         | 
| 12 | 
            +
              o.some_id 9
         | 
| 13 | 
            +
            end
         | 
| @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            describe "ToFactory  Configuration" do
         | 
| 2 | 
            +
              before do
         | 
| 3 | 
            +
                ToFactory.reset_config!
         | 
| 4 | 
            +
              end
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              it do
         | 
| 7 | 
            +
                expect(ToFactory.factories).to eq "./spec/factories"
         | 
| 8 | 
            +
                ToFactory.factories = "factories dir"
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                expect(ToFactory.factories).to eq "factories dir"
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              it do
         | 
| 14 | 
            +
                expect(ToFactory.models).to eq "./app/models"
         | 
| 15 | 
            +
                ToFactory.models = "models dir"
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                expect(ToFactory.models).to eq "models dir"
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
            end
         | 
| @@ -0,0 +1,77 @@ | |
| 1 | 
            +
            describe "FileSync" do
         | 
| 2 | 
            +
              let(:user) { create_user!  }
         | 
| 3 | 
            +
              let(:admin) { create_admin!  }
         | 
| 4 | 
            +
              let(:project) { create_project!  }
         | 
| 5 | 
            +
              let(:version) { ToFactory.new_syntax? ? "new" : "old"}
         | 
| 6 | 
            +
              let(:expected_user_file)             { File.read("./spec/example_factories/#{version}_syntax/user.rb") }
         | 
| 7 | 
            +
              let(:expected_user_with_header_file) { File.read("./spec/example_factories/#{version}_syntax/user_with_header.rb") }
         | 
| 8 | 
            +
              let(:expected_admin_file)            { File.read("./spec/example_factories/#{version}_syntax/admin.rb") }
         | 
| 9 | 
            +
              let(:user_with_header)               { File.read("./spec/example_factories/#{version}_syntax/user_with_header.rb") }
         | 
| 10 | 
            +
              let(:user_admin_with_header)         { File.read("./spec/example_factories/#{version}_syntax/user_admin_with_header.rb") }
         | 
| 11 | 
            +
             | 
| 12 | 
            +
             | 
| 13 | 
            +
              before do
         | 
| 14 | 
            +
                FileUtils.rm_rf "./tmp/factories"
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              def user_file
         | 
| 18 | 
            +
                File.read("./tmp/factories/to_factory/user.rb") rescue nil
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              def project_file
         | 
| 22 | 
            +
                File.read("./tmp/factories/to_factory/project.rb") rescue nil
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
             | 
| 26 | 
            +
              context "with no arguments" do
         | 
| 27 | 
            +
                before do
         | 
| 28 | 
            +
                  user
         | 
| 29 | 
            +
                  admin
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                it "finds the first existing instance" do
         | 
| 33 | 
            +
                  sync = ToFactory::FileSync.new
         | 
| 34 | 
            +
                  sync.perform
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                  expect(user_file ).to match_sexp expected_user_with_header_file
         | 
| 37 | 
            +
                end
         | 
| 38 | 
            +
              end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
              context "with an instance" do
         | 
| 41 | 
            +
                it "writes that instance" do
         | 
| 42 | 
            +
                  sync = ToFactory::FileSync.new(user)
         | 
| 43 | 
            +
                  sync.perform
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                  expect(user_file   ).to match_sexp user_with_header
         | 
| 46 | 
            +
                  expect(project_file).to eq nil
         | 
| 47 | 
            +
                end
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
              context "with a pre-existing file" do
         | 
| 51 | 
            +
                let(:sync) { ToFactory::FileSync.new(user) }
         | 
| 52 | 
            +
                before do
         | 
| 53 | 
            +
                  sync.perform
         | 
| 54 | 
            +
                  expect(user_file).to match_sexp user_with_header
         | 
| 55 | 
            +
                end
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                it "raises an error" do
         | 
| 58 | 
            +
                  expect(lambda{ sync.perform }).to raise_error ToFactory::AlreadyExists
         | 
| 59 | 
            +
                end
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                context "with a named factory" do
         | 
| 62 | 
            +
                  it do
         | 
| 63 | 
            +
                    sync = ToFactory::FileSync.new({:admin => admin})
         | 
| 64 | 
            +
                    sync.perform
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                    parser = ToFactory::Parsing::File.new(user_file)
         | 
| 67 | 
            +
                    result = parser.parse[ToFactory::User]
         | 
| 68 | 
            +
                    expect(result[:admin]).to match_sexp expected_admin_file
         | 
| 69 | 
            +
                    expect(result[:"to_factory/user"]).to match_sexp expected_user_file
         | 
| 70 | 
            +
             | 
| 71 | 
            +
                    expect(lambda{
         | 
| 72 | 
            +
                      sync.perform
         | 
| 73 | 
            +
                    }).to raise_error ToFactory::AlreadyExists
         | 
| 74 | 
            +
                  end
         | 
| 75 | 
            +
                end
         | 
| 76 | 
            +
              end
         | 
| 77 | 
            +
            end
         | 
| @@ -0,0 +1,16 @@ | |
| 1 | 
            +
            describe ToFactory::FileWriter do
         | 
| 2 | 
            +
              let(:fw) { ToFactory::FileWriter.new }
         | 
| 3 | 
            +
              let(:expected)    { File.read "./spec/example_factories/#{version}_syntax/user_with_header.rb"}
         | 
| 4 | 
            +
              let(:user_file_contents)    { File.read "./tmp/factories/to_factory/user.rb"}
         | 
| 5 | 
            +
              let!(:user) { create_user!  }
         | 
| 6 | 
            +
              let!(:admin) { create_admin!  }
         | 
| 7 | 
            +
              it do
         | 
| 8 | 
            +
                fs = ToFactory::FileSync.new
         | 
| 9 | 
            +
                definitions = fs.new_definitions
         | 
| 10 | 
            +
                #sanity check generation isn't broken
         | 
| 11 | 
            +
                expect(definitions.keys).to eq [ToFactory::User]
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                fw.write definitions
         | 
| 14 | 
            +
                expect(user_file_contents).to match_sexp expected
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
            end
         | 
| @@ -0,0 +1,107 @@ | |
| 1 | 
            +
            describe ToFactory do
         | 
| 2 | 
            +
              let!(:user)    { create_user! }
         | 
| 3 | 
            +
              let!(:project) { create_project!}
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              before { FileUtils.rm_rf "./tmp/factories" }
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              def user_file
         | 
| 8 | 
            +
                File.read("./tmp/factories/to_factory/user.rb") rescue nil
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              def project_file
         | 
| 12 | 
            +
                File.read("./tmp/factories/to_factory/project.rb") rescue nil
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              let(:expected_user_file) { File.read "./spec/example_factories/#{version}_syntax/user_with_header.rb"}
         | 
| 16 | 
            +
              let(:expected_project_file) { File.read "./spec/example_factories/#{version}_syntax/project_with_header.rb"}
         | 
| 17 | 
            +
             | 
| 18 | 
            +
              describe "Object#ToFactory" do
         | 
| 19 | 
            +
                it "generates all factories" do
         | 
| 20 | 
            +
                  ToFactory()
         | 
| 21 | 
            +
                  expect(user_file)   .to match_sexp expected_user_file
         | 
| 22 | 
            +
                  expect(project_file).to match_sexp expected_project_file
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                def user_file_includes(content)
         | 
| 26 | 
            +
                  expect(user_file).to include content
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                context "excluding classes" do
         | 
| 30 | 
            +
                  before do
         | 
| 31 | 
            +
                    user
         | 
| 32 | 
            +
                    project
         | 
| 33 | 
            +
                  end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                  it "ignores specified classes" do
         | 
| 36 | 
            +
                    ToFactory(:exclude => ToFactory::User)
         | 
| 37 | 
            +
                    expect(user_file).to be_nil
         | 
| 38 | 
            +
                    expect(project_file).to be_present
         | 
| 39 | 
            +
                  end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                  it "ignores specified classes - sanity check" do
         | 
| 42 | 
            +
                    ToFactory(:exclude => ToFactory::Project)
         | 
| 43 | 
            +
                    expect(user_file).to be_present
         | 
| 44 | 
            +
                    expect(project_file).to be_nil
         | 
| 45 | 
            +
                  end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                  it "doesn't auto generate any if :all is specified" do
         | 
| 48 | 
            +
                    ToFactory(:exclude => :all)
         | 
| 49 | 
            +
                    expect(user_file).to be_nil
         | 
| 50 | 
            +
                    expect(project_file).to be_nil
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                    #sanity check
         | 
| 53 | 
            +
                    ToFactory()
         | 
| 54 | 
            +
                    expect(user_file).to be_present
         | 
| 55 | 
            +
                    expect(project_file).to be_present
         | 
| 56 | 
            +
                  end
         | 
| 57 | 
            +
                end
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                context "with no existing file" do
         | 
| 60 | 
            +
                  it "creates the file" do
         | 
| 61 | 
            +
                    expect(user_file).to be_nil
         | 
| 62 | 
            +
                    ToFactory(user)
         | 
| 63 | 
            +
                    expect(user_file).to be_present
         | 
| 64 | 
            +
                  end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                  context "with single ActiveRecord::Base instance argument" do
         | 
| 67 | 
            +
                    it "creates the file" do
         | 
| 68 | 
            +
                      expect(user_file).to be_nil
         | 
| 69 | 
            +
                      ToFactory(user)
         | 
| 70 | 
            +
                      expect(user_file).to be_present
         | 
| 71 | 
            +
                    end
         | 
| 72 | 
            +
                  end
         | 
| 73 | 
            +
                end
         | 
| 74 | 
            +
             | 
| 75 | 
            +
                context "with an existing file" do
         | 
| 76 | 
            +
                  before do
         | 
| 77 | 
            +
                    expect(user_file).to be_nil
         | 
| 78 | 
            +
                    ToFactory(user)
         | 
| 79 | 
            +
                    expect(user_file).to be_present
         | 
| 80 | 
            +
                  end
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                  context "with a name for the factory" do
         | 
| 83 | 
            +
                    it "appends to the file" do
         | 
| 84 | 
            +
                      if ToFactory.new_syntax?
         | 
| 85 | 
            +
                        user_file_includes('factory(:"to_factory/user"')
         | 
| 86 | 
            +
                      else
         | 
| 87 | 
            +
                        user_file_includes('Factory.define(:"to_factory/user"')
         | 
| 88 | 
            +
                      end
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                      ToFactory(:specific_user => user)
         | 
| 91 | 
            +
                      if ToFactory.new_syntax?
         | 
| 92 | 
            +
                        user_file_includes('factory(:specific_user, :parent => :"to_factory/user"')
         | 
| 93 | 
            +
                      else
         | 
| 94 | 
            +
                        user_file_includes('Factory.define(:specific_user, :parent => :"to_factory/user"')
         | 
| 95 | 
            +
                      end
         | 
| 96 | 
            +
             | 
| 97 | 
            +
             | 
| 98 | 
            +
                    end
         | 
| 99 | 
            +
                  end
         | 
| 100 | 
            +
             | 
| 101 | 
            +
                  it "without a name" do
         | 
| 102 | 
            +
                    expect(lambda{ToFactory(user)}).
         | 
| 103 | 
            +
                           to raise_error ToFactory::AlreadyExists
         | 
| 104 | 
            +
                  end
         | 
| 105 | 
            +
                end
         | 
| 106 | 
            +
              end
         | 
| 107 | 
            +
            end
         | 
| @@ -0,0 +1,16 @@ | |
| 1 | 
            +
            context "full integration" do
         | 
| 2 | 
            +
              let(:user) do
         | 
| 3 | 
            +
                ToFactory::User.create :name => "Jeff", :email => "test@example.com", :some_id => 8, :birthday => Time.now
         | 
| 4 | 
            +
              end
         | 
| 5 | 
            +
             | 
| 6 | 
            +
              it "#to_factory linting the output" do
         | 
| 7 | 
            +
                if ToFactory.new_syntax?
         | 
| 8 | 
            +
                  ToFactory(user)
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  load "./tmp/factories/to_factory/user.rb"
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                  FactoryGirl.lint
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
            end
         | 
| 16 | 
            +
             | 
    
        data/spec/spec_helper.rb
    CHANGED
    
    | @@ -1,17 +1,93 @@ | |
| 1 | 
            -
             | 
| 2 | 
            -
            require  | 
| 3 | 
            -
             | 
| 4 | 
            -
             | 
| 5 | 
            -
             | 
| 6 | 
            -
              require 'active_record'
         | 
| 7 | 
            -
              require 'rspec'
         | 
| 8 | 
            -
              require 'yaml'
         | 
| 9 | 
            -
              require 'fileutils'
         | 
| 10 | 
            -
              require 'ruby-debug'
         | 
| 11 | 
            -
              require 'active_support/core_ext/string'
         | 
| 12 | 
            -
              require 'active_support/core_ext/hash'
         | 
| 1 | 
            +
            begin
         | 
| 2 | 
            +
              require "codeclimate-test-reporter"
         | 
| 3 | 
            +
              CodeClimate::TestReporter.start
         | 
| 4 | 
            +
            rescue LoadError
         | 
| 5 | 
            +
              #ignore on ruby 1.8.x
         | 
| 13 6 | 
             
            end
         | 
| 14 7 |  | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 8 | 
            +
            require 'active_record'
         | 
| 9 | 
            +
            require 'fileutils'
         | 
| 10 | 
            +
            require 'active_support/core_ext/string'
         | 
| 11 | 
            +
            require 'active_support/core_ext/hash'
         | 
| 12 | 
            +
            require "sqlite3"
         | 
| 13 | 
            +
            require "database_cleaner"
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            begin
         | 
| 16 | 
            +
              require "pry-byebug"
         | 
| 17 | 
            +
            rescue LoadError
         | 
| 18 | 
            +
              begin
         | 
| 19 | 
            +
                require "ruby-debug"
         | 
| 20 | 
            +
              rescue LoadError
         | 
| 21 | 
            +
                $stderr.puts "No debugger available for #{RUBY_VERSION}"
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
            end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            require "to_factory"
         | 
| 26 | 
            +
             | 
| 27 | 
            +
            require "./spec/support/models/user"
         | 
| 28 | 
            +
            require "./spec/support/models/project"
         | 
| 29 | 
            +
            require "./spec/support/terse_expect_syntax"
         | 
| 30 | 
            +
            require "./spec/support/match_sexp"
         | 
| 31 | 
            +
             | 
| 32 | 
            +
            module ToFactory::DataCreation
         | 
| 33 | 
            +
              def create_all!
         | 
| 34 | 
            +
                create_user!
         | 
| 35 | 
            +
                create_admin!
         | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              def create_project!
         | 
| 39 | 
            +
                ToFactory::Project.create({:name => "My Project", :objective => "easy testing", :some_id => 9 })
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
              def birthday
         | 
| 43 | 
            +
                Time.find_zone("UTC").parse("2014-07-08T15:30Z")
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              def create_user!
         | 
| 47 | 
            +
                ToFactory::User.create :name => "Jeff", :email => "test@example.com", :some_id => 8, :birthday => birthday
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
              def create_admin!
         | 
| 51 | 
            +
                ToFactory::User.create :name => "Admin", :email => "admin@example.com", :some_id => 9, :birthday => birthday
         | 
| 52 | 
            +
              end
         | 
| 53 | 
            +
            end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
            module ToFactory::SpecSyntaxHelpers
         | 
| 56 | 
            +
              def version
         | 
| 57 | 
            +
                ToFactory.new_syntax? ? "new" : "old"
         | 
| 58 | 
            +
              end
         | 
| 59 | 
            +
            end
         | 
| 60 | 
            +
             | 
| 61 | 
            +
            RSpec.configure do |config|
         | 
| 62 | 
            +
              config.include TerseExpectSyntax
         | 
| 63 | 
            +
              config.include ToFactory::DataCreation
         | 
| 64 | 
            +
              config.include ToFactory::SpecSyntaxHelpers
         | 
| 65 | 
            +
             | 
| 66 | 
            +
              config.before :suite do
         | 
| 67 | 
            +
                ActiveRecord::Base.tap do |base|
         | 
| 68 | 
            +
                  config = {:adapter => "sqlite3", :database => "spec/db/test.sqlite3"}
         | 
| 69 | 
            +
                  base.configurations = {:test => config}.with_indifferent_access
         | 
| 70 | 
            +
                  base.establish_connection :test
         | 
| 71 | 
            +
                end
         | 
| 72 | 
            +
             | 
| 73 | 
            +
                DatabaseCleaner.strategy = :truncation
         | 
| 74 | 
            +
                DatabaseCleaner.clean_with(:truncation)
         | 
| 75 | 
            +
                DatabaseCleaner.start
         | 
| 76 | 
            +
              end
         | 
| 77 | 
            +
             | 
| 78 | 
            +
              config.before(:each) do
         | 
| 79 | 
            +
                FileUtils.rm_rf "tmp/factories"
         | 
| 80 | 
            +
                DatabaseCleaner.clean
         | 
| 81 | 
            +
                ToFactory.reset_config!
         | 
| 82 | 
            +
                ToFactory.models = "./spec/support/models"
         | 
| 83 | 
            +
                ToFactory.factories = "./tmp/factories"
         | 
| 84 | 
            +
              end
         | 
| 85 | 
            +
             | 
| 86 | 
            +
              config.expect_with :rspec do |expectations|
         | 
| 87 | 
            +
                expectations.include_chain_clauses_in_custom_matcher_descriptions = true
         | 
| 88 | 
            +
              end
         | 
| 89 | 
            +
             | 
| 90 | 
            +
              config.mock_with :rspec do |mocks|
         | 
| 91 | 
            +
                mocks.verify_partial_doubles = true
         | 
| 92 | 
            +
              end
         | 
| 17 93 | 
             
            end
         |