Resolved all the TODOs in test/controls.c I care to right now. I still need to write tests for the other failure conditions that aren't tested yet.
This commit is contained in:
parent
9b1be0c77a
commit
dfd045b3cc
|
@ -81,7 +81,7 @@ This function is meant for control implementations to use in the implementation
|
|||
|
||||
It is a programmer error to pass an invalid value for either `type` or `initData`.
|
||||
|
||||
**For control implementations**: This function allocates both the `uiControl` and the memory for the implementation data, and then passes both of these allocations as well as the value of `initData` into your `Init()` method. Return `false` from the `Init()` method if `initData` is invalid; if it is valid, initialize the control and return `true`. To discourage direct use of `uiNewControl()`, you should generally not allow `initData` to be `NULL`, even if there are no parameters. Do **not** return `false` for any other reason, including other forms of initialization failures; see [Error handling](error-handling.md) for details on what to do instead.
|
||||
**For control implementations**: This function allocates both the `uiControl` and the memory for the implementation data, and then passes both of these allocations as well as the value of `initData` into your `Init()` method. Before calling `Init()`, libui will clear the `implData` memory, as with `memset(0)`. Return `false` from the `Init()` method if `initData` is invalid; if it is valid, initialize the control and return `true`. To discourage direct use of `uiNewControl()`, you should generally not allow `initData` to be `NULL`, even if there are no parameters. Do **not** return `false` for any other reason, including other forms of initialization failures; see [Error handling](error-handling.md) for details on what to do instead.
|
||||
|
||||
### `uiControlFree()`
|
||||
|
||||
|
@ -107,7 +107,7 @@ uiprivExtern void uiControlSetParent(uiControl *c, uiControl *parent);
|
|||
|
||||
This function is used by the implementation of a container control to actually establish a parent-child relationship from libui's point of view. This function is only intended to be called by control implementations. You should not call it directly; instead, use the methods provided by your container control to add children.
|
||||
|
||||
This function can only be used to set the parent of an unparented control or to remove its parent. It may not be used to change the parent of an already parented control. It is a programmer error to set the parent of a control that already has a parent to something other than `NULL`, or to set the parent of a control with no parent to `NULL`. (The idea here is to reinforce the concept of container implementations being responsible for setting their children properly, not the user.)
|
||||
This function can only be used to set the parent of an unparented control or to remove its parent. It may not be used to change the parent of an already parented control. It is a programmer error to set the parent of a control that already has a parent to something other than `NULL` (even if to the same parent), or to set the parent of a control with no parent to `NULL`. (The idea here is to reinforce the concept of container implementations being responsible for setting their children properly, not the user.)
|
||||
|
||||
It is a programmer error to pass `NULL` or a non-control for `c`.
|
||||
|
||||
|
|
|
@ -2,6 +2,37 @@
|
|||
#include "test.h"
|
||||
#include "../common/testhooks.h"
|
||||
|
||||
static bool vtableNopInit(uiControl *c, void *implData, void *initData)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static void vtableNopFree(uiControl *c, void *implData)
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
|
||||
// TODO we'll have to eventually find out for real if memset(0) is sufficient to set pointers to NULL or not; C99 doesn't seem to say
|
||||
Test(ControlImplDataIsClearedOnNewControl)
|
||||
{
|
||||
char memory[32];
|
||||
uiControlVtable vt;
|
||||
uint32_t type;
|
||||
uiControl *c;
|
||||
char *implData;
|
||||
|
||||
vt.Size = sizeof (uiControlVtable);
|
||||
vt.Init = vtableNopInit;
|
||||
vt.Free = vtableNopFree;
|
||||
type = uiRegisterControlType("TestControl", &vt, testOSVtable(), sizeof (memory));
|
||||
c = uiNewControl(type, NULL);
|
||||
implData = (char *) uiControlImplData(c);
|
||||
memset(memory, 0, sizeof (memory));
|
||||
if (memcmp(implData, memory, sizeof (memory)) != 0)
|
||||
TestErrorf("control impl data memory not properly cleared on creation");
|
||||
uiControlFree(c);
|
||||
}
|
||||
|
||||
struct counts {
|
||||
unsigned int countInit;
|
||||
unsigned int countFree;
|
||||
|
@ -14,8 +45,6 @@ struct testImplData {
|
|||
static struct counts failInit;
|
||||
static void *testControlFailInit = &failInit;
|
||||
|
||||
// TODO document that impl data is zero-initialized before this is called
|
||||
// TODO we'll also have to eventually deal with the fact that NULL is not required to be 0... or at least confirm that
|
||||
static bool testVtableInit(uiControl *c, void *implData, void *initData)
|
||||
{
|
||||
struct testImplData *d = (struct testImplData *) implData;
|
||||
|
@ -323,7 +352,7 @@ Test(RemovingParentFromExplicitlyParentlessControlIsProgrammerError)
|
|||
endCheckProgrammerError(ctx);
|
||||
}
|
||||
|
||||
Test(ReparentingAlreadyParentedControlIsProgrammerError)
|
||||
Test(ReparentingAlreadyParentedControlToDifferentParentIsProgrammerError)
|
||||
{
|
||||
uiControl *c, *d, *e;
|
||||
void *ctx;
|
||||
|
@ -347,7 +376,28 @@ Test(ReparentingAlreadyParentedControlIsProgrammerError)
|
|||
endCheckProgrammerError(ctx);
|
||||
}
|
||||
|
||||
// TODO document and then test the above but for the same parent
|
||||
Test(ReparentingAlreadyParentedControlToSameParentIsProgrammerError)
|
||||
{
|
||||
uiControl *c, *d;
|
||||
void *ctx;
|
||||
|
||||
ctx = beginCheckProgrammerError("uiControlSetParent(): cannot set a control with a parent to have another parent");
|
||||
|
||||
c = uiNewControl(testControlType(), NULL);
|
||||
d = uiNewControl(testControlType(), NULL);
|
||||
|
||||
// this should fail
|
||||
uiControlSetParent(c, d);
|
||||
uiControlSetParent(c, d);
|
||||
|
||||
// this should not (cleanup)
|
||||
uiControlSetParent(c, NULL);
|
||||
// TODO make sure all cleanups are in reverse order
|
||||
uiControlFree(d);
|
||||
uiControlFree(c);
|
||||
|
||||
endCheckProgrammerError(ctx);
|
||||
}
|
||||
|
||||
Test(GettingImplDataOfNullControlIsProgrammerError)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue