Fixrb

Fix 1ガイド

マッチャー

それぞれが何を比較し、満たされたとき、満たされないときに何を書くか。

マッチャーは要求の後半、レベルのあとに来る部分です(it MUST eq 100)。FixのマッチャーはMatchiのもので、できる限り期待値の側が比較を行います。テスト対象のオブジェクトが自分で自分を保証できないようにするためです。どのマッチャーもすべてのレベルで使え、否定(MUST_NOTまたはSHOULD_NOT)は行にnotと書きます。

マッチャー満たされるのは、実際の値が…
eq(expected)期待値と同じクラスで、eql?で等しいとき
be(expected)まさに同じオブジェクトのとき
be_true, be_false, be_niltrue、false、nilのとき
be_a_kind_of(mod)そのモジュールか、その子孫のインスタンスのとき
be_an_instance_of(klass)まさにそのクラスのインスタンスのとき
be_within(d).of(x)xとの差がd以内の数値のとき
match(regexp)正規表現にマッチするStringかSymbolのとき
satisfy { |value| … }ブロックがtrueと答えるとき
raise_exception(klass)メソッドが発生させた、そのクラスの例外のとき
change(object, :method)object.methodの戻り値を変えたメソッドから返ってきたとき
fix(:Name), fix { … }その仕様を満たすとき

eq

eq(expected)は、実際の値が期待値と同じクラスで、eql?で等しいときに満たされます。文字列、シンボル、配列、ハッシュ、Dataオブジェクトは中身を要素ごとに比較します。数値はクラスも比較するので、1はeq 1.0ではありません。

require "fix"

Fix :Half do
  on :to_s do
    it MUST eq "1/2"
  end

  on :to_f do
    it MUST eq 0.5
  end

  on :numerator do
    it MUST eq 1.0
  end
end

exit(Fix[:Half].test { Rational(1, 2) })
ruby eq_fix.rb
eq_fix.rb:5: Success: expected "1/2" to eq "1/2"
eq_fix.rb:9: Success: expected 0.5 to eq 0.5
eq_fix.rb:13: Failure: expected 1 to eq 1.0

分子はIntegerの1です。1 == 1.0ではあっても、1はFloatの1.0とeql?ではありません。期待するクラスの数値を書くか、計算されたFloatにはbe_withinを使ってください。

be

be(expected)は、実際の値が期待するオブジェクトそのもの(equal?)であるときに満たされます。定数のオブジェクト、シンボル、true、false、nil、あるいはファイルの変数に保持したオブジェクトなどです。

require "fix"

DEFAULTS = { color: :blue }.freeze

Settings = Data.define(:defaults)

Fix :Settings do
  on :defaults do
    it MUST be DEFAULTS
    it MUST eq({ color: :blue })
    it MUST be({ color: :blue })
  end
end

exit(Fix[:Settings].test { Settings.new(DEFAULTS) })
ruby be_fix.rb
be_fix.rb:9: Success: expected {color: :blue} to be {color: :blue}
be_fix.rb:10: Success: expected {color: :blue} to eq {color: :blue}
be_fix.rb:11: Failure: expected {color: :blue} to be {color: :blue}

最後の行は最初の行と同じように読めますが、失敗します。そこに書かれたハッシュはDEFAULTSと同じ中身をもち、eqなら受け入れますが、別のオブジェクトだからです。

be_true, be_false, be_nil

be_true、be_false、be_nilは、それぞれただひとつのオブジェクト、true、false、nilでだけ満たされます。4のように、Rubyが条件式で真として扱う値はtrueではありません。

require "fix"

Fix :Word do
  on :empty? do
    it MUST be_false
  end

  on :index, "z" do
    it MUST be_nil
  end

  on :size do
    it MUST be_true
  end
end

exit(Fix[:Word].test { "word" })
ruby be_true_fix.rb
be_true_fix.rb:5: Success: expected false to be false
be_true_fix.rb:9: Success: expected nil to be nil
be_true_fix.rb:13: Failure: expected 4 to be true

コレクションが空かどうかを確かめるには、それ自身に尋ねます(on(:empty?) { it MUST be_true })。nilでもfalseでもない任意の値を受け入れるには、satisfy { |value| !!value }と書きます。

be_a_kind_of

be_a_kind_of(mod)は、そのクラス、そのサブクラスのいずれか、あるいはそのモジュールをincludeしたクラスのインスタンスで満たされます。

