Commit graph

303 commits

Author SHA1 Message Date
Michael Miller
0758c19a86
Remove need for nillable argument to cast macro
The `class` method cannot be stubbed as this causes the cast (via `as`) to fail at compile-time.
2022-04-16 16:33:37 -06:00
Michael Miller
23d2c014b4
Implement fallback for methods without a stub 2022-04-15 17:27:30 -06:00
Michael Miller
6f04e0b9da
Allow uninteresting messages through mocks
If they are stubbed, but don't match arguments, raise an error.
2022-04-15 17:17:34 -06:00
Michael Miller
4b21c9e6c1
Introduce _spectator_stub_for_method? utility method 2022-04-15 17:16:49 -06:00
Michael Miller
3961662bf6
Experimental mock functionality 2022-04-10 20:01:54 -06:00
Michael Miller
b0e27c69e8
Fix equality check 2022-04-10 15:24:45 -06:00
Michael Miller
e8bac86017
Ensure Stubbable can have stubs defined
Require implementation of _spectator_define_stub.
2022-04-10 11:11:36 -06:00
Michael Miller
519836147e
Restructure stub modifiers 2022-04-02 10:50:27 -06:00
Michael Miller
318e4f3707
Store location information in stub
Allows tracking where a stub was originally defined.
2022-04-02 10:39:59 -06:00
Michael Miller
e61b31e47b
Add docs for allow and receive 2022-04-02 10:25:27 -06:00
Michael Miller
17592287ad
Replace stub value method with call method
Pass method call information to stub.
2022-03-31 23:07:55 -06:00
Michael Miller
2131967ba6
Workaround Crystal compiler bug 2022-03-31 22:54:03 -06:00
Michael Miller
e05ef94ef5
Initial work on stub DSL - found bug in Crystal compiler
```
Module validation failed: Invalid bitcast
  %4 = bitcast i32* %3 to %"(Spectator::Arguments(T, NT) | Nil)", !dbg !16
 (Exception)
  from /crystal/src/int.cr:541:9 in 'finish'
  from /crystal/src/compiler/crystal/codegen/codegen.cr:71:7 in 'codegen'
  from /crystal/src/compiler/crystal/compiler.cr:173:16 in 'compile'
  from /crystal/src/compiler/crystal/command/spec.cr:98:14 in 'spec'
  from /crystal/src/hash.cr:901:11 in '__crystal_main'
  from /crystal/src/crystal/main.cr:115:5 in 'main'
  from src/env/__libc_start_main.c:94:2 in 'libc_start_main_stage2'
Error: you've found a bug in the Crystal compiler. ...
```

I haven't been able to reproduce this in a simplified form.

Passing the `constraint` argument in NullStub#and_return to ValueStub.new triggers the bug.
Removing the `constraint` argument fixes the issue.
2022-03-31 22:49:45 -06:00
Michael Miller
003be48591
Use responds_to? to ensure stub is cast correctly 2022-03-31 22:39:21 -06:00
Michael Miller
9c1357da3f
Allow passing predefined stubs in as keyword args 2022-03-31 18:55:12 -06:00
Michael Miller
231323bbf1
Add return type restriction
This overrides the previous definition which doesn't have a restriction.
This is a workaround for the inability to infer a stub's return type.
2022-03-30 21:10:36 -06:00
Michael Miller
f1a68fee8c
Typo 2022-03-30 20:46:53 -06:00
Michael Miller
39f5ea34eb
Another place that needs a cast 2022-03-30 20:33:19 -06:00
Michael Miller
613c969fbb
Match RSpec behavior 2022-03-30 20:28:36 -06:00
Michael Miller
1327d74040
Fix handling of stubs with the same name 2022-03-26 00:13:24 -06:00
Michael Miller
1199379833
Various fixes 2022-03-26 00:12:37 -06:00
Michael Miller
bbaa8816b8
Weird segfault when inspecting value type is back.
Working around with a crude rescue block.
2022-03-25 23:54:44 -06:00
Michael Miller
b28437ccc4
Match RSpec behavior 2022-03-25 22:32:27 -06:00
Michael Miller
28488d308e
Don't invoke fallback if there are stubs for the method 2022-03-25 22:30:39 -06:00
Michael Miller
fc51c1d730
More logging 2022-03-24 02:39:15 -06:00
Michael Miller
b97a367be7
Method return type restriction takes precedence over original return type
Raise TypeCastError on ambiguous stub values.
2022-03-24 02:01:25 -06:00
Michael Miller
7dd2cffa8e
Inspecting the value's class seems to work now 2022-03-22 22:13:02 -06:00
Michael Miller
d6fcf128ba
Temporarily disable returning un-cast value 2022-03-22 22:11:34 -06:00
Michael Miller
78dea01abe
Manual method existence check 2022-03-20 21:49:21 -06:00
Michael Miller
0fb7be6722
Use stub instead of abstract_stub by default for stub_all
RSpec doesn't appear to raise unexpected message errors for standard object methods (like to_s).
There are also a lot of issues that arise when requiring a stub on common methods.
2022-03-20 15:38:26 -06:00
Michael Miller
03a031ad38
Use absolute path to RESERVED_KEYWORDS 2022-03-19 22:00:12 -06:00
Michael Miller
b23a567ee3
Add require statements 2022-03-19 21:56:58 -06:00
Michael Miller
04f3614ff7
Workaround issue with value not being found
For some reason the `value` method isn't found.
Event casting to TypedStub doesn't work.
2022-03-19 20:00:50 -06:00
Michael Miller
bed84b315d
Add lazy double 2022-03-19 19:32:41 -06:00
Michael Miller
162ad4df33
Report call and args like other error messages 2022-03-19 18:58:06 -06:00
Michael Miller
b3fa2e3bfe
Address Ameba issues 2022-03-19 17:22:13 -06:00
Michael Miller
2f1999b377
Fix call expansion in debug log
Macro expansion somehow generated an error when calling a missing method with a block.

