State machine compromise

14 views
Skip to first unread message

Julien

unread,
Apr 12, 2009, 9:02:38 AM4/12/09
to gmock-dev
We had a discussion around the possibility of having a state machine.
It seems that it was a bit overkill for what we are doing but we could
think of a compromise.

If we had a callback facility we could change object behavior when an
expectation is matched, or do whatever we want

mockFoo.aMethod("foo").returns("bar").exec { println "aMethod is
called" }

Johnny

unread,
Apr 12, 2009, 9:19:52 AM4/12/09
to gmoc...@googlegroups.com
How does this "exec" method implement a state machine? Could you give an
example? And what's the difference between "exec" method and "answer"
method?

在 2009-04-12日的 06:02 -0700,Julien写道:

Julien Gagnet

unread,
Apr 12, 2009, 9:27:41 AM4/12/09
to gmoc...@googlegroups.com
The main difference I guess with the answer method is that we don't
returns the result of the execution in the code under test. The answer
method replace the returns one.

I was thinking we that if we can get callback when and expectation is
match then we could change the state of the concrete object.

2009/4/12 Johnny <johnny...@gmail.com>:

Johnny

unread,
Apr 12, 2009, 9:38:15 AM4/12/09
to gmoc...@googlegroups.com
I think this 'exec' method is equal to the 'then' method in state
machine, but it cannot implement the functionality of the 'when' method.

在 2009-04-12日的 14:27 +0100,Julien Gagnet写道:

Johnny Jian

unread,
Apr 12, 2009, 9:51:41 AM4/12/09
to gmock-dev
If we implement both of the 'then' and 'when' methods, we actually
implement the whole state machine.

On 4月12日, 下午9时38分, Johnny <johnnyjia...@gmail.com> wrote:
> I think this 'exec' method is equal to the 'then' method in state
> machine, but it cannot implement the functionality of the 'when' method.
>
> 在 2009-04-12日的 14:27 +0100,Julien Gagnet写道:
>
> > The main difference I guess with the answer method is that we don't
> > returns the result of the execution in the code under test. The answer
> > method replace the returns one.
>
> > I was thinking we that if we can get callback when and expectation is
> > match then we could change the state of the concrete object.
>
> > 2009/4/12 Johnny <johnnyjia...@gmail.com>:

Julien Gagnet

unread,
Apr 12, 2009, 10:08:59 AM4/12/09
to gmoc...@googlegroups.com
You are right. I think we should do it if we've got time to spend on
it then. And surely after 1.0 to keep the buzz high around gmock.

2009/4/12 Johnny Jian <Johnny...@gmail.com>:

Johnny

unread,
Apr 12, 2009, 10:38:17 AM4/12/09
to gmoc...@googlegroups.com
I think we can have some powerful and flexible state machine
functionalities here, and we don't even need any classes for states,
just string is enough:

def state = 'off'
mock.a().returns(1).then { state = 'on' }
mock.b().returns(2).when { state == 'on' }

There are many other possibilities with these 'then' and 'when'
methods :)
Reply all
Reply to author
Forward
0 new messages