require "fix"

Fix :Count do
  it MUST be_a_kind_of Integer
  it MUST be_a_kind_of Numeric
  it MUST be_a_kind_of Comparable
  it MUST be_a_kind_of String
end

exit(Fix[:Count].test { 42 })
ruby be_a_kind_of_fix.rb
be_a_kind_of_fix.rb:4: Success: expected 42 to be a kind of Integer
be_a_kind_of_fix.rb:5: Success: expected 42 to be a kind of Numeric
be_a_kind_of_fix.rb:6: Success: expected 42 to be a kind of Comparable
be_a_kind_of_fix.rb:7: Failure: expected 42 to be a kind of String

be_an_instance_of

be_an_instance_of(klass)は、まさにそのクラスのインスタンスでだけ満たされ、サブクラスのインスタンスでは満たされません。

require "fix"

Fix :Count do
  it MUST be_an_instance_of Integer
  it MUST be_an_instance_of Numeric
end

exit(Fix[:Count].test { 42 })
ruby be_an_instance_of_fix.rb
be_an_instance_of_fix.rb:4: Success: expected 42 to be an instance of Integer
be_an_instance_of_fix.rb:5: Failure: expected 42 to be an instance of Numeric

be_within

be_within(delta).of(expected)は、期待値との差がdelta以内の数値(Integer、Float、Rational、BigDecimal)で満たされます。ほかのクラスの値、たとえばStringでは満たされません。計算されたFloatはめったに正確ではないので、そのためのマッチャーです。

require "fix"

Circle = Data.define(:radius) do
  def area = Math::PI * radius**2
end

Fix :Circle do
  on :area do
    it MUST be_within(0.01).of(3.14)
    it MUST be_within(0.001).of(3.14)
  end
end

exit(Fix[:Circle].test { Circle.new(radius: 1) })
ruby be_within_fix.rb
be_within_fix.rb:9: Success: expected 3.141592653589793 to be within 0.01 of 3.14
be_within_fix.rb:10: Failure: expected 3.141592653589793 to be within 0.001 of 3.14

match

match(regexp)は、正規表現にマッチするStringかSymbolで満たされ、それ以外では満たされません。

require "fix"

Fix :Email do
  it MUST match(/@example\.com\z/)
  it MUST_NOT match(/\s/)
  it MUST match(/\.org\z/)

  on :to_sym do
    it MUST match(/\Aada@/)
  end
end

exit(Fix[:Email].test { "ada@example.com" })
ruby match_fix.rb
match_fix.rb:4: Success: expected "ada@example.com" to match /@example\.com\z/
match_fix.rb:5: Success: expected "ada@example.com" not to match /\s/
match_fix.rb:6: Failure: expected "ada@example.com" to match /\.org\z/
match_fix.rb:9: Success: expected :"ada@example.com" to match /\Aada@/

satisfy

satisfy { |value| … }は、ブロックが実際の値に対してtrueと答えたときに満たされ、falseと答えたときには満たされません。それ以外の答えはErrorになるので、問いに答え忘れたブロックが通ることはありません。

require "fix"

Fix :Score do
  it MUST satisfy { |score| score.between?(0, 100) }
  it MUST satisfy { |score| score.even? }
  it MUST satisfy { |score| score.to_s }
end

exit(Fix[:Score].test { 75 })
ruby satisfy_fix.rb
satisfy_fix.rb:4: Success: expected 75 to satisfy &block
satisfy_fix.rb:5: Failure: expected 75 to satisfy &block
satisfy_fix.rb:6: Error: TypeError: the block must return true or false (got a String)
  satisfy_fix.rb:9:in '<main>'

ブロックのパラメーターには名前をつけてください。Ruby 3.4以降、ブロックの中の裸のitはそのパラメーターを指しますが、Ruby 3.3ではFixの語です。

raise_exception

raise_exception(klass)は、コンテキストのメソッドがそのクラスかサブクラスの例外を発生させたときに満たされます。KeyErrorはIndexErrorの一種です。raise_exception(klass, message)はさらにそのメッセージを、Stringか、それにマッチするRegexpで求めます。値を返したメソッドでは満たされず、ほかの例外でも満たされません。ほかのマッチャーのもとでは、例外はErrorになります。下のit MUST eq 0がそうで、バックトレースのうちFixとそのgemに属さないフレームが続きます。

require "fix"

