-
-
Notifications
You must be signed in to change notification settings - Fork 25
Runtime assertions with binding.assert
#341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
|
||
|
|
||
| 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\.]/) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
|
|
||
| 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 |
There was a problem hiding this comment.
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/pureentry point that people can use if they want to opt out of patches.There was a problem hiding this comment.
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_assertThere was a problem hiding this comment.
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).There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
/purefor gem authors who might not want a transient dependency to do patching.