Funky code of the day
Funky code of the day
type
ICalculator = interface(IInvokable)
function Add(a, b: Integer): Integer;
end;
...
var
calculator: ICalculator;
begin
calculator := Mock.Create;
calculator.Add(Arg.IsAny, Arg.IsAny).Returns(
function(const x: TCallInfo): TValue
begin
Result := x[0].AsInteger + x[1].AsInteger;
end);
CheckEquals(3, calculator.Add(1, 2));
end;
type
ICalculator = interface(IInvokable)
function Add(a, b: Integer): Integer;
end;
...
var
calculator: ICalculator;
begin
calculator := Mock
calculator.Add(Arg.IsAny
function(const x: TCallInfo): TValue
begin
Result := x[0].AsInteger + x[1].AsInteger;
end);
CheckEquals(3, calculator.Add(1, 2));
end;
o.0 - but why?
ReplyDeleteBecause NSubstitute can do
ReplyDeleteMy old school team mates would rip my hat of and sh*t into my neck if I'll give them such utterly complex implementations for a simple addition... :-D
ReplyDeleteUdo Sommer If it just were an addition and not the inline definition of the behavior of a mock which you otherwise would have to write a class for (and implement all the other methods that ICalculator would probably have but are irrelevant for this particular test).
ReplyDeleteStefan Glienke
ReplyDeleteDon't get me wrong, I have seen this. But they would ask "Why? Simply implement a class.".
Different generations and colliding worlds.
I like funky things. Especially if I don't have to write more code than needed.
Udo Sommer I am not arguing with straw men ;)
ReplyDeletePerhaps it is the example which bends the mind to think that this is a too complex solution for a simple problem. It's also appears to be error prone as you do an explicit cast in the argument retrieval.
ReplyDeleteLars Fosdal I used the same example as in the NSubstitute wiki
ReplyDeleteAnd what else can the arguments be if the signature takes two integers?
Better type deduction for generic parameters would likely make this a lot neater. Oh one can wish...
ReplyDeleteThat is, I like the idea, but just looks clunky compared to what it could have been.
ReplyDeleteAsbjørn Heid oh yes, I fully agree with that. But I doubt they will make it any easier to read soon. Which means: deal with it and the compiler bugs that go with it (:
ReplyDelete