Fix :Stock do
  on :fetch, :pears do
    it MUST raise_exception KeyError
    it MUST raise_exception KeyError, "key not found: :pears"
    it MUST raise_exception IndexError
    it MUST raise_exception ArgumentError
    it MUST eq 0
  end

  on :fetch, :apples do
    it MUST raise_exception KeyError
  end
end

exit(Fix[:Stock].test { { apples: 3 } })
ruby raise_exception_fix.rb
raise_exception_fix.rb:5: Success: expected #<KeyError: key not found: :pears> to raise exception KeyError
raise_exception_fix.rb:6: Success: expected #<KeyError: key not found: :pears> to raise exception KeyError with message "key not found: :pears"
raise_exception_fix.rb:7: Success: expected #<KeyError: key not found: :pears> to raise exception IndexError
raise_exception_fix.rb:8: Failure: expected #<KeyError: key not found: :pears> to raise exception ArgumentError
raise_exception_fix.rb:9: Error: KeyError: key not found: :pears
  raise_exception_fix.rb:17:in '<main>'
raise_exception_fix.rb:13: Failure: expected 3 to raise exception KeyError

change

change(object, :method)は、コンテキストのメソッドを送ったことで、そのオブジェクトのそのメソッドが別の値を返すようになったときに満たされます。.by(n)、.by_at_least(n)、.by_at_most(n)はどれだけ変わるかを、.to(b)と.from(a).to(b)はどの値からどの値へ変わるかを指定します。行には、コンテキストのメソッドが返したものが示されます。ここではclickが新しいカウントを返しています。

require "fix"

COUNTER = Struct.new(:count).new(0)

Clicker = Data.define(:counter) do
  def click = counter.count += 1
  def look = counter.count
end

Fix :Clicker do
  on :click do
    it MUST change(COUNTER, :count).by(1)
  end

  on :click do
    it MUST change(COUNTER, :count).from(0).to(1)
  end

  on :look do
    it MUST change(COUNTER, :count).by_at_least(1)
  end
end

exit(Fix[:Clicker].test { Clicker.new(COUNTER) })
ruby change_fix.rb
change_fix.rb:12: Success: expected 1 to change by 1
change_fix.rb:16: Success: expected 1 to change from 0 to 1
change_fix.rb:20: Failure: expected 0 to change by at least 1

マッチャーはファイルの読み込み時に作られるので、監視するオブジェクトはその時点で存在していなければなりません。サブジェクトとともに存在するletではなく、ここでのように定数を使います。ひとつのコンテキストの期待は同じプロセスで順に実行されるので、同じコンテキストの2つ目のchangeは、1つ目が行ったことを目にします。上の例では期待ごとに専用のコンテキストがあり、それぞれファイルの状態から始まります。lookを送るときにはカウントは再び0で、lookは何も変えません。

fix

fix(:Name)は、実際の値がその名前の仕様を満たすときに満たされます。仕様は、同じファイルのそれより前か、requireしたファイルに書かれている必要があります。fix { … }は、名前をつけずにその場で仕様を書きます。

require "fix"

Fix :Filled do
  on :empty? do
    it MUST be_false
  end
end

Fix :Name do
  it MUST be_an_instance_of String
  it MUST fix(:Filled)
  it MUST fix { on(:strip) { it MUST eq "Ada" } }
end

exit(Fix[:Name].test { "Ada" })
ruby fix_fix.rb
fix_fix.rb:10: Success: expected "Ada" to be an instance of String
fix_fix.rb:11: Success: expected "Ada" to fix Filled
fix_fix.rb:12: Success: expected "Ada" to fix an anonymous specification

仕様の中の仕様については、仕様を組み合わせるで詳しく説明しています。

独自のマッチャー

2つのメソッドをもつオブジェクトなら、どれでもマッチャーになります。ブロックから実際の値を受け取ってtrueかfalseを答えるmatch?と、行のtoのあとに書かれるStringを返すto_sです。

require "fix"

class BeEven
  def match? = yield.even?
  def to_s = "be even"
end

Fix :Pair do
  on :size do
    it MUST BeEven.new
  end

  on :first do
    it MUST BeEven.new
  end
end

exit(Fix[:Pair].test { [1, 2] })
ruby own_fix.rb
own_fix.rb:10: Success: expected 2 to be even
own_fix.rb:14: Failure: expected 1 to be even

MUST_NOT BeEven.newも同じように動きます。答えを読むのはレベルの側です。