Page 9 of 15
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 4:46 am
by Chris Corbyn
arjan.top wrote:chris I think your code is not ok
For example if you have this array:
Code: Select all
array(
'foo' => array(
'bar' => array(
'zip' => 'some value'
)
),
'bar2' => array(
'zip2' => 'some other value'
)
)
... and you want foo/bar2/zip2
There's no foo/bar2/zip2 in your code

There's "foo/bar/zip" and "bar2/zip2", but they are on separate branches. For foo/bar2/zip2 to exist $array['foo']['bar2']['zip2'] needs to exist.
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 4:50 am
by matthijs
hey, but in your test you ask for
, while in the other test it is
If you try
you get a failing test.
[edit]To prevent confusion, I mean;
Code: Select all
public function testGettingValueOnSecondPrimaryBranch() {
$storageModel = $this->_createStorageModel();
$storageModel->setReturnValue('load', array(
'foo' => array(
'bar' => array(
'zip' => 'some value'
)
),
'bar2' => array(
'zip2' => 'some other value'
)
));
$dataStore = $this->_createDataStore($storageModel);
$this->assertEqual('some other value', $dataStore->get('foo/bar2/zip2'));
}
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 4:52 am
by Chris Corbyn
Of course I do. It should fail. Where is foo/bar2/zip2 in that array?
The only paths in that array are foo/bar/zip AND bar2/zip2.
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 4:54 am
by arjan.top
Code: Select all
$this->assertEqual('some other value', $dataStore->get('bar2/zip2'));
foo is missing here
EDIT:
oops new page already, missed some posts ...
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 4:55 am
by Chris Corbyn
Just look at it in it's array form. To get foo/bar2/zip2 we'd need:
Code: Select all
array(
'foo' => array(
'bar2' => array(
'zip2' => 'something'
)
),
'bar' => array(
'zip' => 'some value'
)
)
),
'bar2' => array(
'zip2' => 'some other value'
)
);

The test is incorrect
EDIT | Please see my first post on Page 9 of this discussion

Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 4:57 am
by arjan.top
Yes I see, I made I mistake

Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 5:00 am
by matthijs
Arggh, I saw that too just now
Somehow it's very difficult to count and match brackets...
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 5:00 am
by Chris Corbyn
arjan.top wrote:Yes I see, I made I mistake

That's fine. We need to keep thinking of things which aren't right and then adding a test to prove it. Just happens in this case that the thing you though wasn't right was a result of a little confusion

3 heads are better than 1.
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 5:01 am
by Chris Corbyn
matthijs wrote:Arggh, I saw that too just now
Somehow it's very difficult to count and match brackets...
Exactly the reason I always write multidimensional array as indented nests

The trick is to look up and down vertically.
Look at the veritcal alignment of 'foo' in the array, then look directly down from it; you hit 'bar2', so that mean it's in the same dimension

Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 5:01 am
by arjan.top
matthijs wrote:Arggh, I saw that too just now
Somehow it's very difficult to count and match brackets...
I just looked at how brackets are aligned and I added array in the wrong place

Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 5:02 am
by matthijs
So, if everybody's got his or her brackets aligned again, what's next?

Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 5:03 am
by Chris Corbyn
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 5:05 am
by Chris Corbyn
Right. Sit down and stop talking class!
How's about we make sure that the array we're passing to save() is what we want to pass? So far we haven't validated what is passed to it. This is a new one for you probably matthijs since it's an extra parameter on $storageModel->expectOnce('save', ...).
Just two tests will do. One for a single value in a one dimensional array, then one for our more complex multidimensional array.
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 5:20 am
by matthijs
Maybe you have to explain a bit more about what you expect save to do. I don't entirely understand it. Isn't the storageModel supposed to validate the data?
It's probably simple, but maybe my idea of how the classes work doesn't match up with yours.
Re: TDD workshop. Anybody welcome; Data storage abstraction.
Posted: Sun Apr 27, 2008 5:38 am
by Chris Corbyn
matthijs wrote:Maybe you have to explain a bit more about what you expect save to do. I don't entirely understand it. Isn't the storageModel supposed to validate the data?
It's probably simple, but maybe my idea of how the classes work doesn't match up with yours.
Definitely worth further explanation. We all need to be clear what we're doing. In my opinion, save() should accept an array in exactly the same format as the array provided by load(). When that array is modified (by set() or remove()) the array passed to save() should reflect that modification.
We don't care what happens to the internal array ($this->_store) when set() or remove() are called. We don't even care that such an internal array exists. What we do care about is that save() receives integral data.
I can write a really simple first test if you like? Really simple!
Code: Select all
public function testSavingSingleValuePassesSimpleArray() {
$storageModel = $this->_createStorageModel();
$storageModel->expectOnce('save', array( array('zip' => 'button') ));
$dataStore = $this->_createDataStore($storageModel);
$dataStore->set('zip', 'button');
$dataStore->save();
}
To explain that second parameter on expectOnce(). It takes an array of expected parameters. So in this case case expect one parameter which is an array itself (hence the doubly nested array which looks confusing).