Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

Literal branded types (#303)

This is similar to #293 but instead of wrapping values, you can brand
values. The values themselves are unchanged and not wrapped.

authored by

Joel Drapper and committed by
GitHub
7220c436 34de62d8

+37
+5
lib/literal.rb
··· 11 11 12 12 loader.collapse("#{__dir__}/literal/flags") 13 13 loader.collapse("#{__dir__}/literal/errors") 14 + 14 15 loader.setup 15 16 end 16 17 ··· 70 71 71 72 def self.Tuple(*types) 72 73 Literal::Tuple::Generic.new(*types) 74 + end 75 + 76 + def self.Brand(...) 77 + Literal::Brand.new(...) 73 78 end 74 79 75 80 def self.check(actual:, expected:)
+22
lib/literal/brand.rb
··· 1 + # frozen_string_literal: true 2 + 3 + class Literal::Brand 4 + include Literal::Types 5 + 6 + def initialize(...) 7 + @type = _Constraint(...) 8 + @objects = ObjectSpace::WeakMap.new 9 + end 10 + 11 + def new(object) 12 + Literal.check(expected: @type, actual: object) 13 + @objects[object] = true 14 + object 15 + end 16 + 17 + alias_method :[], :new 18 + 19 + def ===(value) 20 + @objects.key?(value) 21 + end 22 + end
+10
test/brand.test.rb
··· 1 + # frozen_string_literal: true 2 + 3 + UserID = Literal::Brand(String) 4 + 5 + test "branded strings" do 6 + user_id = UserID.new("123") 7 + 8 + assert UserID === user_id 9 + assert_equal "123", user_id 10 + end