<alex-guenther>
Hi, is there a way to stub a C function defined with 'fun'?
<asterite>
You mean, for testing?
<asterite>
@alex-guenther or what do you mean by stubbing here?
<alex-guenther>
Yes exactly
<alex-guenther>
I would like to test what happens, when things go wrong
<asterite>
what’s the C function?
<alex-guenther>
in this case it's sqlite3_prepare_v2
<asterite>
and when do things go wrong?
<asterite>
(the answer is no, you can’t stub in crystal)
<asterite>
but I’m asking because maybe you can test the connection directly without stubbing anything
<alex-guenther>
well in this case I don't know when things go wrong... docs say when things go wrong there will be an return value != SQLITE_OK
<asterite>
Ah, I see…
<asterite>
I wouldn’t test that case and just raise an exception in that case
<alex-guenther>
well I was trying to test that I rise the exception in that case...
<asterite>
It’s ok for us if you don’t test that case
<asterite>
we try to test as much as possible, but not exhaustively
<asterite>
(but if you do want to test it, I can’t imagine a way to do it right now :()
<alex-guenther>
I just realized, that approaching testing the same way as in ruby, might be not the best idea
<asterite>
Yes, it’s harder to test in statically compiled languages
<asterite>
well, in non-dynamic languages, without VM or runtime modification of classes
<asterite>
maybe eventually we’ll have that, maybe… but right now we are very far from that
<alex-guenther>
I was reading 'working effectively with legacy code' there were some examples of preprocessor, linking and compilation seams. Maybe it's possible to replace it during compile time?
<asterite>
replace the fun?
<asterite>
the problem is that it will replaced for that executable for any invocation
<asterite>
(unless that’s what you want to do in your tests)
<alex-guenther>
Could I compile 2 executeables, or one that has a switch what to do...
<asterite>
You could do that with ifdef, yes
<asterite>
but since you can’t redefine a fun, you could (for now) put it behind a method in a module
<asterite>
or just a def where you are coding
<asterite>
class Sqlite; def prepare(…); ifdef test; my_prepare; else; LibSqlite.prepare(…) end end
<asterite>
to get that “test” be true at compile time, for now the only way is to do —cross-compile “test”
<asterite>
but you also have to pass other “flags”, like “linux” and “x86"
<asterite>
we will add another flag, something like —flags “test” eventually
waj has joined #crystal-lang
<alex-guenther>
This could work for now...
<asterite>
yes, in fact it could… if you need to run specs with the original definition and specs with the stubbed definition you could run the specs twice with different flags
<asterite>
I don’t think that’s a bad idea… it won’t be used much, just for testing C bindings with stubbed funcionality
<alex-guenther>
Thanks I'll give it a try
<alex-guenther>
I will try it as soon as I can. See you then. Bye
<asterite>
Bye, take care
<alex-guenther>
And thanks, It's really fun to do stuff with crystal.