Refactor the widget token parsing so we can put it under test. Add simple test that demonstrates tokens split by space.

This commit is contained in:
Andy Williams 2014-11-13 23:26:30 +00:00
parent 227c883b65
commit 5fc5e3abad
6 changed files with 71 additions and 16 deletions

View File

@ -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);
}

View File

@ -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);
/**
* @}

View File

@ -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/ \

View File

@ -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 },
};

View File

@ -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 */

View File

@ -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);
}