Skip to content

Commit

Permalink
fix: fix string allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Sep 29, 2023
1 parent 4e5a750 commit 4c32cd1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bazel-*/
bazel-*
user.bazelrc
compile_commands.json
external/
.cache/
external
.cache
10 changes: 5 additions & 5 deletions test/parse_test_system_notify_statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down
49 changes: 28 additions & 21 deletions test/parse_test_util.hh
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
#pragma once

#include <string>
#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) \
{ \
Expand Down

0 comments on commit 4c32cd1

Please sign in to comment.