Skip to content

Commit

Permalink
[Tests] Increase QMK test coverage (#13789)
Browse files Browse the repository at this point in the history
* Add per-test keymaps

* Add better trace and info logs for failed unit-tests

* Add layer state assertion with tracing message

* Use individual test binaries configuration options

* Add basic qmk functionality tests

* Add tap hold configurations tests

* Add auto shift tests

* `qmk format-c

* Fix tests

Co-authored-by: Nick Brassel <[email protected]>
  • Loading branch information
KarlK90 and tzarc authored Nov 22, 2021
1 parent f4966a1 commit b6054c0
Show file tree
Hide file tree
Showing 49 changed files with 2,688 additions and 265 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,12 @@ define PARSE_ALL_KEYMAPS
endef

define BUILD_TEST
TEST_NAME := $1
TEST_PATH := $1
TEST_NAME := $$(notdir $$(TEST_PATH))
MAKE_TARGET := $2
COMMAND := $1
MAKE_CMD := $$(MAKE) -r -R -C $(ROOT_DIR) -f build_test.mk $$(MAKE_TARGET)
MAKE_VARS := TEST=$$(TEST_NAME) FULL_TESTS="$$(FULL_TESTS)"
MAKE_VARS := TEST=$$(TEST_NAME) TEST_PATH=$$(TEST_PATH) FULL_TESTS="$$(FULL_TESTS)"
MAKE_MSG := $$(MSG_MAKE_TEST)
$$(eval $$(call BUILD))
ifneq ($$(MAKE_TARGET),clean)
Expand All @@ -406,7 +407,7 @@ define PARSE_TEST
ifeq ($$(TEST_NAME),all)
MATCHED_TESTS := $$(TEST_LIST)
else
MATCHED_TESTS := $$(foreach TEST,$$(TEST_LIST),$$(if $$(findstring $$(TEST_NAME),$$(TEST)),$$(TEST),))
MATCHED_TESTS := $$(foreach TEST, $$(TEST_LIST),$$(if $$(findstring $$(TEST_NAME), $$(notdir $$(TEST))), $$(TEST),))
endif
$$(foreach TEST,$$(MATCHED_TESTS),$$(eval $$(call BUILD_TEST,$$(TEST),$$(TEST_TARGET))))
endef
Expand Down
23 changes: 13 additions & 10 deletions build_full_test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

#include $(TMK_PATH)/protocol.mk
$(TEST)_INC := \
tests\test_common\common_config.h

TEST_PATH=tests/$(TEST)

$(TEST)_SRC= \
$(TEST_PATH)/keymap.c \
$(TEST)_SRC := \
$(TMK_COMMON_SRC) \
$(QUANTUM_SRC) \
$(SRC) \
tests/test_common/keymap.c \
tests/test_common/matrix.c \
tests/test_common/test_driver.cpp \
tests/test_common/keyboard_report_util.cpp \
tests/test_common/test_fixture.cpp
$(TEST)_SRC += $(patsubst $(ROOTDIR)/%,%,$(wildcard $(TEST_PATH)/*.cpp))
tests/test_common/test_fixture.cpp \
tests/test_common/test_keymap_key.cpp \
tests/test_common/test_logger.cpp \
$(patsubst $(ROOTDIR)/%,%,$(wildcard $(TEST_PATH)/*.cpp))

$(TEST)_DEFS := $(TMK_COMMON_DEFS) $(OPT_DEFS)

$(TEST)_CONFIG := $(TEST_PATH)/config.h

$(TEST)_DEFS=$(TMK_COMMON_DEFS) $(OPT_DEFS)
$(TEST)_CONFIG=$(TEST_PATH)/config.h
VPATH+=$(TOP_DIR)/tests/test_common
VPATH += $(TOP_DIR)/tests/test_common
19 changes: 10 additions & 9 deletions build_test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ TEST_OBJ = $(BUILD_DIR)/test_obj
OUTPUTS := $(TEST_OBJ)/$(TEST) $(GTEST_OUTPUT)

GTEST_INC := \
$(LIB_PATH)/googletest/googletest/include\
$(LIB_PATH)/googletest/googlemock/include\
$(LIB_PATH)/googletest/googletest/include \
$(LIB_PATH)/googletest/googlemock/include

GTEST_INTERNAL_INC :=\
$(LIB_PATH)/googletest/googletest\
GTEST_INTERNAL_INC := \
$(LIB_PATH)/googletest/googletest \
$(LIB_PATH)/googletest/googlemock

$(GTEST_OUTPUT)_SRC :=\
$(GTEST_OUTPUT)_SRC := \
googletest/src/gtest-all.cc\
googlemock/src/gmock-all.cc

Expand All @@ -33,9 +33,9 @@ $(GTEST_OUTPUT)_INC := $(GTEST_INC) $(GTEST_INTERNAL_INC)
LDFLAGS += -lstdc++ -lpthread -shared-libgcc
CREATE_MAP := no

VPATH +=\
$(LIB_PATH)/googletest\
$(LIB_PATH)/googlemock\
VPATH += \
$(LIB_PATH)/googletest \
$(LIB_PATH)/googlemock \
$(LIB_PATH)/printf

all: elf
Expand All @@ -49,7 +49,8 @@ CONSOLE_ENABLE = yes
endif

ifneq ($(filter $(FULL_TESTS),$(TEST)),)
include tests/$(TEST)/rules.mk
include tests/test_common/build.mk
include $(TEST_PATH)/test.mk
endif

include common_features.mk
Expand Down
34 changes: 0 additions & 34 deletions quantum/quantum_keycodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -558,40 +558,6 @@ enum quantum_keycodes {
PROGRAMMABLE_BUTTON_31,
PROGRAMMABLE_BUTTON_32,

// Dedicated macro keys for Configurator and VIA
MACRO_0,
MACRO_1,
MACRO_2,
MACRO_3,
MACRO_4,
MACRO_5,
MACRO_6,
MACRO_7,
MACRO_8,
MACRO_9,
MACRO_10,
MACRO_11,
MACRO_12,
MACRO_13,
MACRO_14,
MACRO_15,
MACRO_16,
MACRO_17,
MACRO_18,
MACRO_19,
MACRO_20,
MACRO_21,
MACRO_22,
MACRO_23,
MACRO_24,
MACRO_25,
MACRO_26,
MACRO_27,
MACRO_28,
MACRO_29,
MACRO_30,
MACRO_31,

// Start of custom keycode range for keyboards and keymaps - always leave at the end
SAFE_RANGE
};
Expand Down
4 changes: 2 additions & 2 deletions testlist.mk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TEST_LIST = $(notdir $(patsubst %/rules.mk,%,$(wildcard $(ROOT_DIR)/tests/*/rules.mk)))
FULL_TESTS := $(TEST_LIST)
TEST_LIST = $(sort $(patsubst %/test.mk,%, $(shell find $(ROOT_DIR)tests -type f -name test.mk)))
FULL_TESTS := $(notdir $(TEST_LIST))

include $(QUANTUM_PATH)/debounce/tests/testlist.mk
include $(QUANTUM_PATH)/encoder/tests/testlist.mk
Expand Down
19 changes: 19 additions & 0 deletions tests/auto_shift/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* Copyright 2021 Stefan Kerkmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "test_common.h"
20 changes: 20 additions & 0 deletions tests/auto_shift/test.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2021 Stefan Kerkmann
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# --------------------------------------------------------------------------------
# Keep this file, even if it is empty, as a marker that this folder contains tests
# --------------------------------------------------------------------------------

AUTO_SHIFT_ENABLE = yes
73 changes: 73 additions & 0 deletions tests/auto_shift/test_auto_shift.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* Copyright 2021 Stefan Kerkmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "keyboard_report_util.hpp"
#include "keycode.h"
#include "test_common.hpp"
#include "action_tapping.h"
#include "test_fixture.hpp"
#include "test_keymap_key.hpp"

using testing::_;
using testing::InSequence;

class AutoShift : public TestFixture {};

TEST_F(AutoShift, key_release_before_timeout) {
TestDriver driver;
InSequence s;
auto regular_key = KeymapKey(0, 2, 0, KC_A);

set_keymap({regular_key});

/* Press regular key */
EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
regular_key.press();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);

/* Release regular key */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_A)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
regular_key.release();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
}

TEST_F(AutoShift, key_release_after_timeout) {
TestDriver driver;
InSequence s;
auto regular_key = KeymapKey(0, 2, 0, KC_A);

set_keymap({regular_key});

/* Press regular key */
EXPECT_CALL(driver, send_keyboard_mock(_)).Times(0);
regular_key.press();
idle_for(AUTO_SHIFT_TIMEOUT);
testing::Mock::VerifyAndClearExpectations(&driver);

/* Release regular key */
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT, KC_A)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport(KC_LSFT)));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
EXPECT_CALL(driver, send_keyboard_mock(KeyboardReport()));
regular_key.release();
run_one_scan_loop();
testing::Mock::VerifyAndClearExpectations(&driver);
}
3 changes: 1 addition & 2 deletions tests/basic/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@

#pragma once

#define MATRIX_ROWS 4
#define MATRIX_COLS 10
#include "test_common.h"
45 changes: 0 additions & 45 deletions tests/basic/keymap.c

This file was deleted.

18 changes: 18 additions & 0 deletions tests/basic/test.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2017 Fred Sundvik
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# --------------------------------------------------------------------------------
# Keep this file, even if it is empty, as a marker that this folder contains tests
# --------------------------------------------------------------------------------
Loading

0 comments on commit b6054c0

Please sign in to comment.