Skip to content

Commit

Permalink
test: Add some initial unit tests for toxic.
Browse files Browse the repository at this point in the history
These don't do much yet, but will help debug/fix unicode stuff.
  • Loading branch information
iphydf committed Feb 18, 2024
1 parent 5ae055a commit 69f2163
Show file tree
Hide file tree
Showing 11 changed files with 1,624 additions and 1,365 deletions.
22 changes: 22 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
BasedOnStyle: WebKit
ColumnLimit: 100
PointerAlignment: Right
SpacesBeforeTrailingComments: 2
AlignConsecutiveMacros: true
AlignEscapedNewlines: Left
AlwaysBreakTemplateDeclarations: Yes
SpaceBeforeCpp11BracedList: false
Cpp11BracedListStyle: true

IncludeIsMainRegex: '([-_](test|fuzz_test))?$'
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^<.*\.h>'
Priority: 1
SortPriority: 0
- Regex: '^<.*'
Priority: 2
SortPriority: 0
- Regex: '.*'
Priority: 3
SortPriority: 0
5 changes: 5 additions & 0 deletions .restyled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
restylers:
- astyle:
arguments: ["--options=astylerc"]
- clang-format:
image: restyled/restyler-clang-format:v16.0.6
include:
- "**/*.cc"
- "**/*.hh"
72 changes: 59 additions & 13 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
load("//tools/project:build_defs.bzl", "project")

project(license = "gpl3-https")
Expand All @@ -9,33 +9,57 @@ AV_SRCS = [
"src/x11*.c",
]

cc_binary(
name = "toxic",
COPTS = [
"-D_DEFAULT_SOURCE",
"-DGAMES",
"-DHAVE_WIDECHAR",
"-DPACKAGE_DATADIR='\"data\"'",
"-DQRCODE",
] + select({
"//tools/config:linux-x86_64": [
"-DAUDIO",
"-DVIDEO",
"-DPYTHON",
],
"//conditions:default": [],
})

cc_library(
name = "libtoxic",
srcs = glob(
[
"src/*.c",
"src/*.h",
],
exclude = AV_SRCS,
exclude = ["src/main.c"] + AV_SRCS,
) + select({
"//tools/config:linux-x86_64": glob(AV_SRCS),
"//conditions:default": [],
}),
copts = [
"-D_DEFAULT_SOURCE",
"-DGAMES",
"-DHAVE_WIDECHAR",
"-DPACKAGE_DATADIR='\"data\"'",
"-DQRCODE",
copts = COPTS,
deps = [
"//c-toxcore",
"@curl",
"@libconfig",
"@libqrencode",
"@ncurses",
] + select({
"//tools/config:linux-x86_64": [
"-DAUDIO",
"-DVIDEO",
"-DPYTHON",
"@libvpx",
"@openal",
"@python3//:python",
"@x11",
],
"//conditions:default": [],
}),
)

cc_binary(
name = "toxic",
srcs = ["src/main.c"],
copts = COPTS,
deps = [
":libtoxic",
"//c-toxcore",
"@curl",
"@libconfig",
Expand All @@ -58,3 +82,25 @@ sh_test(
srcs = [":toxic"],
args = ["--help"],
)

cc_test(
name = "line_info_test",
size = "small",
srcs = ["src/line_info_test.cc"],
deps = [
":libtoxic",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "misc_tools_test",
size = "small",
srcs = ["src/misc_tools_test.cc"],
deps = [
":libtoxic",
"@com_google_googletest//:gtest",
"@com_google_googletest//:gtest_main",
],
)
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ LDFLAGS += ${USER_LDFLAGS}

OBJ = autocomplete.o avatars.o bootstrap.o chat.o chat_commands.o conference.o configdir.o curl_util.o execute.o
OBJ += file_transfers.o friendlist.o global_commands.o conference_commands.o groupchats.o groupchat_commands.o help.o
OBJ += input.o line_info.o log.o message_queue.o misc_tools.o name_lookup.o notify.o prompt.o qr_code.o settings.o
OBJ += input.o line_info.o log.o main.o message_queue.o misc_tools.o name_lookup.o notify.o prompt.o qr_code.o settings.o
OBJ += term_mplex.o toxic.o toxic_strings.o windows.o

# Check if debug build is enabled
Expand Down
6 changes: 4 additions & 2 deletions src/line_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define _GNU_SOURCE /* needed for wcswidth() */
#endif

#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdlib.h>
Expand Down Expand Up @@ -348,7 +349,7 @@ static int print_wrap(WINDOW *win, struct line_info *line, int max_x, int max_y)
*
* Returns the widechar width of the string.
*/
static uint16_t line_info_add_msg(wchar_t *buf, size_t buf_size, const char *msg)
uint16_t line_info_add_msg(wchar_t *buf, size_t buf_size, const char *msg)
{
if (msg == NULL || msg[0] == '\0') {
return 0;
Expand All @@ -366,7 +367,8 @@ static uint16_t line_info_add_msg(wchar_t *buf, size_t buf_size, const char *msg

return (uint16_t)width;
} else {
fprintf(stderr, "Failed to convert string '%s' to widechar\n", msg);
fprintf(stderr, "Failed to convert string '%s' to widechar (len=%d, error=%s)\n",
msg, wc_msg_len, strerror(errno));
const wchar_t *err = L"Failed to parse message";
uint16_t width = (uint16_t)wcslen(err);
wmemcpy(buf, err, width);
Expand Down
20 changes: 20 additions & 0 deletions src/line_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#include "toxic.h"
#include "windows.h"

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#define MAX_HISTORY 100000
#define MIN_HISTORY 40
#define MAX_LINE_INFO_QUEUE 1024
Expand Down Expand Up @@ -124,4 +128,20 @@ void line_info_init(struct history *hst);
/* returns true if key is a match */
bool line_info_onKey(ToxWindow *self, const Client_Config *c_config, wint_t key);

/**
* Converts the multibyte string `msg` into a wide character string and puts
* the result in `buf`.
*
* @param buf is the buffer to put the wide character string in.
* @param buf_size is the number of wide characters that `buf` can hold.
*
* @return the widechar width of the string.
* @private
*/
uint16_t line_info_add_msg(wchar_t *buf, size_t buf_size, const char *msg);

#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */

#endif /* LINE_INFO_H */
13 changes: 13 additions & 0 deletions src/line_info_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "line_info.h"

#include <gtest/gtest.h>

namespace {

TEST(LineInfo, TextWidth)
{
wchar_t buf[100];
EXPECT_EQ(line_info_add_msg(buf, 100, "Hello, world!"), 13);
}

} // namespace
Loading

0 comments on commit 69f2163

Please sign in to comment.