Skip to content

Commit

Permalink
fix: catch duplicate components
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy committed Sep 29, 2023
1 parent 594dcb4 commit 1c31a90
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ module(
bazel_dep(name = "rules_cc", version = "0.0.8")
bazel_dep(name = "bazel_skylib", version = "1.4.2")
bazel_dep(name = "magic_enum", version = "0.9.3")
bazel_dep(name = "ecsact_runtime", version = "0.5.2")
bazel_dep(name = "ecsact_runtime", version = "0.5.3")
bazel_dep(name = "ecsact_parse", version = "0.3.3")
20 changes: 19 additions & 1 deletion ecsact/interpret/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "ecsact/parse.h"
#include "ecsact/runtime/dynamic.h"
#include "ecsact/runtime/meta.hh"
#include "ecsact/runtime/meta.h"

#include "./detail/file_eval_error.hh"

Expand Down Expand Up @@ -1402,6 +1403,13 @@ static auto eval_system_notify_component_statement(
std::span<const ecsact_statement>& context_stack,
const ecsact_statement& statement
) -> ecsact_eval_error {
if(context_stack.size() < 2) {
return ecsact_eval_error{
.code = ECSACT_EVAL_ERR_INVALID_CONTEXT,
.relevant_content = {},
};
}

auto [context, err] =
expect_context(context_stack, {ECSACT_STATEMENT_SYSTEM_NOTIFY});

Expand Down Expand Up @@ -1430,7 +1438,7 @@ static auto eval_system_notify_component_statement(

auto sys_like_id = find_by_statement<ecsact_system_like_id>( //
package_id,
*context
context_stack[context_stack.size() - 2]
);

auto comp_like_name = std::string( //
Expand Down Expand Up @@ -1464,6 +1472,16 @@ static auto eval_system_notify_component_statement(
};
}

for(auto&& [existing_comp_id, _] :
ecsact::meta::system_notify_settings(*sys_like_id)) {
if(existing_comp_id == *comp_like_id) {
return ecsact_eval_error{
.code = ECSACT_EVAL_ERR_DUPLICATE_NOTIFY_COMPONENT,
.relevant_content = {},
};
}
}

ecsact_set_system_notify_component_setting(
*sys_like_id,
*comp_like_id,
Expand Down
3 changes: 3 additions & 0 deletions ecsact/interpret/eval_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ typedef enum ecsact_eval_error_code {
/// The notify statement must be after all capability statements.
ECSACT_EVAL_ERR_NOTIFY_BEFORE_SYSTEM_COMPONENT,

// More than one system notify component statement for the same component.
ECSACT_EVAL_ERR_DUPLICATE_NOTIFY_COMPONENT,

/// Internal error. Should not happen and is an indiciation of a bug.
ECSACT_EVAL_ERR_INTERNAL = 999,

Expand Down
2 changes: 1 addition & 1 deletion test/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ bazel_dep(name = "rules_cc", version = "0.0.8")
bazel_dep(name = "bazel_skylib", version = "1.4.2")
bazel_dep(name = "googletest", version = "1.14.0")
bazel_dep(name = "ecsact_parse", version = "0.3.3")
bazel_dep(name = "ecsact_runtime", version = "0.5.2")
bazel_dep(name = "ecsact_runtime", version = "0.5.3")

bazel_dep(name = "bazel_sundry")
bazel_dep(name = "ecsact_interpret")
Expand Down
1 change: 1 addition & 0 deletions test/errors/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@ecsact_interpret//bazel:copts.bzl", "copts")

# buildifier: keep sorted
_TESTS = [
"duplicate_notice_components",
"invalid_notify_settings",
"no_capabilities",
"no_package_statement_first",
Expand Down
10 changes: 10 additions & 0 deletions test/errors/duplicate_notice_components.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "gtest/gtest.h"
#include "ecsact/interpret/eval.h"

#include "test_lib.hh"

TEST(DuplicateNoticeComponents, DuplicateNoticeComponents) {
auto errs = ecsact_interpret_test_files({"errors/duplicate_notice_components.ecsact"});
ASSERT_EQ(errs.size(), 1);
ASSERT_EQ(errs[0].eval_error, ECSACT_EVAL_ERR_DUPLICATE_NOTIFY_COMPONENT);
}
14 changes: 14 additions & 0 deletions test/errors/duplicate_notice_components.ecsact
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package error.duplicate_notice_components;

component ExampleComponent {
i32 num;
}

system DuplicateNoticeComponents {
readwrite ExampleComponent;

notify {
always ExampleComponent;
oninit ExampleComponent;
}
}

0 comments on commit 1c31a90

Please sign in to comment.