Skip to content

Commit

Permalink
Add automated tests for sheet command (#210)
Browse files Browse the repository at this point in the history
* Add automated tests for `sheet` command
* Fix last column not loaded/displayed
---------

Co-authored-by: liquidaty <[email protected]>
  • Loading branch information
iamazeem and liquidaty authored Oct 7, 2024
1 parent ecbfe73 commit 2fb306c
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ jobs:
if: runner.os == 'Linux'
run: |
sudo apt update
sudo apt install -y mingw-w64 rpm alien nuget musl-tools
sudo apt install -y mingw-w64 rpm alien nuget musl-tools tmux
sudo apt remove -y jq
- name: Set up macOS (AMD64 and ARM64)
Expand Down
4 changes: 2 additions & 2 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ endif

ZSV=$(BINDIR)/zsv${EXE}

SOURCES= echo paste count count-pull select select-pull 2tsv 2json serialize flatten pretty stack desc sql 2db compare prop rm mv jq
SOURCES=echo paste count count-pull select select-pull 2tsv 2json serialize flatten pretty stack desc sql 2db compare prop rm mv jq
CLI_SOURCES=echo select desc count paste 2tsv pretty sql flatten 2json serialize stack 2db compare prop rm mv jq

ifeq ($(ZSV_BUILD_SHEET),1)
Expand Down Expand Up @@ -470,7 +470,7 @@ check test: test-standalone test-cli
test-standalone:
@${MAKE} -C test test QUIET=1 LEAKS=${LEAKS} CONFIGFILE=${CONFIGFILEPATH} DEBUG=${DEBUG}

test-cli: ${CLI}
test-cli: ${CLI}
@${MAKE} -C test $@ QUIET=1 LEAKS=${LEAKS} CONFIGFILE=${CONFIGFILEPATH} DEBUG=${DEBUG} CLI=${CLI}

clean-all: clean clean-external clean-obj clean-lib clean-util-lib
Expand Down
36 changes: 18 additions & 18 deletions app/sheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ struct ztv_opts {
size_t found_rownum;
};

/* read_table_into_buff: populate the given dest
size_t
read_data(char dest[ZTV_BUFFER_ROWS][ZTV_MAX_COLS][ZTV_MAX_CELL_LEN],
const char *filename, struct zsv_opts *optsp, size_t *max_col_countp,
const char *filter,
size_t start_row, // offset row of input to fetch
size_t start_col, // offset column of input to fetch
size_t buff_row_offset, // offset row from which to start writing data
void *index,
struct ztv_opts *opts);
*/

void display_buffer_subtable(char buffer[ZTV_BUFFER_ROWS][ZTV_MAX_COLS][ZTV_MAX_CELL_LEN], size_t start_row,
size_t buffer_used_row_count, size_t start_col, size_t max_col_count, size_t cursor_row,
size_t cursor_col, size_t input_header_span, struct display_dims *ddims);
Expand Down Expand Up @@ -237,8 +225,11 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
size_t cursor_row = 1; // first row is header
size_t cursor_col = 0;
char *help_suffix = NULL;
size_t rownum_col_offset = 1;
display_buffer_subtable(input_data, buff_offset.row, buff_used_rows, buff_offset.col,
input_dimensions.col_count > ZTV_MAX_COLS ? ZTV_MAX_COLS : input_dimensions.col_count,
input_dimensions.col_count + rownum_col_offset > ZTV_MAX_COLS
? ZTV_MAX_COLS
: input_dimensions.col_count + rownum_col_offset,
cursor_row, cursor_col, header_span, &display_dims);

char cmdbuff[256]; // subcommand buffer
Expand Down Expand Up @@ -295,7 +286,9 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
case ztv_key_move_last_col:
// to do: directly set cursor_col and buff_offset.col
while (cursor_right(display_dims.columns, ZTV_CELL_DISPLAY_WIDTH,
input_dimensions.col_count > ZTV_MAX_COLS ? ZTV_MAX_COLS : input_dimensions.col_count,
input_dimensions.col_count + rownum_col_offset > ZTV_MAX_COLS
? ZTV_MAX_COLS
: input_dimensions.col_count + rownum_col_offset,
&cursor_col, &buff_offset.col) > 0)
;
break;
Expand Down Expand Up @@ -324,8 +317,10 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
break;
case ztv_key_move_right:
cursor_right(display_dims.columns, ZTV_CELL_DISPLAY_WIDTH,
input_dimensions.col_count > ZTV_MAX_COLS ? ZTV_MAX_COLS : input_dimensions.col_count, &cursor_col,
&buff_offset.col);
input_dimensions.col_count + rownum_col_offset > ZTV_MAX_COLS
? ZTV_MAX_COLS
: input_dimensions.col_count + rownum_col_offset,
&cursor_col, &buff_offset.col);
break;
case ztv_key_escape: // escape
if (row_filter) {
Expand Down Expand Up @@ -363,6 +358,7 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
if (*cmdbuff != '\0') {
row_filter = strdup(cmdbuff);
if (row_filter && !*row_filter) {
// empty string
free(row_filter);
row_filter = NULL;
}
Expand All @@ -383,10 +379,12 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
#endif
);
memcpy(&input_dimensions, &filter_dimensions, sizeof(input_dimensions));
// input_offset.row = header_span;
input_offset.row = 0;
buff_offset.row = 0;
cursor_row = 1;
// not sure why but using ncurses, erase() and refresh() needed for screen to properly redraw
erase();
refresh();
} else {
filter_dimensions.row_count = 0;
ztv_set_status(&display_dims, "Not found");
Expand All @@ -410,7 +408,9 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
if (filter_dimensions.row_count)
ztv_set_status(&display_dims, "(%zu filtered rows) ", filter_dimensions.row_count - 1);
display_buffer_subtable(input_data, buff_offset.row, buff_used_rows, buff_offset.col,
input_dimensions.col_count > ZTV_MAX_COLS ? ZTV_MAX_COLS : input_dimensions.col_count,
input_dimensions.col_count + rownum_col_offset > ZTV_MAX_COLS
? ZTV_MAX_COLS
: input_dimensions.col_count + rownum_col_offset,
cursor_row, cursor_col, header_span, &display_dims);
}

