Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions lib/literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,50 @@ module Literal
loader.setup
end

CLASS_METHOD = Kernel.instance_method(:class)
CLASS_VARIABLE_GET_METHOD = Module.instance_method(:class_variable_get)
INSTANCE_VARIABLE_GET_METHOD = Kernel.instance_method(:instance_variable_get)

module BindingAssert
def assert(...) = Literal.assert(self, ...)
end

::Binding.include(Literal::BindingAssert)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make a literal/pure entry point that people can use if they want to opt out of patches.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally would rather opt-in than opt-out, eg literal/binding_assert

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Elaborate? It has no perf cost, no overhead. And this DX is superior to Literal.assert(binding, **constraints).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is mixing something in Binding, which I would find somewhat unexpected unless I explicitly wanted that feature

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the main use case will be people opting in. I was only thinking of adding /pure for gem authors who might not want a transient dependency to do patching.



def self.assert(bind, **kwargs)
kwargs.each do |name, type|
string_name = (Symbol === name) ? name.name : name
first_byte = string_name.getbyte(0)

if first_byte == 64 # @foo
if string_name.getbyte(1) == 64 # @@foo
value = CLASS_VARIABLE_GET_METHOD.bind_call(
CLASS_METHOD.bind_call(bind.receiver), name
)
else # @foo
value = INSTANCE_VARIABLE_GET_METHOD.bind_call(bind.receiver, name)
end
elsif first_byte == 36 # $foo
raise if string_name.match?(/[\s\.]/)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to raise a named exception here with a clear message.

value = eval(string_name)
else # foo
value = bind.local_variable_get(name)
end

unless type === value
raise ::TypeError.new(<<~MESSAGE)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Joel and I discussed that Literal::TypeError needs a refactor to be more generic to allow for simpler errors like this. Personally, I think make TypeError simple like this and move the property stuff into a PropertyError class that inherits from TypeError

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea

Assertion failed!

#{name}
Expected: #{type.inspect}
Actual (#{value.class}): #{value.inspect}
MESSAGE
end
end
end


def self.Value(*args, **kwargs, &block)
value_class = Class.new(Literal::Value)

Expand Down
50 changes: 50 additions & 0 deletions test/assert.test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
$a = 1

test "assert with local variable" do
refute_raises do
a = 1
b = "Hello"

binding.assert(a: Integer, b: String)
binding.assert("a" => Integer, "b" => String)
end

assert_raises TypeError do
a = 1
binding.assert(a: String)
end
end

test "assert with global variable" do
refute_raises do
binding.assert("$a": Integer)
end

assert_raises TypeError do
binding.assert("$a": String)
end
end

test "assert with instance variable" do
refute_raises do
@a = 1
binding.assert("@a": Integer)
end

assert_raises TypeError do
@a = 1
binding.assert("@a": String)
end
end

test "assert with class variable" do
refute_raises do
@@a = 1
binding.assert("@@a": Integer)
end

assert_raises TypeError do
@@a = 1
binding.assert("@@a": String)
end
end
Loading