diff --git a/Makefile b/Makefile index e65e629..598be07 100644 --- a/Makefile +++ b/Makefile @@ -21,35 +21,40 @@ UNITY_C = $(TEST_DIR)/unity/unity.c TEST_SRC_FILES = $(wildcard $(TEST_DIR)/*.c) TEST_OBJ_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_OBJ_DIR)/%.o, $(TEST_SRC_FILES)) +# Stupid things. +RESETCOLOR = \x1b[0m +WHITE = $(RESETCOLOR)\x1b[37m +WHITE_BOLD = $(RESETCOLOR)\x1b[37;1m + all: $(TARGET) # Link to final binary. $(TARGET): $(OBJ_FILES) - @ echo -e "\x1b[37;1mLinking \x1b[0m\x1b[37m$(TARGET)\x1b[37;1m...\x1b[0m\x1b[37m $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)\x1b[0m" + @ echo -e "$(WHITE_BOLD)Linking $(WHITE)$(TARGET)$(WHITE_BOLD)...$(RESETCOLOR) $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)" @ $(LINK) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS) # Compile project sources. $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(SRC_DIR)/include/%.h @ mkdir -p $(OBJ_DIR) - @ echo -e "\x1b[37;1mCompiling \x1b[0m\x1b[37m$<\x1b[37;1m... \x1b[0m\x1b[37m$(CC) $(CFLAGS) -c $< -o $@\x1b[0m" + @ echo -e "$(WHITE_BOLD)Compiling $(WHITE)$<$(WHITE_BOLD)... $(RESETCOLOR)$(CC) $(CFLAGS) -c $< -o $@" @ $(CC) $(CFLAGS) -c $< -o $@ # Compile test sources. $(TEST_OBJ_DIR)/%.o: $(TEST_DIR)/%.c @ mkdir -p $(TEST_OBJ_DIR) - @ echo -e "\x1b[37;1mCompiling Test \x1b[0m\x1b[37m$<\x1b[37;1m... \x1b[0m\x1b[37m$(CC) $(CFLAGS) -I$(SRC_DIR)/include -c $< -o $@\x1b[0m" + @ echo -e "$(WHITE_BOLD)Compiling Test $(WHITE)$<$(WHITE_BOLD)... $(WHITE)$(CC) $(CFLAGS) -I$(SRC_DIR)/include -c $< -o $@$(RESETCOLOR)" @ $(CC) $(CFLAGS) -I$(SRC_DIR)/include -c $< -o $@ # Link the test executable. test: $(TEST_OBJ_FILES) $(OBJ_FILES_NOMAIN) $(UNITY_C) - @ echo -e "\x1b[37;1mLinking test binary\x1b[0m\x1b[37m...\x1b[0m" - @ $(LINK) $(TEST_OBJ_FILES) $(OBJ_FILES_NOMAIN) $(UNITY_C) -o $(TEST_BUILD_DIR)/test.out - @ echo -e "\x1b[37;1mRunning tests\x1b[0m\x1b[37m...\x1b[0m" + @ echo -e "$(WHITE_BOLD)Linking test binary$(WHITE)...$(RESETCOLOR)" + @ $(LINK) -DUNITY_OUTPUT_COLOR $(TEST_OBJ_FILES) $(OBJ_FILES_NOMAIN) $(UNITY_C) -o $(TEST_BUILD_DIR)/test.out + @ echo -e "$(WHITE_BOLD)Running tests$(WHITE)...$(RESETCOLOR)" @ ./$(TEST_BUILD_DIR)/test.out clean: - @ echo -e "\x1b[37;1mCleaning up...\x1b[0m\x1b[37m $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET)\x1b[0m" + @ echo -e "$(WHITE_BOLD)Cleaning up...$(WHITE) $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET)$(RESETCOLOR)" @ rm -rf $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET) -.PHONY: all clean test +.PHONY: all clean test nocolor diff --git a/src/include/lexer.h b/src/include/lexer.h new file mode 100644 index 0000000..9c2798b --- /dev/null +++ b/src/include/lexer.h @@ -0,0 +1,12 @@ +#ifndef LEXER_H +#define LEXER_H + +#include "token.h" + +// Lexer: converts text to tokens. +typedef struct Lexer { + char* src; + +} lexer_t; + +#endif diff --git a/src/include/token.h b/src/include/token.h new file mode 100644 index 0000000..a758e43 --- /dev/null +++ b/src/include/token.h @@ -0,0 +1,18 @@ +#ifndef TOKEN_H +#define TOKEN_H + +typedef enum TokenType { + TOKEN_TYPE_CALL, + TOKEN_TYPE_NUMBER, +} TokenType; + +// Token. +typedef struct Token { + TokenType type; // The type of the Token. + char* val; // The text of the Token. +} Token; + +Token* token_init(TokenType type, char* val); +void token_destroy(Token* token); + +#endif diff --git a/src/token.c b/src/token.c new file mode 100644 index 0000000..a227d60 --- /dev/null +++ b/src/token.c @@ -0,0 +1,17 @@ +#include + +#include "include/token.h" + +Token* token_init(TokenType type, char* val) { + Token* t = malloc(sizeof(Token)); + + t->type = type; + t->val = val; + + return t; +} + +void token_destroy(Token* t) { + free(t->val); + free(t); +} diff --git a/test/main.c b/test/main.c new file mode 100644 index 0000000..7a793a0 --- /dev/null +++ b/test/main.c @@ -0,0 +1,12 @@ +#include "unity/unity.h" +#include "registry.h" + +void (*tests[30])(); + +void setUp() {} +void tearDown() {} + +int main() { + run_all_tests(); + return 0; +} diff --git a/test/registry.c b/test/registry.c new file mode 100644 index 0000000..bef64c5 --- /dev/null +++ b/test/registry.c @@ -0,0 +1,19 @@ +#include "unity/unity_internals.h" +#include "registry.h" + +#define TESTS_MAX 128 + +static Test tests[TESTS_MAX]; +static int test_count = 0; + +void register_test(Test t) { + if (test_count < TESTS_MAX) { + tests[test_count] = t; + test_count++; + } else + printf("Maximum number of tests (%d) exceeded.\n", TESTS_MAX); +} + +void run_all_tests() { + for (int i = 0; i < test_count; i++) tests[i](); +} diff --git a/test/registry.h b/test/registry.h new file mode 100644 index 0000000..c964751 --- /dev/null +++ b/test/registry.h @@ -0,0 +1,15 @@ +#ifndef REGISTRY_H +#define REGISTRY_H + +#include + +// Test functions neither consume nor return anything. +typedef void (*Test)(void); + +// Register a new test function. +void register_test(Test t); + +// Run all registered tests. +void run_all_tests(); + +#endif diff --git a/test/token.c b/test/token.c new file mode 100644 index 0000000..8a383d7 --- /dev/null +++ b/test/token.c @@ -0,0 +1,32 @@ +#include +#include "unity/unity.h" + +#include "registry.h" +#include "unity/unity_internals.h" +#include "../src/include/token.h" + +void test_token_init() { + char* s = malloc(sizeof("Hello, world!")); + s = "Hello, world!"; + Token* t = token_init(TOKEN_TYPE_CALL, s); + TEST_ASSERT_EQUAL(TOKEN_TYPE_NUMBER, t->type); + TEST_ASSERT_EQUAL_STRING("Hellso, world!", t->val); +} + +void test_token_destroy() { + char* s = malloc(1 * sizeof(char)); + *s = 'h'; + Token* t = token_init(TOKEN_TYPE_CALL, s); + token_destroy(t); +} + +void token_test() { + UNITY_BEGIN(); + RUN_TEST(test_token_init); + RUN_TEST(test_token_destroy); + UNITY_END(); +} + +__attribute__((constructor)) void register_tests_token() { + register_test(token_test); +} diff --git a/test/unity/unity.c b/test/unity/unity.c index 04d716d..f04711c 100644 --- a/test/unity/unity.c +++ b/test/unity/unity.c @@ -24,10 +24,10 @@ void UNITY_OUTPUT_CHAR(int); struct UNITY_STORAGE_T Unity; #ifdef UNITY_OUTPUT_COLOR -const char UNITY_PROGMEM UnityStrOk[] = "\033[42mOK\033[0m"; -const char UNITY_PROGMEM UnityStrPass[] = "\033[42mPASS\033[0m"; -const char UNITY_PROGMEM UnityStrFail[] = "\033[41mFAIL\033[0m"; -const char UNITY_PROGMEM UnityStrIgnore[] = "\033[43mIGNORE\033[0m"; +const char UNITY_PROGMEM UnityStrOk[] = "\033[32;1mOK\033[0m"; +const char UNITY_PROGMEM UnityStrPass[] = "\033[32;1mPASS\033[0m"; +const char UNITY_PROGMEM UnityStrFail[] = "\033[31;1mFAIL\033[0m"; +const char UNITY_PROGMEM UnityStrIgnore[] = "\033[30;1mIGNORE\033[0m"; #else const char UNITY_PROGMEM UnityStrOk[] = "OK"; const char UNITY_PROGMEM UnityStrPass[] = "PASS"; diff --git a/test/util.c b/test/util.c index 9734197..5eef12e 100644 --- a/test/util.c +++ b/test/util.c @@ -1,17 +1,19 @@ #include "unity/unity.h" - -#include "../src/include//util.h" - -void setUp() {} -void tearDown() {} +#include "registry.h" +#include "unity/unity_internals.h" +#include "../src/include/util.h" void test_is_even() { TEST_ASSERT_EQUAL(0, is_even(1)); TEST_ASSERT_EQUAL(1, is_even(2)); } -int main() { +void util_test() { UNITY_BEGIN(); RUN_TEST(test_is_even); - return UNITY_END(); + UNITY_END(); +} + +__attribute__((constructor)) void register_tests_util() { + register_test(util_test); }