-
Notifications
You must be signed in to change notification settings - Fork 5
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
Mocks fail to compile for methods with free variables #48
Comments
It appears that the mock code doesn't account for free variables in return types. |
A fix for this issue and various related ones has been pushed to master. Can you confirm this fixes the issue with free variables in type annotations? I suspect there may be other issues porting chars.cr to the latest Spectator mocks. Please open an issue if you encounter any. |
@icy-arctic-fox awesome, that got me further. I now think I'm hitting another spectator bug. Example code: #
# Returns an Array of random, but unique, bytes from the `CharSet`.
#
def random_distinct_bytes(length : Int) : Array(Int32)
@byte_set.to_a.shuffle[0,length]
end
#
# Returns an Array of random, but unique, bytes from the `CharSet`.
#
def random_distinct_bytes(length : Array(Int),
random = Random::DEFAULT) : Array(Int32)
bytes.shuffle[0,length.sample(random)]
end
#
# Returns an Array of random, but unique, bytes from the `CharSet`.
#
def random_distinct_bytes(length : Range(Int, Int)) : Array(Int32)
@byte_set.to_a.shuffle[0,rand(length)]
end Example spec code: describe "#random_distinct_bytes" do
it "must return a random Array of unique bytes" do
bytes = subject.random_distinct_bytes(10)
expect(bytes.uniq).to be == bytes
expect(bytes.all? { |b| subject.includes?(b) }).to be(true)
end
context "with a range of lengths" do
it "must return a random Array of unique bytes with a varying length" do
bytes = subject.random_distinct_bytes(5..10)
expect(bytes.uniq).to be == bytes
expect(bytes.size).to be_between(5, 10)
expect(bytes.all? { |b| subject.includes?(b) }).to be(true)
end
end
end Error:
|
Pushed another change to master that should fix range comparisons. The underlying issue is that a union of argument types are being compared against each other. Case-equality matching ( (1..10) === 'a' Which doesn't make sense, but could be a possible comparison with the union types. What I'm doing for now is using If you (or anyone else) has a better solution, I'd love to hear it! |
@icy-arctic-fox got a little further. Crystal code: @[AlwaysInline]
def +(other : CharSet) : CharSet
self | other
end Crystal spec: describe "#+" do
mock Chars::CharSet do
stub :|, other : Chars::CharSet
end
let(other) { described_class.new }
let(return_value) { described_class.new }
it "must call #+" do
expect(subject).to receive(:|).with(other).and_return(return_value)
expect(subject + other).to be(return_value)
end
end Error:
|
It looks like the old (v0.9) stub syntax is being used. A method definition ( mock Chars::CharSet do
stub :|, other : Chars::CharSet
end
# can be just:
mock Chars::CharSet The v0.11 syntax doesn't require a I think this code should work: Spectator.describe Chars::CharSet do
mock Chars::CharSet
describe "#|" do
let(other) { mock(Chars::CharSet) }
let(return_value) { mock(Chars::CharSet) }
it "must call #+" do
expect(subject).to receive(:|).with(other).and_return(return_value)
expect(subject | other).to be(return_value)
end
end
end More info about the new syntax and mock system can be found on the wiki. |
Success! After adding the double |
I was attempting to copy the code you had before. This bit from the original: let(other) { described_class.new }
let(return_value) { described_class.new } The Spectator.describe Chars::CharSet do
def_mock Chars::CharSet
let(other) { new_mock(Chars::CharSet) }
let(return_value) { new_mock(Chars::CharSet) }
end Hope that makes sense. 😕 I'm glad it works now! Real world usages of Spectator are invaluable for testing it. |
I was trying to test chars.cr against Crystal 1.6.2 and Spectator 0.11.4, but got this strange error coming from deep within
enumerable.cr
. The beginning of the error is a macro expansion of a method stub. I assume I need to update how I'm defining the stubs?Error Trace
The text was updated successfully, but these errors were encountered: