Merge pull request #421 from ttytm/ci/add-test

This commit is contained in:
Turiiya 2024-06-16 04:43:51 +02:00 committed by GitHub
commit 6cefcb1f3d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 325 additions and 6 deletions

View File

@ -77,7 +77,7 @@ jobs:
- name: Build TLS Release Target
if: matrix.arch == 'x64'
run: make CC="$CC" WEBUI_USE_TLS=1
- name: Build examples
- name: Build Examples
if: matrix.arch == 'x64'
run: |
examples_base_dir=$(pwd)/examples/C
@ -96,6 +96,35 @@ jobs:
fi
done
exit $exit_code
- name: Setup Browser
if: matrix.arch == 'x64'
uses: browser-actions/setup-chrome@v1
- name: Setup Tests
if: matrix.arch == 'x64'
run: |
sudo apt update && sudo apt install xvfb
cd tests
make call_c_from_js.c
make fail_test.c
ls -1
- name: Test Static
if: matrix.arch == 'x64'
timeout-minutes: 2
run: |
cd tests
call_c_from_js=$(pwd)/call_c_from_js
fail_test=$(pwd)/fail_test
xvfb-run "$call_c_from_js"
xvfb-run --auto-servernum --server-num=1 "$fail_test" && exit 1 || true
- name: Test Dynamic
if: matrix.arch == 'x64'
timeout-minutes: 2
run: |
cd tests
call_c_from_js=$(pwd)/call_c_from_js-dyn
fail_test=$(pwd)/fail_test-dyn
xvfb-run "$call_c_from_js"
xvfb-run --auto-servernum --server-num=1 "$fail_test" && exit 1 || true
- name: Prepare Artifact
run: |
cp -r include dist

12
.gitignore vendored
View File

@ -8,6 +8,13 @@ build/
*.out
*.def
# All sub-level build binaries
*/**/*
!*/**/*/
!*/**/*.*
!*makefile
!Makefile
# Compiled Object files
*.slo
*.lo
@ -108,11 +115,6 @@ Thumbs.db
# User-specific private settings
*.DotSettings.user
# Example binaries
examples/**/main
examples/**/main-dyn
examples/**/text-editor/text-editor
# Compressed
*.zip
*.gz

156
tests/GNUmakefile Normal file
View File

@ -0,0 +1,156 @@
# WebUI C Tests
# == 1. VARIABLES =============================================================
MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
PROJECT_DIR := $(dir $(MAKEFILE_PATH))/../
TEST_SRC := $(firstword $(MAKECMDGOALS))
TEST_OUT = $(patsubst %.c,%,$(TEST_SRC))
LIB_DIR := $(PROJECT_DIR)/dist
ifeq ($(WEBUI_DEBUG), 1)
LIB_DIR := $(LIB_DIR)/debug
endif
INCLUDE_DIR := $(PROJECT_DIR)/include
WEBUI_LIB_NAME = webui-2
ifeq ($(WEBUI_USE_TLS), 1)
WEBUI_LIB_NAME = webui-2-secure
endif
# Build the WebUI library if running via `make BUILD_LIB=true`
CC ?= gcc
BUILD_LIB ?=
ifeq ($(CC), cc)
ifeq ($(shell uname),Darwin)
CC = clang
else
CC = gcc
endif
endif
# BUILD FLAGS
STATIC_BUILD_FLAGS = $(TEST_SRC) -I"$(INCLUDE_DIR)" -L"$(LIB_DIR)"
DYN_BUILD_FLAGS = $(TEST_SRC) -I"$(INCLUDE_DIR)" -L"$(LIB_DIR)"
# Platform conditions
ifeq ($(OS),Windows_NT)
# Windows
PLATFORM := windows
SHELL := CMD
STATIC_BUILD_FLAGS += -l$(WEBUI_LIB_NAME)-static -lws2_32 -Wall -luser32 -static
COPY_LIB_CMD := @copy "$(LIB_DIR)\$(WEBUI_LIB_NAME).dll" "$(WEBUI_LIB_NAME).dll"
DYN_BUILD_FLAGS += "$(WEBUI_LIB_NAME).dll" -lws2_32 -Wall -luser32
STATIC_OUT := $(TEST_OUT).exe
DYN_OUT := $(TEST_OUT)-dyn.exe
LWS2_OPT := -lws2_32 -lOle32
STRIP_OPT := --strip-all
CONSOLE_APP := -Wl,-subsystem=console
GUI_APP := -Wl,-subsystem=windows
else
STATIC_BUILD_FLAGS += -lpthread -lm -l$(WEBUI_LIB_NAME)-static
DYN_BUILD_FLAGS += -lpthread -lm
STATIC_OUT := $(TEST_OUT)
DYN_OUT := $(TEST_OUT)-dyn
ifeq ($(shell uname),Darwin)
# MacOS
PLATFORM := macos
COPY_LIB_CMD := @cp "$(LIB_DIR)/$(WEBUI_LIB_NAME).dylib" "$(WEBUI_LIB_NAME).dylib"
DYN_BUILD_FLAGS += "./$(WEBUI_LIB_NAME).dylib"
WKWEBKIT_LINK_FLAGS := -framework Cocoa -framework WebKit
else
# Linux
PLATFORM := linux
COPY_LIB_CMD := @cp "$(LIB_DIR)/$(WEBUI_LIB_NAME).so" "$(WEBUI_LIB_NAME).so"
STATIC_BUILD_FLAGS += -ldl
DYN_BUILD_FLAGS += -ldl "./$(WEBUI_LIB_NAME).so"
STRIP_OPT := --strip-all
ifeq ($(CC),clang)
LLVM_OPT := llvm-
endif
endif
endif
# == 2.TARGETS ================================================================
*.c: all
.PHONY: all
all: release
.PHONY: debug
debug:
ifeq ($(BUILD_LIB),true)
@cd "$(PROJECT_DIR)" && $(MAKE) debug
endif
# Static with Debug info
ifneq ($(WEBUI_USE_TLS), 1)
@echo "Building '$(TEST_SRC)' ($(CC) debug static)..."
@$(CC) -g $(CONSOLE_APP) $(STATIC_BUILD_FLAGS) $(LWS2_OPT) $(WKWEBKIT_LINK_FLAGS) -o $(STATIC_OUT)
endif
# Dynamic with Debug info
@echo "Building '$(TEST_SRC)' ($(CC) debug dynamic)..."
$(COPY_LIB_CMD)
@$(CC) -g $(CONSOLE_APP) $(DYN_BUILD_FLAGS) $(LWS2_OPT) $(WKWEBKIT_LINK_FLAGS) -o $(DYN_OUT)
# Clean
ifeq ($(PLATFORM),windows)
@- del *.o >nul 2>&1
else
@- rm -f *.o
@- rm -rf *.dSYM # macOS
endif
@echo "Done."
.PHONY: .release
release:
ifeq ($(BUILD_LIB),true)
@cd "$(PROJECT_DIR)" && $(MAKE)
endif
# Static Release
ifneq ($(WEBUI_USE_TLS), 1)
@echo "Building '$(TEST_SRC)' ($(CC) static)..."
@$(CC) -Os $(GUI_APP) $(STATIC_BUILD_FLAGS) $(LWS2_OPT) $(WKWEBKIT_LINK_FLAGS) -o $(STATIC_OUT)
@$(LLVM_OPT)strip $(STRIP_OPT) $(STATIC_OUT)
endif
# Dynamic Release
@echo "Building '$(TEST_SRC)' ($(CC) dynamic)..."
$(COPY_LIB_CMD)
@$(CC) $(GUI_APP) $(DYN_BUILD_FLAGS) $(LWS2_OPT) $(WKWEBKIT_LINK_FLAGS) -o $(DYN_OUT)
@$(LLVM_OPT)strip $(STRIP_OPT) $(DYN_OUT)
# Clean
ifeq ($(PLATFORM),windows)
@- del *.o >nul 2>&1
else
@- rm -f *.o
@- rm -rf *.dSYM # macOS
endif
@echo "Done."
clean: --clean-$(PLATFORM)
# INTERNAL TARGETS
.PHONY: --clean-linux
--clean-linux: --clean-unix
.PHONY: --clean-macos
--clean-macos: clean-unix
.PHONY: --clean-unix
--clean-unix:
- rm -f *.o
- rm -f *.a
- rm -f *.so
- rm -f *.dylib
- rm -rf *.dSYM
.PHONY: --clean-windows
--clean-windows:
- del *.o >nul 2>&1
- del *.dll >nul 2>&1
- del *.a >nul 2>&1
- del *.exe >nul 2>&1

