im quite familar with Mocktio. But never needed something like this before:
Within my Test i like to verify that a method gets called with several values that represent a kind of "loading started". lets say "loading started" and "loading finished with result T". The order of those state change matters!
So, in pseudo code it would do something like this:
resultObject ...
clientcode.requestDataload()
// Order of below state changes is imporant
verifyThat(resultObject.data).wasCalledWith("loading started")
verifyThat(resultObject.data).wasCalledWith("loading finished with result T")
Thanks for pointing me into some direction.
You can use the verifiy method from Mockito:
verify(mockInstance, times(1)).method(ArgumentMatchers.eq("what you want"))
where method is the method that the call need to be checked, and the ArgumentMatchers are the arguments passed to the method for the call (you can use any() too).
You can use InOrder:
val inOrder = inOrder(yourMock)
inOrder.verify(yourMock).yourMethod("loading started")
inOrder.verify(yourMock).yourMethod("loading finished with result T")
Related
I understand that this may be a duplicate of Any equivalent of Ruby's public_send method?. I'd like to explain what I am doing, and perhaps someone could advice.
I've been porting a ruby app over the last few days to learn Crystal. I've had to cut out a lot of functionality due to lack of send but today I've hit a main artery in my program.
I have a Hash which contains keystroke as key, and method as value. Based on what key is struck, the appropriate method is called. This obviously uses the send to implement the same.
From the linked question, I understand that Crystal is compiled so dynamic method calls are not permitted. However, if you look at the Vim editor, a user can map keys to methods, too. And vi(m) is written in C.
I am wondering if I missed something.
I know I could probably hardcode a switch statement with each key and call the appropriate method, but that still does not allow the user to assign a key to a method. Is there any alternative to this very large switch-case method ?
(I am guessing that rather than check the key in the when part, I would check the binding and call the method.
binding = #bindings[key]
case binding
when :up
up
when :down
down
when .....
else
end
Any better solution ?
I'm not sure that this way most simple and convenient (perhaps more experienced developers will correct me below) but i would use the Proc:
def method1
puts "i'm method1"
end
def method2
puts "i'm method2"
end
def method3
puts "i'm method3"
end
hash = {
"ctrl": -> { method1 },
"shift": -> { method2 },
"alt": -> { method3 }
}
binding = ["ctrl", "shift", "alt"].sample
hash[binding].call #=> i'm method2
See working example
We have a business need to collect specific bindings in every JSF page. and we do that inside overridden ViewHandlerWrapper class
I use the below code inside renderView method to get the whole expression value property for every RichInputText and it's work fine for me
ValueExpression valExp = Inputcomponent.getValueExpression("value");
String ExpressionString = valExp.getExpressionString();
output was: #{binding.EmployeeId.inputValue}
When I do the same against RichButtin I got null value as following:
ValueExpression valExp = Btncomponent.getValueExpression("actionlistener");
String ExpressionString = valExp.getExpressionString();
What is the wrong in my last peace of code?
Obtaining a ValueExpression form a RichInputText works because, as the name suggests, it evaluates to a value, which may or may not be an EL expression, let alone a method.
On the other hand, a RichButton does not really have to evaluate to something; rather, it aims to invoke behavior (i.e. a method), from which you would want a MethodExpression - though in this case, the closest we get to it is a MethodBinding.
Luckily, UIXCommand, a superclass of RichButton, provides two methods from which you can obtain your action listeners:
public final MethodBinding getActionListener()
From the MethodBinding returned, you can invoke getExpressionString() so you can get what you wanted - such as some actionListener EL string like #{binding.bean.actionListenerMethod}.
public final ActionListener[] getActionListeners()
Might be worth mentioning, though there is not much merit for this in your use case. It simply returns the listeners on which you can manually process the events.
I have some expectations like EXPECT_CALL (...)
EXPECT_CALL(t1, foo()).Times(1);
I want to create the oposite.
I expect that a certain function won't be executed.
What is the method I should use?
Something like EXPECT_NOT_CALL (...) ?
In GTest something similar to EXPECT_NOT_CALL doesn't exist however, there are several options to receive this behaviour:
1.Create a StrictMock. In StrictMock any unexpected called cause a failure.
2.Use .Times(0):
EXPECT_CALL(t1, foo()).Times(0);
In this option you use the count mechanism however, it checks if the value is 0.(so any call leads a failure...)
3.Use method 2 and create a Macro:
#define EXPECT_NOT_CALL(a,b) EXPECT_CALL(a, b).Times(0);
When using MockFor, how can I get it to verify that a method was invoked at least n times? I've tried ignoring the method call after setting up a demand, like this:
import groovy.mock.interceptor.MockFor;
import org.junit.Test
class FilterTest {
interface Filter {
boolean isEnabled()
}
#Test
public void test() {
MockFor mockContext = new MockFor(Filter)
// Expect at least one call
mockContext.demand.isEnabled {true}
mockContext.ignore.isEnabled {false}
// Obtaining a usuable mock instance
def filter = mockContext.proxyInstance()
// Fake calling the method
filter.isEnabled()
filter.isEnabled()
// Verify invoked at least once?
mockContext.verify(filter)
}
}
However, I get an assertion failure:
junit.framework.AssertionFailedError: verify[0]: expected 1..1 call(s) to
'isEnabled' but was called 0 time(s).
You can't combine "demand" and "ignore" in that way, since the "ignore" statement overrides the "demand" statement.
Instead, you can specify the valid range like this:
mockContext.demand.isEnabled(1..10) {true}
which will accept 1 to 10 number of invokations (but not zero or eleven or more).
I'm not aware of any way to specify an open ended upper bound in ranges, though, which you imply that you need when you say "at least n times".
In most practical situations, I believe you can get away with specifying a sufficiently large upper bound (like 100).
EDIT: Removed the "hack" suggestion (it didn't work as I expected)
I'm working on a game engine in C++ using Lua for NPC behaviour. I ran into some problems during the design.
For everything that needs more than one frame for execution I wanted to use a linked list of processes (which are C++ classes). So this:
goto(point_a)
say("Oh dear, this lawn looks really scruffy!")
mowLawn()
would create a GotoProcess object, which would have a pointer to a SayProcess object, which would have a pointer to a MowLawnProcess object. These objects would be created instantly when the NPC is spawned, no further scripting needed.
The first of these objects will be updated each frame. When it's finished, it will be deleted and the next one will be used for updating.
I extended this model by a ParallelProcess which would contain multiple processes that are updated simultaneously.
I found some serious problems. Look at this example: I want a character to walk to point_a and then go berserk and just attack anybody who comes near. The script would look like that:
goto(point_a)
while true do
character = getNearestCharacterId()
attack(character)
end
That wouldn't work at all with my design. First of all, the character variable would be set at the beginning, when the character hasn't even started walking to point_a. Then, then script would continue adding AttackProcesses forever due to the while loop.
I could implement a WhileProcess for the loop and evaluate the script line by line. I doubt this would increase readability of the code though.
Is there another common approach I didn't think of to tackle this problem?
I think the approach you give loses a lot of the advantages of using a scripting language. It will break with conditionals as well as loops.
With coroutines all you really need to do is:
npc_behaviour = coroutine.create(
function()
goto(point_a)
coroutine.yield()
say("Oh dear, this lawn looks really scruffy!")
coroutine.yield()
mowLawn()
coroutine.yield()
end
)
goto, say and mowLawn return immediately but initiate the action in C++. Once C++ completes those actions it calls coroutine.resume(npc_behaviour)
To avoid all the yields you can hide them inside the goto etc. functions, or do what I do which is have a waitFor function like:
function waitFor(id)
while activeEvents[id] ~= nil do
coroutine.yield()
end
end
activeEvents is just a Lua table which keeps track of all the things which are currently in progress - so a goto will add an ID to the table when it starts, and remove it when it finishes, and then every time an action finishes, all coroutines are activated to check if the action they're waiting for is finished.
Have you looked at Finite State Machines ? If I were you I wouldn't use a linked list but a stack. I think the end result is the same.
stack:push(action:new(goto, character, point_a))
stack:push(action:new(say, character, "Oh dear, this lawn was stomped by a mammoth!"))
stack:push(action:new(mowLawn, character))
Executing the actions sequentially would give something like :
while stack.count > 0 do -- do all actions in the stack
action = stack:peek() -- gets the action on top of the stack
while action.over ~= true do -- continue action until it is done
action:execute() -- execute is what the action actually does
end
stack:pop() -- action over, remove it and proceed to next one
end
The goto and other functions would look like this :
function goto(action, character, point)
-- INSTANT MOVE YEAH
character.x = point.x
character.y = point.y
action.over = true -- set the overlying action to be over
end
function attack(action, character, target)
-- INSTANT DEATH WOOHOO
target.hp = 0
action.over = true -- attack is a punctual action
end
function berserk(action, character)
attack(action, character, getNearestCharacterId()) -- Call the underlying attack
action.over = false -- but don't set action as done !
end
So whenever you stack:push(action:new(berserk, character)) it will loop on attacking a different target every time.
I also made you a stack and action implementation in object lua here. Haven't tried it. May be bugged like hell. Good luck with your game !
I don't know the reasons behind you design, and there might be simpler / more idiomatic ways to it.
However, would writing a custom "loop" process that would somehow take a function as it's argument do the trick ?
goto(point_a)
your_loop(function ()
character = getNearestCharacterId()
attack(character)
end)
Since Lua has closures (see here in the manual), the function could be attached to your 'LoopProcess', and you call this same function at each frame. You would probably have to implement your LoopProcess so that that it's never removed from the process list ...
If you want your loop to be able to stop, it's a bit more complicated ; you would have to pass another function containing the test logic (and again, you LoopProcess would have to call this every frame, or something).
Hoping I understood your problem ...