```
syntax error in :1
Error: unknown token: 'n'
```

It seems like 'n' is from the '\n' that was previously generated:

```
Log.debug { "Got undefined method " + "baz do\n  yield\nend" }
```
2022-03-19 16:32:25 -06:00
Michael Miller
17e97cb39a
Explode method expansion
Somewhere along the way the block argument gets dropped.
Even though `method` is a `Def` that accepts a block, the `&block` portion is dropped.
Possible Crystal compiler bug.
2022-03-19 16:13:11 -06:00
Michael Miller
15dd2ea6f1
Inject stubbable method that accepts a block 2022-03-19 16:09:35 -06:00
Michael Miller
70787ce2d4
Extend NullDouble from Double 2022-03-19 13:00:42 -06:00
Michael Miller
d40040c8ee
Remove extra block body leftover from debug
Update docs to show example of default method implementation.
2022-03-19 12:37:46 -06:00
Michael Miller
25f546245a
Handle injecting stubbed abstract method 2022-03-19 12:33:09 -06:00
Michael Miller
9091b8d027
Literally inject stubs from double block 2022-03-19 12:28:00 -06:00
Michael Miller
a508aeb4a3
Use a shared macro to handle stub type cast 2022-03-17 02:45:50 -06:00
Michael Miller
751ea77172
Add docs explaining how stub macros work 2022-03-17 02:18:32 -06:00
Michael Miller
4f7a4a3453
Avoid issue with TypeCastError message
For some reason, attempting to get the class of the stubbed value causes a segfault.
This appears to get swallowed in some cases.
Trying to inspect the value was difficult without triggering some error.
In the failing case, the value is 42 (returned by a stubbed foo method).
The method is supposed to return a String, and this error checking should be triggered (line 119).
The value is a union, and inspecting the bytes contained in it looks like it's prefixed with the Crystal type ID for Int32.
However it looks like Crystal tries to treat it as a Bool? I'm not sure.
Very weird behavior and instead of wasting more time on it, I'm omitting the returned type from the error.
This type of casting error shouldn't be common.
This seems like a Crystal bug, but I'm having a hard time distilling the issue.
2022-03-16 22:40:24 -06:00
Michael Miller
280f117e23
Use TypedStub instead of ValueStub
Test possible issues around stub returning nil
2022-03-16 21:07:20 -06:00
Michael Miller
8e38d3b054
Add intermediate TypedStub 2022-03-16 21:05:43 -06:00
Michael Miller
dc74a741cf
Bit of method cleanup 2022-03-15 22:55:24 -06:00
Michael Miller
332ab1cebc
Smarter handling of stub fallback 2022-03-15 22:46:43 -06:00
Michael Miller
01fc91e854
Add stubs for primitive methods 2022-03-14 21:53:09 -06:00
Michael Miller
64cd921a7e
Add block arguments and yield 2022-03-12 23:50:30 -07:00
Michael Miller
03aa282512
Don't stub primitive methods 2022-03-12 23:02:55 -07:00
Michael Miller
291b4c75a8
Compiler error if stub defined in a method 2022-03-12 23:02:33 -07:00
Michael Miller
94872a498d
Better string representation of method call 2022-03-12 19:26:18 -07:00
Michael Miller
20087f1c57
Treat any tokens starting with _spectator as reserved 2022-03-12 19:17:38 -07:00
Michael Miller
5ad29f486f
Better formatting for empty arguments 2022-03-12 17:49:07 -07:00
Michael Miller
ca827a3c52
Initial work on null (object) double 2022-03-12 17:07:34 -07:00
Michael Miller
f82719746d
Stubable -> Stubbable 2022-03-12 15:45:48 -07:00
Michael Miller
7931847164
Cleanup and docs 2022-03-12 15:43:12 -07:00
Michael Miller
151926fd25
Fix captured arguments not handling splat 2022-03-12 14:31:39 -07:00
Michael Miller
ea46af00a6
Add method to define stubs 2022-03-12 14:31:19 -07:00
Michael Miller
123dd0efca
Obscure parameters to avoid accidental usage 2022-03-12 13:58:20 -07:00
Michael Miller
c8864b169e
Quick def methods should accept any args 2022-03-12 13:51:58 -07:00
Michael Miller
f497aeb79d
Fix redefinition of method with splat argument 2022-03-12 13:15:26 -07:00
Michael Miller
c1a14d1ad2
Add logging 2022-03-12 12:59:44 -07:00
Michael Miller
59f966d0e0
Fix missing double name
Rename internal method to _spectator_stubbed_name.
This is a better name for mocks and doubles.
Improve some handling around stubbed name.
2022-03-12 12:17:27 -07:00
Michael Miller
56f1c77a6c
Move method masking macro to Stubable
Rename to stub_all.
2022-03-12 11:56:54 -07:00
Michael Miller
85cc28c499
Utility method to define a method and a stub override 2022-03-12 11:41:47 -07:00
Michael Miller
8de457d80c
Handle previous_def and super - don't redefine method 2022-03-12 11:34:35 -07:00
Michael Miller
554e215898
Prevent ugly "has no type" errors 2022-03-12 11:33:56 -07:00
Michael Miller
12eeb02a8d
Move method redefinition for stub to Stubable
Require stub or raise UnexpectedMessage for all double methods.
Expose abstract_stub macro to require a stub.
2022-03-12 11:11:22 -07:00
Michael Miller
9b94245bd8
Stub's type might not match return type (subclass or union) 2022-03-12 11:08:58 -07:00
Michael Miller
d891422b3a
Don't need to recreate method by hand 2022-03-12 11:08:29 -07:00
Michael Miller
76c1d6a096
Some rework of stubs and doubles 2022-03-12 09:39:32 -07:00
Michael Miller
ae14a47329
Rename Response to Stub 2022-03-05 20:09:56 -07:00
Michael Miller
ced98778a4
Document and test MethodCall 2022-03-05 16:15:42 -07:00
Michael Miller
13f185b801
Capture and report arguments in exception message 2022-03-05 16:06:01 -07:00
Michael Miller
d16c55984c
Fixup macros 2022-03-05 15:39:19 -07:00
Michael Miller
2e15686d2d
Use internal arg name 2022-03-05 12:31:00 -07:00
Michael Miller
ec57105afd
Support response constraints
Reduce union expansion by casting at key points.
2022-03-05 12:21:31 -07:00
Michael Miller
2adc867843
Initial work on response constraints 2022-03-05 10:41:39 -07:00
Michael Miller
de7cd90d11
Add type capturing arguments 2022-03-04 17:16:45 -07:00
Michael Miller
d7f8c2b958
Cleanup and document double 2022-03-03 23:12:35 -07:00
Michael Miller
7e65d94cee
Match RSpec double name string 2022-03-03 22:41:18 -07:00
Michael Miller
9594604770
Obscure and disallow double name method 2022-03-03 22:34:57 -07:00
Michael Miller
5644d54470
Support double names 2022-03-03 22:31:40 -07:00
Michael Miller
26deea3d20
Initial work on Double 2022-03-03 22:10:09 -07:00
Michael Miller
d6112bd2dd
Rip out mocks 2022-01-26 12:41:41 -07:00
Michael Miller
f1f1659836
Fix adding stubs to class methods on mocked types 2021-10-11 23:03:50 -06:00
Michael Miller
5692a77a1b
Update naming from partial to target 2021-10-11 23:03:12 -06:00
Michael Miller
36354082c7
Pass arguments to original/fallback for stubbed top-level methods
Addresses https://github.com/icy-arctic-fox/spectator/issues/36
2021-09-28 19:02:42 -06:00
Michael Miller
6c55301d0b
Merge remote-tracking branch 'origin/master' into release/0.10 2021-07-11 00:16:25 -06:00
Michael Miller
4d030fb85a
Fix no_args being passed incorrectly 2021-07-10 22:43:54 -06:00
Michael Miller
d9bb8e33fe
Fix finding stubs on types (static methods) 2021-07-10 22:41:10 -06:00
Michael Miller
23fd075d51
Add stub when using expect().to receive()
This appears to match RSpec's behavior.
2021-07-10 20:28:51 -06:00
Michael Miller
384fb3093f
Allow stubs returning nil to be used on NoReturn calls 2021-07-10 20:19:10 -06:00
Michael Miller
e47ae613a5
Check for and allow expected exits 2021-07-10 19:27:01 -06:00
Michael Miller
294bd61a25
Don't force top-level namespace 2021-07-10 14:17:14 -06:00
Michael Miller
b07e13aeb4
Remove custom handling of NoReturn 2021-07-10 13:57:23 -06:00
Michael Miller
a395e26bae
Revert "Fix methods not being stubbed with expect().to receive syntax"
This reverts commit 1c865d1f4b.
2021-07-10 13:21:21 -06:00
Michael Miller
06f9f380c9
Workaround for stubbing NoReturn method with Nil
Fixes spec/issues/github_issue_29_spec.cr:15
2021-07-10 12:54:09 -06:00
Michael Miller
82f26dbb91
Don't return nil for NoReturn stubs
Raise instead, which prevents making the method's return type nilable.
2021-07-08 11:36:07 -06:00
Michael Miller
6a01ab3531
Merge master into release/0.10 2021-07-05 11:32:45 -06:00
Michael Miller
92dbfc2a8e
Avoid calling NoReturn methods 2021-07-02 21:52:56 -06:00
Michael Miller
1c865d1f4b
Fix methods not being stubbed with expect().to receive syntax 2021-07-02 21:45:16 -06:00
Michael Miller
83ab5f56f0
Use previous_def if method was defined in class 2021-07-02 21:01:41 -06:00
Michael Miller
e3f6fb3c7c
Add no_args support for stubs
Fixes https://github.com/icy-arctic-fox/spectator/issues/28
2021-07-02 18:43:39 -06:00
Michael Miller
e30d5c1981
Use multiple << on a single line 2021-05-30 14:21:42 -06:00
Michael Miller
74b78b7ca8
Rename Source to Location 2021-02-12 22:46:22 -07:00
Michael Miller
0f44403053
Fix namespaces preventing structs from being mocked 2021-01-30 21:11:51 -07:00
Michael Miller
b07dca697e
Remove seemingly unused context stubs code 2021-01-30 17:27:17 -07:00
Michael Miller
4500ebcddc
Update old references to Value and Block 2021-01-16 11:02:29 -07:00
Michael Miller
b91103b40a
Handle case where block is provided with return type 2020-11-07 11:04:03 -07:00
Michael Miller
8966c0976b
Allow manual specification of return type 2020-11-07 10:56:33 -07:00
Michael Miller
9ac6121201
New stub syntax to support operator methods
Works like:
  stub :[], index : Int32 { 42 }