102
tests/call_c_from_js.c Normal file
View File

@ -0,0 +1,102 @@
#include "webui.h"
const char *doc =
"<html style=\"background: #654da9; color: #eee\">"
"<head>"
" <script src=\"webui.js\"></script>"
"</head>"
"<body>"
" <script>"
" setTimeout(async () => {"
" await webui.assert_int(1, 23, 345);"
" await webui.assert_float(1.0, 2.3, 3.45);"
" await webui.assert_string('foo', 'bar', 'baz');"
" await webui.assert_bool(true, false, true);"
" await webui.assert_close();"
" }, 500)"
" </script>"
"</body>"
"</html>";
void assert_int(webui_event_t *e) {
size_t count = webui_get_count(e);
assert(count == 3);
long long num = webui_get_int(e);
long long num1 = webui_get_int_at(e, 0);
long long num2 = webui_get_int_at(e, 1);
long long num3 = webui_get_int_at(e, 2);
assert(num == 1);
assert(num1 == num);
assert(num2 == 23);
assert(num3 == 345);
}
void assert_float(webui_event_t *e) {
size_t count = webui_get_count(e);
assert(count == 3);
double num = webui_get_float(e);
double num1 = webui_get_float_at(e, 0);
double num2 = webui_get_float_at(e, 1);
double num3 = webui_get_float_at(e, 2);
printf("num1: %f, num2: %f, num3: %f\n", num1, num2, num3);
// TODO: enable asserts after get_float is fixed.
// assert(num == 1.0);
// assert(num1 == num);
// assert(num2 == 2.3);
// assert(num3 == 3.45);
}
void assert_string(webui_event_t *e) {
size_t count = webui_get_count(e);
assert(count == 3);
const char *str = webui_get_string(e);
const char *str1 = webui_get_string_at(e, 0);
const char *str2 = webui_get_string_at(e, 1);
const char *str3 = webui_get_string_at(e, 2);
assert(strcmp(str, "foo") == 0);
assert(strcmp(str1, str) == 0);
assert(strcmp(str2, "bar") == 0);
assert(strcmp(str3, "baz") == 0);
}
void assert_bool(webui_event_t *e) {
size_t count = webui_get_count(e);
assert(count == 3);
long long b = webui_get_bool(e);
long long b1 = webui_get_bool_at(e, 0);
long long b2 = webui_get_bool_at(e, 1);
long long b3 = webui_get_bool_at(e, 2);
assert(b == true);
assert(b1 == b);
assert(b2 == false);
assert(b3 == true);
}
void assert_close(webui_event_t *e) {
// Closing often leads to a seqfault at the moment. Therefore, just a sysexit for now.
// webui_close(e->window);
exit(EXIT_SUCCESS);
}
int main() {
size_t w = webui_new_window();
webui_bind(w, "assert_int", assert_int);
webui_bind(w, "assert_float", assert_float);
webui_bind(w, "assert_string", assert_string);
webui_bind(w, "assert_bool", assert_bool);
webui_bind(w, "assert_close", assert_close);
webui_show(w, doc);
webui_wait();
webui_clean();
return 0;
}

30
tests/fail_test.c Normal file
View File

@ -0,0 +1,30 @@
#include "webui.h"
const char *doc =
"<html style=\"background: #654da9; color: #eee\">"
"<head>"
" <script src=\"webui.js\"></script>"
"</head>"
"<body>"
" <script>"
" setTimeout(async () => {"
" await webui.call(\"fail\");"
" }, 500)"
" </script>"
"</body>"
"</html>";
void fail(webui_event_t *e) {
// This test should help to ensure that test failures can be detected from CI.
exit(EXIT_FAILURE);
}
int main() {
size_t w = webui_new_window();
webui_bind(w, "fail", fail);
webui_show(w, doc);
webui_wait();
return 0;
}