Skip to content

feat: add WithWorldGuard and HandlerContext system parameters #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
File renamed without changes.
3 changes: 3 additions & 0 deletions assets/tests/data/access/multiple_read_refs.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let entity = Entity.from_raw.call(9999);
// does not throw
let out = entity.eq.call(entity);
11 changes: 11 additions & 0 deletions assets/tests/data/add/vec3.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
let a = Vec3.new_.call(1.0, 2.0, 3.0);
let b = Vec3.new_.call(4.0, 5.0, 6.0);

assert((a + 1).x == 2.0, "Addition did not work");
assert((a + 1).y == 3.0, "Addition did not work");
assert((a + 1).z == 4.0, "Addition did not work");

assert((a + b).x == 5.0, "Addition did not work");
assert((a + b).y == 7.0, "Addition did not work");
assert((a + b).z == 9.0, "Addition did not work");

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let entity = world.spawn_.call();
let type = world.get_type_by_name.call("TestComponent");

assert_throws(||{
world.add_default_component.call(entity, type);
},"Missing type data ReflectDefault or ReflectFromWorld for type: .*TestComponent.*");
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let entity = world.spawn_.call();
let _type = world.get_type_by_name.call("CompWithDefaultAndComponentData");
world.add_default_component.call(entity, _type);

let added = world.has_component.call(entity, _type);
assert(type_of(added) != "()", "Component not added");