Addresses https://github.com/icy-arctic-fox/spectator/issues/14
2020-10-01 18:23:28 -06:00
Michael Miller
e68697a212
Fix stubbed yielding method calling original instead of stub 2020-09-27 10:30:51 -06:00
Michael Miller
0970f02465
Yield instead of forwarding block
Forwarding the block like this captures it, which prevents yield from 
being used.
Addresses https://gitlab.com/arctic-fox/spectator/-/issues/54
2020-09-27 10:14:30 -06:00
Michael Miller
e2c2ebafd3 No need to specify arguments to super and previous_def 2020-04-28 08:30:40 -06:00
Michael Miller
68bac59cab Hacky solution for https://gitlab.com/arctic-fox/spectator/-/issues/44
Don't use a proc stub, which doesn't support arguments.
2020-03-28 11:41:49 -06:00
Chris Watson
e53b1167b3 Make sure superclass isn't nil 2020-03-20 16:34:37 +00:00
Michael Miller
e343a97f37 Don't use a splat here 2020-03-07 17:05:52 -07:00
Michael Miller
2aba67edae Fix idiosyncrasies around class method stubs
The new method misbehaves here.
It always appeared as defined, even though it isn't explicitly defined 
in the class.
2020-02-14 18:14:32 -07:00
Michael Miller
a7db9ff0bb Don't force previous_def on class methods 2020-02-10 17:46:36 -07:00
Michael Miller
061028782b Workaround for what seems to be a Crystal macro bug
The syntax:
stub instance.==(other) { true }
effectively becomes:
stub instance.==(other { true })
2020-01-01 21:48:58 -07:00
Michael Miller
8ccc08b472 Fix reflection (verify double) on self arg type
There is still an issue with using _ for a placeholder.
2019-12-13 20:25:18 -07:00
Michael Miller
16625cf495 Registry#expect takes a stub 2019-12-13 19:56:43 -07:00
Michael Miller
a38625f8a7 Remove GenericMethodCall
This type isn't needed. MethodCall is suitable after moving arguments to
it.
2019-12-13 19:54:06 -07:00
Michael Miller
a6aa00eb38 Fix double default stub with type 2019-12-13 19:17:08 -07:00
Michael Miller
4c9bbbe07b Formatting 2019-12-08 15:11:23 -07:00
Michael Miller
0ae5cdc9ae Remove debug 2019-12-08 15:11:08 -07:00
Michael Miller
1a30a56f2a Better stringified call 2019-12-08 15:10:49 -07:00
Michael Miller
91696d490f Check if underlying type responds to method 2019-12-08 15:08:13 -07:00
Michael Miller
067c789019 Initial work on type reflection for verifying doubles 2019-12-08 14:52:54 -07:00
Michael Miller
07ce835724 VerifyingDouble should be abstract 2019-12-08 14:51:48 -07:00
Michael Miller
8b0f509689 Detect when to use a verifying double 2019-12-08 11:37:08 -07:00
Michael Miller
8197a82ace Expect stubs not method names
Needed for argument syntax:
`expect(dbl).to receive(:foo).with(:bar)`
2019-11-29 09:25:58 -07:00
Michael Miller
c710961be1 Fix returned type inferred to NoReturn 2019-11-29 08:53:36 -07:00
Michael Miller
5d84536e1e Implement and_call_original 2019-11-20 19:17:51 -07:00
Michael Miller
f8563e86c7 Pass along original block 2019-11-20 16:29:06 -07:00
Michael Miller
2dee50f19e Pass original (and type constraint) to stub via block 2019-11-20 15:43:45 -07:00
Michael Miller
51a47fec98 Remove unused RT parameter 2019-11-20 15:05:04 -07:00
Michael Miller
27acf7bced Allow receive block syntax 2019-11-19 17:28:00 -07:00
Michael Miller
b062472d98 Revert "Initial support for and_yield"
This reverts commit e49bd0d57a.

Removing support for stubbed yield.
Attempting to yield with different a different arity or argument types causes compilation errors.
I don't see any easy fix for this.
I would rather have no yield support than broken support.
2019-11-17 13:30:09 -07:00
Michael Miller
e49bd0d57a Initial support for and_yield 2019-11-17 13:12:39 -07:00
Michael Miller
b896a7f1d5 Implement stub that raises 2019-11-17 12:53:18 -07:00
Michael Miller
6e287f864b Implement returning multiple values 2019-11-17 12:26:02 -07:00
Michael Miller
5da231a5e8 Check expectations for anonymous double 2019-11-17 10:14:03 -07:00
Michael Miller
ac9b3ad1fe Implement expect_any_instance_of 2019-11-16 21:27:18 -07:00
Michael Miller
201fe614d1 Expect messages on double, but don't stub them 2019-11-16 10:59:13 -07:00