Skip to content
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

Attempting to mock File results in error #49

Open
watzon opened this issue Jan 22, 2023 · 4 comments
Open

Attempting to mock File results in error #49

watzon opened this issue Jan 22, 2023 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@watzon
Copy link
Contributor

watzon commented Jan 22, 2023

Not sure if this is part of the intended purpose of mocks or not, but I'm attempting to mock the File class and I get the following error:

...
 > 38 |         # Attempt to find a stub that satisfies the method call and arguments.
 > 39 |         # Finding a suitable stub is delegated to the type including the `Stubbable` module.
 > 40 |         if __temp_270 = _spectator_find_stub(__temp_269)
 > 41 |           # Cast the stub or return value to the expected type.
 > 42 |           # This is necessary to match the expected return type of the original method.
 > 43 |           _spectator_cast_stub_value(__temp_270, __temp_269, typeof(previous_def(prefix, suffix, *dir: direncoding: encodinginvalid: invalid) { |*_spectator_yargs| yield *_spectator_yargs }),
 > 44 |           :raise)
 > 45 |         else
 > 46 |           # Delegate missing stub behavior to concrete type.
 > 47 |           _spectator_stub_fallback(__temp_269, typeof(previous_def(prefix, suffix, *dir: direncoding: encodinginvalid: invalid) { |*_spectator_yargs| yield *_spectator_yargs })) do
 > 48 |             # Use the default response for the method.
 > 49 |             previous_def(prefix, suffix, *dir: direncoding: encodinginvalid: invalid) { |*_spectator_yargs| yield *_spectator_yargs }
 > 50 |           end
 > 51 |         end
 > 52 |       end
 > 53 |     
Error: unexpected token: "direncoding"
@watzon
Copy link
Contributor Author

watzon commented Jan 22, 2023

Traced the issue to stubbable.cr, line 129-164. Seems like there are some cases that it's not handling well.

@icy-arctic-fox icy-arctic-fox self-assigned this Jan 23, 2023
@icy-arctic-fox icy-arctic-fox added the bug Something isn't working label Jan 23, 2023
@icy-arctic-fox
Copy link
Owner

icy-arctic-fox commented Jan 23, 2023

The original issue has been resolved, however there's another issue that prevents mocking File. The compiler does not allow casting a union of a pointer and nil to non-nil. For instance:

a = true ? Pointer(UInt8).null : nil
b = a.as(Pointer(UInt8))
p! a, typeof(a), b, typeof(b)

I'm not sure if this is intended or a bug in the compiler. This code works for other non-pointer types.

https://gitlab.com/arctic-fox/spectator/-/jobs/3648109240#L35

crystal-lang/crystal#13000

@watzon
Copy link
Contributor Author

watzon commented Jan 24, 2023

Ahh that's what that question was in reference to

@icy-arctic-fox
Copy link
Owner

It doesn't appear that the issue with pointers in unions will be fixed soon. A workaround in Spectator would be substantial. There's a possible fix that may even reduce compilation time, but it could take a while to implement.

icy-arctic-fox added a commit that referenced this issue Jan 27, 2023
The constructed previous_def call was malformed for stub methods.
Resolves the original issue in
#49
icy-arctic-fox added a commit that referenced this issue Jan 27, 2023
icy-arctic-fox added a commit that referenced this issue Jan 27, 2023
Fix splat argument expansion in method redefinition

See merge request arctic-fox/spectator!36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants