mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
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.
This commit is contained in:
parent
003be48591
commit
e05ef94ef5
4 changed files with 48 additions and 0 deletions
11
spec/spectator/dsl/mocks/stubs_spec.cr
Normal file
11
spec/spectator/dsl/mocks/stubs_spec.cr
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
require "../../../spec_helper"
|
||||||
|
|
||||||
|
Spectator.describe "Stubs DSL" do
|
||||||
|
double(:foobar, foo: 42, bar: "baz")
|
||||||
|
let(dbl) { double(:foobar) }
|
||||||
|
|
||||||
|
specify do
|
||||||
|
allow(dbl).to receive(:foo).and_return(123)
|
||||||
|
expect(dbl.foo).to eq(123)
|
||||||
|
end
|
||||||
|
end
|
|
@ -127,5 +127,13 @@ module Spectator::DSL
|
||||||
macro double(**value_methods)
|
macro double(**value_methods)
|
||||||
::Spectator::LazyDouble.new({{**value_methods}})
|
::Spectator::LazyDouble.new({{**value_methods}})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
macro allow(stubbable)
|
||||||
|
::Spectator::Allow.new({{stubbable}})
|
||||||
|
end
|
||||||
|
|
||||||
|
macro receive(method)
|
||||||
|
::Spectator::NullStub.new({{method.id.symbolize}})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
14
src/spectator/mocks/allow.cr
Normal file
14
src/spectator/mocks/allow.cr
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
require "./stub"
|
||||||
|
require "./stubbable"
|
||||||
|
|
||||||
|
module Spectator
|
||||||
|
struct Allow(T)
|
||||||
|
def initialize(@target : T)
|
||||||
|
{% raise "Target of `allow` must be stubbable (a mock or double)." unless T < Stubbable %}
|
||||||
|
end
|
||||||
|
|
||||||
|
def to(stub : Stub) : Nil
|
||||||
|
@target._spectator_define_stub(stub)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
15
src/spectator/mocks/null_stub.cr
Normal file
15
src/spectator/mocks/null_stub.cr
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
require "./typed_stub"
|
||||||
|
require "./value_stub"
|
||||||
|
|
||||||
|
module Spectator
|
||||||
|
# Stub that does nothing and returns nil.
|
||||||
|
class NullStub < TypedStub(Nil)
|
||||||
|
def value : Nil
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def and_return(value)
|
||||||
|
ValueStub.new(method, value, constraint)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue