From 510da0b12c7bccba40dcc08cb334760b25eabc83 Mon Sep 17 00:00:00 2001 From: Dominic Sayers Date: Tue, 16 May 2017 15:20:08 +0100 Subject: [PATCH 1/6] Rubocop autofix --- .gitignore | 7 +- .rubocop.yml | 59 +++++++++ .travis.yml | 22 ++-- Appraisals | 11 ++ Gemfile | 23 +++- README.md | 15 +-- Rakefile | 16 ++- gemfiles/3.0.gemfile | 13 -- gemfiles/3.1.gemfile | 13 -- gemfiles/3.2.gemfile | 13 -- gemfiles/4.0.gemfile | 13 -- gemfiles/4.1.beta.gemfile | 13 -- gemfiles/rails_4.2.gemfile | 33 +++++ gemfiles/rails_4.2.gemfile.lock | 117 ++++++++++++++++++ gemfiles/rails_5.0.gemfile | 33 +++++ gemfiles/rails_5.0.gemfile.lock | 117 ++++++++++++++++++ gemfiles/rails_5.1.gemfile | 33 +++++ gemfiles/rails_5.1.gemfile.lock | 117 ++++++++++++++++++ .../machinist/install/install_generator.rb | 14 +-- .../machinist/model/model_generator.rb | 6 +- lib/machinist.rb | 1 - lib/machinist/active_record/blueprint.rb | 2 - lib/machinist/active_record/lathe.rb | 3 - lib/machinist/blueprint.rb | 35 +++--- lib/machinist/exceptions.rb | 2 - lib/machinist/lathe.rb | 13 +- lib/machinist/machinable.rb | 14 +-- lib/machinist/version.rb | 2 +- machinist.gemspec | 24 ++-- spec/active_record_spec.rb | 39 +++--- spec/blueprint_spec.rb | 64 +++++----- spec/exceptions_spec.rb | 14 +-- spec/inheritance_spec.rb | 70 +++++------ spec/machinable_spec.rb | 38 +++--- spec/support/active_record_environment.rb | 22 ++-- 35 files changed, 736 insertions(+), 295 deletions(-) create mode 100755 .rubocop.yml create mode 100755 Appraisals mode change 100644 => 100755 Gemfile mode change 100644 => 100755 README.md mode change 100644 => 100755 Rakefile delete mode 100644 gemfiles/3.0.gemfile delete mode 100644 gemfiles/3.1.gemfile delete mode 100644 gemfiles/3.2.gemfile delete mode 100644 gemfiles/4.0.gemfile delete mode 100644 gemfiles/4.1.beta.gemfile create mode 100755 gemfiles/rails_4.2.gemfile create mode 100644 gemfiles/rails_4.2.gemfile.lock create mode 100755 gemfiles/rails_5.0.gemfile create mode 100644 gemfiles/rails_5.0.gemfile.lock create mode 100755 gemfiles/rails_5.1.gemfile create mode 100644 gemfiles/rails_5.1.gemfile.lock mode change 100644 => 100755 machinist.gemspec diff --git a/.gitignore b/.gitignore index 7c80de9..50b5e04 100755 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ -.ruby-version -.ruby-gemset *.gem *.rbc .bundle @@ -8,8 +6,12 @@ .rbenv-* .rbx .rbxpkg +.rspec +.ruby-gemset +.ruby-version .rvmrc .yardoc +_yardoc coverage doc/ Gemfile.lock @@ -21,4 +23,3 @@ tags test/tmp test/version_tmp tmp -_yardoc diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100755 index 0000000..646b534 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,59 @@ +--- +require: rubocop-rspec + +AllCops: + TargetRubyVersion: 2.2 + DisplayCopNames: true + Exclude: + - 'tmp/**/*' + +Style/MixinGrouping: + Exclude: + - 'spec/**/*.rb' + +Metrics/BlockLength: + CountComments: false # count full line comments? + Exclude: + - '**/*_spec.rb' + +StringLiterals: + EnforcedStyle: single_quotes + Enabled: true + +DotPosition: + EnforcedStyle: leading + Enabled: true + +ClassAndModuleChildren: + EnforcedStyle: nested + Enabled: true + +Documentation: + Enabled: false + +FileName: + Enabled: true + +LineLength: + Max: 120 + Enabled: true + +Style/ExtraSpacing: + Enabled: true + +Lint/LiteralInInterpolation: + AutoCorrect: true + +Style/ModuleFunction: + EnforcedStyle: extend_self # Allows us to have private methods too + +Style/PercentLiteralDelimiters: + # Hound and CodeClimate are currently using an old version of Rubocop with + # different defaults, so we set them explicitly here. + PreferredDelimiters: + default: () + '%i': '[]' + '%I': '[]' + '%r': '{}' + '%w': '[]' + '%W': '[]' diff --git a/.travis.yml b/.travis.yml index 6888b39..175e75d 100755 --- a/.travis.yml +++ b/.travis.yml @@ -1,26 +1,22 @@ +--- language: ruby + rvm: - ruby-head - - 2.1.0 - - 2.0.0 - - rbx-2.1.1 - - 1.9.3 - - jruby-19mode # JRuby in 1.9 mode - - rbx-19mode + - 2.4.1 + - 2.3.4 + - 2.2.6 + - 2.1.10 gemfile: - - gemfiles/3.0.gemfile - - gemfiles/3.1.gemfile - - gemfiles/3.2.gemfile - - gemfiles/4.0.gemfile - - gemfiles/4.1.gemfile - gemfiles/4.2.gemfile + - gemfiles/5.0.gemfile + - gemfiles/5.1.gemfile branches: only: - master + matrix: allow_failures: - - rvm: jruby-18mode - - rvm: jruby-19mode - rvm: ruby-head diff --git a/Appraisals b/Appraisals new file mode 100755 index 0000000..f76d745 --- /dev/null +++ b/Appraisals @@ -0,0 +1,11 @@ +appraise 'rails-4.2' do + gem 'activerecord', '~> 4.2.8', group: :test, require: false +end + +appraise 'rails-5.0' do + gem 'activerecord', '~> 5.0.3', group: :test, require: false +end + +appraise 'rails-5.1' do + gem 'activerecord', '~> 5.1.1', group: :test, require: false +end diff --git a/Gemfile b/Gemfile old mode 100644 new mode 100755 index 4bf5e22..9b29375 --- a/Gemfile +++ b/Gemfile @@ -1,10 +1,27 @@ -source "http://rubygems.org" +source 'http://rubygems.org' gemspec +ruby RUBY_VERSION + +group :development do + gem 'appraisal', require: false + gem 'rdoc', require: false + gem 'rubocop-rspec', require: false +end + +group :test do + gem 'codeclimate-test-reporter', require: false + gem 'coveralls', require: false + gem 'fuubar', require: false + gem 'rspec', require: false + gem 'rspec_junit_formatter', require: false + gem 'simplecov', '~> 0.14', require: false + gem 'test-unit', require: false +end platforms :ruby do - gem "sqlite3" + gem 'sqlite3' end platforms :jruby do - gem "activerecord-jdbcsqlite3-adapter" + gem 'activerecord-jdbcsqlite3-adapter' end diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 50d4b56..0593b58 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ # Machinist 2 -*Fixtures aren't fun. Machinist is.* +*Fixtures aren't fun. Machinist was.* -- [Home page](http://github.com/liquid/machinist) +- [Home page](http://github.com/dominicsayers/machinist) - [Google group](http://groups.google.com/group/machinist-users), for support -- [Bug tracker](http://github.com/liquid/machinist/issues), for reporting Machinist bugs +- [Bug tracker](http://github.com/dominicsayers/machinist/issues), for reporting Machinist bugs If you want Machinist 1, [go here](http://github.com/notahat/machinist/tree/1.0-maintenance). @@ -218,14 +218,14 @@ You can refer to already assigned attributes when constructing a new attribute: Read the code! No, really. I wrote this code to be read. -Check out [the specs](https://github.com/notahat/machinist/tree/master/spec), +Check out [the specs](https://github.com/dominicsayers/machinist/tree/master/spec), starting with [the spec for -Machinable](https://github.com/notahat/machinist/blob/master/spec/machinable_spec.rb). +Machinable](https://github.com/dominicsayers/machinist/blob/master/spec/machinable_spec.rb). ## Compatibility -Please have a look at the Travis Build Status. [![Travis Build Status](https://travis-ci.org/liquid/machinist.png?branch=master)](https://travis-ci.org/liquid/machinist) +Please have a look at the Travis Build Status. [![Travis Build Status](https://travis-ci.org/dominicsayers/machinist.png?branch=master)](https://travis-ci.org/dominicsayers/machinist) I've tested this with: @@ -265,7 +265,7 @@ If anybody wants to take over maintenance, let me know. ## Contributors -Machinist is maintained by Pete Yandell ([pete@notahat.com](mailto:pete@notahat.com), [@notahat](http://twitter.com/notahat)) +Machinist was maintained by Pete Yandell ([pete@notahat.com](mailto:pete@notahat.com), [@notahat](http://twitter.com/notahat)) Other contributors include: @@ -290,6 +290,7 @@ Other contributors include: [Gareth Townsend](http://github.com/quamen), [Matt Wastrodowski](http://github.com/towski), [Ian White](http://github.com/ianwhite) +[Dominic Sayers](https://github.com/dominicsayers) Thanks to Thoughtbot's [Factory Girl](http://github.com/thoughtbot/factory_girl/tree/master). Machinist was diff --git a/Rakefile b/Rakefile old mode 100644 new mode 100755 index fdcaa9a..02d61ec --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,15 @@ -require "bundler/gem_tasks" +begin + require 'rubygems' + require 'bundler/setup' + require 'bundler/gem_tasks' + require 'rspec/core/rake_task' -require 'rspec/core/rake_task' -RSpec::Core::RakeTask.new(:spec) + RSpec::Core::RakeTask.new(:spec) + + task default: :spec +rescue LoadError + puts 'rspec is not available' +end require 'rdoc/task' RDoc::Task.new(:rdoc) do |rdoc| @@ -10,5 +18,3 @@ RDoc::Task.new(:rdoc) do |rdoc| rdoc.options << '--line-numbers' rdoc.rdoc_files.include('lib') end - -task :default => :spec diff --git a/gemfiles/3.0.gemfile b/gemfiles/3.0.gemfile deleted file mode 100644 index 42e126c..0000000 --- a/gemfiles/3.0.gemfile +++ /dev/null @@ -1,13 +0,0 @@ -source 'https://rubygems.org' - -gem 'activerecord', '~> 3.0.20' - -platforms :ruby do - gem "sqlite3" -end - -platforms :jruby do - gem "activerecord-jdbcsqlite3-adapter" -end - -gemspec :path => '../' diff --git a/gemfiles/3.1.gemfile b/gemfiles/3.1.gemfile deleted file mode 100644 index bedd534..0000000 --- a/gemfiles/3.1.gemfile +++ /dev/null @@ -1,13 +0,0 @@ -source 'https://rubygems.org' - -gem 'activerecord', '~> 3.1.12' - -platforms :ruby do - gem "sqlite3" -end - -platforms :jruby do - gem "activerecord-jdbcsqlite3-adapter" -end - -gemspec :path => '../' diff --git a/gemfiles/3.2.gemfile b/gemfiles/3.2.gemfile deleted file mode 100644 index bde4ca7..0000000 --- a/gemfiles/3.2.gemfile +++ /dev/null @@ -1,13 +0,0 @@ -source 'https://rubygems.org' - -gem 'activerecord', '~> 3.2.16' - -platforms :ruby do - gem "sqlite3" -end - -platforms :jruby do - gem "activerecord-jdbcsqlite3-adapter" -end - -gemspec :path => '../' diff --git a/gemfiles/4.0.gemfile b/gemfiles/4.0.gemfile deleted file mode 100644 index c9fcda0..0000000 --- a/gemfiles/4.0.gemfile +++ /dev/null @@ -1,13 +0,0 @@ -source 'https://rubygems.org' - -gem 'activerecord', '~> 4.0.2' - -platforms :ruby do - gem "sqlite3" -end - -platforms :jruby do - gem "activerecord-jdbcsqlite3-adapter" -end - -gemspec :path => '../' diff --git a/gemfiles/4.1.beta.gemfile b/gemfiles/4.1.beta.gemfile deleted file mode 100644 index 0e3b050..0000000 --- a/gemfiles/4.1.beta.gemfile +++ /dev/null @@ -1,13 +0,0 @@ -source 'https://rubygems.org' - -gem 'activerecord', '~> 4.1.0.beta1' - -platforms :ruby do - gem "sqlite3" -end - -platforms :jruby do - gem "activerecord-jdbcsqlite3-adapter" -end - -gemspec :path => '../' diff --git a/gemfiles/rails_4.2.gemfile b/gemfiles/rails_4.2.gemfile new file mode 100755 index 0000000..30ac46d --- /dev/null +++ b/gemfiles/rails_4.2.gemfile @@ -0,0 +1,33 @@ +# This file was generated by Appraisal + +source "http://rubygems.org" + +ruby RUBY_VERSION + +gem "activerecord", "~> 4.2.8", group: :test, require: false + +group :development do + gem "appraisal", require: false + gem "rubocop-rspec", require: false + gem "rdoc", require: false +end + +group :test do + gem "test-unit", require: false + gem "codeclimate-test-reporter", require: false + gem "coveralls", require: false + gem "fuubar", require: false + gem "rspec", require: false + gem "rspec_junit_formatter", require: false + gem "simplecov", "~> 0.14", require: false +end + +platforms :ruby do + gem "sqlite3" +end + +platforms :jruby do + gem "activerecord-jdbcsqlite3-adapter" +end + +gemspec path: "../" diff --git a/gemfiles/rails_4.2.gemfile.lock b/gemfiles/rails_4.2.gemfile.lock new file mode 100644 index 0000000..898ef26 --- /dev/null +++ b/gemfiles/rails_4.2.gemfile.lock @@ -0,0 +1,117 @@ +PATH + remote: .. + specs: + machinist (2.0) + +GEM + remote: http://rubygems.org/ + specs: + activemodel (4.2.8) + activesupport (= 4.2.8) + builder (~> 3.1) + activerecord (4.2.8) + activemodel (= 4.2.8) + activesupport (= 4.2.8) + arel (~> 6.0) + activesupport (4.2.8) + i18n (~> 0.7) + minitest (~> 5.1) + thread_safe (~> 0.3, >= 0.3.4) + tzinfo (~> 1.1) + appraisal (2.2.0) + bundler + rake + thor (>= 0.14.0) + arel (6.0.4) + ast (2.3.0) + builder (3.2.3) + codeclimate-test-reporter (1.0.7) + simplecov + coveralls (0.8.21) + json (>= 1.8, < 3) + simplecov (~> 0.14.1) + term-ansicolor (~> 1.3) + thor (~> 0.19.4) + tins (~> 1.6) + diff-lcs (1.3) + docile (1.1.5) + fuubar (2.2.0) + rspec-core (~> 3.0) + ruby-progressbar (~> 1.4) + i18n (0.8.1) + json (2.1.0) + minitest (5.10.2) + parser (2.4.0.0) + ast (~> 2.2) + power_assert (1.0.2) + powerpack (0.1.1) + rainbow (2.2.2) + rake + rake (12.0.0) + rdoc (5.1.0) + rspec (3.6.0) + rspec-core (~> 3.6.0) + rspec-expectations (~> 3.6.0) + rspec-mocks (~> 3.6.0) + rspec-core (3.6.0) + rspec-support (~> 3.6.0) + rspec-expectations (3.6.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.6.0) + rspec-mocks (3.6.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.6.0) + rspec-support (3.6.0) + rspec_junit_formatter (0.2.3) + builder (< 4) + rspec-core (>= 2, < 4, != 2.12.0) + rubocop (0.48.1) + parser (>= 2.3.3.1, < 3.0) + powerpack (~> 0.1) + rainbow (>= 1.99.1, < 3.0) + ruby-progressbar (~> 1.7) + unicode-display_width (~> 1.0, >= 1.0.1) + rubocop-rspec (1.15.1) + rubocop (>= 0.42.0) + ruby-progressbar (1.8.1) + simplecov (0.14.1) + docile (~> 1.1.0) + json (>= 1.8, < 3) + simplecov-html (~> 0.10.0) + simplecov-html (0.10.0) + sqlite3 (1.3.13) + term-ansicolor (1.6.0) + tins (~> 1.0) + test-unit (3.2.3) + power_assert + thor (0.19.4) + thread_safe (0.3.6) + tins (1.13.3) + tzinfo (1.2.3) + thread_safe (~> 0.1) + unicode-display_width (1.2.1) + +PLATFORMS + ruby + +DEPENDENCIES + activerecord (~> 4.2.8) + activerecord-jdbcsqlite3-adapter + appraisal + codeclimate-test-reporter + coveralls + fuubar + machinist! + rdoc + rspec + rspec_junit_formatter + rubocop-rspec + simplecov (~> 0.14) + sqlite3 + test-unit + +RUBY VERSION + ruby 2.3.4p301 + +BUNDLED WITH + 1.14.6 diff --git a/gemfiles/rails_5.0.gemfile b/gemfiles/rails_5.0.gemfile new file mode 100755 index 0000000..e5057bf --- /dev/null +++ b/gemfiles/rails_5.0.gemfile @@ -0,0 +1,33 @@ +# This file was generated by Appraisal + +source "http://rubygems.org" + +ruby RUBY_VERSION + +gem "activerecord", "~> 5.0.3", group: :test, require: false + +group :development do + gem "appraisal", require: false + gem "rubocop-rspec", require: false + gem "rdoc", require: false +end + +group :test do + gem "test-unit", require: false + gem "codeclimate-test-reporter", require: false + gem "coveralls", require: false + gem "fuubar", require: false + gem "rspec", require: false + gem "rspec_junit_formatter", require: false + gem "simplecov", "~> 0.14", require: false +end + +platforms :ruby do + gem "sqlite3" +end + +platforms :jruby do + gem "activerecord-jdbcsqlite3-adapter" +end + +gemspec path: "../" diff --git a/gemfiles/rails_5.0.gemfile.lock b/gemfiles/rails_5.0.gemfile.lock new file mode 100644 index 0000000..9dcfecd --- /dev/null +++ b/gemfiles/rails_5.0.gemfile.lock @@ -0,0 +1,117 @@ +PATH + remote: .. + specs: + machinist (2.0) + +GEM + remote: http://rubygems.org/ + specs: + activemodel (5.0.3) + activesupport (= 5.0.3) + activerecord (5.0.3) + activemodel (= 5.0.3) + activesupport (= 5.0.3) + arel (~> 7.0) + activesupport (5.0.3) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (~> 0.7) + minitest (~> 5.1) + tzinfo (~> 1.1) + appraisal (2.2.0) + bundler + rake + thor (>= 0.14.0) + arel (7.1.4) + ast (2.3.0) + builder (3.2.3) + codeclimate-test-reporter (1.0.7) + simplecov + concurrent-ruby (1.0.5) + coveralls (0.8.21) + json (>= 1.8, < 3) + simplecov (~> 0.14.1) + term-ansicolor (~> 1.3) + thor (~> 0.19.4) + tins (~> 1.6) + diff-lcs (1.3) + docile (1.1.5) + fuubar (2.2.0) + rspec-core (~> 3.0) + ruby-progressbar (~> 1.4) + i18n (0.8.1) + json (2.1.0) + minitest (5.10.2) + parser (2.4.0.0) + ast (~> 2.2) + power_assert (1.0.2) + powerpack (0.1.1) + rainbow (2.2.2) + rake + rake (12.0.0) + rdoc (5.1.0) + rspec (3.6.0) + rspec-core (~> 3.6.0) + rspec-expectations (~> 3.6.0) + rspec-mocks (~> 3.6.0) + rspec-core (3.6.0) + rspec-support (~> 3.6.0) + rspec-expectations (3.6.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.6.0) + rspec-mocks (3.6.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.6.0) + rspec-support (3.6.0) + rspec_junit_formatter (0.2.3) + builder (< 4) + rspec-core (>= 2, < 4, != 2.12.0) + rubocop (0.48.1) + parser (>= 2.3.3.1, < 3.0) + powerpack (~> 0.1) + rainbow (>= 1.99.1, < 3.0) + ruby-progressbar (~> 1.7) + unicode-display_width (~> 1.0, >= 1.0.1) + rubocop-rspec (1.15.1) + rubocop (>= 0.42.0) + ruby-progressbar (1.8.1) + simplecov (0.14.1) + docile (~> 1.1.0) + json (>= 1.8, < 3) + simplecov-html (~> 0.10.0) + simplecov-html (0.10.0) + sqlite3 (1.3.13) + term-ansicolor (1.6.0) + tins (~> 1.0) + test-unit (3.2.3) + power_assert + thor (0.19.4) + thread_safe (0.3.6) + tins (1.13.3) + tzinfo (1.2.3) + thread_safe (~> 0.1) + unicode-display_width (1.2.1) + +PLATFORMS + ruby + +DEPENDENCIES + activerecord (~> 5.0.3) + activerecord-jdbcsqlite3-adapter + appraisal + codeclimate-test-reporter + coveralls + fuubar + machinist! + rdoc + rspec + rspec_junit_formatter + rubocop-rspec + simplecov (~> 0.14) + sqlite3 + test-unit + +RUBY VERSION + ruby 2.3.4p301 + +BUNDLED WITH + 1.14.6 diff --git a/gemfiles/rails_5.1.gemfile b/gemfiles/rails_5.1.gemfile new file mode 100755 index 0000000..e7e7a73 --- /dev/null +++ b/gemfiles/rails_5.1.gemfile @@ -0,0 +1,33 @@ +# This file was generated by Appraisal + +source "http://rubygems.org" + +ruby RUBY_VERSION + +gem "activerecord", "~> 5.1.1", group: :test, require: false + +group :development do + gem "appraisal", require: false + gem "rubocop-rspec", require: false + gem "rdoc", require: false +end + +group :test do + gem "test-unit", require: false + gem "codeclimate-test-reporter", require: false + gem "coveralls", require: false + gem "fuubar", require: false + gem "rspec", require: false + gem "rspec_junit_formatter", require: false + gem "simplecov", "~> 0.14", require: false +end + +platforms :ruby do + gem "sqlite3" +end + +platforms :jruby do + gem "activerecord-jdbcsqlite3-adapter" +end + +gemspec path: "../" diff --git a/gemfiles/rails_5.1.gemfile.lock b/gemfiles/rails_5.1.gemfile.lock new file mode 100644 index 0000000..c56ab08 --- /dev/null +++ b/gemfiles/rails_5.1.gemfile.lock @@ -0,0 +1,117 @@ +PATH + remote: .. + specs: + machinist (2.0) + +GEM + remote: http://rubygems.org/ + specs: + activemodel (5.1.1) + activesupport (= 5.1.1) + activerecord (5.1.1) + activemodel (= 5.1.1) + activesupport (= 5.1.1) + arel (~> 8.0) + activesupport (5.1.1) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (~> 0.7) + minitest (~> 5.1) + tzinfo (~> 1.1) + appraisal (2.2.0) + bundler + rake + thor (>= 0.14.0) + arel (8.0.0) + ast (2.3.0) + builder (3.2.3) + codeclimate-test-reporter (1.0.7) + simplecov + concurrent-ruby (1.0.5) + coveralls (0.8.21) + json (>= 1.8, < 3) + simplecov (~> 0.14.1) + term-ansicolor (~> 1.3) + thor (~> 0.19.4) + tins (~> 1.6) + diff-lcs (1.3) + docile (1.1.5) + fuubar (2.2.0) + rspec-core (~> 3.0) + ruby-progressbar (~> 1.4) + i18n (0.8.1) + json (2.1.0) + minitest (5.10.2) + parser (2.4.0.0) + ast (~> 2.2) + power_assert (1.0.2) + powerpack (0.1.1) + rainbow (2.2.2) + rake + rake (12.0.0) + rdoc (5.1.0) + rspec (3.6.0) + rspec-core (~> 3.6.0) + rspec-expectations (~> 3.6.0) + rspec-mocks (~> 3.6.0) + rspec-core (3.6.0) + rspec-support (~> 3.6.0) + rspec-expectations (3.6.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.6.0) + rspec-mocks (3.6.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.6.0) + rspec-support (3.6.0) + rspec_junit_formatter (0.2.3) + builder (< 4) + rspec-core (>= 2, < 4, != 2.12.0) + rubocop (0.48.1) + parser (>= 2.3.3.1, < 3.0) + powerpack (~> 0.1) + rainbow (>= 1.99.1, < 3.0) + ruby-progressbar (~> 1.7) + unicode-display_width (~> 1.0, >= 1.0.1) + rubocop-rspec (1.15.1) + rubocop (>= 0.42.0) + ruby-progressbar (1.8.1) + simplecov (0.14.1) + docile (~> 1.1.0) + json (>= 1.8, < 3) + simplecov-html (~> 0.10.0) + simplecov-html (0.10.0) + sqlite3 (1.3.13) + term-ansicolor (1.6.0) + tins (~> 1.0) + test-unit (3.2.3) + power_assert + thor (0.19.4) + thread_safe (0.3.6) + tins (1.13.3) + tzinfo (1.2.3) + thread_safe (~> 0.1) + unicode-display_width (1.2.1) + +PLATFORMS + ruby + +DEPENDENCIES + activerecord (~> 5.1.1) + activerecord-jdbcsqlite3-adapter + appraisal + codeclimate-test-reporter + coveralls + fuubar + machinist! + rdoc + rspec + rspec_junit_formatter + rubocop-rspec + simplecov (~> 0.14) + sqlite3 + test-unit + +RUBY VERSION + ruby 2.3.4p301 + +BUNDLED WITH + 1.14.6 diff --git a/lib/generators/machinist/install/install_generator.rb b/lib/generators/machinist/install/install_generator.rb index 3840062..9ff51bf 100644 --- a/lib/generators/machinist/install/install_generator.rb +++ b/lib/generators/machinist/install/install_generator.rb @@ -3,20 +3,20 @@ module Generators #:nodoc: class InstallGenerator < Rails::Generators::Base #:nodoc: source_root File.expand_path('../templates', __FILE__) - class_option :test_framework, :type => :string, :aliases => "-t", :desc => "Test framework to use Machinist with" - class_option :cucumber, :type => :boolean, :desc => "Set up access to Machinist from Cucumber" + class_option :test_framework, type: :string, aliases: '-t', desc: 'Test framework to use Machinist with' + class_option :cucumber, type: :boolean, desc: 'Set up access to Machinist from Cucumber' def blueprints_file if rspec? - copy_file "blueprints.rb", "spec/support/blueprints.rb" + copy_file 'blueprints.rb', 'spec/support/blueprints.rb' else - copy_file "blueprints.rb", "test/blueprints.rb" + copy_file 'blueprints.rb', 'test/blueprints.rb' end end def test_helper if test_unit? - inject_into_file("test/test_helper.rb", :after => "require 'rails/test_help'\n") do + inject_into_file('test/test_helper.rb', after: "require 'rails/test_help'\n") do "require File.expand_path(File.dirname(__FILE__) + '/blueprints')\n" end end @@ -24,11 +24,11 @@ def test_helper def cucumber_support if cucumber? - template "machinist.rb.erb", "features/support/machinist.rb" + template 'machinist.rb.erb', 'features/support/machinist.rb' end end - private + private def rspec? options[:test_framework].to_sym == :rspec diff --git a/lib/generators/machinist/model/model_generator.rb b/lib/generators/machinist/model/model_generator.rb index 6027950..d18df41 100644 --- a/lib/generators/machinist/model/model_generator.rb +++ b/lib/generators/machinist/model/model_generator.rb @@ -1,13 +1,11 @@ module Machinist module Generators #:nodoc: class ModelGenerator < Rails::Generators::NamedBase #:nodoc: - argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" + argument :attributes, type: :array, default: [], banner: 'field:type field:type' def create_blueprint - append_file "spec/support/blueprints.rb", "\n#{class_name}.blueprint do\n # Attributes here\nend\n" + append_file 'spec/support/blueprints.rb', "\n#{class_name}.blueprint do\n # Attributes here\nend\n" end - end end end - diff --git a/lib/machinist.rb b/lib/machinist.rb index b9366cf..f7ed011 100644 --- a/lib/machinist.rb +++ b/lib/machinist.rb @@ -3,4 +3,3 @@ require 'machinist/exceptions' require 'machinist/lathe' require 'machinist/machinable' - diff --git a/lib/machinist/active_record/blueprint.rb b/lib/machinist/active_record/blueprint.rb index a226b7e..35616be 100644 --- a/lib/machinist/active_record/blueprint.rb +++ b/lib/machinist/active_record/blueprint.rb @@ -1,6 +1,5 @@ module Machinist::ActiveRecord class Blueprint < Machinist::Blueprint - # Make and save an object. def make!(attributes = {}) object = make(attributes) @@ -11,6 +10,5 @@ def make!(attributes = {}) def lathe_class #:nodoc: Machinist::ActiveRecord::Lathe end - end end diff --git a/lib/machinist/active_record/lathe.rb b/lib/machinist/active_record/lathe.rb index fc1f335..d5e8f0e 100644 --- a/lib/machinist/active_record/lathe.rb +++ b/lib/machinist/active_record/lathe.rb @@ -1,7 +1,5 @@ module Machinist::ActiveRecord - class Lathe < Machinist::Lathe - def make_one_value(attribute, args) #:nodoc: if block_given? raise_argument_error(attribute) unless args.empty? @@ -19,6 +17,5 @@ def make_association(attribute, args) #:nodoc: raise_argument_error(attribute) end end - end end diff --git a/lib/machinist/blueprint.rb b/lib/machinist/blueprint.rb index 3411e29..4ff6fa8 100644 --- a/lib/machinist/blueprint.rb +++ b/lib/machinist/blueprint.rb @@ -1,8 +1,6 @@ module Machinist - # A Blueprint defines a method of constructing objects of a particular class. class Blueprint - # Construct a blueprint for the given +klass+. # # Pass in the +:parent+ option to define a parent blueprint to apply after @@ -24,7 +22,7 @@ def make(attributes = {}) lathe = lathe_class.new(@klass, new_serial_number, attributes) lathe.instance_eval(&@block) - each_ancestor {|blueprint| lathe.instance_eval(&blueprint.block) } + each_ancestor { |blueprint| lathe.instance_eval(&blueprint.block) } lathe.object end @@ -39,14 +37,14 @@ def lathe_class # Returns the parent blueprint for this blueprint. def parent_blueprint case @parent - when nil - nil - when Blueprint - # @parent references the parent blueprint directly. - @parent - else - # @parent is a class in which we should look for a blueprint. - find_blueprint_in_superclass_chain(@parent) + when nil + nil + when Blueprint + # @parent references the parent blueprint directly. + @parent + else + # @parent is a class in which we should look for a blueprint. + find_blueprint_in_superclass_chain(@parent) end end @@ -59,31 +57,28 @@ def each_ancestor end end - protected + protected - def new_serial_number #:nodoc: - parent_blueprint = self.parent_blueprint # Cache this for speed. + def new_serial_number #:nodoc: + parent_blueprint = self.parent_blueprint # Cache this for speed. if parent_blueprint parent_blueprint.new_serial_number else @serial_number ||= 0 @serial_number += 1 - sprintf("%04d", @serial_number) + sprintf('%04d', @serial_number) end end - private + private def find_blueprint_in_superclass_chain(klass) - until has_blueprint?(klass) || klass.nil? - klass = klass.superclass - end + klass = klass.superclass until has_blueprint?(klass) || klass.nil? klass && klass.blueprint end def has_blueprint?(klass) klass.respond_to?(:blueprint) && !klass.blueprint.nil? end - end end diff --git a/lib/machinist/exceptions.rb b/lib/machinist/exceptions.rb index a6072dc..6dc51b3 100644 --- a/lib/machinist/exceptions.rb +++ b/lib/machinist/exceptions.rb @@ -1,5 +1,4 @@ module Machinist - # Raised when make! is called on a class whose blueprints don't support # saving. class BlueprintCantSaveError < RuntimeError @@ -28,5 +27,4 @@ def message "No #{@name} blueprint defined for class #{@klass.name}" end end - end diff --git a/lib/machinist/lathe.rb b/lib/machinist/lathe.rb index 87ed61d..e8a3732 100644 --- a/lib/machinist/lathe.rb +++ b/lib/machinist/lathe.rb @@ -1,19 +1,17 @@ module Machinist - # When you make an object, the blueprint for that object is instance-evaled # against a Lathe. # # The Lathe implements all the methods that are available to the blueprint, # including method_missing to let the blueprint define attributes. class Lathe - def initialize(klass, serial_number, attributes = {}) @klass = klass @serial_number = serial_number @assigned_attributes = {} @object = @klass.new - attributes.each {|key, value| assign_attribute(key, value) } + attributes.each { |key, value| assign_attribute(key, value) } end # Returns a unique serial number for the object under construction. @@ -35,10 +33,10 @@ def method_missing(attribute, *args, &block) #:nodoc: undef_method :id if respond_to?(:id) undef_method :type if respond_to?(:type) - protected + protected def make_attribute(attribute, args, &block) #:nodoc: - count = args.shift if args.first.is_a?(Fixnum) + count = args.shift if args.first.is_a?(Integer) if count Array.new(count) { make_one_value(attribute, args, &block) } else @@ -57,12 +55,11 @@ def assign_attribute(key, value) #:nodoc: end def attribute_assigned?(key) #:nodoc: - @assigned_attributes.has_key?(key.to_sym) + @assigned_attributes.key?(key.to_sym) end def raise_argument_error(attribute) #:nodoc: - raise ArgumentError.new("Invalid arguments to attribute #{attribute} in blueprint") + raise ArgumentError, "Invalid arguments to attribute #{attribute} in blueprint" end - end end diff --git a/lib/machinist/machinable.rb b/lib/machinist/machinable.rb index 202593f..455932f 100644 --- a/lib/machinist/machinable.rb +++ b/lib/machinist/machinable.rb @@ -1,5 +1,4 @@ module Machinist - # Extend classes with this module to define the blueprint and make methods. module Machinable # Define a blueprint with the given name for this class. @@ -16,7 +15,7 @@ def blueprint(name = :master, &block) @blueprints ||= {} if block_given? parent = (name == :master ? superclass : self) # Where should we look for the parent blueprint? - @blueprints[name] = blueprint_class.new(self, :parent => parent, &block) + @blueprints[name] = blueprint_class.new(self, parent: parent, &block) end @blueprints[name] end @@ -48,7 +47,7 @@ def make(*args) # Arguments are the same as for make. def make!(*args) decode_args_to_make(*args) do |blueprint, attributes| - raise BlueprintCantSaveError.new(blueprint) unless blueprint.respond_to?(:make!) + raise BlueprintCantSaveError, blueprint unless blueprint.respond_to?(:make!) blueprint.make!(attributes) end end @@ -66,7 +65,7 @@ def blueprint_class Machinist::Blueprint end - private + private # Parses the arguments to make. # @@ -74,11 +73,11 @@ def blueprint_class # construct an object from them. The block may be called multiple times to # construct multiple objects. def decode_args_to_make(*args) #:nodoc: - shift_arg = lambda {|klass| args.shift if args.first.is_a?(klass) } - count = shift_arg[Fixnum] + shift_arg = ->(klass) { args.shift if args.first.is_a?(klass) } + count = shift_arg[Integer] name = shift_arg[Symbol] || :master attributes = shift_arg[Hash] || {} - raise ArgumentError.new("Couldn't understand arguments") unless args.empty? + raise ArgumentError, "Couldn't understand arguments" unless args.empty? @blueprints ||= {} blueprint = @blueprints[name] @@ -90,6 +89,5 @@ def decode_args_to_make(*args) #:nodoc: Array.new(count) { yield(blueprint, attributes) } end end - end end diff --git a/lib/machinist/version.rb b/lib/machinist/version.rb index be549e6..ce22243 100644 --- a/lib/machinist/version.rb +++ b/lib/machinist/version.rb @@ -1,3 +1,3 @@ module Machinist - VERSION = "2.0" + VERSION = '2.0'.freeze end diff --git a/machinist.gemspec b/machinist.gemspec old mode 100644 new mode 100755 index 3237a63..459269d --- a/machinist.gemspec +++ b/machinist.gemspec @@ -1,24 +1,20 @@ # -*- encoding: utf-8 -*- + lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'machinist/version' Gem::Specification.new do |gem| - gem.name = "machinist" + gem.name = 'machinist' gem.version = Machinist::VERSION - gem.authors = ["Pete Yandell", "Attila Györffy"] - gem.email = ["attila@attilagyorffy.com"] - gem.description = %q{Machinist makes it easy to create objects for use in tests. It generates data for the attributes you don't care about, and constructs any necessary associated objects, leaving you to specify only the fields you care about in your test.} - gem.summary = %q{Fixtures aren't fun. Machinist is.} - gem.homepage = "http://github.com/liquid/machinist" - - gem.add_development_dependency "activerecord" - gem.add_development_dependency "rake" - gem.add_development_dependency "rspec" - gem.add_development_dependency "rdoc" + gem.authors = ['Pete Yandell', 'Attila Györffy', 'Dominic Sayers'] + gem.email = ['dominic@sayers.cc'] + gem.description = "Machinist makes it easy to create objects for use in tests. It generates data for the attributes you don't care about, and constructs any necessary associated objects, leaving you to specify only the fields you care about in your test." + gem.summary = "Fixtures aren't fun. Machinist is." + gem.homepage = 'http://github.com/liquid/machinist' - gem.files = `git ls-files`.split($/) - gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } + gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) + gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) } gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) - gem.require_paths = ["lib"] + gem.require_paths = ['lib'] end diff --git a/spec/active_record_spec.rb b/spec/active_record_spec.rb index b8412e3..ef8befc 100644 --- a/spec/active_record_spec.rb +++ b/spec/active_record_spec.rb @@ -4,37 +4,37 @@ describe Machinist::ActiveRecord do include ActiveRecordEnvironment - before(:each) do + before do empty_database! end - context "make" do - it "returns an unsaved object" do - Post.blueprint { } + context 'make' do + it 'returns an unsaved object' do + Post.blueprint {} post = Post.make post.should be_a(Post) post.should be_new_record end end - context "make!" do - it "makes and saves objects" do - Post.blueprint { } + context 'make!' do + it 'makes and saves objects' do + Post.blueprint {} post = Post.make! post.should be_a(Post) post.should_not be_new_record end - it "raises an exception for an invalid object" do - User.blueprint { } + it 'raises an exception for an invalid object' do + User.blueprint {} lambda { - User.make!(:username => "") + User.make!(username: '') }.should raise_error(ActiveRecord::RecordInvalid) end end - context "associations support" do - it "handles belongs_to associations" do + context 'associations support' do + it 'handles belongs_to associations' do User.blueprint do username { "user_#{sn}" } end @@ -48,11 +48,11 @@ post.author.should_not be_new_record end - it "handles has_many associations" do + it 'handles has_many associations' do Post.blueprint do comments(3) end - Comment.blueprint { } + Comment.blueprint {} post = Post.make! post.should be_a(Post) post.should_not be_new_record @@ -63,7 +63,7 @@ end end - it "handles habtm associations" do + it 'handles habtm associations' do Post.blueprint do tags(3) end @@ -80,12 +80,12 @@ end end - it "handles overriding associations" do + it 'handles overriding associations' do User.blueprint do username { "user_#{sn}" } end Post.blueprint do - author { User.make(:username => "post_author_#{sn}") } + author { User.make(username: "post_author_#{sn}") } end post = Post.make! post.should be_a(Post) @@ -96,13 +96,12 @@ end end - context "error handling" do - it "raises an exception for an attribute with no value" do + context 'error handling' do + it 'raises an exception for an attribute with no value' do User.blueprint { username } lambda { User.make }.should raise_error(ArgumentError) end end - end diff --git a/spec/blueprint_spec.rb b/spec/blueprint_spec.rb index 4c02034..845f68b 100644 --- a/spec/blueprint_spec.rb +++ b/spec/blueprint_spec.rb @@ -2,75 +2,73 @@ require 'ostruct' describe Machinist::Blueprint do - - it "makes an object of the given class" do - blueprint = Machinist::Blueprint.new(OpenStruct) { } + it 'makes an object of the given class' do + blueprint = described_class.new(OpenStruct) {} blueprint.make.should be_an(OpenStruct) end - it "constructs an attribute from the blueprint" do - blueprint = Machinist::Blueprint.new(OpenStruct) do - name { "Fred" } + it 'constructs an attribute from the blueprint' do + blueprint = described_class.new(OpenStruct) do + name { 'Fred' } end - blueprint.make.name.should == "Fred" + blueprint.make.name.should == 'Fred' end - it "constructs an array for an attribute in the blueprint" do - blueprint = Machinist::Blueprint.new(OpenStruct) do + it 'constructs an array for an attribute in the blueprint' do + blueprint = described_class.new(OpenStruct) do things(3) { Object.new } end things = blueprint.make.things things.should be_an(Array) things.should have(3).elements - things.each {|thing| thing.should be_an(Object) } + things.each { |thing| thing.should be_an(Object) } things.uniq.should == things end - it "allows passing in attributes to override the blueprint" do + it 'allows passing in attributes to override the blueprint' do block_called = false - blueprint = Machinist::Blueprint.new(OpenStruct) do - name { block_called = true; "Fred" } + blueprint = described_class.new(OpenStruct) do + name { block_called = true; 'Fred' } end - blueprint.make(:name => "Bill").name.should == "Bill" + blueprint.make(name: 'Bill').name.should == 'Bill' block_called.should be_false end - it "provides a serial number within the blueprint" do - blueprint = Machinist::Blueprint.new(OpenStruct) do + it 'provides a serial number within the blueprint' do + blueprint = described_class.new(OpenStruct) do name { "Fred #{sn}" } end - blueprint.make.name.should == "Fred 0001" - blueprint.make.name.should == "Fred 0002" + blueprint.make.name.should == 'Fred 0001' + blueprint.make.name.should == 'Fred 0002' end - it "provides access to the object being constructed within the blueprint" do - blueprint = Machinist::Blueprint.new(OpenStruct) do - title { "Test" } + it 'provides access to the object being constructed within the blueprint' do + blueprint = described_class.new(OpenStruct) do + title { 'Test' } body { object.title } end - blueprint.make.body.should == "Test" + blueprint.make.body.should == 'Test' end - it "allows attribute names to be strings" do - blueprint = Machinist::Blueprint.new(OpenStruct) do - name { "Fred" } + it 'allows attribute names to be strings' do + blueprint = described_class.new(OpenStruct) do + name { 'Fred' } end - blueprint.make("name" => "Bill").name.should == "Bill" + blueprint.make('name' => 'Bill').name.should == 'Bill' end # These are normally a problem because of name clashes with the standard (but # deprecated) Ruby methods. This test makes sure we work around this. - it "works with type and id attributes" do + it 'works with type and id attributes' do klass = Class.new do attr_accessor :id, :type end - blueprint = Machinist::Blueprint.new(klass) do - id { "custom id" } - type { "custom type" } + blueprint = described_class.new(klass) do + id { 'custom id' } + type { 'custom type' } end object = blueprint.make - object.id.should == "custom id" - object.type.should == "custom type" + object.id.should == 'custom id' + object.type.should == 'custom type' end - end diff --git a/spec/exceptions_spec.rb b/spec/exceptions_spec.rb index c6b84af..1a19aba 100644 --- a/spec/exceptions_spec.rb +++ b/spec/exceptions_spec.rb @@ -1,20 +1,18 @@ require File.dirname(__FILE__) + '/spec_helper' -describe Machinist, "exceptions" do - +describe Machinist, 'exceptions' do describe Machinist::BlueprintCantSaveError do - it "presents the right message" do - blueprint = Machinist::Blueprint.new(String) { } + it 'presents the right message' do + blueprint = Machinist::Blueprint.new(String) {} exception = Machinist::BlueprintCantSaveError.new(blueprint) - exception.message.should == "make! is not supported by blueprints for class String" + exception.message.should == 'make! is not supported by blueprints for class String' end end describe Machinist::NoBlueprintError do - it "presents the right message" do + it 'presents the right message' do exception = Machinist::NoBlueprintError.new(String, :master) - exception.message.should == "No master blueprint defined for class String" + exception.message.should == 'No master blueprint defined for class String' end end - end diff --git a/spec/inheritance_spec.rb b/spec/inheritance_spec.rb index cbe1cf6..397ef62 100644 --- a/spec/inheritance_spec.rb +++ b/spec/inheritance_spec.rb @@ -19,86 +19,84 @@ class Son < Dad end describe Machinist::Blueprint do - - describe "explicit inheritance" do - it "inherits attributes from the parent blueprint" do - parent_blueprint = Machinist::Blueprint.new(OpenStruct) do - name { "Fred" } + describe 'explicit inheritance' do + it 'inherits attributes from the parent blueprint' do + parent_blueprint = described_class.new(OpenStruct) do + name { 'Fred' } age { 97 } end - child_blueprint = Machinist::Blueprint.new(OpenStruct, :parent => parent_blueprint) do - name { "Bill" } + child_blueprint = described_class.new(OpenStruct, parent: parent_blueprint) do + name { 'Bill' } end child = child_blueprint.make - child.name.should == "Bill" + child.name.should == 'Bill' child.age.should == 97 end - it "takes the serial number from the parent" do - parent_blueprint = Machinist::Blueprint.new(OpenStruct) do + it 'takes the serial number from the parent' do + parent_blueprint = described_class.new(OpenStruct) do parent_serial { sn } end - child_blueprint = Machinist::Blueprint.new(OpenStruct, :parent => parent_blueprint) do + child_blueprint = described_class.new(OpenStruct, parent: parent_blueprint) do child_serial { sn } end - parent_blueprint.make.parent_serial.should == "0001" - child_blueprint.make.child_serial.should == "0002" - parent_blueprint.make.parent_serial.should == "0003" + parent_blueprint.make.parent_serial.should == '0001' + child_blueprint.make.child_serial.should == '0002' + parent_blueprint.make.parent_serial.should == '0003' end end - describe "class inheritance" do - before(:each) do + describe 'class inheritance' do + before do [InheritanceSpecs::Grandpa, InheritanceSpecs::Dad, InheritanceSpecs::Son].each(&:clear_blueprints!) end - it "inherits blueprinted attributes from the parent class" do + it 'inherits blueprinted attributes from the parent class' do InheritanceSpecs::Dad.blueprint do - name { "Fred" } + name { 'Fred' } end - InheritanceSpecs::Son.blueprint { } - InheritanceSpecs::Son.make.name.should == "Fred" + InheritanceSpecs::Son.blueprint {} + InheritanceSpecs::Son.make.name.should == 'Fred' end - it "overrides blueprinted attributes in the child class" do + it 'overrides blueprinted attributes in the child class' do InheritanceSpecs::Dad.blueprint do - name { "Fred" } + name { 'Fred' } end InheritanceSpecs::Son.blueprint do - name { "George" } + name { 'George' } end - InheritanceSpecs::Dad.make.name.should == "Fred" - InheritanceSpecs::Son.make.name.should == "George" + InheritanceSpecs::Dad.make.name.should == 'Fred' + InheritanceSpecs::Son.make.name.should == 'George' end - it "inherits from blueprinted attributes in ancestor class" do + it 'inherits from blueprinted attributes in ancestor class' do InheritanceSpecs::Grandpa.blueprint do - name { "Fred" } + name { 'Fred' } end - InheritanceSpecs::Son.blueprint { } - InheritanceSpecs::Grandpa.make.name.should == "Fred" - lambda { InheritanceSpecs::Dad.make }.should raise_error(RuntimeError) - InheritanceSpecs::Son.make.name.should == "Fred" + InheritanceSpecs::Son.blueprint {} + InheritanceSpecs::Grandpa.make.name.should == 'Fred' + -> { InheritanceSpecs::Dad.make }.should raise_error(RuntimeError) + InheritanceSpecs::Son.make.name.should == 'Fred' end - it "follows inheritance for named blueprints correctly" do + it 'follows inheritance for named blueprints correctly' do InheritanceSpecs::Dad.blueprint do - name { "John" } + name { 'John' } age { 56 } end InheritanceSpecs::Dad.blueprint(:special) do - name { "Paul" } + name { 'Paul' } end InheritanceSpecs::Son.blueprint(:special) do age { 37 } end - InheritanceSpecs::Son.make(:special).name.should == "John" + InheritanceSpecs::Son.make(:special).name.should == 'John' InheritanceSpecs::Son.make(:special).age.should == 37 end end - end diff --git a/spec/machinable_spec.rb b/spec/machinable_spec.rb index fd659bf..3b55b88 100644 --- a/spec/machinable_spec.rb +++ b/spec/machinable_spec.rb @@ -13,40 +13,39 @@ class Comment end describe Machinist::Machinable do - - before(:each) do + before do MachinableSpecs::Post.clear_blueprints! end - it "makes an object" do + it 'makes an object' do MachinableSpecs::Post.blueprint do - title { "First Post" } + title { 'First Post' } end post = MachinableSpecs::Post.make post.should be_a(MachinableSpecs::Post) - post.title.should == "First Post" + post.title.should == 'First Post' end - it "makes an object from a named blueprint" do + it 'makes an object from a named blueprint' do MachinableSpecs::Post.blueprint do - title { "First Post" } - body { "Woot!" } + title { 'First Post' } + body { 'Woot!' } end MachinableSpecs::Post.blueprint(:extra) do - title { "Extra!" } + title { 'Extra!' } end post = MachinableSpecs::Post.make(:extra) post.should be_a(MachinableSpecs::Post) - post.title.should == "Extra!" - post.body.should == "Woot!" + post.title.should == 'Extra!' + post.body.should == 'Woot!' end - it "makes an array of objects" do + it 'makes an array of objects' do MachinableSpecs::Post.blueprint do - title { "First Post" } + title { 'First Post' } end posts = MachinableSpecs::Post.make(3) @@ -54,12 +53,12 @@ class Comment posts.should have(3).elements posts.each do |post| post.should be_a(MachinableSpecs::Post) - post.title.should == "First Post" + post.title.should == 'First Post' end end - it "makes array attributes from the blueprint" do - MachinableSpecs::Comment.blueprint { } + it 'makes array attributes from the blueprint' do + MachinableSpecs::Comment.blueprint {} MachinableSpecs::Post.blueprint do comments(3) { MachinableSpecs::Comment.make } end @@ -72,7 +71,7 @@ class Comment end end - it "fails without a blueprint" do + it 'fails without a blueprint' do expect { MachinableSpecs::Post.make }.to raise_error(Machinist::NoBlueprintError) do |exception| exception.klass.should == MachinableSpecs::Post exception.name.should == :master @@ -84,12 +83,11 @@ class Comment end end - it "fails when calling make! on an unsavable object" do - MachinableSpecs::Post.blueprint { } + it 'fails when calling make! on an unsavable object' do + MachinableSpecs::Post.blueprint {} expect { MachinableSpecs::Post.make! }.to raise_error(Machinist::BlueprintCantSaveError) do |exception| exception.blueprint.klass.should == MachinableSpecs::Post end end - end diff --git a/spec/support/active_record_environment.rb b/spec/support/active_record_environment.rb index 932231f..f620849 100644 --- a/spec/support/active_record_environment.rb +++ b/spec/support/active_record_environment.rb @@ -2,32 +2,32 @@ require 'machinist/active_record' ActiveRecord::Base.establish_connection( - :adapter => "sqlite3", - :database => ":memory:", - :timeout => 500 + adapter: 'sqlite3', + database: ':memory:', + timeout: 500 ) -ActiveRecord::Schema.define(:version => 0) do - create_table :users, :force => true do |t| +ActiveRecord::Schema.define(version: 0) do + create_table :users, force: true do |t| t.column :username, :string end - create_table :posts, :force => true do |t| + create_table :posts, force: true do |t| t.column :title, :string t.column :author_id, :integer t.column :body, :text end - create_table :comments, :force => true do |t| + create_table :comments, force: true do |t| t.column :post_id, :integer t.column :body, :text end - create_table :tags, :force => true do |t| + create_table :tags, force: true do |t| t.column :name, :string end - create_table :posts_tags, :id => false, :force => true do |t| + create_table :posts_tags, id: false, force: true do |t| t.column :post_id, :integer t.column :tag_id, :integer end @@ -40,7 +40,7 @@ class User < ActiveRecord::Base class Post < ActiveRecord::Base has_many :comments - belongs_to :author, :class_name => "User" + belongs_to :author, class_name: 'User' has_and_belongs_to_many :tags end @@ -53,12 +53,10 @@ class Tag < ActiveRecord::Base end module ActiveRecordEnvironment - def empty_database! [User, Post, Comment].each do |klass| klass.delete_all klass.clear_blueprints! end end - end From c0072e3290f56d6ce932b7158261782f363a54bd Mon Sep 17 00:00:00 2001 From: Dominic Sayers Date: Tue, 16 May 2017 15:22:40 +0100 Subject: [PATCH 2/6] Convert specs to RSpec 3.6.0 syntax with Transpec This conversion is done by Transpec 3.3.0 with the following command: transpec * 59 conversions from: obj.should to: expect(obj).to * 32 conversions from: == expected to: eq(expected) * 9 conversions from: obj.should_not to: expect(obj).not_to * 5 conversions from: collection.should have(n).items to: expect(collection.size).to eq(n) * 2 conversions from: lambda { }.should to: expect { }.to * 1 conversion from: -> { }.should to: expect { }.to * 1 conversion from: =~ /pattern/ to: match(/pattern/) * 1 conversion from: be_false to: be_falsey For more details: https://github.com/yujinakayama/transpec#supported-conversions --- spec/active_record_spec.rb | 54 +++++++++++++++++++------------------- spec/blueprint_spec.rb | 28 ++++++++++---------- spec/exceptions_spec.rb | 4 +-- spec/inheritance_spec.rb | 26 +++++++++--------- spec/machinable_spec.rb | 34 ++++++++++++------------ 5 files changed, 73 insertions(+), 73 deletions(-) diff --git a/spec/active_record_spec.rb b/spec/active_record_spec.rb index ef8befc..acc58f3 100644 --- a/spec/active_record_spec.rb +++ b/spec/active_record_spec.rb @@ -12,8 +12,8 @@ it 'returns an unsaved object' do Post.blueprint {} post = Post.make - post.should be_a(Post) - post.should be_new_record + expect(post).to be_a(Post) + expect(post).to be_new_record end end @@ -21,15 +21,15 @@ it 'makes and saves objects' do Post.blueprint {} post = Post.make! - post.should be_a(Post) - post.should_not be_new_record + expect(post).to be_a(Post) + expect(post).not_to be_new_record end it 'raises an exception for an invalid object' do User.blueprint {} - lambda { + expect { User.make!(username: '') - }.should raise_error(ActiveRecord::RecordInvalid) + }.to raise_error(ActiveRecord::RecordInvalid) end end @@ -42,10 +42,10 @@ author end post = Post.make! - post.should be_a(Post) - post.should_not be_new_record - post.author.should be_a(User) - post.author.should_not be_new_record + expect(post).to be_a(Post) + expect(post).not_to be_new_record + expect(post.author).to be_a(User) + expect(post.author).not_to be_new_record end it 'handles has_many associations' do @@ -54,12 +54,12 @@ end Comment.blueprint {} post = Post.make! - post.should be_a(Post) - post.should_not be_new_record - post.should have(3).comments + expect(post).to be_a(Post) + expect(post).not_to be_new_record + expect(post.size).to eq(3) post.comments.each do |comment| - comment.should be_a(Comment) - comment.should_not be_new_record + expect(comment).to be_a(Comment) + expect(comment).not_to be_new_record end end @@ -71,12 +71,12 @@ name { "tag_#{sn}" } end post = Post.make! - post.should be_a(Post) - post.should_not be_new_record - post.should have(3).tags + expect(post).to be_a(Post) + expect(post).not_to be_new_record + expect(post.size).to eq(3) post.tags.each do |tag| - tag.should be_a(Tag) - tag.should_not be_new_record + expect(tag).to be_a(Tag) + expect(tag).not_to be_new_record end end @@ -88,20 +88,20 @@ author { User.make(username: "post_author_#{sn}") } end post = Post.make! - post.should be_a(Post) - post.should_not be_new_record - post.author.should be_a(User) - post.author.should_not be_new_record - post.author.username.should =~ /^post_author_\d+$/ + expect(post).to be_a(Post) + expect(post).not_to be_new_record + expect(post.author).to be_a(User) + expect(post.author).not_to be_new_record + expect(post.author.username).to match(/^post_author_\d+$/) end end context 'error handling' do it 'raises an exception for an attribute with no value' do User.blueprint { username } - lambda { + expect { User.make - }.should raise_error(ArgumentError) + }.to raise_error(ArgumentError) end end end diff --git a/spec/blueprint_spec.rb b/spec/blueprint_spec.rb index 845f68b..e472070 100644 --- a/spec/blueprint_spec.rb +++ b/spec/blueprint_spec.rb @@ -4,14 +4,14 @@ describe Machinist::Blueprint do it 'makes an object of the given class' do blueprint = described_class.new(OpenStruct) {} - blueprint.make.should be_an(OpenStruct) + expect(blueprint.make).to be_an(OpenStruct) end it 'constructs an attribute from the blueprint' do blueprint = described_class.new(OpenStruct) do name { 'Fred' } end - blueprint.make.name.should == 'Fred' + expect(blueprint.make.name).to eq('Fred') end it 'constructs an array for an attribute in the blueprint' do @@ -19,10 +19,10 @@ things(3) { Object.new } end things = blueprint.make.things - things.should be_an(Array) - things.should have(3).elements - things.each { |thing| thing.should be_an(Object) } - things.uniq.should == things + expect(things).to be_an(Array) + expect(things.size).to eq(3) + things.each { |thing| expect(thing).to be_an(Object) } + expect(things.uniq).to eq(things) end it 'allows passing in attributes to override the blueprint' do @@ -30,16 +30,16 @@ blueprint = described_class.new(OpenStruct) do name { block_called = true; 'Fred' } end - blueprint.make(name: 'Bill').name.should == 'Bill' - block_called.should be_false + expect(blueprint.make(name: 'Bill').name).to eq('Bill') + expect(block_called).to be_falsey end it 'provides a serial number within the blueprint' do blueprint = described_class.new(OpenStruct) do name { "Fred #{sn}" } end - blueprint.make.name.should == 'Fred 0001' - blueprint.make.name.should == 'Fred 0002' + expect(blueprint.make.name).to eq('Fred 0001') + expect(blueprint.make.name).to eq('Fred 0002') end it 'provides access to the object being constructed within the blueprint' do @@ -47,14 +47,14 @@ title { 'Test' } body { object.title } end - blueprint.make.body.should == 'Test' + expect(blueprint.make.body).to eq('Test') end it 'allows attribute names to be strings' do blueprint = described_class.new(OpenStruct) do name { 'Fred' } end - blueprint.make('name' => 'Bill').name.should == 'Bill' + expect(blueprint.make('name' => 'Bill').name).to eq('Bill') end # These are normally a problem because of name clashes with the standard (but @@ -68,7 +68,7 @@ type { 'custom type' } end object = blueprint.make - object.id.should == 'custom id' - object.type.should == 'custom type' + expect(object.id).to eq('custom id') + expect(object.type).to eq('custom type') end end diff --git a/spec/exceptions_spec.rb b/spec/exceptions_spec.rb index 1a19aba..1ee1c54 100644 --- a/spec/exceptions_spec.rb +++ b/spec/exceptions_spec.rb @@ -5,14 +5,14 @@ it 'presents the right message' do blueprint = Machinist::Blueprint.new(String) {} exception = Machinist::BlueprintCantSaveError.new(blueprint) - exception.message.should == 'make! is not supported by blueprints for class String' + expect(exception.message).to eq('make! is not supported by blueprints for class String') end end describe Machinist::NoBlueprintError do it 'presents the right message' do exception = Machinist::NoBlueprintError.new(String, :master) - exception.message.should == 'No master blueprint defined for class String' + expect(exception.message).to eq('No master blueprint defined for class String') end end end diff --git a/spec/inheritance_spec.rb b/spec/inheritance_spec.rb index 397ef62..f945225 100644 --- a/spec/inheritance_spec.rb +++ b/spec/inheritance_spec.rb @@ -31,8 +31,8 @@ class Son < Dad end child = child_blueprint.make - child.name.should == 'Bill' - child.age.should == 97 + expect(child.name).to eq('Bill') + expect(child.age).to eq(97) end it 'takes the serial number from the parent' do @@ -44,9 +44,9 @@ class Son < Dad child_serial { sn } end - parent_blueprint.make.parent_serial.should == '0001' - child_blueprint.make.child_serial.should == '0002' - parent_blueprint.make.parent_serial.should == '0003' + expect(parent_blueprint.make.parent_serial).to eq('0001') + expect(child_blueprint.make.child_serial).to eq('0002') + expect(parent_blueprint.make.parent_serial).to eq('0003') end end @@ -60,7 +60,7 @@ class Son < Dad name { 'Fred' } end InheritanceSpecs::Son.blueprint {} - InheritanceSpecs::Son.make.name.should == 'Fred' + expect(InheritanceSpecs::Son.make.name).to eq('Fred') end it 'overrides blueprinted attributes in the child class' do @@ -70,8 +70,8 @@ class Son < Dad InheritanceSpecs::Son.blueprint do name { 'George' } end - InheritanceSpecs::Dad.make.name.should == 'Fred' - InheritanceSpecs::Son.make.name.should == 'George' + expect(InheritanceSpecs::Dad.make.name).to eq('Fred') + expect(InheritanceSpecs::Son.make.name).to eq('George') end it 'inherits from blueprinted attributes in ancestor class' do @@ -79,9 +79,9 @@ class Son < Dad name { 'Fred' } end InheritanceSpecs::Son.blueprint {} - InheritanceSpecs::Grandpa.make.name.should == 'Fred' - -> { InheritanceSpecs::Dad.make }.should raise_error(RuntimeError) - InheritanceSpecs::Son.make.name.should == 'Fred' + expect(InheritanceSpecs::Grandpa.make.name).to eq('Fred') + expect { InheritanceSpecs::Dad.make }.to raise_error(RuntimeError) + expect(InheritanceSpecs::Son.make.name).to eq('Fred') end it 'follows inheritance for named blueprints correctly' do @@ -95,8 +95,8 @@ class Son < Dad InheritanceSpecs::Son.blueprint(:special) do age { 37 } end - InheritanceSpecs::Son.make(:special).name.should == 'John' - InheritanceSpecs::Son.make(:special).age.should == 37 + expect(InheritanceSpecs::Son.make(:special).name).to eq('John') + expect(InheritanceSpecs::Son.make(:special).age).to eq(37) end end end diff --git a/spec/machinable_spec.rb b/spec/machinable_spec.rb index 3b55b88..afbcab6 100644 --- a/spec/machinable_spec.rb +++ b/spec/machinable_spec.rb @@ -23,8 +23,8 @@ class Comment end post = MachinableSpecs::Post.make - post.should be_a(MachinableSpecs::Post) - post.title.should == 'First Post' + expect(post).to be_a(MachinableSpecs::Post) + expect(post.title).to eq('First Post') end it 'makes an object from a named blueprint' do @@ -38,9 +38,9 @@ class Comment end post = MachinableSpecs::Post.make(:extra) - post.should be_a(MachinableSpecs::Post) - post.title.should == 'Extra!' - post.body.should == 'Woot!' + expect(post).to be_a(MachinableSpecs::Post) + expect(post.title).to eq('Extra!') + expect(post.body).to eq('Woot!') end it 'makes an array of objects' do @@ -49,11 +49,11 @@ class Comment end posts = MachinableSpecs::Post.make(3) - posts.should be_an(Array) - posts.should have(3).elements + expect(posts).to be_an(Array) + expect(posts.size).to eq(3) posts.each do |post| - post.should be_a(MachinableSpecs::Post) - post.title.should == 'First Post' + expect(post).to be_a(MachinableSpecs::Post) + expect(post.title).to eq('First Post') end end @@ -64,22 +64,22 @@ class Comment end post = MachinableSpecs::Post.make - post.comments.should be_a(Array) - post.comments.should have(3).elements + expect(post.comments).to be_a(Array) + expect(post.comments.size).to eq(3) post.comments.each do |comment| - comment.should be_a(MachinableSpecs::Comment) + expect(comment).to be_a(MachinableSpecs::Comment) end end it 'fails without a blueprint' do expect { MachinableSpecs::Post.make }.to raise_error(Machinist::NoBlueprintError) do |exception| - exception.klass.should == MachinableSpecs::Post - exception.name.should == :master + expect(exception.klass).to eq(MachinableSpecs::Post) + expect(exception.name).to eq(:master) end expect { MachinableSpecs::Post.make(:some_name) }.to raise_error(Machinist::NoBlueprintError) do |exception| - exception.klass.should == MachinableSpecs::Post - exception.name.should == :some_name + expect(exception.klass).to eq(MachinableSpecs::Post) + expect(exception.name).to eq(:some_name) end end @@ -87,7 +87,7 @@ class Comment MachinableSpecs::Post.blueprint {} expect { MachinableSpecs::Post.make! }.to raise_error(Machinist::BlueprintCantSaveError) do |exception| - exception.blueprint.klass.should == MachinableSpecs::Post + expect(exception.blueprint.klass).to eq(MachinableSpecs::Post) end end end From 413f4c4807c2f9eb52c61f5f083e1a29ca4fb50c Mon Sep 17 00:00:00 2001 From: Dominic Sayers Date: Tue, 16 May 2017 15:51:51 +0100 Subject: [PATCH 3/6] Further Rubocop recommendations --- .gitignore | 1 - .rspec | 2 + .rubocop.yml | 1 + .rubocop_todo.yml | 92 ++++++++++++++ .travis.yml | 6 +- Gemfile | 1 - README.md | 8 ++ gemfiles/rails_4.2.gemfile | 3 +- gemfiles/rails_4.2.gemfile.lock | 4 - gemfiles/rails_5.0.gemfile | 3 +- gemfiles/rails_5.0.gemfile.lock | 4 - gemfiles/rails_5.1.gemfile | 3 +- gemfiles/rails_5.1.gemfile.lock | 4 - machinist.gemspec | 4 +- spec/{ => machinist}/active_record_spec.rb | 15 ++- .../blueprint_inheritance_spec.rb} | 3 +- spec/{ => machinist}/blueprint_spec.rb | 8 +- spec/{ => machinist}/exceptions_spec.rb | 4 +- spec/{ => machinist}/machinable_spec.rb | 4 +- spec/spec_helper.rb | 113 +++++++++++++++++- 20 files changed, 235 insertions(+), 48 deletions(-) create mode 100755 .rspec create mode 100755 .rubocop_todo.yml rename spec/{ => machinist}/active_record_spec.rb (89%) mode change 100644 => 100755 rename spec/{inheritance_spec.rb => machinist/blueprint_inheritance_spec.rb} (97%) mode change 100644 => 100755 rename spec/{ => machinist}/blueprint_spec.rb (94%) mode change 100644 => 100755 rename spec/{ => machinist}/exceptions_spec.rb (87%) mode change 100644 => 100755 rename spec/{ => machinist}/machinable_spec.rb (96%) mode change 100644 => 100755 mode change 100644 => 100755 spec/spec_helper.rb diff --git a/.gitignore b/.gitignore index 50b5e04..30817d2 100755 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,6 @@ .rbenv-* .rbx .rbxpkg -.rspec .ruby-gemset .ruby-version .rvmrc diff --git a/.rspec b/.rspec new file mode 100755 index 0000000..83e16f8 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--require spec_helper diff --git a/.rubocop.yml b/.rubocop.yml index 646b534..6fe558b 100755 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,6 @@ --- require: rubocop-rspec +inherit_from: .rubocop_todo.yml AllCops: TargetRubyVersion: 2.2 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100755 index 0000000..8015a65 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,92 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2017-05-16 15:42:20 +0100 using RuboCop version 0.48.1. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +Metrics/AbcSize: + Max: 17 + +# Offense count: 1 +Metrics/CyclomaticComplexity: + Max: 7 + +# Offense count: 1 +# Configuration parameters: CountComments. +Metrics/MethodLength: + Max: 13 + +# Offense count: 1 +Metrics/PerceivedComplexity: + Max: 8 + +# Offense count: 1 +RSpec/DescribeMethod: + Exclude: + - 'spec/machinist/exceptions_spec.rb' + +# Offense count: 17 +# Configuration parameters: Max. +RSpec/ExampleLength: + Exclude: + - 'spec/machinist/active_record_spec.rb' + - 'spec/machinist/blueprint_inheritance_spec.rb' + - 'spec/machinist/blueprint_spec.rb' + - 'spec/machinist/machinable_spec.rb' + +# Offense count: 2 +RSpec/IteratedExpectation: + Exclude: + - 'spec/machinist/blueprint_spec.rb' + - 'spec/machinist/machinable_spec.rb' + +# Offense count: 21 +RSpec/MultipleExpectations: + Max: 6 + +# Offense count: 2 +# Configuration parameters: EnforcedStyle, SupportedStyles. +# SupportedStyles: nested, compact +Style/ClassAndModuleChildren: + Exclude: + - 'lib/machinist/active_record/blueprint.rb' + - 'lib/machinist/active_record/lathe.rb' + +# Offense count: 1 +# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms. +# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS +Style/FileName: + Exclude: + - 'Appraisals' + +# Offense count: 1 +# Configuration parameters: EnforcedStyle, SupportedStyles. +# SupportedStyles: format, sprintf, percent +Style/FormatString: + Exclude: + - 'lib/machinist/blueprint.rb' + +# Offense count: 3 +# Configuration parameters: MinBodyLength. +Style/GuardClause: + Exclude: + - 'lib/generators/machinist/install/install_generator.rb' + - 'lib/machinist/lathe.rb' + +# Offense count: 1 +Style/MethodMissing: + Exclude: + - 'lib/machinist/lathe.rb' + +# Offense count: 1 +# Configuration parameters: NamePrefix, NamePrefixBlacklist, NameWhitelist. +# NamePrefix: is_, has_, have_ +# NamePrefixBlacklist: is_, has_, have_ +# NameWhitelist: is_a? +Style/PredicateName: + Exclude: + - 'spec/**/*' + - 'lib/machinist/blueprint.rb' diff --git a/.travis.yml b/.travis.yml index 175e75d..f537aac 100755 --- a/.travis.yml +++ b/.travis.yml @@ -9,9 +9,9 @@ rvm: - 2.1.10 gemfile: - - gemfiles/4.2.gemfile - - gemfiles/5.0.gemfile - - gemfiles/5.1.gemfile + - gemfiles/rails_4.2.gemfile + - gemfiles/rails_5.0.gemfile + - gemfiles/rails_5.1.gemfile branches: only: diff --git a/Gemfile b/Gemfile index 9b29375..72682e0 100755 --- a/Gemfile +++ b/Gemfile @@ -15,7 +15,6 @@ group :test do gem 'rspec', require: false gem 'rspec_junit_formatter', require: false gem 'simplecov', '~> 0.14', require: false - gem 'test-unit', require: false end platforms :ruby do diff --git a/README.md b/README.md index 0593b58..3d7a059 100755 --- a/README.md +++ b/README.md @@ -2,6 +2,14 @@ *Fixtures aren't fun. Machinist was.* +[![Gem version](https://badge.fury.io/rb/machinist.svg)](https://rubygems.org/gems/machinist) +[![Gem downloads](https://img.shields.io/gem/dt/machinist.svg)](https://rubygems.org/gems/machinist) +[![Build Status](https://travis-ci.org/dominicsayers/machinist.svg?branch=master)](https://travis-ci.org/dominicsayers/machinist) +[![Code Climate](https://codeclimate.com/github/dominicsayers/machinist/badges/gpa.svg)](https://codeclimate.com/github/dominicsayers/machinist) +[![Test Coverage](https://codeclimate.com/github/dominicsayers/machinist/badges/coverage.svg)](https://codeclimate.com/github/dominicsayers/machinist/coverage) +[![Dependency Status](https://gemnasium.com/badges/github.com/dominicsayers/machinist.svg)](https://gemnasium.com/github.com/dominicsayers/machinist) +[![Security](https://hakiri.io/github/dominicsayers/machinist/master.svg)](https://hakiri.io/github/dominicsayers/machinist/master) + - [Home page](http://github.com/dominicsayers/machinist) - [Google group](http://groups.google.com/group/machinist-users), for support - [Bug tracker](http://github.com/dominicsayers/machinist/issues), for reporting Machinist bugs diff --git a/gemfiles/rails_4.2.gemfile b/gemfiles/rails_4.2.gemfile index 30ac46d..51a17fe 100755 --- a/gemfiles/rails_4.2.gemfile +++ b/gemfiles/rails_4.2.gemfile @@ -8,12 +8,11 @@ gem "activerecord", "~> 4.2.8", group: :test, require: false group :development do gem "appraisal", require: false - gem "rubocop-rspec", require: false gem "rdoc", require: false + gem "rubocop-rspec", require: false end group :test do - gem "test-unit", require: false gem "codeclimate-test-reporter", require: false gem "coveralls", require: false gem "fuubar", require: false diff --git a/gemfiles/rails_4.2.gemfile.lock b/gemfiles/rails_4.2.gemfile.lock index 898ef26..ffee889 100644 --- a/gemfiles/rails_4.2.gemfile.lock +++ b/gemfiles/rails_4.2.gemfile.lock @@ -43,7 +43,6 @@ GEM minitest (5.10.2) parser (2.4.0.0) ast (~> 2.2) - power_assert (1.0.2) powerpack (0.1.1) rainbow (2.2.2) rake @@ -82,8 +81,6 @@ GEM sqlite3 (1.3.13) term-ansicolor (1.6.0) tins (~> 1.0) - test-unit (3.2.3) - power_assert thor (0.19.4) thread_safe (0.3.6) tins (1.13.3) @@ -108,7 +105,6 @@ DEPENDENCIES rubocop-rspec simplecov (~> 0.14) sqlite3 - test-unit RUBY VERSION ruby 2.3.4p301 diff --git a/gemfiles/rails_5.0.gemfile b/gemfiles/rails_5.0.gemfile index e5057bf..82059d3 100755 --- a/gemfiles/rails_5.0.gemfile +++ b/gemfiles/rails_5.0.gemfile @@ -8,12 +8,11 @@ gem "activerecord", "~> 5.0.3", group: :test, require: false group :development do gem "appraisal", require: false - gem "rubocop-rspec", require: false gem "rdoc", require: false + gem "rubocop-rspec", require: false end group :test do - gem "test-unit", require: false gem "codeclimate-test-reporter", require: false gem "coveralls", require: false gem "fuubar", require: false diff --git a/gemfiles/rails_5.0.gemfile.lock b/gemfiles/rails_5.0.gemfile.lock index 9dcfecd..2f36786 100644 --- a/gemfiles/rails_5.0.gemfile.lock +++ b/gemfiles/rails_5.0.gemfile.lock @@ -43,7 +43,6 @@ GEM minitest (5.10.2) parser (2.4.0.0) ast (~> 2.2) - power_assert (1.0.2) powerpack (0.1.1) rainbow (2.2.2) rake @@ -82,8 +81,6 @@ GEM sqlite3 (1.3.13) term-ansicolor (1.6.0) tins (~> 1.0) - test-unit (3.2.3) - power_assert thor (0.19.4) thread_safe (0.3.6) tins (1.13.3) @@ -108,7 +105,6 @@ DEPENDENCIES rubocop-rspec simplecov (~> 0.14) sqlite3 - test-unit RUBY VERSION ruby 2.3.4p301 diff --git a/gemfiles/rails_5.1.gemfile b/gemfiles/rails_5.1.gemfile index e7e7a73..a4a49e9 100755 --- a/gemfiles/rails_5.1.gemfile +++ b/gemfiles/rails_5.1.gemfile @@ -8,12 +8,11 @@ gem "activerecord", "~> 5.1.1", group: :test, require: false group :development do gem "appraisal", require: false - gem "rubocop-rspec", require: false gem "rdoc", require: false + gem "rubocop-rspec", require: false end group :test do - gem "test-unit", require: false gem "codeclimate-test-reporter", require: false gem "coveralls", require: false gem "fuubar", require: false diff --git a/gemfiles/rails_5.1.gemfile.lock b/gemfiles/rails_5.1.gemfile.lock index c56ab08..b39ad5b 100644 --- a/gemfiles/rails_5.1.gemfile.lock +++ b/gemfiles/rails_5.1.gemfile.lock @@ -43,7 +43,6 @@ GEM minitest (5.10.2) parser (2.4.0.0) ast (~> 2.2) - power_assert (1.0.2) powerpack (0.1.1) rainbow (2.2.2) rake @@ -82,8 +81,6 @@ GEM sqlite3 (1.3.13) term-ansicolor (1.6.0) tins (~> 1.0) - test-unit (3.2.3) - power_assert thor (0.19.4) thread_safe (0.3.6) tins (1.13.3) @@ -108,7 +105,6 @@ DEPENDENCIES rubocop-rspec simplecov (~> 0.14) sqlite3 - test-unit RUBY VERSION ruby 2.3.4p301 diff --git a/machinist.gemspec b/machinist.gemspec index 459269d..77bd979 100755 --- a/machinist.gemspec +++ b/machinist.gemspec @@ -9,7 +9,9 @@ Gem::Specification.new do |gem| gem.version = Machinist::VERSION gem.authors = ['Pete Yandell', 'Attila Györffy', 'Dominic Sayers'] gem.email = ['dominic@sayers.cc'] - gem.description = "Machinist makes it easy to create objects for use in tests. It generates data for the attributes you don't care about, and constructs any necessary associated objects, leaving you to specify only the fields you care about in your test." + gem.description = 'Machinist makes it easy to create objects for use in tests. It generates data for the ' \ + "attributes you don't care about, and constructs any necessary associated objects, leaving you "\ + 'to specify only the fields you care about in your test.' gem.summary = "Fixtures aren't fun. Machinist is." gem.homepage = 'http://github.com/liquid/machinist' diff --git a/spec/active_record_spec.rb b/spec/machinist/active_record_spec.rb old mode 100644 new mode 100755 similarity index 89% rename from spec/active_record_spec.rb rename to spec/machinist/active_record_spec.rb index acc58f3..2ed4b39 --- a/spec/active_record_spec.rb +++ b/spec/machinist/active_record_spec.rb @@ -1,7 +1,6 @@ -require File.dirname(__FILE__) + '/spec_helper' require 'support/active_record_environment' -describe Machinist::ActiveRecord do +RSpec.describe Machinist::ActiveRecord do include ActiveRecordEnvironment before do @@ -27,9 +26,9 @@ it 'raises an exception for an invalid object' do User.blueprint {} - expect { + expect do User.make!(username: '') - }.to raise_error(ActiveRecord::RecordInvalid) + end.to raise_error(ActiveRecord::RecordInvalid) end end @@ -56,7 +55,7 @@ post = Post.make! expect(post).to be_a(Post) expect(post).not_to be_new_record - expect(post.size).to eq(3) + expect(post.comments.size).to eq(3) post.comments.each do |comment| expect(comment).to be_a(Comment) expect(comment).not_to be_new_record @@ -73,7 +72,7 @@ post = Post.make! expect(post).to be_a(Post) expect(post).not_to be_new_record - expect(post.size).to eq(3) + expect(post.tags.size).to eq(3) post.tags.each do |tag| expect(tag).to be_a(Tag) expect(tag).not_to be_new_record @@ -99,9 +98,9 @@ context 'error handling' do it 'raises an exception for an attribute with no value' do User.blueprint { username } - expect { + expect do User.make - }.to raise_error(ArgumentError) + end.to raise_error(ArgumentError) end end end diff --git a/spec/inheritance_spec.rb b/spec/machinist/blueprint_inheritance_spec.rb old mode 100644 new mode 100755 similarity index 97% rename from spec/inheritance_spec.rb rename to spec/machinist/blueprint_inheritance_spec.rb index f945225..3b86a08 --- a/spec/inheritance_spec.rb +++ b/spec/machinist/blueprint_inheritance_spec.rb @@ -1,4 +1,3 @@ -require File.dirname(__FILE__) + '/spec_helper' require 'ostruct' module InheritanceSpecs @@ -18,7 +17,7 @@ class Son < Dad end end -describe Machinist::Blueprint do +RSpec.describe Machinist::Blueprint do describe 'explicit inheritance' do it 'inherits attributes from the parent blueprint' do parent_blueprint = described_class.new(OpenStruct) do diff --git a/spec/blueprint_spec.rb b/spec/machinist/blueprint_spec.rb old mode 100644 new mode 100755 similarity index 94% rename from spec/blueprint_spec.rb rename to spec/machinist/blueprint_spec.rb index e472070..6872360 --- a/spec/blueprint_spec.rb +++ b/spec/machinist/blueprint_spec.rb @@ -1,7 +1,6 @@ -require File.dirname(__FILE__) + '/spec_helper' require 'ostruct' -describe Machinist::Blueprint do +RSpec.describe Machinist::Blueprint do it 'makes an object of the given class' do blueprint = described_class.new(OpenStruct) {} expect(blueprint.make).to be_an(OpenStruct) @@ -28,7 +27,10 @@ it 'allows passing in attributes to override the blueprint' do block_called = false blueprint = described_class.new(OpenStruct) do - name { block_called = true; 'Fred' } + name do + block_called = true + 'Fred' + end end expect(blueprint.make(name: 'Bill').name).to eq('Bill') expect(block_called).to be_falsey diff --git a/spec/exceptions_spec.rb b/spec/machinist/exceptions_spec.rb old mode 100644 new mode 100755 similarity index 87% rename from spec/exceptions_spec.rb rename to spec/machinist/exceptions_spec.rb index 1ee1c54..a3dc783 --- a/spec/exceptions_spec.rb +++ b/spec/machinist/exceptions_spec.rb @@ -1,6 +1,4 @@ -require File.dirname(__FILE__) + '/spec_helper' - -describe Machinist, 'exceptions' do +RSpec.describe Machinist, 'exceptions' do describe Machinist::BlueprintCantSaveError do it 'presents the right message' do blueprint = Machinist::Blueprint.new(String) {} diff --git a/spec/machinable_spec.rb b/spec/machinist/machinable_spec.rb old mode 100644 new mode 100755 similarity index 96% rename from spec/machinable_spec.rb rename to spec/machinist/machinable_spec.rb index afbcab6..ddd29e4 --- a/spec/machinable_spec.rb +++ b/spec/machinist/machinable_spec.rb @@ -1,5 +1,3 @@ -require File.dirname(__FILE__) + '/spec_helper' - module MachinableSpecs class Post extend Machinist::Machinable @@ -12,7 +10,7 @@ class Comment end end -describe Machinist::Machinable do +RSpec.describe Machinist::Machinable do before do MachinableSpecs::Post.clear_blueprints! end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb old mode 100644 new mode 100755 index 67626b4..30790be --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,110 @@ -$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib' -$LOAD_PATH.unshift File.dirname(__FILE__) +# Configure Simplecov and Coveralls +unless ENV['NO_SIMPLECOV'] + require 'simplecov' + require 'coveralls' + + SimpleCov.start { add_filter '/spec/' } + Coveralls.wear! if ENV['COVERALLS_REPO_TOKEN'] +end -require 'rubygems' -require 'test/unit' -require 'rspec' require 'machinist' + +# This file was generated by the `rspec --init` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# The `.rspec` file also contains a few flags that are not defaults but that +# users commonly want. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = 'tmp/rspec/examples.txt' + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # This setting enables warnings. It's recommended, but in some cases may + # be too noisy due to issues in dependencies. + config.warnings = true + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = 'doc' + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +end From 6e317dad4d33677ce3e19d17a703313daad41afa Mon Sep 17 00:00:00 2001 From: Dominic Sayers Date: Tue, 16 May 2017 16:02:09 +0100 Subject: [PATCH 4/6] Make class declaration more robust for different rubies --- lib/machinist/lathe.rb | 2 +- lib/machinist/machinable.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) mode change 100644 => 100755 lib/machinist/lathe.rb mode change 100644 => 100755 lib/machinist/machinable.rb diff --git a/lib/machinist/lathe.rb b/lib/machinist/lathe.rb old mode 100644 new mode 100755 index e8a3732..7b8bcad --- a/lib/machinist/lathe.rb +++ b/lib/machinist/lathe.rb @@ -36,7 +36,7 @@ def method_missing(attribute, *args, &block) #:nodoc: protected def make_attribute(attribute, args, &block) #:nodoc: - count = args.shift if args.first.is_a?(Integer) + count = args.shift if args.first.is_a?(0.class) if count Array.new(count) { make_one_value(attribute, args, &block) } else diff --git a/lib/machinist/machinable.rb b/lib/machinist/machinable.rb old mode 100644 new mode 100755 index 455932f..bbb7d59 --- a/lib/machinist/machinable.rb +++ b/lib/machinist/machinable.rb @@ -74,7 +74,7 @@ def blueprint_class # construct multiple objects. def decode_args_to_make(*args) #:nodoc: shift_arg = ->(klass) { args.shift if args.first.is_a?(klass) } - count = shift_arg[Integer] + count = shift_arg[0.class] name = shift_arg[Symbol] || :master attributes = shift_arg[Hash] || {} raise ArgumentError, "Couldn't understand arguments" unless args.empty? From 48487dd955f8004e06d4021950ae64bf5adfab6e Mon Sep 17 00:00:00 2001 From: Dominic Sayers Date: Tue, 16 May 2017 16:24:47 +0100 Subject: [PATCH 5/6] Reduce ABCSize --- lib/machinist/machinable.rb | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/machinist/machinable.rb b/lib/machinist/machinable.rb index bbb7d59..e79af5b 100755 --- a/lib/machinist/machinable.rb +++ b/lib/machinist/machinable.rb @@ -73,15 +73,8 @@ def blueprint_class # construct an object from them. The block may be called multiple times to # construct multiple objects. def decode_args_to_make(*args) #:nodoc: - shift_arg = ->(klass) { args.shift if args.first.is_a?(klass) } - count = shift_arg[0.class] - name = shift_arg[Symbol] || :master - attributes = shift_arg[Hash] || {} - raise ArgumentError, "Couldn't understand arguments" unless args.empty? - - @blueprints ||= {} - blueprint = @blueprints[name] - raise NoBlueprintError.new(self, name) unless blueprint + count, name, attributes = *decode_args(args) + blueprint = ensure_blueprint(name) if count.nil? yield(blueprint, attributes) @@ -89,5 +82,19 @@ def decode_args_to_make(*args) #:nodoc: Array.new(count) { yield(blueprint, attributes) } end end + + def decode_args(args) + shift_arg = ->(klass) { args.shift if args.first.is_a?(klass) } + count = shift_arg[0.class] + name = shift_arg[Symbol] || :master + attributes = shift_arg[Hash] || {} + raise ArgumentError, "Couldn't understand arguments" unless args.empty? + [count, name, attributes] + end + + def ensure_blueprint(name) + @blueprints ||= {} + @blueprints[name] || raise(NoBlueprintError.new(self, name)) + end end end From 29afdbe8fb5cc036b649c9be52a95a27ca6098c2 Mon Sep 17 00:00:00 2001 From: Dominic Sayers Date: Tue, 16 May 2017 16:27:37 +0100 Subject: [PATCH 6/6] Allow Ruby 2.1 to fail with Rails 5+ --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index f537aac..9cb689f 100755 --- a/.travis.yml +++ b/.travis.yml @@ -19,4 +19,8 @@ branches: matrix: allow_failures: + - gemfile: gemfiles/rails_5.0.gemfile + rvm: 2.1.10 + - gemfile: gemfiles/rails_5.1.gemfile + rvm: 2.1.10 - rvm: ruby-head