doctest-rspec gem でコメントに書いた ruby のコードを rspec で実行する

doctest-rspec gem でコメントに書いた ruby のコードを rspec で実行します

## <i class='fa fa-download'></i> インストール
```sh
$ gem install doctest-rspec
```

## <i class='fa fa-flask'></i> サンプル
### <i class='fa fa-code'></i> 検証コード
* プロダクトコード

```ruby:lib/sample.rb
class FizzBuzz
  # list
  #
  # Usage:
  #   >> FizzBuzz.new.run(15)
  #   => ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]
  def run(limit)
    (1..limit).map do |num|
      case
      when fizzbuzz?(num) then "FizzBuzz"
      when buzz?(num) then "Buzz"
      when fizz?(num) then "Fizz"
      else num.to_s
      end
    end
  end

  # fizzbuzz
  #
  # Usage:
  #   >> FizzBuzz.new.fizzbuzz?(15)
  #   => true
  #   >> FizzBuzz.new.fizzbuzz?(16)
  #   => false
  def fizzbuzz?(num)
    num % 15 == 0
  end

  # buzz
  #
  # Usage:
  #   >> FizzBuzz.new.buzz?(5)
  #   => true
  #   >> FizzBuzz.new.buzz?(4)
  #   => false
  def buzz?(num)
    num % 5 == 0
  end

  # fizz
  #
  # Usage:
  #   >> FizzBuzz.new.fizz?(3)
  #   => true
  #   >> FizzBuzz.new.fizz?(4)
  #   => false
  def fizz?(num)
    num % 3 == 0
  end
end
```

* テストコード

```ruby:spec/sample_spec.rb
require 'doctest/rspec'
require 'sample'

describe FizzBuzz do
  doctest FizzBuzz
end
```

### <i class='fa fa-play-circle'></i> テスト
* 正常に動作するケース

```sh
$ rspec -fd

FizzBuzz
  doctests for FizzBuzz
    at /path/to/lib/sample.rb:6
    at /path/to/lib/sample.rb:22
    at /path/to/lib/sample.rb:24
    at /path/to/lib/sample.rb:33
    at /path/to/lib/sample.rb:35
    at /path/to/lib/sample.rb:44
    at /path/to/lib/sample.rb:46

Finished in 0.00183 seconds (files took 0.11472 seconds to load)
7 examples, 0 failures
```

* 意図的にバグを仕込んだ場合

```ruby:lib/sample.rb
# 略
  def fizz?(num)
    # わざと 4 にします
    num % 4 == 0
    #num % 3 == 0
  end
# 略
```

```sh
$ rspec -fd
FizzBuzz
  doctests for FizzBuzz
    at /path/to/lib/sample.rb:6 (FAILED - 1)
    at /path/to/lib/sample.rb:22
    at /path/to/lib/sample.rb:24
    at /path/to/lib/sample.rb:33
    at /path/to/lib/sample.rb:35
    at /path/to/lib/sample.rb:44 (FAILED - 2)
    at /path/to/lib/sample.rb:46 (FAILED - 3)

Failures:

  1) FizzBuzz doctests for FizzBuzz at /path/to/lib/sample.rb:6
     Failure/Error: DEFAULT_FAILURE_NOTIFIER = lambda { |failure, _opts| raise failure }
       expected ["1", "2", "3", "Fizz", "Buzz", "6", "7", "Fizz", "9", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"] to equal ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]
# スタックトレースは省略

  3) FizzBuzz doctests for FizzBuzz at /path/to/lib/sample.rb:46
     Failure/Error: DEFAULT_FAILURE_NOTIFIER = lambda { |failure, _opts| raise failure }
       expected true to equal false
# スタックトレースは省略

Finished in 0.0152 seconds (files took 0.10469 seconds to load)
7 examples, 3 failures

Failed examples:

rspec './spec/sample_spec.rb[1:1:1]' # FizzBuzz doctests for FizzBuzz at /path/to/lib/sample.rb:6
rspec './spec/sample_spec.rb[1:1:6]' # FizzBuzz doctests for FizzBuzz at /path/to/lib/sample.rb:44
rspec './spec/sample_spec.rb[1:1:7]' # FizzBuzz doctests for FizzBuzz at /path/to/lib/sample.rb:46
```

## <i class="fa fa-pencil" style="font-size:1em;"></i> 雑感
[Elixir の Doctests](http://qiita.com/tbpgr/items/2ea8a20c93eeb6959c80) 相当のことができました。
気になるのは日本でこのライブラリを使っている人がほとんどいなそうなことですね。 

## <i class="fa fa-book" style="font-size:1em;"></i> 外部資料
* [doctest/doctest-rspec](https://github.com/doctest/doctest-rspec)
* [doctest-rspec - rspec](https://rubygems.org/gems/doctest-rspec)