0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CrystalでParameterizedテストをする際のイディオム

Last updated at Posted at 2016-06-28

title.png

CrystalでParameterizedテストをする際のイディオムについてまとめます。

サンプル

※以下のコードは Crystal の作者である Ary Borenszweig さんにコメントにて指摘していただき、
修正していただいたバージョンです。Ary Borenszweig さんありがとうございます

require "spec"

module FizzBuzz
  extend self

  def fizzbuzz(limit)
    (1..limit).each_with_object([] of String) do |e, memo|
      memo << case
      when e % 15 == 0 then "FizzBuzz"
      when e % 5 == 0  then "Buzz"
      when e % 3 == 0  then "Fizz"
      else                  e.to_s
      end
    end
  end
end

def assert_fizzbuzz(kace, limit, expected)
  it kace do
    actual = FizzBuzz.fizzbuzz(limit)
    actual.should eq(expected)
  end
end

module FizzBuzz::Test
  describe "fizzbuzz" do
    assert_fizzbuzz "Fizz only", 3, ["1", "2", "Fizz"]
    assert_fizzbuzz "Fizz, Buzz", 5, ["1", "2", "Fizz", "4", "Buzz"]
    assert_fizzbuzz "Fizz, Buzz, FizzBuzz", 15, ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]
  end
end

実行

$ crystal spec
...

Finished in 0.63 milliseconds
3 examples, 0 failures, 0 errors, 0 pending

Snippet

atom_crystal_snippet という Atom の Snippet を公開しています。
この Snippet に Parameterized テストをする際のイディオムを追加しました。

atom_crystal_snippet_tb - atom.io

0
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?