mirror of
https://github.com/swaywm/sway.git
synced 2024-12-01 19:06:38 +00:00
Add readline tests
This commit is contained in:
parent
8758a2bd04
commit
689935ed39
|
@ -10,7 +10,7 @@ char *read_line(FILE *file) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
while (1) {
|
while (1) {
|
||||||
int c = getc(file);
|
int c = fgetc(file);
|
||||||
if (c == '\n' && lastChar == '\\'){
|
if (c == '\n' && lastChar == '\\'){
|
||||||
--length; // Ignore last character.
|
--length; // Ignore last character.
|
||||||
lastChar = '\0';
|
lastChar = '\0';
|
||||||
|
@ -51,7 +51,7 @@ char *read_line_buffer(FILE *file, char *string, size_t string_len) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
while (1) {
|
while (1) {
|
||||||
int c = getc(file);
|
int c = fgetc(file);
|
||||||
if (c == EOF || c == '\n' || c == '\0') {
|
if (c == EOF || c == '\n' || c == '\0') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
add_subdirectory(list)
|
add_subdirectory(list)
|
||||||
|
add_subdirectory(readline)
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
#define _POSIX_C_SOURCE 200809L
|
#define _POSIX_C_SOURCE 200809L
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "tests.h"
|
#include "tests.h"
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
|
9
test/common/readline/CMakeLists.txt
Normal file
9
test/common/readline/CMakeLists.txt
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
configure_test(
|
||||||
|
SUBPROJECT common
|
||||||
|
NAME readline
|
||||||
|
SOURCES
|
||||||
|
${PROJECT_SOURCE_DIR}/common/readline.c
|
||||||
|
readline.c
|
||||||
|
WRAPPERS
|
||||||
|
fgetc
|
||||||
|
)
|
61
test/common/readline/readline.c
Normal file
61
test/common/readline/readline.c
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "tests.h"
|
||||||
|
#include "readline.h"
|
||||||
|
|
||||||
|
int __wrap_fgetc(FILE *stream) {
|
||||||
|
return mock_type(int);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void prep_string(const char *str) {
|
||||||
|
while (*str) {
|
||||||
|
will_return(__wrap_fgetc, *str++);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_eof_line_ending(void **state) {
|
||||||
|
prep_string("hello");
|
||||||
|
will_return(__wrap_fgetc, EOF);
|
||||||
|
char *line = read_line(NULL);
|
||||||
|
assert_string_equal(line, "hello");
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_newline(void **state) {
|
||||||
|
prep_string("hello\n");
|
||||||
|
char *line = read_line(NULL);
|
||||||
|
assert_string_equal(line, "hello");
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_continuation(void **state) {
|
||||||
|
prep_string("hello \\\nworld");
|
||||||
|
will_return(__wrap_fgetc, EOF);
|
||||||
|
char *line = read_line(NULL);
|
||||||
|
assert_string_equal(line, "hello world");
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_expand_buffer(void **state) {
|
||||||
|
const char *test = "This is a very very long string. "
|
||||||
|
"This string is so long that it may in fact be greater "
|
||||||
|
"than 128 bytes (or octets) in length, which is suitable "
|
||||||
|
"for triggering a realloc";
|
||||||
|
prep_string(test);
|
||||||
|
will_return(__wrap_fgetc, EOF);
|
||||||
|
char *line = read_line(NULL);
|
||||||
|
assert_string_equal(line, test);
|
||||||
|
free(line);
|
||||||
|
assert_int_equal(realloc_calls(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
const struct CMUnitTest tests[] = {
|
||||||
|
cmocka_unit_test(test_eof_line_ending),
|
||||||
|
cmocka_unit_test(test_newline),
|
||||||
|
cmocka_unit_test(test_continuation),
|
||||||
|
cmocka_unit_test(test_expand_buffer),
|
||||||
|
};
|
||||||
|
return cmocka_run_group_tests(tests, reset_mem_wrappers, NULL);
|
||||||
|
}
|
|
@ -8,8 +8,10 @@ do
|
||||||
ret+=$?
|
ret+=$?
|
||||||
done
|
done
|
||||||
|
|
||||||
if grep 'enable-coverage:BOOL=YES' "$1/CMakeCache.txt"
|
if (( $ret == 0 ))
|
||||||
then
|
then
|
||||||
|
if grep 'enable-coverage:BOOL=YES' "$1/CMakeCache.txt"
|
||||||
|
then
|
||||||
echo "Generating coverage reports"
|
echo "Generating coverage reports"
|
||||||
rm -rf "$1/coverage"
|
rm -rf "$1/coverage"
|
||||||
mkdir "$1/coverage"
|
mkdir "$1/coverage"
|
||||||
|
@ -19,6 +21,7 @@ then
|
||||||
lcov --remove "$1/coverage/lcov.info" 'test/*' '/usr/*' \
|
lcov --remove "$1/coverage/lcov.info" 'test/*' '/usr/*' \
|
||||||
--output-file "$1/coverage/lcov.info.clean"
|
--output-file "$1/coverage/lcov.info.clean"
|
||||||
genhtml -o "$1/coverage/" "$1/coverage/lcov.info.clean"
|
genhtml -o "$1/coverage/" "$1/coverage/lcov.info.clean"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
exit $ret
|
exit $ret
|
||||||
|
|
Loading…
Reference in a new issue