パターン
どの実装からも切り離された仕様の、4つの使い方。
Fixの仕様はクラスを名指ししません。サブジェクトが何をするかを述べ、サブジェクトを選ぶのはテストです。この4つのパターンは、そこから生まれます。
ひとつの仕様、複数の実装
2つのクラスが同じ仕事をするなら、ひとつの仕様がその仕事を記述し、それぞれの実装をその仕様でテストします。仕様は専用のファイルfix/specs/stack.rbに置きます。名前が_fix.rbで終わらないので、fixがそれを実行することはありません。
require "fix"
Fix :Stack do
on :empty? do
it MUST be_true
end
on :push, 1 do
on :push, 2 do
on :pop do
it MUST eq 2
end
end
end
on :pop do
it MUST be_nil
end
end
各実装には、それをrequireしてサブジェクトをテストする短いファイルがあります。fix/array_fix.rbではArrayです。
require_relative "specs/stack"
exit(Fix[:Stack].test { [] })
プロジェクトの連結リストはlib/linked_stack.rbにあります。
class LinkedStack
Node = Data.define(:value, :below)
def initialize = @top = nil
def empty? = @top.nil?
def push(value) = tap { @top = Node.new(value, @top) }
def pop
value = @top&.value
@top = @top&.below
value
end
end
そして、そのテストファイルfix/linked_stack_fix.rbです。
require_relative "specs/stack"
require_relative "../lib/linked_stack"
exit(Fix[:Stack].test { LinkedStack.new })
fix fix/specs/stack.rb:5: Success: expected true to be true fix/specs/stack.rb:11: Success: expected 2 to eq 2 fix/specs/stack.rb:17: Success: expected nil to be nil fix/specs/stack.rb:5: Success: expected true to be true fix/specs/stack.rb:11: Success: expected 2 to eq 2 fix/specs/stack.rb:17: Success: expected nil to be nil 2 files met
各行は仕様を指し、同じ3行が2回並びます。最後の数は、満たされない実装があればそのファイルを示します。実装が増えても、短いファイルがひとつ増えるだけです。
依存ライブラリの契約
プロジェクトは依存ライブラリの振る舞いに頼っています。たとえば、JSON.parseは指定しない限り文字列のキーを返す、ということです。依存ライブラリの仕様はそれを書き留め、次のアップグレードのときに、それがまだ成り立つかどうかを教えてくれます。
require "fix"
require "json"
Fix :JSON do
on :generate, { "id" => 1 } do
it MUST eq '{"id":1}'
end
on :parse, '{"id":1}' do
it MUST eq({ "id" => 1 })
end
on :parse, '{"id":1}', symbolize_names: true do
it MUST eq({ id: 1 })
end
end
exit(Fix[:JSON].test { JSON })
ruby json_fix.rb
json_fix.rb:6: Success: expected "{\"id\":1}" to eq "{\"id\":1}"
json_fix.rb:10: Success: expected {"id" => 1} to eq {"id" => 1}
json_fix.rb:14: Success: expected {id: 1} to eq {id: 1}
サブジェクトはモジュールそのもので、各onはプロジェクトが行う呼び出しです。書くのはプロジェクトが使う部分だけで、ライブラリ全体ではありません。
3つのレベルの契約
インターフェースには、必須のメソッド、推奨されるメソッド、任意のメソッドがあることがよくあります。3つのレベルはそれを表します。ここでは、プラグインはテキストを変換しなければならず(MUST)、小文字の名前をもつべきであり(SHOULD)、バージョンをもってもよい(MAY)とします。
require "fix"
Fix :Plugin do
on :call, "text" do
it MUST be_an_instance_of String
end
on :name do
it SHOULD match(/\A[a-z]+\z/)
end
on :version do
it MAY match(/\A\d+\.\d+\z/)
end
end
class Upcase
def call(text) = text.upcase
def name = "Upcase"
end
class Reverse
def call(text) = text.reverse
def name = "reverse"
def version = "1.2"
end
results = [
Fix[:Plugin].test { Upcase.new },
Fix[:Plugin].test { Reverse.new }
]
exit(results.all?)
ruby plugin_fix.rb plugin_fix.rb:5: Success: expected "TEXT" to be an instance of String plugin_fix.rb:9: Warning: expected "Upcase" to match /\A[a-z]+\z/ plugin_fix.rb:13: Info: NoMethodError: undefined method 'version' for an instance of Upcase plugin_fix.rb:5: Success: expected "txet" to be an instance of String plugin_fix.rb:9: Success: expected "reverse" to match /\A[a-z]+\z/ plugin_fix.rb:13: Success: expected "1.2" to match /\A\d+\.\d+\z/
最初の3行はUpcaseのものです。名前が小文字ではないのでWarning、バージョンがないのでInfoです。どちらも成功として扱われるので、2つのプラグインはどちらも契約を満たし、終了ステータスは0です。callのないプラグインならErrorになり、成功とはなりません。
gemに同梱する仕様
ストア、アダプター、プラグインといったインターフェースを定義するgemは、rackがRack::Lintを同梱しているように、そのインターフェースの仕様を同梱できます。インターフェースを実装する人は自分の実装をその仕様でテストし、gemをアップグレードすれば新しい仕様も手に入ります。gemはそれをlibのファイルlib/kvstore/specification.rbに置き、そのファイルがFixをrequireします。読み込むのは実装する人だけなので、fixはgemの依存ではなく、実装する人のプロジェクトの依存になります。
require "fix"
# What the kvstore gem asks of a store, shipped with the gem for those
# who write one.
Fix :KVStore do
on :fetch, :missing do
it MUST be_nil
end
on :store, :answer, 42 do
on :fetch, :answer do
it MUST eq 42
end
end
on :delete, :missing do
it SHOULD be_nil
end
on :size do
it MAY eq 0
end
end
実装は、それ自身のプロジェクトでそのファイルをrequireして自分をテストします。Gemfileにgemがあればrequire "kvstore/specification"です。ここでは例を短くするため、2つのプロジェクトを並べ、実装をテストファイルの中に置いています。
# With the gem in a Gemfile, this is require "kvstore/specification";
# here the two projects sit side by side.
require_relative "../../kvstore/lib/kvstore/specification"
class HashStore
def initialize = @entries = {}
def fetch(key) = @entries[key]
def store(key, value) = tap { @entries[key] = value }
def delete(key) = @entries.delete(key)
end
exit(Fix[:KVStore].test { HashStore.new })
ruby hash_store/fix/hash_store_fix.rb kvstore/lib/kvstore/specification.rb:7: Success: expected nil to be nil kvstore/lib/kvstore/specification.rb:12: Success: expected 42 to eq 42 kvstore/lib/kvstore/specification.rb:17: Success: expected nil to be nil kvstore/lib/kvstore/specification.rb:21: Info: NoMethodError: undefined method 'size' for an instance of HashStore
各行はgemの中の仕様を指します。実装する人は、求められていることを書かれた場所で読めます。ストアにはsizeがありませんが、仕様はそれを任意としているのでInfoとなり、ストアは契約を満たします。