let component = world.get_component.call(entity, _type);
assert(component["_0"] == "Default", "Component did not have default value, got: " + component["_0"]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let entity = world.spawn_.call();
let _type = world.get_type_by_name.call("CompWithDefault");

assert_throws(||{
world.add_default_component.call(entity, _type);
}, "Missing type data ReflectComponent for type: .*CompWithDefault.*")
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let entity = world.spawn_.call();
let _type = world.get_type_by_name.call("CompWithFromWorldAndComponentData");
world.add_default_component.call(entity, _type);

let added = world.has_component.call(entity, _type);
assert(type_of(added) != "()", "Component not added");

let component = world.get_component.call(entity, _type);
assert(component["_0"] == "Default", "Component did not have default value, got: " + component["_0"])
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let entity = world.spawn_.call();
let _type = world.get_type_by_name.call("CompWithFromWorld");

assert_throws(||{
world.add_default_component.call(entity, _type);
}, "Missing type data ReflectComponent for type: .*CompWithFromWorld.*")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn on_test() {
assert!(type_of(world) != "()", "World was not found");
assert!(type_of(world.get_type_by_name.call("TestComponent")) != "()", "Could not find TestComponent type");
Entity.from_raw.call(1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
assert!(type_of(world) != "()", "World was not found");
assert!(type_of(world.get_type_by_name.call("TestComponent")) != "()", "Could not find TestComponent type");
let out = Entity.from_raw.call(1);
6 changes: 6 additions & 0 deletions assets/tests/data/clear/vec.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let res_type = world.get_type_by_name.call("TestResourceWithVariousFields");
let res = world.get_resource.call(res_type);

res.vec_usize.clear.call();

assert(res.vec_usize.len.call() == 0, "Clear did not work");
18 changes: 18 additions & 0 deletions assets/tests/data/construct/simple_enum.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
let type = world.get_type_by_name.call("SimpleEnum");

// Struct Variant
let constructed = construct.call(type, #{ variant: "Struct", foo: 123 });

assert(constructed.variant_name.call() == "Struct", "Value was constructed incorrectly, expected constructed.variant to be Struct but got " + constructed.variant_name.call());
assert(constructed.foo == 123, "Value was constructed incorrectly, expected constructed.foo to be 123 but got " + constructed.foo);

// TupleStruct Variant
constructed = construct.call(type, #{ variant: "TupleStruct", "_0": 123 });

assert(constructed.variant_name.call() == "TupleStruct", "Value was constructed incorrectly, expected constructed.variant to be TupleStruct but got " + constructed.variant_name.call());
assert(constructed["_0"] == 123, "Value was constructed incorrectly, expected constructed._0 to be 123 but got " + constructed["_0"]);

// Unit Variant
constructed = construct.call(type, #{ variant: "Unit" });

assert(constructed.variant_name.call() == "Unit", "Value was constructed incorrectly, expected constructed.variant to be Unit but got " + constructed.variant_name.call());
4 changes: 4 additions & 0 deletions assets/tests/data/construct/simple_struct.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let type = world.get_type_by_name.call("SimpleStruct");
let constructed = construct.call(type, #{ foo: 123 });

assert(constructed.foo == 123, "Value was constructed incorrectly, expected constructed.foo to be 123 but got " + constructed.foo);
4 changes: 4 additions & 0 deletions assets/tests/data/construct/simple_tuple_struct.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let type = world.get_type_by_name.call("SimpleTupleStruct");
let constructed = construct.call(type, #{ "_0": 123 });

assert(constructed["_0"] == 123, "Value was constructed incorrectly, expected constructed.foo to be 123 but got " + constructed["_0"]);
7 changes: 7 additions & 0 deletions assets/tests/despawn/despawns_only_root.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let entity = world.spawn_.call();
let child = world.spawn_.call();
world.push_children.call(entity, [child]);
world.despawn.call(entity);

assert(world.has_entity.call(entity) == false, "Parent should be despawned");
assert(world.has_entity.call(child) == true, "Child should not be despawned");
3 changes: 3 additions & 0 deletions assets/tests/despawn/invalid_entity_errors.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
assert_throws(||{
world.despawn_recursive.call(Entity.from_raw.call(9999))
}, "Missing or invalid entity");
7 changes: 7 additions & 0 deletions assets/tests/despawn_descendants/despawns_only_child.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let entity = world.spawn_.call();
let child = world.spawn_.call();
world.push_children.call(entity, [child]);
world.despawn_descendants.call(entity);

assert(world.has_entity.call(entity) == true, "Parent should not be despawned");
assert(world.has_entity.call(child) == false, "Child should be despawned");
3 changes: 3 additions & 0 deletions assets/tests/despawn_descendants/invalid_entity_errors.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
assert_throws(||{
world.despawn_recursive.call(Entity.from_raw.call(9999));
}, "Missing or invalid entity")
7 changes: 7 additions & 0 deletions assets/tests/despawn_recursive/despawns_recursively.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let entity = world.spawn_.call();
let child = world.spawn_.call();
world.push_children.call(entity, [child]);
world.despawn_recursive.call(entity);

assert(world.has_entity.call(entity) == false, "Parent should be despawned");
assert(world.has_entity.call(child) == false, "Child should be despawned");
3 changes: 3 additions & 0 deletions assets/tests/despawn_recursive/invalid_entity_errors.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
assert_throws(||{
world.despawn_recursive.call(Entity.from_raw.call(9999));
}, "Missing or invalid entity")
File renamed without changes.
10 changes: 10 additions & 0 deletions assets/tests/div/vec3.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let a = Vec3.new_.call(2.0, 4.0, 6.0);
let b = Vec3.new_.call(1.0, 2.0, 3.0);

assert((a / 2).x == 1.0, "Division did not work");
assert((a / 2).y == 2.0, "Division did not work");
assert((a / 2).z == 3.0, "Division did not work");

assert((a / b).x == 2.0, "Division did not work");
assert((a / b).y == 2.0, "Division did not work");
assert((a / b).z == 2.0, "Division did not work");
File renamed without changes.
7 changes: 7 additions & 0 deletions assets/tests/eq/vec3.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let a = Vec3.new_.call(2.0, -4.0, 6.0);
let b = Vec3.new_.call(4.0, 5.0, 6.0);


assert((a == b) == false, "Equality did not work");
assert((a != b) == true, "Inequality did not work");
assert((a == a) == true, "Equality did not work");
14 changes: 14 additions & 0 deletions assets/tests/functions/contains_reflect_reference_functions.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

let Resource = world.get_type_by_name.call("TestResource");
let resource = world.get_resource.call(Resource);

let functions = resource.functions.call();
assert(functions.len() > 0, "functions should not be empty");

let available_names = [];

for function_ref in functions {
available_names.push(function_ref.name);
}

assert("display_ref" in available_names, "functions should contain display_ref, but got: " + available_names);
9 changes: 9 additions & 0 deletions assets/tests/get_children/has_children_returns_them.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let entity = world.spawn_.call();
let child = world.spawn_.call();

world.push_children.call(entity, [child]);

let children = world.get_children.call(entity);

assert(children.len == 1, "Expected 1 child");
assert(children[0].index.call() == child.index.call(), "Child is the wrong entity");
3 changes: 3 additions & 0 deletions assets/tests/get_children/invalid_entity_errors.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
assert_throws(||{
world.get_children.call(Entity.from_raw.call(9999));
}, "Missing or invalid entity");
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let entity = world.spawn_.call();
let children = world.get_children.call(entity);

assert(children.len == 0);
6 changes: 6 additions & 0 deletions assets/tests/get_component/component_no_component_data.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let component = world.get_type_by_name.call("CompWithDefault");
let entity = world._get_entity_with_test_component.call("CompWithDefault");
let retrieved = world.get_component.call(entity, component);

assert(type_of(retrieved) != "()", "Component was not found");
assert(retrieved["_0"] == "Initial Value", "Component data was not retrieved correctly, retrieved._0 was: " + retrieved["_0"]);
6 changes: 6 additions & 0 deletions assets/tests/get_component/component_with_component_data.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let component = world.get_type_by_name.call("TestComponent");
let entity = world._get_entity_with_test_component.call("TestComponent");
let retrieved = world.get_component.call(entity, component);

assert(type_of(retrieved) != "()", "Component was not found");
assert(retrieved.strings[0] == "Initial", "Component data was not retrieved correctly, retrieved.strings[0] was: " + retrieved.strings[0]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let component = world.get_type_by_name.call("TestComponent");
let entity = world.spawn_.call();
let retrieved = world.get_component.call(entity, component);

assert(type_of(retrieved) == "()", "Component found");
9 changes: 9 additions & 0 deletions assets/tests/get_parent/has_parent_returns_it.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let entity = world.spawn_.call();
let child = world.spawn_.call();

world.push_children.call(entity, [child]);

let parent = world.get_parent.call(child);

assert(type_of(parent) != "()", "Expected a parent");
assert(parent.index.call() == entity.index.call(), "Parent is the wrong entity");
4 changes: 4 additions & 0 deletions assets/tests/get_parent/invalid_entity_errors.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

assert_throws(||{
world.get_parent.call(Entity.from_raw.call(9999));
}, "Missing or invalid entity");
4 changes: 4 additions & 0 deletions assets/tests/get_parent/no_parent_returns_nil.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let entity = world.spawn_.call();
let parent = world.get_parent.call(entity);

assert(type_of(parent) == "()", "Expected no parents");
2 changes: 2 additions & 0 deletions assets/tests/get_resource/missing_resource_returns_nil.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let type = world._get_mock_resource_type.call();
assert(type_of(world.get_resource.call(type)) == "()", "Resource should not exist");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let resource = world.get_type_by_name.call("ResourceWithDefault");

let retrieved = world.get_resource.call(resource);
assert(type_of(retrieved) != "()", "Resource should exist");
assert(retrieved["_0"] == "Initial Value", "Resource should have default value but got: " + retrieved["_0"]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let resource = world.get_type_by_name.call("TestResource");

let retrieved = world.get_resource.call(resource);
assert(type_of(retrieved) != "()", "Resource should exist");
assert(retrieved.bytes[1] == 1, "Resource should have default value but got resource with #retrieved.bytes[1]: " + retrieved.bytes[1]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let type = world.get_type_by_name.call("MissingType");

assert(type == (), "Unregistered type was found");
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
let type = world.get_type_by_name.call("TestComponent");

assert(type_of(type) != "()", "Registered type was not found");

let expected_type_name = "test_utils::test_data::TestComponent";
let expected_short_name = "TestComponent";

let received_type_name = type.type_name.call(type);
let received_short_name = type.short_name.call(type);


// assert(type != (), 'Type not found')
// assert(received.type_name == expected.type_name, 'type_name mismatch, expected: ' .. expected.type_name .. ', got: ' .. received.type_name)
// assert(received.short_name == expected.short_name, 'short_name mismatch, expected: ' .. expected.short_name .. ', got: ' .. received.short_name)

assert(received_type_name == expected_type_name, "type_name mismatch, expected: " + expected_type_name + ", got: " + received_type_name);
assert(received_short_name == expected_short_name, "short_name mismatch, expected: " + expected_short_name + ", got: " + received_short_name);
1 change: 1 addition & 0 deletions assets/tests/globals/dynamic_globals_are_in_scope.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert(global_hello_world.call() == "hi!", "global_hello_world() == 'hi!'")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let entity = world.spawn_.call();
let type = world._get_mock_component_type.call();

assert(world.has_component.call(entity, type) == false, "Entity should not have component");
3 changes: 3 additions & 0 deletions assets/tests/has_component/no_component_data.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let entity = world._get_entity_with_test_component.call("CompWithDefault");
let component = world.get_type_by_name.call("CompWithDefault");
assert(world.has_component.call(entity, component) == true, "Component was not found");
3 changes: 3 additions & 0 deletions assets/tests/has_component/with_component_data.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let entity = world._get_entity_with_test_component.call("TestComponent");
let component = world.get_type_by_name.call("TestComponent");
assert(world.has_component.call(entity, component) == true, "Component was not found");
2 changes: 2 additions & 0 deletions assets/tests/has_resource/existing_no_resource_data.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let component = world.get_type_by_name.call("ResourceWithDefault");
assert(world.has_resource.call(component) == true, "Resource was not found");
2 changes: 2 additions & 0 deletions assets/tests/has_resource/existing_with_resource_data.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let component = world.get_type_by_name.call("TestResource");
assert(world.has_resource.call(component) == true, "Resource was not found");
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let type = world._get_mock_resource_type.call();
assert(world.has_resource.call(type) == false, "Resource should not exist");
7 changes: 7 additions & 0 deletions assets/tests/hashmap/can_pass_and_return_hashmap.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let my_map = make_hashmap.call(#{
key1: 2,
key2: 3,
});

assert(my_map["key1"] == 2, "map[\"key1\"] should be 2");
assert(my_map["key2"] == 3, "map[\"key2\"] should be 3");
6 changes: 6 additions & 0 deletions assets/tests/insert/vec.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let res_type = world.get_type_by_name.call("TestResourceWithVariousFields");
let res = world.get_resource.call(res_type);

res.vec_usize.insert.call(2, 42);

assert(res.vec_usize[2] == 42, "insert did not work");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let entity = world.spawn_.call();

world.insert_children.call(entity,0 ,[]);

assert(world.get_children.call(entity).len == 0);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let entity = world.spawn_.call();
let child = world.spawn_.call();
let child2 = world.spawn_.call();

world.insert_children.call(entity, 0, [child]);
world.insert_children.call(entity, 0, [child2]);

assert(world.get_children.call(entity)[0].index.call() == child2.index.call());
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let entity = world.spawn_.call();
let child = world.spawn_.call();
let child2 = world.spawn_.call();
world.insert_children.call(entity, 0, [child, child2]);

assert(world.get_children.call(entity).len == 2);
10 changes: 10 additions & 0 deletions assets/tests/insert_children/invalid_entity_errors.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let fake_entity = Entity.from_raw.call(9999);

assert_throws(||{
world.insert_children.call(fake_entity, 0, [fake_entity]);
}, "Missing or invalid entity");

let entity = world.spawn_.call();
assert_throws(||{
world.insert_children.call(entity, 0, [fake_entity]);
}, "Missing or invalid entity");
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let entity = world.spawn_.call();
let type = world.get_type_by_name.call("TestComponent");
let entity_with_component = world._get_entity_with_test_component.call("TestComponent");
let existing_component = world.get_component.call(entity_with_component, type);

assert(world.has_component.call(entity, type) == false, "Expected entity to not have component before adding, test invalid");
world.insert_component.call(entity, type, existing_component);
assert(world.has_component.call(entity, type) == true, "Expected entity to have component after adding");
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let entity = world.spawn_.call();
let _type = world.get_type_by_name.call("CompWithDefault");
let entity_with_component = world._get_entity_with_test_component.call("CompWithDefault");
let existing_component = world.get_component.call(entity_with_component, _type);

assert_throws(||{
world.insert_component.call(entity, _type, existing_component);
}, "Missing type data ReflectComponent for type: .*CompWithDefault.*");
File renamed without changes.
Loading
Loading