Expand Down
15 changes: 8 additions & 7 deletions app/sheet/read-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ static int read_data(char dest[ZTV_BUFFER_ROWS][ZTV_MAX_COLS][ZTV_MAX_CELL_LEN],
}

// row number
size_t rownum_offset = 0;
size_t rownum_column_offset = 0;
if (ztv_opts->hide_row_nums == 0) {
if (rows_read == 0) // header
snprintf(dest[rows_read][0], ZTV_MAX_CELL_LEN, "Row #");
else if (snprintf(dest[rows_read][0], ZTV_MAX_CELL_LEN, "%zu", original_row_num - 1) >=
ZTV_MAX_CELL_LEN) // unlikely!
sprintf(dest[rows_read][0], "########");
rownum_offset = 1;
rownum_column_offset = 1;
}

for (size_t i = start_col; i < col_count && i + rownum_offset < ZTV_MAX_COLS; i++) {
for (size_t i = start_col; i < col_count + rownum_column_offset && i < ZTV_MAX_COLS; i++) {
struct zsv_cell c = zsv_get_cell(parser, i);
if (c.len) {
char done = 0;
Expand All @@ -93,14 +93,15 @@ static int read_data(char dest[ZTV_BUFFER_ROWS][ZTV_MAX_COLS][ZTV_MAX_CELL_LEN],
size_t bytes_to_copy =
utf8_bytes_up_to_max_width_and_replace_newlines(c.str, c.len, ZTV_MAX_CELL_LEN, &used_width, &err);
if (!err) {
memcpy(dest[rows_read][i + rownum_offset], c.str, bytes_to_copy);
dest[rows_read][i + rownum_offset][bytes_to_copy] = '\0';
memcpy(dest[rows_read][i + rownum_column_offset], c.str, bytes_to_copy);
dest[rows_read][i + rownum_column_offset][bytes_to_copy] = '\0';
done = 1;
}
}
if (!done) {
memcpy(dest[rows_read][i + rownum_offset], c.str, c.len < ZTV_MAX_CELL_LEN ? c.len : ZTV_MAX_CELL_LEN - 1);
dest[rows_read][i + rownum_offset][c.len] = '\0';
memcpy(dest[rows_read][i + rownum_column_offset], c.str,
c.len < ZTV_MAX_CELL_LEN ? c.len : ZTV_MAX_CELL_LEN - 1);
dest[rows_read][i + rownum_column_offset][c.len] = '\0';
}
}
}
Expand Down
42 changes: 41 additions & 1 deletion app/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,18 @@ BUILD_DIR=${THIS_LIB_BASE}/build/${BUILD_SUBDIR}/${CCBN}
TMP_DIR=${THIS_LIB_BASE}/tmp
TEST_DATA_DIR=${THIS_LIB_BASE}/data

SOURCES= echo count count-pull select select-pull sql 2json serialize flatten pretty desc stack 2db 2tsv jq compare
SOURCES=echo count count-pull select select-pull sql 2json serialize flatten pretty desc stack 2db 2tsv jq compare
TARGETS=$(addprefix ${BUILD_DIR}/bin/zsv_,$(addsuffix ${EXE},${SOURCES}))

TESTS=test-blank-leading-rows $(addprefix test-,${SOURCES}) test-rm test-mv test-2json-help

ifeq ($(ZSV_BUILD_SHEET),1)
SOURCES+=sheet
CFLAGS+=-DZSV_BUILD_SHEET
CFLAGS+=${CFLAGS_NCURSES}
LDFLAGS+=${LDFLAGS_NCURSES}
endif

COLOR_NONE=\033[0m
COLOR_GREEN=\033[1;32m
COLOR_RED=\033[1;31m
Expand Down Expand Up @@ -549,3 +556,36 @@ test-compare: test-%: ${BUILD_DIR}/bin/zsv_%${EXE}
@(${PREFIX} $< ../../data/compare/t1.csv ../../data/compare/t2.csv --add AccentCity --sort -k country -k city ${REDIRECT1} ${TMP_DIR}/[email protected] && \
${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL})

test-sheet: test-%: ${BUILD_DIR}/bin/zsv_%${EXE} worldcitiespop_mil.csv
@${TEST_INIT}
@(tmux new-session -x 1000 -y 5 -d -s $@ "${PREFIX} $< worldcitiespop_mil.csv" && \
tmux set-window-option -t test-sheet prefix none && \
sleep 0.5 && \
tmux capture-pane -t $@ -p ${REDIRECT1} ${TMP_DIR}/$@1.out && \
tmux send-keys -t $@ "q" && \
${CMP} ${TMP_DIR}/$@1.out expected/$@1.out && ${TEST_PASS} || ${TEST_FAIL})

@(tmux new-session -x 1000 -y 5 -d -s $@ "${PREFIX} $< worldcitiespop_mil.csv" && \
tmux set-window-option -t test-sheet prefix none && \
sleep 0.5 && \
tmux send-keys -t $@ "C-F" && \
tmux capture-pane -t $@ -p ${REDIRECT1} ${TMP_DIR}/[email protected] && \
tmux send-keys -t $@ "q" && \
${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL})

@(tmux new-session -x 1000 -y 5 -d -s $@ "${PREFIX} $< worldcitiespop_mil.csv" && \
tmux set-window-option -t test-sheet prefix none && \
sleep 0.5 && \
tmux send-keys -t $@ "C-F" "C-F" "C-B" && \
tmux capture-pane -t $@ -p ${REDIRECT1} ${TMP_DIR}/[email protected] && \
tmux send-keys -t $@ "q" && \
${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL})

@(tmux new-session -x 1000 -y 5 -d -s $@ "${PREFIX} $< worldcitiespop_mil.csv" && \
tmux set-window-option -t test-sheet prefix none && \
sleep 0.5 && \
tmux send-keys -t $@ "f" "sarmaj" Enter && \
sleep 0.5 && \
tmux capture-pane -t $@ -p ${REDIRECT1} ${TMP_DIR}/[email protected] && \
tmux send-keys -t $@ "q" && \
${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL})
5 changes: 5 additions & 0 deletions app/test/expected/test-sheet1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Row # Country City AccentCit Region Populatio Latitude Longitude
1 ir sarmaj-e Sarmaj-e 13 34.3578 47.5207
2 ad aixirival Aixirival 06 42.466666 1.5
3 mm mokho-atw Mokho-atw 09 18.033333 96.75
1
5 changes: 5 additions & 0 deletions app/test/expected/test-sheet2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Row # Country City AccentCit Region Populatio Latitude Longitude
4 id selingon Selingon 17 -8.8374 116.4914
5 ir berimvand Berimvand 13 34.2953 47.1096
6 pl chomiaza Chomiaza 73 52.7508 17.841793
4
5 changes: 5 additions & 0 deletions app/test/expected/test-sheet3.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Row # Country City AccentCit Region Populatio Latitude Longitude
1 ir sarmaj-e Sarmaj-e 13 34.3578 47.5207


1
8 changes: 4 additions & 4 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ for arg ; do
--enable-sheet|--enable-sheet=yes) TRY_SHEET=yes;;
--enable-sheet=auto) TRY_SHEET=auto;;
--disable-sheet|--enable-sheet=no) TRY_SHEET=no;;

--enable-lto|--enable-lto=yes) TRY_LTO=yes;;
--enable-lto|--enable-lto=auto) TRY_LTO=auto;;
--disable-lto|--enable-lto=no) TRY_LTO=no;;
Expand Down Expand Up @@ -522,17 +522,17 @@ tryflag CFLAGS_OPENMP -fopenmp
if [ "$TRY_SHEET" != "no" ]; then
if tryldflag LDFLAGS_NCURSES -lncurses -L${PREFIX}/lib ; then
NCLIB=ncurses
if [ "NCURSES_DYNAMIC" != "" ] ; then
if [ "$NCURSES_DYNAMIC" != "" ] ; then
CFLAGS_NCURSES="-DHAVE_NCURSES -DNCURSES_STATIC"
else
CFLAGS_NCURSES=-DHAVE_NCURSES
fi
fi
fi
if tryldflag LDFLAGS_NCURSESW -lncursesw -L${PREFIX}/lib ; then
LDFLAGS_NCURSES="$LDFLAGS_NCURSES -lncursesw"
NCLIB="$NCLIB ncursesw"
if [ "$CFLAGS_NCURSES" = "" ]; then
if [ "NCURSES_DYNAMIC" != "" ] ; then
if [ "$NCURSES_DYNAMIC" != "" ] ; then
CFLAGS_NCURSES="-DHAVE_NCURSESW -DNCURSESW_STATIC"
else
CFLAGS_NCURSES="-DHAVE_NCURSESW"
Expand Down

0 comments on commit 2fb306c

Please sign in to comment.