Fixrb

fix-its

The word its for Fix: a method of the subject and its expectation on one line.

Fix::Its adds the word its to the language of fix: a method of the subject and one expectation about what it returns, on one line. its(:size) { MUST eq 3 } is on(:size) { it MUST eq 3 }, and the result points at the line of its.

Version
1.0.0
Ruby
3.3 or later, with fork
Dependencies
fix
License
MIT

Install

Add gem "fix-its" to your Gemfile, or run gem install fix-its, then require "fix/its", which requires fix.

Usage

its takes the name of the method and its arguments, as on does, and a block that gives the requirement: a level and a matcher. Inside an on or a with, it sends its method to the value of that context.

require "fix/its"

Fix :Word do
  its(:size) { MUST eq 5 }
  its(:upcase) { MUST eq "HELLO" }
  its(:start_with?, "he") { MUST be_true }

  on :chars do
    its(:first) { MUST eq "h" }
  end
end

exit(Fix[:Word].test { "hello" })
ruby word.rb
word.rb:4: Success: expected 5 to eq 5
word.rb:5: Success: expected "HELLO" to eq "HELLO"
word.rb:6: Success: expected true to be true
word.rb:9: Success: expected "h" to eq "h"

The block is the context of the on, read once with the rest of the document, and its last expression is the requirement; a block that gives anything else raises ArgumentError at load, as does a missing block. Since its is a word of the language, a let cannot be named so. In Ruby 3.4 and later, a bare it in a block is the parameter of the block: its(:size) { MUST eq it } builds eq nil; write the value.

Documentation