From 4c32cd171c5738c09decc33eb8964b1354e25435 Mon Sep 17 00:00:00 2001 From: Ezekiel Warren Date: Thu, 28 Sep 2023 20:09:14 -0700 Subject: [PATCH] fix: fix string allocation --- .gitignore | 6 +-- test/parse_test_system_notify_statement.cc | 10 ++--- test/parse_test_util.hh | 49 ++++++++++++---------- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 16b226c..4926fff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ -bazel-*/ +bazel-* user.bazelrc compile_commands.json -external/ -.cache/ +external +.cache diff --git a/test/parse_test_system_notify_statement.cc b/test/parse_test_system_notify_statement.cc index 8e2dcd4..963c71c 100644 --- a/test/parse_test_system_notify_statement.cc +++ b/test/parse_test_system_notify_statement.cc @@ -9,8 +9,8 @@ using namespace std::string_literals; TEST(Parse, SystemNotifyShorthand) { - auto statement = ecsact_statement{}; - STATEMENT_OK(&statement, SYSTEM, "notify always;"); + PREPARE_TEST_STATEMENT(); + STATEMENT_OK(SYSTEM, "notify always;"); EXPECT_EQ(statement.type, ECSACT_STATEMENT_SYSTEM_NOTIFY); @@ -21,8 +21,8 @@ TEST(Parse, SystemNotifyShorthand) { } TEST(Parse, SystemNotifyLongForm) { - auto statement = ecsact_statement{}; - STATEMENT_OK(&statement, SYSTEM_NOTIFY, "always ExampleComponent;"); + PREPARE_TEST_STATEMENT(); + STATEMENT_OK(SYSTEM_NOTIFY, "always ExampleComponent;"); EXPECT_EQ(statement.type, ECSACT_STATEMENT_SYSTEM_NOTIFY_COMPONENT); @@ -35,7 +35,7 @@ TEST(Parse, SystemNotifyLongForm) { statement.data.system_notify_component_statement.component_name ); - STATEMENT_OK(&statement, SYSTEM_NOTIFY, "onchange AnotherComponent;"); + STATEMENT_OK(SYSTEM_NOTIFY, "onchange AnotherComponent;"); EXPECT_EQ(statement.type, ECSACT_STATEMENT_SYSTEM_NOTIFY_COMPONENT); diff --git a/test/parse_test_util.hh b/test/parse_test_util.hh index a0975a3..82531db 100644 --- a/test/parse_test_util.hh +++ b/test/parse_test_util.hh @@ -1,28 +1,35 @@ #pragma once +#include #include "gtest/gtest.h" -#define STATEMENT_OK(OutStatement, Context, StatementString) \ - { \ - auto context = ecsact_statement{}; \ - context.type = ECSACT_STATEMENT_##Context; \ - context.data = {}; \ - \ - auto statement_str = std::string{StatementString}; \ - auto status = ecsact_parse_status{}; \ - \ - auto read_amount = ecsact_parse_statement( \ - statement_str.data(), \ - statement_str.size(), \ - &context, \ - OutStatement, \ - &status \ - ); \ - \ - EXPECT_EQ(status.code, ECSACT_PARSE_STATUS_OK); \ - EXPECT_EQ(read_amount, statement_str.size()); \ - } \ - static_assert(true, "Macro requires ;") +#define PREPARE_TEST_STATEMENT() \ + auto statement = ecsact_statement{}; \ + auto context = ecsact_statement{}; \ + auto statement_str = std::string{}; \ + auto status = ecsact_parse_status{}; \ + auto read_amount = int32_t{}; \ + static_assert(true, "macro requires semi-colon") + +#define STATEMENT_OK(Context, StatementString) \ + statement = ecsact_statement{}; \ + context = ecsact_statement{}; \ + context.type = ECSACT_STATEMENT_##Context; \ + context.data = {}; \ + \ + statement_str = std::string{StatementString}; \ + status = ecsact_parse_status{}; \ + \ + read_amount = ecsact_parse_statement( \ + statement_str.data(), \ + statement_str.size(), \ + &context, \ + &statement, \ + &status \ + ); \ + \ + EXPECT_EQ(status.code, ECSACT_PARSE_STATUS_OK); \ + EXPECT_EQ(read_amount, statement_str.size()) #define STATEMENT_BAD(Context, StatementString, BadStatus) \ { \