From 5fc5e3abad79fb4bb6cac3a685e258f9ed794e40 Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Thu, 13 Nov 2014 23:26:30 +0000 Subject: [PATCH] Refactor the widget token parsing so we can put it under test. Add simple test that demonstrates tokens split by space. --- elm_code/lib/elm_code_widget.c | 39 ++++++++++++++---------- elm_code/lib/elm_code_widget.h | 1 + elm_code/tests/Makefile.am | 1 + elm_code/tests/elm_code_suite.c | 1 + elm_code/tests/elm_code_suite.h | 1 + elm_code/tests/elm_code_test_widget.c | 44 +++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 elm_code/tests/elm_code_test_widget.c diff --git a/elm_code/lib/elm_code_widget.c b/elm_code/lib/elm_code_widget.c index ce7eb86..32e00de 100644 --- a/elm_code/lib/elm_code_widget.c +++ b/elm_code/lib/elm_code_widget.c @@ -31,14 +31,34 @@ static void _elm_code_widget_fill_line_token(Evas_Textgrid_Cell *cells, int coun } } -static void _elm_code_widget_fill_line(Evas_Object *o, Evas_Textgrid_Cell *cells, Elm_Code_Line *line) +EAPI void elm_code_widget_fill_line_tokens(Evas_Textgrid_Cell *cells, int count, Elm_Code_Line *line) { Eina_List *item; Elm_Code_Token *token; + int start, length; + start = 1; + length = strlen(line->content); + + EINA_LIST_FOREACH(line->tokens, item, token) + { + + _elm_code_widget_fill_line_token(cells, count, start, token->start, ELM_CODE_TOKEN_TYPE_DEFAULT); + + // TODO handle a token starting before the previous finishes + _elm_code_widget_fill_line_token(cells, count, token->start, token->end, token->type); + + start = token->end + 1; + } + + _elm_code_widget_fill_line_token(cells, count, start, length, ELM_CODE_TOKEN_TYPE_DEFAULT); +} + +static void _elm_code_widget_fill_line(Evas_Object *o, Evas_Textgrid_Cell *cells, Elm_Code_Line *line) +{ char *chr; unsigned int length, x; - int w, start; + int w; if (!_elm_code_widget_resize(o)) return; @@ -60,20 +80,7 @@ static void _elm_code_widget_fill_line(Evas_Object *o, Evas_Textgrid_Cell *cells cells[x].bg = line->status; } - start = 1; - - EINA_LIST_FOREACH(line->tokens, item, token) - { - - _elm_code_widget_fill_line_token(cells, w, start, token->start, ELM_CODE_TOKEN_TYPE_DEFAULT); - - // TODO handle a token starting before the previous finishes - _elm_code_widget_fill_line_token(cells, w, token->start, token->end, token->type); - - start = token->end + 1; - } - - _elm_code_widget_fill_line_token(cells, w, start, length, ELM_CODE_TOKEN_TYPE_DEFAULT); + elm_code_widget_fill_line_tokens(cells, w, line); evas_object_textgrid_update_add(o, 0, line->number - 1, w, 1); } diff --git a/elm_code/lib/elm_code_widget.h b/elm_code/lib/elm_code_widget.h index dda5df8..8c2bf77 100644 --- a/elm_code/lib/elm_code_widget.h +++ b/elm_code/lib/elm_code_widget.h @@ -27,6 +27,7 @@ extern "C" { EAPI Evas_Object *elm_code_widget_add(Evas_Object *parent, Elm_Code *code); EAPI void elm_code_widget_fill(Evas_Object *o, Elm_Code *code); +EAPI void elm_code_widget_fill_line_tokens(Evas_Textgrid_Cell *cells, int count, Elm_Code_Line *line); /** * @} diff --git a/elm_code/tests/Makefile.am b/elm_code/tests/Makefile.am index aa71521..75ae85c 100644 --- a/elm_code/tests/Makefile.am +++ b/elm_code/tests/Makefile.am @@ -7,6 +7,7 @@ elm_code_suite_SOURCES = \ elm_code_file_test_load.c \ elm_code_file_test_memory.c \ elm_code_test_basic.c \ +elm_code_test_widget.c \ elm_code_suite.c elm_code_suite_CPPFLAGS = -I$(top_builddir)/elm_code/lib/ \ diff --git a/elm_code/tests/elm_code_suite.c b/elm_code/tests/elm_code_suite.c index 1f52521..1d9763b 100644 --- a/elm_code/tests/elm_code_suite.c +++ b/elm_code/tests/elm_code_suite.c @@ -15,6 +15,7 @@ static const struct { } tests[] = { { "file_load", elm_code_file_test_load }, { "file_memory", elm_code_file_test_memory }, + { "widget", elm_code_test_widget }, { "basic", elm_code_test_basic }, }; diff --git a/elm_code/tests/elm_code_suite.h b/elm_code/tests/elm_code_suite.h index b9d1bdf..bc2cc89 100644 --- a/elm_code/tests/elm_code_suite.h +++ b/elm_code/tests/elm_code_suite.h @@ -7,6 +7,7 @@ void elm_code_file_test_load(TCase *tc); void elm_code_file_test_memory(TCase *tc); +void elm_code_test_widget(TCase *tc); void elm_code_test_basic(TCase *tc); #endif /* _EDLM_CODE_SUITE_H */ diff --git a/elm_code/tests/elm_code_test_widget.c b/elm_code/tests/elm_code_test_widget.c new file mode 100644 index 0000000..77bedd1 --- /dev/null +++ b/elm_code/tests/elm_code_test_widget.c @@ -0,0 +1,44 @@ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "elm_code_suite.h" + +static void _assert_cell_type(Evas_Textgrid_Cell cell, Elm_Code_Token_Type type) +{ + ck_assert(cell.fg == type); +} + +START_TEST (elm_code_widget_token_render_simple_test) +{ + Elm_Code_File *file; + Elm_Code_Line *line; + int length; + + Evas_Textgrid_Cell cells[25]; + + file = elm_code_file_new(); + elm_code_file_line_append(file, "some \"test content\", 45"); + line = elm_code_file_line_get(file, 1); + length = strlen(line->content); + + elm_code_file_line_token_add(file, 1, 6+1, 18+1, ELM_CODE_TOKEN_TYPE_COMMENT); + elm_code_file_line_token_add(file, 1, 22+1, 23+1, ELM_CODE_TOKEN_TYPE_COMMENT); + + elm_code_widget_fill_line_tokens(cells, length, line); + _assert_cell_type(cells[0], ELM_CODE_TOKEN_TYPE_DEFAULT); + _assert_cell_type(cells[3], ELM_CODE_TOKEN_TYPE_DEFAULT); + _assert_cell_type(cells[5], ELM_CODE_TOKEN_TYPE_DEFAULT); + _assert_cell_type(cells[15], ELM_CODE_TOKEN_TYPE_COMMENT); + _assert_cell_type(cells[19], ELM_CODE_TOKEN_TYPE_DEFAULT); + _assert_cell_type(cells[22], ELM_CODE_TOKEN_TYPE_COMMENT); + + elm_code_file_free(file); +} +END_TEST + +void elm_code_test_widget(TCase *tc) +{ + tcase_add_test(tc, elm_code_widget_token_render_simple_test); +} +