Fixrb

spectus

RFC 2119の要求レベルを備えた期待。

期待は、値が何をMUST(しなければならない)、SHOULD(すべきである)、MAY(してもよい)かを、RFC 2119の言葉で述べます。期待はレベル、否定、マッチャーからなり、ブロックとともに呼ぶと、どうだったかを伝える結果を返します。満たされない期待のために例外を発生させることはありません。SpectusはFix 1.0のエンジンで、matchiのマッチャーのようにmatch?とto_sをもつ、どのマッチャーとも動きます。

バージョン
6.0.1
Ruby
3.3以降
依存gem
defi
ライセンス
MIT

インストール

Gemfileにgem "spectus"を加えるか、gem install spectusを実行してください。以下の例では、マッチャーにgem "matchi"を使います。

使い方

Spectus.must、must_not、should、should_not、mayは、マッチャーから要求を作ります。RFCに否定形がないので、mayにも否定形はありません。callはブロックとともにマッチャーを実行し、結果を返します。

require "spectus"
require "matchi"

eq = Matchi::Eq.new(42)
requirement = Spectus.must(eq)
requirement.level  # => :MUST
requirement.negate # => false

result = requirement.call { 6 * 7 }
result.passed?     # => true
result.outcome     # => :success
result.actual      # => "42"
result.description # => "eq 42"
result.to_s        # => "Success: expected 42 to eq 42"

Spectus.must_not(eq).call { 42 }.to_s # => "Failure: expected 42 not to eq 42"
Spectus.should(eq).call { 2 }.to_s    # => "Warning: expected 2 to eq 42"
Spectus.must(eq).call { 1 / 0 }.to_s  # => "Error: ZeroDivisionError: divided by 0"

判定

2つの問いがレベルを分けます。メソッドは存在するか。MUSTとSHOULDはそれを求め、MAYは求めません。仕様どおりに動くか。MUSTとMAYはそれを求め、SHOULDは推奨します。そこから、match?が何を答えたか、あるいは何を発生させたかによって5つの判定が生まれ、そのうちSuccess、Warning、Infoは成功として扱われます。

レベル真偽メソッドがないほかの例外
MUSTsuccessfailureerrorerror
SHOULDsuccesswarningerrorerror
MAYsuccessfailureinfoerror

否定は最初の2列を入れ替えます。メソッドがないとは、ブロックが返したまさにその値に対して、match?がNoMethodErrorを発生させた場合です。match?はtrueかfalseを答えなければならず、それ以外の答えはErrorになります。

require "spectus"
require "matchi"

# A method the value does not have: an error for MUST and SHOULD, an info
# for MAY, where the method is optional.
blank = Matchi::Predicate.new(:be_blank)
Spectus.must(blank).call { "" }.outcome   # => :error
Spectus.should(blank).call { "" }.outcome # => :error
Spectus.may(blank).call { "" }.outcome    # => :info
Spectus.may(blank).call { "" }.passed?    # => true

# A match that is false: a failure, or a warning for SHOULD.
empty = Matchi::Predicate.new(:be_empty)
Spectus.must(empty).call { [1] }.outcome   # => :failure
Spectus.should(empty).call { [1] }.outcome # => :warning
Spectus.may(empty).call { [1] }.outcome    # => :failure

# An exception the block raises reaches the matcher, which may expect it.
raises = Matchi::RaiseException.new(ZeroDivisionError)
Spectus.must(raises).call { 1 / 0 }.outcome          # => :success
Spectus.must(Matchi::Eq.new(1)).call { 1 / 0 }.outcome     # => :error
Spectus.must(Matchi::Eq.new(1)).call { 1 / 0 }.error_class # => "ZeroDivisionError"

結果

結果は凍結された素のデータです。レベル、否定、判定、マッチャーの説明、inspectが書いたとおりの実際の値、そして例外があればそのクラス、メッセージ、バックトレースを、慎重に読み取ったUTF-8の文字列としてもちます。ユーザーのコードを再び実行することなく、Marshalで直列化でき、Ractor間で共有でき、表示できます。forkするランナーに必要なのはまさにそれです。

require "spectus"
require "matchi"

# A result is plain data, frozen: it can be marshalled and shared.
result = Spectus.must(Matchi::Eq.new(42)).call { raise "boom" }
result.frozen?                              # => true
result.error_class                          # => "RuntimeError"
result.error_message                        # => "boom"
result.actual                               # => "#<RuntimeError: boom>"
result.summary                              # => "RuntimeError: boom"
Marshal.load(Marshal.dump(result)) == result # => true
Ractor.shareable?(result)                   # => true

# The texts are read defensively: nothing in a result runs user code again.
loud = Object.new
def loud.inspect = raise("no")
Spectus.must(Matchi::Eq.new(1)).call { loud }.actual # => "an object of Object"

ブロックとmatch?はdefiの方針のもとで実行されます。メソッドがそれ自身で発生させた例外は結果に収まり、Interrupt、シグナル、NoMemoryError、Timeoutの期限切れは素通りします。

ドキュメント