From 35217e0ccb716eb0e155b43b2d5033646886626b Mon Sep 17 00:00:00 2001 From: Nicola Fontana Date: Tue, 28 Apr 2020 21:54:15 +0200 Subject: [PATCH] adg: table cell testing More tests on AdgTableCell. Improved code to be more compliant with the default behavior of the other entities. --- src/adg/adg-table-cell.c | 32 +++++-- src/adg/tests/test-table-cell.c | 201 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 225 insertions(+), 8 deletions(-) diff --git a/src/adg/adg-table-cell.c b/src/adg/adg-table-cell.c index 9a669d3b..372dd7a9 100644 --- a/src/adg/adg-table-cell.c +++ b/src/adg/adg-table-cell.c @@ -201,8 +201,11 @@ adg_table_cell_new_before(AdgTableCell *before_cell) AdgTableCell * adg_table_cell_new_with_width(AdgTableRow *table_row, gdouble width) { - AdgTableCell *table_cell = adg_table_cell_new(table_row); + AdgTableCell *table_cell; + + g_return_val_if_fail(width >= 0, NULL); + table_cell = adg_table_cell_new(table_row); if (table_cell != NULL) adg_table_cell_set_width(table_cell, width); @@ -236,8 +239,11 @@ adg_table_cell_new_full(AdgTableRow *table_row, gdouble width, const gchar *name, const gchar *title, gboolean has_frame) { - AdgTableCell *table_cell = adg_table_cell_new(table_row); + AdgTableCell *table_cell; + + g_return_val_if_fail(width >= 0, NULL); + table_cell = adg_table_cell_new(table_row); if (table_cell == NULL) return NULL; @@ -266,6 +272,8 @@ adg_table_cell_new_full(AdgTableRow *table_row, gdouble width, void adg_table_cell_dispose(AdgTableCell *table_cell) { + g_return_if_fail(table_cell != NULL); + _adg_cell_set_title(table_cell, NULL); _adg_cell_set_value(table_cell, NULL); } @@ -281,7 +289,11 @@ adg_table_cell_dispose(AdgTableCell *table_cell) void adg_table_cell_free(AdgTableCell *table_cell) { - AdgTableRow *table_row = table_cell->row; + AdgTableRow *table_row; + + g_return_if_fail(table_cell != NULL); + + table_row = table_cell->row; if (table_cell->row != NULL) { AdgTable *table = adg_table_row_get_table(table_row); @@ -337,7 +349,7 @@ adg_table_cell_get_table(AdgTableCell *table_cell) /** * adg_table_cell_set_title: * @table_cell: a valid #AdgTableCell - * @title: the new title entity + * @title: (nullable): the new title entity * * Sets @title as the new title entity of @table_cell. The top left * corner of the bounding box of @title will be cohincident to @@ -453,7 +465,8 @@ adg_table_cell_title(AdgTableCell *table_cell) * not NULL) is referenced with * g_object_ref_sink(). * - * @value can be NULL, in which case the old entity is removed. + * @value can be NULL, in which case the old entity + * is removed. * * Since: 1.0 **/ @@ -470,12 +483,15 @@ adg_table_cell_set_value(AdgTableCell *table_cell, AdgEntity *value) /** * adg_table_cell_set_text_value: * @table_cell: a valid #AdgTableCell - * @value: a text string + * @value: (nullable): a text string * * Convenient function to set a the value of a cell using an #AdgToyText * entity with a value font dress picked from #AdgTable:table-dress with * a call to adg_table_style_get_value_dress(). * + * @value can be NULL, in which case the old entity + * is removed. + * * Since: 1.0 **/ void @@ -490,8 +506,10 @@ adg_table_cell_set_text_value(AdgTableCell *table_cell, const gchar *value) g_return_if_fail(table_cell != NULL); - if (value == NULL) + if (value == NULL) { adg_table_cell_set_value(table_cell, NULL); + return; + } if (table_cell->value) { gchar *old_value; diff --git a/src/adg/tests/test-table-cell.c b/src/adg/tests/test-table-cell.c index b71fe80b..4d40bdda 100644 --- a/src/adg/tests/test-table-cell.c +++ b/src/adg/tests/test-table-cell.c @@ -22,6 +22,198 @@ #include +static void +_adg_behavior_misc(void) +{ + AdgTable *table; + AdgTableRow *row; + AdgTableCell *cell; + + table = adg_table_new(); + row = adg_table_row_new(table); + + /* Sanity check */ + g_assert_null(adg_table_cell_dup(NULL)); + g_assert_null(adg_table_cell_new(NULL)); + g_assert_null(adg_table_cell_new_before(NULL)); + g_assert_null(adg_table_cell_new_with_width(NULL, 123)); + g_assert_null(adg_table_cell_new_full(NULL, 123, NULL, NULL, FALSE)); + g_assert_null(adg_table_cell_get_row(NULL)); + g_assert_null(adg_table_cell_get_table(NULL)); + adg_table_cell_dispose(NULL); + adg_table_cell_free(NULL); + + /* Check default constructor */ + cell = adg_table_cell_new(row); + g_assert_nonnull(cell); + g_assert_true(adg_table_cell_get_row(cell) == row); + g_assert_true(adg_table_cell_get_table(cell) == table); + + /* Check dependent construction */ + g_assert_nonnull(adg_table_cell_dup(cell)); + g_assert_nonnull(adg_table_cell_new_before(cell)); + + /* Check default destructor: other AdgTableCell instances will be + * implicitely destroyed when the parent AdgTable is destroyed */ + adg_table_cell_free(cell); + + /* Check alternative valid construction conditions */ + g_assert_nonnull(adg_table_cell_new_with_width(row, 0)); + g_assert_nonnull(adg_table_cell_new_with_width(row, 123)); + g_assert_nonnull(adg_table_cell_new_full(row, 12, "name", "title", FALSE)); + g_assert_nonnull(adg_table_cell_new_full(row, 34, NULL, "title", TRUE)); + g_assert_nonnull(adg_table_cell_new_full(row, 56, "name", NULL, FALSE)); + g_assert_nonnull(adg_table_cell_new_full(row, 78, NULL, NULL, TRUE)); + g_assert_nonnull(adg_table_cell_new_full(row, 0, NULL, NULL, TRUE)); + + /* Check invalid conditions */ + g_assert_null(adg_table_cell_new_with_width(row, -1)); + g_assert_null(adg_table_cell_new_full(row, -1, NULL, NULL, TRUE)); + + adg_entity_destroy(ADG_ENTITY(table)); +} + +static void +_adg_property_title(void) +{ + AdgTable *table; + AdgTableRow *row; + AdgTableCell *cell; + AdgEntity *entity; + + table = adg_table_new(); + row = adg_table_row_new(table); + cell = adg_table_cell_new(row); + entity = ADG_ENTITY(adg_logo_new()); + + /* Sanity check */ + g_assert_null(adg_table_cell_title(NULL)); + adg_table_cell_set_title(NULL, NULL); + adg_table_cell_set_title(NULL, entity); + + /* A newly created cell should not have any content */ + g_assert_null(adg_table_cell_title(cell)); + + /* Check explicit setting */ + adg_table_cell_set_title(cell, entity); + g_assert_nonnull(adg_table_cell_title(cell)); + g_assert_true(adg_table_cell_title(cell) == entity); + + /* Check explicit unsetting */ + adg_table_cell_set_title(cell, NULL); + g_assert_null(adg_table_cell_title(cell)); + + /* Check implicit setting during construction */ + cell = adg_table_cell_new_full(row, 12, NULL, "title", FALSE); + g_assert_nonnull(adg_table_cell_title(cell)); + + /* Check the content is not set implicitely */ + cell = adg_table_cell_new_full(row, 12, NULL, NULL, FALSE); + g_assert_null(adg_table_cell_title(cell)); + + adg_entity_destroy(ADG_ENTITY(table)); + adg_entity_destroy(entity); +} + +static void +_adg_property_value(void) +{ + AdgTable *table; + AdgTableRow *row; + AdgTableCell *cell; + AdgEntity *entity; + + table = adg_table_new(); + row = adg_table_row_new(table); + cell = adg_table_cell_new(row); + entity = ADG_ENTITY(adg_logo_new()); + + /* Sanity check */ + g_assert_null(adg_table_cell_value(NULL)); + adg_table_cell_set_value(NULL, NULL); + adg_table_cell_set_value(NULL, entity); + + /* A newly created cell should not have any content */ + g_assert_null(adg_table_cell_value(cell)); + + /* Check explicit setting */ + adg_table_cell_set_value(cell, entity); + g_assert_nonnull(adg_table_cell_value(cell)); + g_assert_true(adg_table_cell_value(cell) == entity); + + /* Check explicit unsetting */ + adg_table_cell_set_value(cell, NULL); + g_assert_null(adg_table_cell_value(cell)); + + /* Check text setting */ + adg_table_cell_set_text_value(cell, "value"); + g_assert_nonnull(adg_table_cell_value(cell)); + + /* Check text unsetting */ + adg_table_cell_set_text_value(cell, NULL); + g_assert_null(adg_table_cell_value(cell)); + + adg_entity_destroy(ADG_ENTITY(table)); + adg_entity_destroy(entity); +} + +static void +_adg_property_width(void) +{ + AdgTable *table; + AdgTableRow *row; + AdgTableCell *cell; + + table = adg_table_new(); + row = adg_table_row_new(table); + cell = adg_table_cell_new(row); + + /* Sanity check */ + g_assert_cmpint(adg_table_cell_get_width(NULL), ==, 0); + adg_table_cell_set_width(NULL, 123); + + /* Check explicit setting */ + g_assert_cmpint(adg_table_cell_get_width(cell), ==, 0); + adg_table_cell_set_width(cell, 321); + g_assert_cmpint(adg_table_cell_get_width(cell), ==, 321); + adg_table_cell_set_width(cell, 0); + g_assert_cmpint(adg_table_cell_get_width(cell), ==, 0); + + /* Check implicit setting during construction */ + cell = adg_table_cell_new_with_width(row, 456); + g_assert_cmpint(adg_table_cell_get_width(cell), ==, 456); + cell = adg_table_cell_new_with_width(row, 0); + g_assert_cmpint(adg_table_cell_get_width(cell), ==, 0); + + adg_entity_destroy(ADG_ENTITY(table)); +} + +static void +_adg_property_frame(void) +{ + AdgTable *table; + AdgTableRow *row; + AdgTableCell *cell; + + table = adg_table_new(); + row = adg_table_row_new(table); + cell = adg_table_cell_new(row); + + /* Sanity check */ + g_assert_false(adg_table_cell_has_frame(NULL)); + adg_table_cell_switch_frame(NULL, FALSE); + + /* Check setting and unsetting */ + g_assert_false(adg_table_cell_has_frame(cell)); + adg_table_cell_switch_frame(cell, TRUE); + g_assert_true(adg_table_cell_has_frame(cell)); + adg_table_cell_switch_frame(cell, FALSE); + g_assert_false(adg_table_cell_has_frame(cell)); + + adg_entity_destroy(ADG_ENTITY(table)); +} + + int main(int argc, char *argv[]) { @@ -38,7 +230,14 @@ main(int argc, char *argv[]) adg_test_add_boxed_checks("/adg/table-cell/type/boxed", ADG_TYPE_TABLE_CELL, adg_table_cell_new(row)); + g_test_add_func("/adg/table-cell/behavior/misc", _adg_behavior_misc); + g_test_add_func("/adg/table-cell/property/title", _adg_property_title); + g_test_add_func("/adg/table-cell/property/value", _adg_property_value); + g_test_add_func("/adg/table-cell/property/width", _adg_property_width); + g_test_add_func("/adg/table-cell/property/frame", _adg_property_frame); + result = g_test_run(); - g_object_unref(table); + adg_entity_destroy(ADG_ENTITY(table)); + return result; } -- 2.11.4.GIT