$ bundle install Fetching gem metadata from https://rubygems.org/.................... Resolving dependencies... Using ast 2.4.0 Using bundler 2.1.4 Using jaro_winkler 1.5.4 Using parallel 1.19.1 Fetching parser 2.7.0.2 Installing parser 2.7.0.2 Using rainbow 3.0.0 Using ruby-progressbar 1.10.1 Fetching unicode-display_width 1.6.1 Installing unicode-display_width 1.6.1 Fetching rubocop 0.79.0 Installing rubocop 0.79.0 Bundle complete! 1 Gemfile dependency, 9 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed.
Gemfile:3:8: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. source "https://rubygems.org" ^^^^^^^^^^^^^^^^^^^^^^ Gemfile:5:21: C: Layout/SpaceInsideBlockBraces: Space between { and | missing. git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } ^^ ...
なにかいろいろ出てきましたね。RuboCop の詳細に関しては RuboCop is 何?を参照ください。--lint オプションをつけて実施してみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$ rubocop --lint Inspecting 5 files ...W.
Offenses:
test/fizz_buzz_test.rb:109:7: : Parenthesize the param %w[2 4 13 3 1 10].sort { |a, b| a.to_i <=> b.to_i } to make sure that the block will be associated with the %w[2 4 13 3 1 10].sort method call. assert_equal %w[1 2 3 4 10 13], ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ test/fizz_buzz_test.rb:111:7: W: Lint/AmbiguousBlockAssociation: Parenthesize the param %w[2 4 13 3 1 10].sort { |b, a| a.to_i <=> b.to_i } to make sure that the block will be associated with the %w[2 4 13 3 1 10].sort method call. assert_equal %w[13 10 4 3 2 1], ... ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test/fizz_buzz_test.rb:15:11: C: Naming/MethodName: Use snake_case for method names. def test_3を渡したら文字列Fizzを返す ^^^^^^^^^^^^^^^^^^^^^ ...
まだ、自動修正できなかった部分があるようですね。この部分はチェック対象から外すことにしましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$ rubocop --auto-gen-config Added inheritance from `.rubocop_todo.yml` in `.rubocop.yml`. Phase 1 of 2: run Layout/LineLength cop Inspecting 5 files .....
5 files inspected, no offenses detected Created .rubocop_todo.yml. Phase 2 of 2: run all cops Inspecting 5 files .C.CW
5 files inspected, 110 offenses detected Created .rubocop_todo.yml.
$ bundle install Fetching gem metadata from https://rubygems.org/.................. Resolving dependencies... Fetching ansi 1.5.0 Installing ansi 1.5.0 Using ast 2.4.0 Fetching builder 3.2.4 Installing builder 3.2.4 Using bundler 2.1.4 Using docile 1.3.2 Using jaro_winkler 1.5.4 Using json 2.3.0 Fetching minitest 5.14.0 Installing minitest 5.14.0 Using ruby-progressbar 1.10.1 Fetching minitest-reporters 1.4.2 Installing minitest-reporters 1.4.2 Using parallel 1.19.1 Using parser 2.7.0.2 Using rainbow 3.0.0 Using unicode-display_width 1.6.1 Using rubocop 0.79.0 Using simplecov-html 0.10.2 Using simplecov 0.17.1 Bundle complete! 4 Gemfile dependencies, 17 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed.
Rake::TestTask.new do|test| test.test_files = Dir['./test/fizz_buzz_test.rb'] test.verbose = true end
タスクが登録されたか確認してみましょう。
1 2
$ rake -T rake test# Run tests
タスクが登録されたことが確認できたのでタスクを実行します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
$ rake test /Users/k2works/.rbenv/versions/2.5.5/bin/ruby -w -I"lib" -I"/Users/k2works/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rake-13.0.1/lib""/Users/k2works/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/gems/rake-13.0.1/lib/rake/rake_test_loader.rb""./test/fizz_buzz_test.rb" /Users/k2works/Projects/hiroshima-arc/tdd_rb/docs/src/article/code/test/fizz_buzz_test.rb:79: warning: method redefined; discarding old test_特定の条件を満たす要素だけを配列に入れて返す /Users/k2works/Projects/hiroshima-arc/tdd_rb/docs/src/article/code/test/fizz_buzz_test.rb:74: warning: previous definition of test_特定の条件を満たす要素だけを配列に入れて返す was here /Users/k2works/Projects/hiroshima-arc/tdd_rb/docs/src/article/code/test/fizz_buzz_test.rb:94: warning: method redefined; discarding old test_新しい要素の配列を返す /Users/k2works/Projects/hiroshima-arc/tdd_rb/docs/src/article/code/test/fizz_buzz_test.rb:89: warning: previous definition of test_新しい要素の配列を返す was here /Users/k2works/Projects/hiroshima-arc/tdd_rb/docs/src/article/code/test/fizz_buzz_test.rb:104: warning: method redefined; discarding old test_配列の中から条件に一致する要素を取得する /Users/k2works/Projects/hiroshima-arc/tdd_rb/docs/src/article/code/test/fizz_buzz_test.rb:99: warning: previous definition of test_配列の中から条件に一致する要素を取得する was here /Users/k2works/Projects/hiroshima-arc/tdd_rb/docs/src/article/code/test/fizz_buzz_test.rb:138: warning: method redefined; discarding old test_畳み込み演算を行う /Users/k2works/Projects/hiroshima-arc/tdd_rb/docs/src/article/code/test/fizz_buzz_test.rb:133: warning: previous definition of test_畳み込み演算を行う was here Started with run options --seed 5886
deftest_配列の中から条件に一致する要素を取得する result = %w[apple orange pineapple strawberry apricot].grep(/^a/) assert_equal %w[apple apricot], result end
deftest_ブロック内の条件式が真である間までの要素を返す result = [1, 2, 3, 4, 5, 6, 7, 8, 9].take_while { |item| item < 6 } assert_equal [1, 2, 3, 4, 5], result end
deftest_ブロック内の条件式が真である以降の要素を返す result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].drop_while { |item| item < 6 } assert_equal [6, 7, 8, 9, 10], result end
deftest_injectメソッドで畳み込み演算を行う result = [1, 2, 3, 4, 5].inject(0) { |total, n| total + n } assert_equal 15, result end
deftest_reduceメソッドで畳み込み演算を行う result = [1, 2, 3, 4, 5].reduce { |total, n| total + n } assert_equal 15, result end end end
テストを再実行して警告メッセージが消えたこと確認します。
1 2 3 4 5 6 7 8
$ rake test /home/gitpod/.rvm/rubies/ruby-2.6.3/bin/ruby -w -I"lib" -I"/home/gitpod/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib""/home/gitpod/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/rake-12.3.2/lib/rake/rake_test_loader.rb""./test/fizz_buzz_test.rb" Started with run options --seed 10674
# Add files and commands to this file, like the example: # watch(%r{file/path}) { `command(s)` } # guard :shelldo watch(%r{lib/(.*).rb}) { |_m|`rake test` } end
guard :minitestdo # with Minitest::Unit watch(%r{test\/*.rb}) end
guard :rubocop, cli:%w[--auto-correct --format fuubar --format html -o ./tmp/rubocop_results.html]do watch(/(.*).rb/) end
$ guard start Warning: the running version of Bundler (2.1.3) is older than the version that created the lockfile (2.1.4). We suggest you to upgrade to the version that created the lockfile by running `gem install bundler:2.1.4`. 03:49:28 - INFO - Guard::Minitest 2.4.6 is running, with Minitest::Unit 5.14.0! 03:49:28 - INFO - Running: all tests Coverage report generated for MiniTest to /workspace/tdd_rb/coverage. 4 / 11 LOC (36.36%) covered. Started with run options --guard --seed 1256
Rake::TestTask.new do|test| test.test_files = Dir['./test/fizz_buzz_test.rb'] test.verbose = true end
desc 'Run Format' task :formatdo sh 'rubocop --fix-layout' end
desc 'Run Guard' task :guarddo sh 'guard start' end
自動実行タスクを起動しましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
$ rake guard start 03:52:01 - INFO - Guard::Minitest 2.4.6 is running, with Minitest::Unit 5.14.0! 03:52:01 - INFO - Running: all tests Coverage report generated for MiniTest to /workspace/tdd_rb/coverage. 4 / 11 LOC (36.36%) covered. Started with run options --guard --seed 3219
05:00:34 - INFO - Running: all tests Coverage report generated for MiniTest to /workspace/tdd_rb/coverage. 4 / 11 LOC (36.36%) covered. Started with run options --guard --seed 16292