diff --git a/Makefile.am b/Makefile.am index ae088e27..e40c14af 100644 --- a/Makefile.am +++ b/Makefile.am @@ -143,7 +143,16 @@ update-manpage: $(top_srcdir)/doc/rofi-manpage.markdown ## # Rofi test program ## -check_PROGRAMS=history_test textbox_test helper_test helper_expand helper_config_cmdline_parser widget_test +check_PROGRAMS=\ + history_test\ + textbox_test\ + helper_test\ + helper_expand\ + helper_pidfile\ + helper_tokenize\ + helper_config_cmdline_parser\ + widget_test + history_test_CFLAGS=\ $(AM_CFLAGS)\ @@ -185,6 +194,36 @@ textbox_test_LDADD=\ $(cairo_LIBS)\ $(libsn_LIBS) +helper_pidfile_CFLAGS=$(textbox_test_CFLAGS) +helper_pidfile_LDADD=$(textbox_test_LDADD) +helper_pidfile_SOURCES=\ + config/config.c\ + include/rofi.h\ + include/mode.h\ + include/mode-private.h\ + source/helper.c\ + include/helper.h\ + include/xrmoptions.h\ + source/xrmoptions.c\ + source/x11-helper.c\ + inlucde/x11-helper.h\ + test/helper-pidfile.c + +helper_tokenize_CFLAGS=$(textbox_test_CFLAGS) +helper_tokenize_LDADD=$(textbox_test_LDADD) +helper_tokenize_SOURCES=\ + config/config.c\ + include/rofi.h\ + include/mode.h\ + include/mode-private.h\ + source/helper.c\ + include/helper.h\ + include/xrmoptions.h\ + source/xrmoptions.c\ + source/x11-helper.c\ + inlucde/x11-helper.h\ + test/helper-tokenize.c + widget_test_LDADD=$(textbox_test_LDADD) widget_test_CFLAGS=$(textbox_test_CFLAGS) widget_test_SOURCES=\ @@ -271,13 +310,14 @@ TESTS=\ history_test\ helper_test\ helper_expand\ + helper_pidfile\ + helper_tokenize\ helper_config_cmdline_parser\ + textbox_test\ widget_test .PHONY: test-x -test-x: $(bin_PROGRAMS) textbox_test - echo "Test 1" - $(top_srcdir)/test/run_test.sh 123 $(top_builddir)/textbox_test $(top_builddir) +test-x: $(bin_PROGRAMS) echo "Test 2" $(top_srcdir)/test/run_test.sh 200 $(top_srcdir)/test/run_errormsg_test.sh $(top_builddir) echo "Test 3" diff --git a/test/helper-config-cmdline-parser.c b/test/helper-config-cmdline-parser.c index 861b43bf..c1521aa5 100644 --- a/test/helper-config-cmdline-parser.c +++ b/test/helper-config-cmdline-parser.c @@ -47,17 +47,45 @@ int main ( int argc, char ** argv ) char **list = NULL; int llength = 0; char * test_str = - "{host} {terminal} -e bash -c \"{ssh-client} {host}; echo '{terminal} {host}'\""; + "{host} {terminal} -e bash -c \"{ssh-client} {host}; echo '{terminal} {host}'\" -i -3 -u 4"; helper_parse_setup ( test_str, &list, &llength, "{host}", "chuck", "{terminal}", "x-terminal-emulator", NULL ); - TASSERT ( llength == 6 ); + TASSERT ( llength == 10); TASSERT ( strcmp ( list[0], "chuck" ) == 0 ); TASSERT ( strcmp ( list[1], "x-terminal-emulator" ) == 0 ); TASSERT ( strcmp ( list[2], "-e" ) == 0 ); TASSERT ( strcmp ( list[3], "bash" ) == 0 ); TASSERT ( strcmp ( list[4], "-c" ) == 0 ); TASSERT ( strcmp ( list[5], "ssh chuck; echo 'x-terminal-emulator chuck'" ) == 0 ); + TASSERT ( strcmp ( list[6], "-i" ) == 0 ); + TASSERT ( strcmp ( list[7], "-3" ) == 0 ); + TASSERT ( strcmp ( list[8], "-u" ) == 0 ); + TASSERT ( strcmp ( list[9], "4" ) == 0 ); + + cmd_set_arguments ( llength, list); + TASSERT( find_arg ( "-e") == 2 ); + TASSERT( find_arg ( "-x") == -1 ); + char *str; + TASSERT( find_arg_str ( "-e", &str) == TRUE ); + TASSERT ( str == list[3] ); + TASSERT( find_arg_str ( "-x", &str) == FALSE ); + // Should be unmodified. + TASSERT ( str == list[3] ); + + unsigned int u = 1234; + unsigned int i = -1234; + TASSERT ( find_arg_uint ( "-x", &u ) == FALSE ); + TASSERT ( u == 1234 ); + TASSERT ( find_arg_int ( "-x", &i ) == FALSE ); + TASSERT ( i == -1234 ); + TASSERT ( find_arg_uint ( "-u", &u ) == TRUE ); + TASSERT ( u == 4 ); + TASSERT ( find_arg_uint ( "-i", &u ) == TRUE ); + TASSERT ( u == 4294967293 ); + TASSERT ( find_arg_int ( "-i", &i ) == TRUE ); + TASSERT ( i == -3 ); + g_strfreev ( list ); } diff --git a/test/helper-pidfile.c b/test/helper-pidfile.c new file mode 100644 index 00000000..dae85829 --- /dev/null +++ b/test/helper-pidfile.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include +#include +#include "xcb-internal.h" +#include "rofi.h" +#include "settings.h" + +static int test = 0; + +#define TASSERT( a ) { \ + assert ( a ); \ + printf ( "Test %i passed (%s)\n", ++test, # a ); \ +} + +int rofi_view_error_dialog ( const char *msg, G_GNUC_UNUSED int markup ) +{ + fputs ( msg, stderr ); + return TRUE; +} + +int show_error_message ( const char *msg, int markup ) +{ + fputs ( msg, stderr ); + return 0; +} +xcb_screen_t *xcb_screen; +xcb_ewmh_connection_t xcb_ewmh; +int xcb_screen_nbr; +#include + +int main ( int argc, char ** argv ) +{ + if ( setlocale ( LC_ALL, "" ) == NULL ) { + fprintf ( stderr, "Failed to set locale.\n" ); + return EXIT_FAILURE; + } + // Pid test. + // Tests basic functionality of writing it, locking, seeing if I can write same again + // And close/reopen it again. + { + const char *path = "/tmp/rofi-test.pid"; + TASSERT( create_pid_file ( NULL ) == -1 ); + int fd = create_pid_file ( path ); + TASSERT( fd >= 0 ); + int fd2 = create_pid_file ( path ); + TASSERT ( fd2 < 0 ); + + remove_pid_file ( fd ); + fd = create_pid_file ( path ); + TASSERT( fd >= 0 ); + remove_pid_file ( fd ); + } +} diff --git a/test/helper-test.c b/test/helper-test.c index 4ae5694a..4ed00128 100644 --- a/test/helper-test.c +++ b/test/helper-test.c @@ -98,4 +98,20 @@ int main ( int argc, char ** argv ) TASSERT ( g_utf8_collate ( str, "Valid utf8 until �( we continue here") == 0 ); g_free(str); } + // Pid test. + // Tests basic functionality of writing it, locking, seeing if I can write same again + // And close/reopen it again. + { + const char *path = "/tmp/rofi-test.pid"; + TASSERT( create_pid_file ( NULL ) == -1 ); + int fd = create_pid_file ( path ); + TASSERT( fd >= 0 ); + int fd2 = create_pid_file ( path ); + TASSERT ( fd2 < 0 ); + + remove_pid_file ( fd ); + fd = create_pid_file ( path ); + TASSERT( fd >= 0 ); + remove_pid_file ( fd ); + } } diff --git a/test/helper-tokenize.c b/test/helper-tokenize.c new file mode 100644 index 00000000..ea82da03 --- /dev/null +++ b/test/helper-tokenize.c @@ -0,0 +1,251 @@ +#include +#include +#include +#include +#include +#include +#include +#include "xcb-internal.h" +#include "rofi.h" +#include "settings.h" + +static int test = 0; + +#define TASSERT( a ) { \ + assert ( a ); \ + printf ( "Test %i passed (%s)\n", ++test, # a ); \ +} + +int rofi_view_error_dialog ( const char *msg, G_GNUC_UNUSED int markup ) +{ + fputs ( msg, stderr ); + return TRUE; +} + +int show_error_message ( const char *msg, int markup ) +{ + fputs ( msg, stderr ); + return 0; +} +xcb_screen_t *xcb_screen; +xcb_ewmh_connection_t xcb_ewmh; +int xcb_screen_nbr; +#include + +int main ( int argc, char ** argv ) +{ + if ( setlocale ( LC_ALL, "" ) == NULL ) { + fprintf ( stderr, "Failed to set locale.\n" ); + return EXIT_FAILURE; + } + // Pid test. + // Tests basic functionality of writing it, locking, seeing if I can write same again + // And close/reopen it again. + { + tokenize_free ( NULL ); + } + { + config.matching_method = MM_NORMAL; + GRegex **tokens = tokenize ( "noot", FALSE ); + + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap Noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "Nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "noOTap mies") == TRUE ); + + tokenize_free ( tokens ); + + tokens = tokenize ( "noot", TRUE ); + + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap Noot mies") == FALSE ); + TASSERT ( token_match ( tokens, "Nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "noOTap mies") == FALSE ); + + tokenize_free ( tokens ); + tokens = tokenize ( "no ot", FALSE ); + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "noap miesot") == TRUE ); + tokenize_free ( tokens ); + } + { + config.matching_method = MM_GLOB; + GRegex **tokens = tokenize ( "noot", FALSE ); + + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap Noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "Nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "noOTap mies") == TRUE ); + + tokenize_free ( tokens ); + + tokens = tokenize ( "noot", TRUE ); + + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap Noot mies") == FALSE ); + TASSERT ( token_match ( tokens, "Nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "noOTap mies") == FALSE ); + tokenize_free ( tokens ); + + tokens = tokenize ( "no ot", FALSE ); + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "noap miesot") == TRUE ); + tokenize_free ( tokens ); + + tokens = tokenize ( "n?ot", FALSE ); + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "noap miesot") == FALSE); + tokenize_free ( tokens ); + tokens = tokenize ( "n*ot", FALSE ); + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "noap miesot") == TRUE); + tokenize_free ( tokens ); + + tokens = tokenize ( "n* ot", FALSE ); + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "noap miesot") == TRUE); + TASSERT ( token_match ( tokens, "ot nap mies") == TRUE); + tokenize_free ( tokens ); + } + { + config.matching_method = MM_FUZZY; + GRegex **tokens = tokenize ( "noot", FALSE ); + + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap Noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "Nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "noOTap mies") == TRUE ); + + tokenize_free ( tokens ); + + tokens = tokenize ( "noot", TRUE ); + + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap Noot mies") == FALSE ); + TASSERT ( token_match ( tokens, "Nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "noOTap mies") == FALSE ); + tokenize_free ( tokens ); + + tokens = tokenize ( "no ot", FALSE ); + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "noap miesot") == TRUE ); + tokenize_free ( tokens ); + + tokens = tokenize ( "n ot", FALSE ); + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "noap miesot") == TRUE); + tokenize_free ( tokens ); + tokens = tokenize ( "ont", FALSE ); + TASSERT ( token_match ( tokens, "aap noot mies") == FALSE); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap nmiest") == TRUE ); + tokenize_free ( tokens ); + + tokens = tokenize ( "o n t", FALSE ); + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "noap miesot") == TRUE); + TASSERT ( token_match ( tokens, "ot nap mies") == TRUE); + tokenize_free ( tokens ); + } + { + config.matching_method = MM_REGEX; + GRegex **tokens = tokenize ( "noot", FALSE ); + + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap Noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "Nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "noOTap mies") == TRUE ); + + tokenize_free ( tokens ); + + tokens = tokenize ( "noot", TRUE ); + + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap Noot mies") == FALSE ); + TASSERT ( token_match ( tokens, "Nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "noOTap mies") == FALSE ); + tokenize_free ( tokens ); + + tokens = tokenize ( "no ot", FALSE ); + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "noap miesot") == TRUE ); + tokenize_free ( tokens ); + + tokens = tokenize ( "n.?ot", FALSE ); + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "noap miesot") == FALSE); + tokenize_free ( tokens ); + tokens = tokenize ( "n[oa]{2}t", FALSE ); + TASSERT ( token_match ( tokens, "aap noot mies") == TRUE ); + TASSERT ( token_match ( tokens, "aap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == TRUE ); + TASSERT ( token_match ( tokens, "noat miesot") == TRUE); + TASSERT ( token_match ( tokens, "noaat miesot") == FALSE); + tokenize_free ( tokens ); + + tokens = tokenize ( "^(aap|noap)\\sMie.*", FALSE ); + TASSERT ( token_match ( tokens, "aap noot mies") == FALSE ); + TASSERT ( token_match ( tokens, "aap mies") == TRUE); + TASSERT ( token_match ( tokens, "nooaap mies") == FALSE ); + TASSERT ( token_match ( tokens, "nootap mies") == FALSE ); + TASSERT ( token_match ( tokens, "noap miesot") == TRUE); + TASSERT ( token_match ( tokens, "ot nap mies") == FALSE ); + tokenize_free ( tokens ); + } +}