From 881056ea9b1a606d1d979be631795a3a07d5621e Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Thu, 20 Oct 2016 09:41:32 +0200 Subject: [PATCH] Add some more tests, make some textbox api's private --- Makefile.am | 1 + include/widgets/textbox.h | 35 ------------------------------- source/view.c | 2 +- source/widgets/textbox.c | 41 +++++++++++++++++++++++++++++-------- test/helper-test.c | 20 ++++++++++++++++++ test/textbox-test.c | 43 +++++++++++++++++++++++++++++++-------- 6 files changed, 89 insertions(+), 53 deletions(-) diff --git a/Makefile.am b/Makefile.am index 05bd7884..ae088e27 100644 --- a/Makefile.am +++ b/Makefile.am @@ -366,6 +366,7 @@ coverage/index.html: coverage.info .PHONY: coverage-clean coverage-clean: -rm -r coverage.info coverage/ + -find $(top_builddir) -name '*.gcda' | xargs rm .PHONY: .FORCE .FORCE: diff --git a/include/widgets/textbox.h b/include/widgets/textbox.h index 15de87dc..0a051ff4 100644 --- a/include/widgets/textbox.h +++ b/include/widgets/textbox.h @@ -136,13 +136,6 @@ int textbox_keybinding ( textbox *tb, KeyBindingAction action ); */ gboolean textbox_append_char ( textbox *tb, const char *pad, const int pad_len ); -/** - * @param tb Handle to the textbox - * - * Move the cursor to the end of the string. - */ -void textbox_cursor_end ( textbox *tb ); - /** * @param tb Handle to the textbox * @param pos New cursor position @@ -207,34 +200,6 @@ int textbox_get_font_width ( const textbox *tb ); */ double textbox_get_estimated_char_width ( void ); -/** - * @param tb Handle to the textbox - * - * Delete character before cursor. - */ -void textbox_cursor_bkspc ( textbox *tb ); - -/** - * @param tb Handle to the textbox - * - * Delete character after cursor. - */ -void textbox_cursor_del ( textbox *tb ); - -/** - * @param tb Handle to the textbox - * - * Move cursor one position backward. - */ -void textbox_cursor_dec ( textbox *tb ); - -/** - * @param tb Handle to the textbox - * - * Move cursor one position forward. - */ -void textbox_cursor_inc ( textbox *tb ); - /** * @param tb Handle to the textbox * @param pos The start position diff --git a/source/view.c b/source/view.c index b1ef0953..30a6da14 100644 --- a/source/view.c +++ b/source/view.c @@ -656,7 +656,7 @@ inline static void rofi_view_nav_row_select ( RofiViewState *state ) char *str = mode_get_completion ( state->sw, state->line_map[selected] ); textbox_text ( state->text, str ); g_free ( str ); - textbox_cursor_end ( state->text ); + textbox_keybinding ( state->text, MOVE_END ); state->refilter = TRUE; } } diff --git a/source/widgets/textbox.c b/source/widgets/textbox.c index d03ceb4a..db4c847f 100644 --- a/source/widgets/textbox.c +++ b/source/widgets/textbox.c @@ -43,6 +43,13 @@ static void textbox_free ( widget * ); static int textbox_get_width ( widget * ); static int _textbox_get_height ( widget * ); +/** + * @param tb Handle to the textbox + * + * Move the cursor to the end of the string. + */ +static void textbox_cursor_end ( textbox *tb ); + /** * Font + font color cache. * Avoid re-loading font on every change on every textbox. @@ -406,14 +413,22 @@ void textbox_cursor ( textbox *tb, int pos ) widget_queue_redraw ( WIDGET ( tb ) ); } -// move right -void textbox_cursor_inc ( textbox *tb ) +/** + * @param tb Handle to the textbox + * + * Move cursor one position forward. + */ +static void textbox_cursor_inc ( textbox *tb ) { textbox_cursor ( tb, tb->cursor + 1 ); } -// move left -void textbox_cursor_dec ( textbox *tb ) +/** + * @param tb Handle to the textbox + * + * Move cursor one position backward. + */ +static void textbox_cursor_dec ( textbox *tb ) { textbox_cursor ( tb, tb->cursor - 1 ); } @@ -481,7 +496,7 @@ static void textbox_cursor_dec_word ( textbox *tb ) } // end of line -void textbox_cursor_end ( textbox *tb ) +static void textbox_cursor_end ( textbox *tb ) { if ( tb->text == NULL ) { tb->cursor = 0; @@ -547,8 +562,12 @@ void textbox_delete ( textbox *tb, int pos, int dlen ) tb->update = TRUE; } -// delete on character -void textbox_cursor_del ( textbox *tb ) +/** + * @param tb Handle to the textbox + * + * Delete character after cursor. + */ +static void textbox_cursor_del ( textbox *tb ) { if ( tb->text == NULL ) { return; @@ -556,8 +575,12 @@ void textbox_cursor_del ( textbox *tb ) textbox_delete ( tb, tb->cursor, 1 ); } -// back up and delete one character -void textbox_cursor_bkspc ( textbox *tb ) +/** + * @param tb Handle to the textbox + * + * Delete character before cursor. + */ +static void textbox_cursor_bkspc ( textbox *tb ) { if ( tb->cursor > 0 ) { textbox_cursor_dec ( tb ); diff --git a/test/helper-test.c b/test/helper-test.c index 9d70c61a..4ae5694a 100644 --- a/test/helper-test.c +++ b/test/helper-test.c @@ -78,4 +78,24 @@ int main ( int argc, char ** argv ) TASSERTE ( levenshtein ( "aap", "noot aap mies" ), 10 ); TASSERTE ( levenshtein ( "noot aap mies", "aap" ), 10 ); TASSERTE ( levenshtein ( "otp", "noot aap" ), 5 ); + /** + * Quick converision check. + */ + { + char *str = rofi_latin_to_utf8_strdup ( "\xA1\xB5", 2); + TASSERT ( g_utf8_collate ( str, "¡µ") == 0 ); + g_free(str); + } + + { + char *str = rofi_force_utf8("Valid utf8", 10); + TASSERT ( g_utf8_collate ( str, "Valid utf8") == 0 ); + g_free(str); + char in[] = "Valid utf8 until \xc3\x28 we continue here"; + TASSERT ( g_utf8_validate ( in, -1, NULL ) == FALSE ); + str = rofi_force_utf8(in, strlen(in)); + TASSERT ( g_utf8_validate ( str, -1, NULL ) == TRUE ); + TASSERT ( g_utf8_collate ( str, "Valid utf8 until �( we continue here") == 0 ); + g_free(str); + } } diff --git a/test/textbox-test.c b/test/textbox-test.c index 5383e8e9..fc5209ca 100644 --- a/test/textbox-test.c +++ b/test/textbox-test.c @@ -57,7 +57,7 @@ int main ( G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv ) NORMAL, "test" ); TASSERT ( box != NULL ); - textbox_cursor_end ( box ); + textbox_keybinding ( box, MOVE_END ); TASSERT ( box->cursor == 4 ); textbox_cursor ( box, -1 ); TASSERT ( box->cursor == 0 ); @@ -67,7 +67,7 @@ int main ( G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv ) TASSERT ( box->cursor == 2 ); textbox_insert ( box, 3, "bo", 2 ); TASSERT ( strcmp ( box->text, "tesbot" ) == 0 ); - textbox_cursor_end ( box ); + textbox_keybinding ( box, MOVE_END ); TASSERT ( box->cursor == 6 ); TASSERT ( widget_get_width ( WIDGET ( box ) ) > 0 ); @@ -78,19 +78,19 @@ int main ( G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv ) TASSERT ( textbox_get_estimated_char_width ( ) > 0 ); - textbox_cursor_bkspc ( box ); + textbox_keybinding ( box, REMOVE_CHAR_BACK ); TASSERT ( strcmp ( box->text, "tesbo" ) == 0 ); TASSERT ( box->cursor == 5 ); - textbox_cursor_dec ( box ); + textbox_keybinding ( box, MOVE_CHAR_BACK ); TASSERT ( box->cursor == 4 ); - textbox_cursor_del ( box ); + textbox_keybinding ( box, REMOVE_CHAR_FORWARD ); TASSERT ( strcmp ( box->text, "tesb" ) == 0 ); - textbox_cursor_dec ( box ); + textbox_keybinding ( box, MOVE_CHAR_BACK ); TASSERT ( box->cursor == 3 ); - textbox_cursor_inc ( box ); + textbox_keybinding ( box, MOVE_CHAR_FORWARD); TASSERT ( box->cursor == 4 ); - textbox_cursor_inc ( box ); + textbox_keybinding ( box, MOVE_CHAR_FORWARD); TASSERT ( box->cursor == 4 ); // Cursor after delete section. textbox_delete ( box, 0, 1 ); @@ -122,6 +122,33 @@ int main ( G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv ) TASSERT ( strcmp ( box->text, "aapmies" ) == 0 ); TASSERT ( box->cursor == 5 ); + + textbox_text ( box, "aap noot mies"); + textbox_cursor ( box, 8 ); + textbox_keybinding ( box, REMOVE_WORD_BACK ); + TASSERT ( box->cursor == 4); + TASSERT ( strcmp ( box->text, "aap mies") == 0 ); + textbox_keybinding ( box, REMOVE_TO_EOL ); + TASSERT ( box->cursor == 4); + TASSERT ( strcmp ( box->text, "aap ") == 0 ); + textbox_text ( box, "aap noot mies"); + textbox_cursor ( box, 8 ); + textbox_keybinding ( box, REMOVE_WORD_FORWARD ); + TASSERT ( strcmp ( box->text, "aap noot") == 0 ); + textbox_keybinding ( box, MOVE_FRONT ); + TASSERT ( box->cursor == 0); + textbox_keybinding ( box, CLEAR_LINE ); + TASSERT ( strcmp ( box->text, "") == 0 ); + textbox_text ( box, "aap noot mies"); + textbox_keybinding ( box, MOVE_END); + textbox_keybinding ( box, MOVE_WORD_BACK ); + TASSERT ( box->cursor == 9); + textbox_keybinding ( box, MOVE_WORD_BACK ); + TASSERT ( box->cursor == 4); + textbox_keybinding ( box, REMOVE_TO_SOL ); + TASSERT ( strcmp ( box->text, "noot mies") == 0 ); + TASSERT ( box->cursor == 0); + textbox_font ( box, HIGHLIGHT ); //textbox_draw ( box, draw );