From e4c9e82b99a6cfd51eaacd4e5ac58e285b5b8ed9 Mon Sep 17 00:00:00 2001 From: "D. Moonfire" Date: Sat, 14 Jan 2023 16:51:06 -0600 Subject: [PATCH] refactor: cleaning up code to newer C# standards --- src/MfGames.Gallium/Entity.cs | 853 +++++++++--------- src/MfGames.Gallium/JoinEntityExtensions.cs | 41 +- .../SelectComponentExtensions.cs | 3 +- .../SelectComponentOrDefaultExtensions.cs | 3 +- src/MfGames.Gallium/SelectEntityExtensions.cs | 477 +++++----- src/MfGames.Gallium/SplitEntityExtensions.cs | 4 +- src/MfGames.Gallium/WhereEntityExtensions.cs | 73 +- .../WhereEntityHasExtensions.cs | 39 +- .../WhereEntityNotHasExtensions.cs | 39 +- tests/MfGames.Gallium.Tests/EntityTests.cs | 329 +++---- .../EnumerableEntityTests.cs | 368 ++++---- .../MfGames.Gallium.Tests/GalliumTestsBase.cs | 59 +- .../MfGames.Gallium.Tests/ITestComponent3.cs | 7 +- .../MfGames.Gallium.Tests.csproj | 10 +- tests/MfGames.Gallium.Tests/TestComponent1.cs | 7 +- tests/MfGames.Gallium.Tests/TestComponent2.cs | 7 +- .../MfGames.Gallium.Tests/TestComponent3a.cs | 7 +- .../MfGames.Gallium.Tests/TestComponent3b.cs | 7 +- 18 files changed, 1180 insertions(+), 1153 deletions(-) diff --git a/src/MfGames.Gallium/Entity.cs b/src/MfGames.Gallium/Entity.cs index 3137989..7f60251 100644 --- a/src/MfGames.Gallium/Entity.cs +++ b/src/MfGames.Gallium/Entity.cs @@ -3,471 +3,470 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Threading; -namespace MfGames.Gallium +namespace MfGames.Gallium; + +/// +/// A low-overhead entity with identification. +/// +public record Entity { - /// - /// A low-overhead entity with identification. - /// - public record Entity + /// + public virtual bool Equals(Entity? other) { - /// - public virtual bool Equals(Entity? other) + if (ReferenceEquals(null, other)) { - if (ReferenceEquals(null, other)) - { - return false; - } - - if (ReferenceEquals(this, other)) - { - return true; - } - - return this.Id == other.Id; - } - - /// - public override int GetHashCode() - { - return this.Id; - } - - private ImmutableDictionary Components { get; set; } - - /// - /// The internal ID to ensure the entities are unique. Since we are not - /// worried about serialization or using the identifiers from one call - /// to another, we can use a simple interlocked identifier instead of - /// a factory or provider method. - /// - private static int nextId; - - public Entity() - : this(Interlocked.Increment(ref nextId)) - { - } - - private Entity(int id) - { - this.Id = id; - this.Components = ImmutableDictionary.Create(); - } - - /// - /// Gets a value indicating whether the entity has a specific type of - /// component registered. - /// - /// The component type. - /// True if the type exists, otherwise false. - public bool Has() - { - return this.Has(typeof(T1)); - } - - /// - /// Gets a value indicating whether the entity has components of the given types - /// registered. - /// - /// The first component type. - /// The second component type. - /// - /// True if there are components of the given type exists, otherwise - /// false. - /// - public bool HasAll() - { - return this.HasAll(typeof(T1), typeof(T2)); - } - - /// - /// Gets a value indicating whether the entity has components of the given types - /// registered. - /// - /// The first component type. - /// The second component type. - /// The third component type. - /// - /// True if there are components of the given type exists, otherwise - /// false. - /// - public bool HasAll() - { - return this.HasAll(typeof(T1), typeof(T2), typeof(T3)); - } - - /// - /// Gets a value indicating whether the entity has components of the given types - /// registered. - /// - /// The first component type. - /// The second component type. - /// The third component type. - /// The third component type. - /// - /// True if there are components of the given type exists, otherwise - /// false. - /// - public bool HasAll() - { - return this.HasAll(typeof(T1), typeof(T2), typeof(T3), typeof(T4)); - } - - /// - /// Gets a value indicating whether the entity has a specific type of - /// component registered. - /// - /// The component type. - /// True if the type exists, otherwise false. - public bool Has(Type type) - { - return this.Components.ContainsKey(type); - } - - /// - /// Gets a value indicating whether the entity has components for all the given - /// types. - /// - /// The component type. - /// The component type. - /// True if the type exists, otherwise false. - public bool HasAll( - Type t1, - Type t2) - { - return this.Has(t1) && this.Components.ContainsKey(t2); - } - - /// - /// Gets a value indicating whether the entity has components for all the given - /// types. - /// - /// The component type. - /// The component type. - /// The component type. - /// True if the type exists, otherwise false. - public bool HasAll( - Type t1, - Type t2, - Type t3) - { - return this.HasAll(t1, t2) && this.Components.ContainsKey(t3); - } - - /// - /// Gets a value indicating whether the entity has components for all the given - /// types. - /// - /// The component type. - /// The component type. - /// The component type. - /// The component type. - /// True if the type exists, otherwise false. - public bool HasAll( - Type t1, - Type t2, - Type t3, - Type t4) - { - return this.HasAll(t1, t2, t3) && this.Components.ContainsKey(t4); - } - - /// - /// Retrieves a registered component of the given type. - /// - /// The component type. - /// The registered object. - public TType Get() - { - return (TType)this.Components[typeof(TType)]; - } - - /// - /// Retrieves a registered component of the given type and casts it to - /// TType. - /// - /// The component key. - /// The component type. - /// The registered object. - public TType Get(Type type) - { - return (TType)this.Components[type]; - } - - /// - /// Gets the number of components registered in the entity. - /// - public int Count => this.Components.Count; - - /// - /// Gets the given component type if inside the entity, otherwise the - /// default value. - /// - /// The component type. - /// The found component or default (typically null). - public TType? GetOptional() - { - return this.Has() ? this.Get() : default; - } - - /// - /// Attempts to get the value, if present. If not, this returns false - /// and the value is undefined. Otherwise, this method returns true - /// and the actual value inside that variable. - /// - /// The value if contained in the entity. - /// The component type. - /// True if found, otherwise false. - public bool TryGet(out T1 value) - { - if (this.Has()) - { - value = this.Get(); - - return true; - } - - value = default!; - return false; } - /// - /// Attempts to get the values, if present. If not, this returns false - /// and the value is undefined. Otherwise, this method returns true - /// and the actual value inside that variable. - /// - /// The value if contained in the entity. - /// The value if contained in the entity. - /// The first component type. - /// The second component type. - /// True if found, otherwise false. - public bool TryGet( - out T1 value1, - out T2 value2) + if (ReferenceEquals(this, other)) { - if (this.HasAll()) - { - value1 = this.Get(); - value2 = this.Get(); - - return true; - } - - value1 = default!; - value2 = default!; - - return false; + return true; } - /// - /// Attempts to get the values, if present. If not, this returns false - /// and the value is undefined. Otherwise, this method returns true - /// and the actual value inside that variable. - /// - /// The value if contained in the entity. - /// The value if contained in the entity. - /// The value if contained in the entity. - /// The first component type. - /// The second component type. - /// The third component type. - /// True if found, otherwise false. - public bool TryGet( - out T1 value1, - out T2 value2, - out T3 value3) + return this.Id == other.Id; + } + + /// + public override int GetHashCode() + { + return this.Id; + } + + private ImmutableDictionary Components { get; set; } + + /// + /// The internal ID to ensure the entities are unique. Since we are not + /// worried about serialization or using the identifiers from one call + /// to another, we can use a simple interlocked identifier instead of + /// a factory or provider method. + /// + private static int nextId; + + public Entity() + : this(Interlocked.Increment(ref nextId)) + { + } + + private Entity(int id) + { + this.Id = id; + this.Components = ImmutableDictionary.Create(); + } + + /// + /// Gets a value indicating whether the entity has a specific type of + /// component registered. + /// + /// The component type. + /// True if the type exists, otherwise false. + public bool Has() + { + return this.Has(typeof(T1)); + } + + /// + /// Gets a value indicating whether the entity has components of the given types + /// registered. + /// + /// The first component type. + /// The second component type. + /// + /// True if there are components of the given type exists, otherwise + /// false. + /// + public bool HasAll() + { + return this.HasAll(typeof(T1), typeof(T2)); + } + + /// + /// Gets a value indicating whether the entity has components of the given types + /// registered. + /// + /// The first component type. + /// The second component type. + /// The third component type. + /// + /// True if there are components of the given type exists, otherwise + /// false. + /// + public bool HasAll() + { + return this.HasAll(typeof(T1), typeof(T2), typeof(T3)); + } + + /// + /// Gets a value indicating whether the entity has components of the given types + /// registered. + /// + /// The first component type. + /// The second component type. + /// The third component type. + /// The third component type. + /// + /// True if there are components of the given type exists, otherwise + /// false. + /// + public bool HasAll() + { + return this.HasAll(typeof(T1), typeof(T2), typeof(T3), typeof(T4)); + } + + /// + /// Gets a value indicating whether the entity has a specific type of + /// component registered. + /// + /// The component type. + /// True if the type exists, otherwise false. + public bool Has(Type type) + { + return this.Components.ContainsKey(type); + } + + /// + /// Gets a value indicating whether the entity has components for all the given + /// types. + /// + /// The component type. + /// The component type. + /// True if the type exists, otherwise false. + public bool HasAll( + Type t1, + Type t2) + { + return this.Has(t1) && this.Components.ContainsKey(t2); + } + + /// + /// Gets a value indicating whether the entity has components for all the given + /// types. + /// + /// The component type. + /// The component type. + /// The component type. + /// True if the type exists, otherwise false. + public bool HasAll( + Type t1, + Type t2, + Type t3) + { + return this.HasAll(t1, t2) && this.Components.ContainsKey(t3); + } + + /// + /// Gets a value indicating whether the entity has components for all the given + /// types. + /// + /// The component type. + /// The component type. + /// The component type. + /// The component type. + /// True if the type exists, otherwise false. + public bool HasAll( + Type t1, + Type t2, + Type t3, + Type t4) + { + return this.HasAll(t1, t2, t3) && this.Components.ContainsKey(t4); + } + + /// + /// Retrieves a registered component of the given type. + /// + /// The component type. + /// The registered object. + public TType Get() + { + return (TType)this.Components[typeof(TType)]; + } + + /// + /// Retrieves a registered component of the given type and casts it to + /// TType. + /// + /// The component key. + /// The component type. + /// The registered object. + public TType Get(Type type) + { + return (TType)this.Components[type]; + } + + /// + /// Gets the number of components registered in the entity. + /// + public int Count => this.Components.Count; + + /// + /// Gets the given component type if inside the entity, otherwise the + /// default value. + /// + /// The component type. + /// The found component or default (typically null). + public TType? GetOptional() + { + return this.Has() ? this.Get() : default; + } + + /// + /// Attempts to get the value, if present. If not, this returns false + /// and the value is undefined. Otherwise, this method returns true + /// and the actual value inside that variable. + /// + /// The value if contained in the entity. + /// The component type. + /// True if found, otherwise false. + public bool TryGet(out T1 value) + { + if (this.Has()) { - if (this.HasAll()) - { - value1 = this.Get(); - value2 = this.Get(); - value3 = this.Get(); + value = this.Get(); - return true; - } - - value1 = default!; - value2 = default!; - value3 = default!; - - return false; + return true; } - /// - /// Attempts to get the values, if present. If not, this returns false - /// and the value is undefined. Otherwise, this method returns true - /// and the actual value inside that variable. - /// - /// The value if contained in the entity. - /// The value if contained in the entity. - /// The value if contained in the entity. - /// The value if contained in the entity. - /// The first component type. - /// The second component type. - /// The third component type. - /// The fourth component type. - /// True if found, otherwise false. - public bool TryGet( - out T1 value1, - out T2 value2, - out T3 value3, - out T4 value4) + value = default!; + + return false; + } + + /// + /// Attempts to get the values, if present. If not, this returns false + /// and the value is undefined. Otherwise, this method returns true + /// and the actual value inside that variable. + /// + /// The value if contained in the entity. + /// The value if contained in the entity. + /// The first component type. + /// The second component type. + /// True if found, otherwise false. + public bool TryGet( + out T1 value1, + out T2 value2) + { + if (this.HasAll()) { - if (this.HasAll()) - { - value1 = this.Get(); - value2 = this.Get(); - value3 = this.Get(); - value4 = this.Get(); + value1 = this.Get(); + value2 = this.Get(); - return true; - } - - value1 = default!; - value2 = default!; - value3 = default!; - value4 = default!; - - return false; + return true; } - /// - /// Sets the component in the entity, regardless if there was a - /// component already registered. - /// - /// The component to register. - /// The component type. - /// The entity for chaining. - /// - public Entity Set(T1 component) + value1 = default!; + value2 = default!; + + return false; + } + + /// + /// Attempts to get the values, if present. If not, this returns false + /// and the value is undefined. Otherwise, this method returns true + /// and the actual value inside that variable. + /// + /// The value if contained in the entity. + /// The value if contained in the entity. + /// The value if contained in the entity. + /// The first component type. + /// The second component type. + /// The third component type. + /// True if found, otherwise false. + public bool TryGet( + out T1 value1, + out T2 value2, + out T3 value3) + { + if (this.HasAll()) { - if (component == null) - { - throw new ArgumentNullException(nameof(component)); - } + value1 = this.Get(); + value2 = this.Get(); + value3 = this.Get(); - if (this.Components.TryGetValue(typeof(T1), out object? value) - && value is T1 - && value.Equals(component)) - { - return this; - } - - return this with - { - Components = this.Components.SetItem(typeof(T1), component), - }; + return true; } - /// - /// Adds a component to the entity. - /// - /// The component to register. - /// The component type. - /// - /// The same entity if the component is already registered, otherwise a - /// cloned entity with the new component. - /// - /// - public Entity Add(T1 component) + value1 = default!; + value2 = default!; + value3 = default!; + + return false; + } + + /// + /// Attempts to get the values, if present. If not, this returns false + /// and the value is undefined. Otherwise, this method returns true + /// and the actual value inside that variable. + /// + /// The value if contained in the entity. + /// The value if contained in the entity. + /// The value if contained in the entity. + /// The value if contained in the entity. + /// The first component type. + /// The second component type. + /// The third component type. + /// The fourth component type. + /// True if found, otherwise false. + public bool TryGet( + out T1 value1, + out T2 value2, + out T3 value3, + out T4 value4) + { + if (this.HasAll()) { - if (component == null) - { - throw new ArgumentNullException(nameof(component)); - } + value1 = this.Get(); + value2 = this.Get(); + value3 = this.Get(); + value4 = this.Get(); - if (this.Has()) - { - throw new ArgumentException( - "An element with the same type (" - + typeof(T1).FullName - + ") already exists.", - nameof(component)); - } - - if (this.Components.TryGetValue(typeof(T1), out object? value) - && value is T1 - && value.Equals(component)) - { - return this; - } - - return this with - { - Components = this.Components.Add(typeof(T1), component), - }; + return true; } - /// - /// Removes a component to the entity. - /// - /// The component type. - /// - /// The same entity if the component is already removed, otherwise a - /// cloned entity without the new component. - /// - /// - public Entity Remove() + value1 = default!; + value2 = default!; + value3 = default!; + value4 = default!; + + return false; + } + + /// + /// Sets the component in the entity, regardless if there was a + /// component already registered. + /// + /// The component to register. + /// The component type. + /// The entity for chaining. + /// + public Entity Set(T1 component) + { + if (component == null) { - return this.Remove(typeof(TType)); + throw new ArgumentNullException(nameof(component)); } - /// - /// Removes a component to the entity. - /// - /// - /// The same entity if the component is already removed, otherwise a - /// cloned entity without the new component. - /// - /// The component type to remove. - public Entity Remove(Type type) + if (this.Components.TryGetValue(typeof(T1), out object? value) + && value is T1 + && value.Equals(component)) { - if (!this.Has(type)) - { - return this; - } - - return this with - { - Components = this.Components.Remove(type), - }; + return this; } - /// - /// Gets the identifier of the entity. This should be treated as an - /// opaque field. - /// - public int Id { get; private init; } - - /// - /// Creates a copy of the entity, including copying the identifier. - /// - /// - public Entity ExactCopy() + return this with { - return this with { }; + Components = this.Components.SetItem(typeof(T1), component) + }; + } + + /// + /// Adds a component to the entity. + /// + /// The component to register. + /// The component type. + /// + /// The same entity if the component is already registered, otherwise a + /// cloned entity with the new component. + /// + /// + public Entity Add(T1 component) + { + if (component == null) + { + throw new ArgumentNullException(nameof(component)); } - /// - /// Creates a copy of the entity, including components, but with a new - /// identifier. - /// - /// - public Entity Copy() + if (this.Has()) { - return this with - { - Id = Interlocked.Increment(ref nextId), - }; + throw new ArgumentException( + "An element with the same type (" + + typeof(T1).FullName + + ") already exists.", + nameof(component)); } - /// - /// Retrieves a list of the component types currently registered in the - /// Entity. - /// - /// An enumerable of the various component keys. - public IEnumerable GetComponentTypes() + if (this.Components.TryGetValue(typeof(T1), out object? value) + && value is T1 + && value.Equals(component)) { - return this.Components.Keys; + return this; } + + return this with + { + Components = this.Components.Add(typeof(T1), component) + }; + } + + /// + /// Removes a component to the entity. + /// + /// The component type. + /// + /// The same entity if the component is already removed, otherwise a + /// cloned entity without the new component. + /// + /// + public Entity Remove() + { + return this.Remove(typeof(TType)); + } + + /// + /// Removes a component to the entity. + /// + /// + /// The same entity if the component is already removed, otherwise a + /// cloned entity without the new component. + /// + /// The component type to remove. + public Entity Remove(Type type) + { + if (!this.Has(type)) + { + return this; + } + + return this with + { + Components = this.Components.Remove(type) + }; + } + + /// + /// Gets the identifier of the entity. This should be treated as an + /// opaque field. + /// + public int Id { get; private init; } + + /// + /// Creates a copy of the entity, including copying the identifier. + /// + /// + public Entity ExactCopy() + { + return this with { }; + } + + /// + /// Creates a copy of the entity, including components, but with a new + /// identifier. + /// + /// + public Entity Copy() + { + return this with + { + Id = Interlocked.Increment(ref nextId) + }; + } + + /// + /// Retrieves a list of the component types currently registered in the + /// Entity. + /// + /// An enumerable of the various component keys. + public IEnumerable GetComponentTypes() + { + return this.Components.Keys; } } diff --git a/src/MfGames.Gallium/JoinEntityExtensions.cs b/src/MfGames.Gallium/JoinEntityExtensions.cs index 3a0bae5..d903a3f 100644 --- a/src/MfGames.Gallium/JoinEntityExtensions.cs +++ b/src/MfGames.Gallium/JoinEntityExtensions.cs @@ -2,28 +2,27 @@ using System; using System.Collections.Generic; using System.Linq; -namespace MfGames.Gallium +namespace MfGames.Gallium; + +public static class JoinEntityExtensions { - public static class JoinEntityExtensions + /// + /// Merges two sets of entities using the identifier to determine which + /// entities are the same. The `merge` function takes both of the + /// entities with the Entity from the `input` first and the one from + /// `other` second. The returning entity is put into the collection. If + /// an entity from the input is not found in other, then it is just + /// passed on. + /// + /// The enumerable of entities to merge to. + /// The collection of entities to merge from. + /// The callback to merge the two. + /// An sequence of entities, merged and unmerged. + public static IEnumerable JoinEntity( + this IEnumerable input, + ICollection other, + Func merge) { - /// - /// Merges two sets of entities using the identifier to determine which - /// entities are the same. The `merge` function takes both of the - /// entities with the Entity from the `input` first and the one from - /// `other` second. The returning entity is put into the collection. If - /// an entity from the input is not found in other, then it is just - /// passed on. - /// - /// The enumerable of entities to merge to. - /// The collection of entities to merge from. - /// The callback to merge the two. - /// An sequence of entities, merged and unmerged. - public static IEnumerable JoinEntity( - this IEnumerable input, - ICollection other, - Func merge) - { - return input.Join(other, a => a.Id, a => a.Id, merge); - } + return input.Join(other, a => a.Id, a => a.Id, merge); } } diff --git a/src/MfGames.Gallium/SelectComponentExtensions.cs b/src/MfGames.Gallium/SelectComponentExtensions.cs index 2b881d8..3e28196 100644 --- a/src/MfGames.Gallium/SelectComponentExtensions.cs +++ b/src/MfGames.Gallium/SelectComponentExtensions.cs @@ -16,7 +16,8 @@ public static class SelectComponentExtensions /// The entities to process. /// The component type being searched. /// A sequence of T1. - public static IEnumerable SelectComponent(this IEnumerable entities) + public static IEnumerable SelectComponent( + this IEnumerable entities) { foreach (Entity entity in entities) { diff --git a/src/MfGames.Gallium/SelectComponentOrDefaultExtensions.cs b/src/MfGames.Gallium/SelectComponentOrDefaultExtensions.cs index 74c453f..df74e2a 100644 --- a/src/MfGames.Gallium/SelectComponentOrDefaultExtensions.cs +++ b/src/MfGames.Gallium/SelectComponentOrDefaultExtensions.cs @@ -37,7 +37,8 @@ public static class SelectComponentOrDefaultExtensions /// The entities to process. /// The component type being searched. /// A sequence of T1. - public static IEnumerable SelectComponentOrDefault(this IEnumerable entities) + public static IEnumerable SelectComponentOrDefault( + this IEnumerable entities) { foreach (Entity entity in entities) { diff --git a/src/MfGames.Gallium/SelectEntityExtensions.cs b/src/MfGames.Gallium/SelectEntityExtensions.cs index ee27935..0798db5 100644 --- a/src/MfGames.Gallium/SelectEntityExtensions.cs +++ b/src/MfGames.Gallium/SelectEntityExtensions.cs @@ -1,260 +1,279 @@ using System; using System.Collections.Generic; -namespace MfGames.Gallium +namespace MfGames.Gallium; + +public static class SelectEntityExtensions { - public static class SelectEntityExtensions + /// + /// Selects an entity from the given list, filtering on entities with + /// the given components. + /// + /// The entities to parse. + /// + /// The transformation function for the entity and selected components. If this + /// returns null, then the entity + /// will be filtered out. + /// + /// + /// If true, then entities without all the components are included. Otherwise, they + /// are excluded. + /// + /// The type of the first component. + /// An enumeration of transformed entities. + public static IEnumerable SelectEntity( + this IEnumerable entities, + Func selectWithComponents, + bool includeEntitiesWithoutComponents = true) { - /// - /// Selects an entity from the given list, filtering on entities with - /// the given components. - /// - /// The entities to parse. - /// - /// The transformation function for the entity and selected components. If this - /// returns null, then the entity - /// will be filtered out. - /// - /// - /// If true, then entities without all the components are included. Otherwise, they - /// are excluded. - /// - /// The type of the first component. - /// An enumeration of transformed entities. - public static IEnumerable SelectEntity( - this IEnumerable entities, - Func selectWithComponents, - bool includeEntitiesWithoutComponents = true) - { - return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null); - } + return entities.SelectEntity( + selectWithComponents, + includeEntitiesWithoutComponents ? a => a : a => null); + } - /// - /// Selects an entity from the given list, filtering on entities with - /// the given components. - /// - /// The entities to parse. - /// - /// The transformation function for the entity and selected components. If this - /// returns null, then the entity - /// will be filtered out. - /// - /// - /// If true, then entities without all the components are included. Otherwise, they - /// are excluded. - /// - /// The type of the first component. - /// The type of the second component. - /// An enumeration of transformed entities. - public static IEnumerable SelectEntity( - this IEnumerable entities, - Func selectWithComponents, - bool includeEntitiesWithoutComponents = true) - { - return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null); - } + /// + /// Selects an entity from the given list, filtering on entities with + /// the given components. + /// + /// The entities to parse. + /// + /// The transformation function for the entity and selected components. If this + /// returns null, then the entity + /// will be filtered out. + /// + /// + /// If true, then entities without all the components are included. Otherwise, they + /// are excluded. + /// + /// The type of the first component. + /// The type of the second component. + /// An enumeration of transformed entities. + public static IEnumerable SelectEntity( + this IEnumerable entities, + Func selectWithComponents, + bool includeEntitiesWithoutComponents = true) + { + return entities.SelectEntity( + selectWithComponents, + includeEntitiesWithoutComponents ? a => a : a => null); + } - /// - /// Selects an entity from the given list, filtering on entities with - /// the given components. - /// - /// The entities to parse. - /// - /// The transformation function for the entity and selected components. If this - /// returns null, then the entity - /// will be filtered out. - /// - /// - /// If true, then entities without all the components are included. Otherwise, they - /// are excluded. - /// - /// The type of the first component. - /// The type of the second component. - /// The type of the third component. - /// An enumeration of transformed entities. - public static IEnumerable SelectEntity( - this IEnumerable entities, - Func selectWithComponents, - bool includeEntitiesWithoutComponents = true) - { - return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null); - } + /// + /// Selects an entity from the given list, filtering on entities with + /// the given components. + /// + /// The entities to parse. + /// + /// The transformation function for the entity and selected components. If this + /// returns null, then the entity + /// will be filtered out. + /// + /// + /// If true, then entities without all the components are included. Otherwise, they + /// are excluded. + /// + /// The type of the first component. + /// The type of the second component. + /// The type of the third component. + /// An enumeration of transformed entities. + public static IEnumerable SelectEntity( + this IEnumerable entities, + Func selectWithComponents, + bool includeEntitiesWithoutComponents = true) + { + return entities.SelectEntity( + selectWithComponents, + includeEntitiesWithoutComponents ? a => a : a => null); + } - /// - /// Selects an entity from the given list, filtering on entities with - /// the given components. - /// - /// The entities to parse. - /// - /// The transformation function for the entity and selected components. If this - /// returns null, then the entity - /// will be filtered out. - /// - /// - /// If true, then entities without all the components are included. Otherwise, they - /// are excluded. - /// - /// The type of the first component. - /// The type of the second component. - /// The type of the third component. - /// The type of the fourth component. - /// An enumeration of transformed entities. - public static IEnumerable SelectEntity( - this IEnumerable entities, - Func selectWithComponents, - bool includeEntitiesWithoutComponents = true) - { - return entities.SelectEntity(selectWithComponents, includeEntitiesWithoutComponents ? a => a : a => null); - } + /// + /// Selects an entity from the given list, filtering on entities with + /// the given components. + /// + /// The entities to parse. + /// + /// The transformation function for the entity and selected components. If this + /// returns null, then the entity + /// will be filtered out. + /// + /// + /// If true, then entities without all the components are included. Otherwise, they + /// are excluded. + /// + /// The type of the first component. + /// The type of the second component. + /// The type of the third component. + /// The type of the fourth component. + /// An enumeration of transformed entities. + public static IEnumerable SelectEntity( + this IEnumerable entities, + Func selectWithComponents, + bool includeEntitiesWithoutComponents = true) + { + return entities.SelectEntity( + selectWithComponents, + includeEntitiesWithoutComponents ? a => a : a => null); + } - /// - /// Selects an entity from the given list, filtering on entities with - /// the given components. - /// - /// The entities to parse. - /// - /// The transformation function for the entity and selected components. If this - /// returns null, then the entity - /// will be filtered out. - /// - /// - /// The optional transformation function for entities that do not have all the - /// components. If returns null, - /// then the entity will not be included. - /// - /// The type of the first component. - /// An enumeration of transformed entities. - public static IEnumerable SelectEntity( - this IEnumerable entities, - Func selectWithComponents, - Func selectWithoutComponents) + /// + /// Selects an entity from the given list, filtering on entities with + /// the given components. + /// + /// The entities to parse. + /// + /// The transformation function for the entity and selected components. If this + /// returns null, then the entity + /// will be filtered out. + /// + /// + /// The optional transformation function for entities that do not have all the + /// components. If returns null, + /// then the entity will not be included. + /// + /// The type of the first component. + /// An enumeration of transformed entities. + public static IEnumerable SelectEntity( + this IEnumerable entities, + Func selectWithComponents, + Func selectWithoutComponents) + { + foreach (Entity entity in entities) { - foreach (Entity entity in entities) + Entity? result = entity.TryGet(out T1 value1) + ? selectWithComponents?.Invoke(entity, value1) + : selectWithoutComponents?.Invoke(entity); + + if (result != null) { - Entity? result = entity.TryGet(out T1 value1) - ? selectWithComponents?.Invoke(entity, value1) - : selectWithoutComponents?.Invoke(entity); - - if (result != null) - { - yield return result; - } + yield return result; } } + } - /// - /// Selects an entity from the given list, filtering on entities with - /// the given components. - /// - /// The entities to parse. - /// - /// The transformation function for the entity and selected components. If this - /// returns null, then the entity - /// will be filtered out. - /// - /// - /// The optional transformation function for entities that do not have all the - /// components. If returns null, - /// then the entity will not be included. - /// - /// The type of the first component. - /// The type of the second component. - /// An enumeration of transformed entities. - public static IEnumerable SelectEntity( - this IEnumerable entities, - Func selectWithComponents, - Func selectWithoutComponents) + /// + /// Selects an entity from the given list, filtering on entities with + /// the given components. + /// + /// The entities to parse. + /// + /// The transformation function for the entity and selected components. If this + /// returns null, then the entity + /// will be filtered out. + /// + /// + /// The optional transformation function for entities that do not have all the + /// components. If returns null, + /// then the entity will not be included. + /// + /// The type of the first component. + /// The type of the second component. + /// An enumeration of transformed entities. + public static IEnumerable SelectEntity( + this IEnumerable entities, + Func selectWithComponents, + Func selectWithoutComponents) + { + foreach (Entity entity in entities) { - foreach (Entity entity in entities) - { - Entity? result = entity.TryGet(out T1 value1) && entity.TryGet(out T2 value2) + Entity? result = entity.TryGet(out T1 value1) + && entity.TryGet(out T2 value2) ? selectWithComponents?.Invoke(entity, value1, value2) : selectWithoutComponents?.Invoke(entity); - if (result != null) - { - yield return result; - } + if (result != null) + { + yield return result; } } + } - /// - /// Selects an entity from the given list, filtering on entities with - /// the given components. - /// - /// The entities to parse. - /// - /// The transformation function for the entity and selected components. If this - /// returns null, then the entity - /// will be filtered out. - /// - /// - /// The optional transformation function for entities that do not have all the - /// components. If returns null, - /// then the entity will not be included. - /// - /// The type of the first component. - /// The type of the second component. - /// The type of the third component. - /// An enumeration of transformed entities. - public static IEnumerable SelectEntity( - this IEnumerable entities, - Func selectWithComponents, - Func selectWithoutComponents) + /// + /// Selects an entity from the given list, filtering on entities with + /// the given components. + /// + /// The entities to parse. + /// + /// The transformation function for the entity and selected components. If this + /// returns null, then the entity + /// will be filtered out. + /// + /// + /// The optional transformation function for entities that do not have all the + /// components. If returns null, + /// then the entity will not be included. + /// + /// The type of the first component. + /// The type of the second component. + /// The type of the third component. + /// An enumeration of transformed entities. + public static IEnumerable SelectEntity( + this IEnumerable entities, + Func selectWithComponents, + Func selectWithoutComponents) + { + foreach (Entity entity in entities) { - foreach (Entity entity in entities) - { - Entity? result = - entity.TryGet(out T1 value1) && entity.TryGet(out T2 value2) && entity.TryGet(out T3 value3) - ? selectWithComponents?.Invoke(entity, value1, value2, value3) - : selectWithoutComponents?.Invoke(entity); + Entity? result = + entity.TryGet(out T1 value1) + && entity.TryGet(out T2 value2) + && entity.TryGet(out T3 value3) + ? selectWithComponents?.Invoke( + entity, + value1, + value2, + value3) + : selectWithoutComponents?.Invoke(entity); - if (result != null) - { - yield return result; - } + if (result != null) + { + yield return result; } } + } - /// - /// Selects an entity from the given list, filtering on entities with - /// the given components. - /// - /// The entities to parse. - /// - /// The transformation function for the entity and selected components. If this - /// returns null, then the entity - /// will be filtered out. - /// - /// - /// The optional transformation function for entities that do not have all the - /// components. If returns null, - /// then the entity will not be included. - /// - /// The type of the first component. - /// The type of the second component. - /// The type of the third component. - /// The type of the third component. - /// An enumeration of transformed entities. - public static IEnumerable SelectEntity( - this IEnumerable entities, - Func selectWithComponents, - Func selectWithoutComponents) + /// + /// Selects an entity from the given list, filtering on entities with + /// the given components. + /// + /// The entities to parse. + /// + /// The transformation function for the entity and selected components. If this + /// returns null, then the entity + /// will be filtered out. + /// + /// + /// The optional transformation function for entities that do not have all the + /// components. If returns null, + /// then the entity will not be included. + /// + /// The type of the first component. + /// The type of the second component. + /// The type of the third component. + /// The type of the third component. + /// An enumeration of transformed entities. + public static IEnumerable SelectEntity( + this IEnumerable entities, + Func selectWithComponents, + Func selectWithoutComponents) + { + foreach (Entity entity in entities) { - foreach (Entity entity in entities) - { - Entity? result = - entity.TryGet(out T1 value1) - && entity.TryGet(out T2 value2) - && entity.TryGet(out T3 value3) - && entity.TryGet(out T4 value4) - ? selectWithComponents?.Invoke(entity, value1, value2, value3, value4) - : selectWithoutComponents?.Invoke(entity); + Entity? result = + entity.TryGet(out T1 value1) + && entity.TryGet(out T2 value2) + && entity.TryGet(out T3 value3) + && entity.TryGet(out T4 value4) + ? selectWithComponents?.Invoke( + entity, + value1, + value2, + value3, + value4) + : selectWithoutComponents?.Invoke(entity); - if (result != null) - { - yield return result; - } + if (result != null) + { + yield return result; } } } diff --git a/src/MfGames.Gallium/SplitEntityExtensions.cs b/src/MfGames.Gallium/SplitEntityExtensions.cs index 43333ec..41fc301 100644 --- a/src/MfGames.Gallium/SplitEntityExtensions.cs +++ b/src/MfGames.Gallium/SplitEntityExtensions.cs @@ -189,7 +189,9 @@ public static class SplitEntityExtensions Type t1, Func test) { - return SplitEntity(entities, a => a.Has(t1) && test(a, a.Get(t1))); + return SplitEntity( + entities, + a => a.Has(t1) && test(a, a.Get(t1))); } /// diff --git a/src/MfGames.Gallium/WhereEntityExtensions.cs b/src/MfGames.Gallium/WhereEntityExtensions.cs index b5d56d3..8a140e8 100644 --- a/src/MfGames.Gallium/WhereEntityExtensions.cs +++ b/src/MfGames.Gallium/WhereEntityExtensions.cs @@ -2,47 +2,46 @@ using System; using System.Collections.Generic; using System.Linq; -namespace MfGames.Gallium +namespace MfGames.Gallium; + +public static class WhereEntityExtensions { - public static class WhereEntityExtensions + public static IEnumerable WhereEntity( + this IEnumerable entities, + Func include) { - public static IEnumerable WhereEntity( - this IEnumerable entities, - Func include) - { - return entities.Where(x => x.Has() && include(x, x.Get())); - } + return entities.Where(x => x.Has() && include(x, x.Get())); + } - public static IEnumerable WhereEntity( - this IEnumerable entities, - Func include) - { - return entities.Where( - x => x.HasAll() - && include(x, x.Get(), x.Get())); - } + public static IEnumerable WhereEntity( + this IEnumerable entities, + Func include) + { + return entities.Where( + x => x.HasAll() + && include(x, x.Get(), x.Get())); + } - public static IEnumerable WhereEntity( - this IEnumerable entities, - Func include) - { - return entities.Where( - x => x.HasAll() - && include(x, x.Get(), x.Get(), x.Get())); - } + public static IEnumerable WhereEntity( + this IEnumerable entities, + Func include) + { + return entities.Where( + x => x.HasAll() + && include(x, x.Get(), x.Get(), x.Get())); + } - public static IEnumerable WhereEntity( - this IEnumerable entities, - Func include) - { - return entities.Where( - x => x.HasAll() - && include( - x, - x.Get(), - x.Get(), - x.Get(), - x.Get())); - } + public static IEnumerable WhereEntity( + this IEnumerable entities, + Func include) + { + return entities.Where( + x => x.HasAll() + && include( + x, + x.Get(), + x.Get(), + x.Get(), + x.Get())); } } diff --git a/src/MfGames.Gallium/WhereEntityHasExtensions.cs b/src/MfGames.Gallium/WhereEntityHasExtensions.cs index 48228d1..ae50671 100644 --- a/src/MfGames.Gallium/WhereEntityHasExtensions.cs +++ b/src/MfGames.Gallium/WhereEntityHasExtensions.cs @@ -1,28 +1,31 @@ using System.Collections.Generic; using System.Linq; -namespace MfGames.Gallium +namespace MfGames.Gallium; + +public static class WhereEntityHasExtensions { - public static class WhereEntityHasExtensions + public static IEnumerable WhereEntityHas( + this IEnumerable entities) { - public static IEnumerable WhereEntityHas(this IEnumerable entities) - { - return entities.Where(x => x.Has()); - } + return entities.Where(x => x.Has()); + } - public static IEnumerable WhereEntityHasAll(this IEnumerable entities) - { - return entities.Where(x => x.HasAll()); - } + public static IEnumerable WhereEntityHasAll + (this IEnumerable entities) + { + return entities.Where(x => x.HasAll()); + } - public static IEnumerable WhereEntityHasAll(this IEnumerable entities) - { - return entities.Where(x => x.HasAll()); - } + public static IEnumerable WhereEntityHasAll + (this IEnumerable entities) + { + return entities.Where(x => x.HasAll()); + } - public static IEnumerable WhereEntityHasAll(this IEnumerable entities) - { - return entities.Where(x => x.HasAll()); - } + public static IEnumerable WhereEntityHasAll + (this IEnumerable entities) + { + return entities.Where(x => x.HasAll()); } } diff --git a/src/MfGames.Gallium/WhereEntityNotHasExtensions.cs b/src/MfGames.Gallium/WhereEntityNotHasExtensions.cs index 7d33131..58e22d4 100644 --- a/src/MfGames.Gallium/WhereEntityNotHasExtensions.cs +++ b/src/MfGames.Gallium/WhereEntityNotHasExtensions.cs @@ -1,28 +1,31 @@ using System.Collections.Generic; using System.Linq; -namespace MfGames.Gallium +namespace MfGames.Gallium; + +public static class WhereEntityNotHasExtensions { - public static class WhereEntityNotHasExtensions + public static IEnumerable WhereEntityNotHas( + this IEnumerable entities) { - public static IEnumerable WhereEntityNotHas(this IEnumerable entities) - { - return entities.Where(x => !x.Has()); - } + return entities.Where(x => !x.Has()); + } - public static IEnumerable WhereEntityNotHasAll(this IEnumerable entities) - { - return entities.Where(x => !x.HasAll()); - } + public static IEnumerable WhereEntityNotHasAll + (this IEnumerable entities) + { + return entities.Where(x => !x.HasAll()); + } - public static IEnumerable WhereEntityNotHasAll(this IEnumerable entities) - { - return entities.Where(x => !x.HasAll()); - } + public static IEnumerable WhereEntityNotHasAll + (this IEnumerable entities) + { + return entities.Where(x => !x.HasAll()); + } - public static IEnumerable WhereEntityNotHasAll(this IEnumerable entities) - { - return entities.Where(x => !x.HasAll()); - } + public static IEnumerable WhereEntityNotHasAll + (this IEnumerable entities) + { + return entities.Where(x => !x.HasAll()); } } diff --git a/tests/MfGames.Gallium.Tests/EntityTests.cs b/tests/MfGames.Gallium.Tests/EntityTests.cs index 8821bbf..803f5f2 100644 --- a/tests/MfGames.Gallium.Tests/EntityTests.cs +++ b/tests/MfGames.Gallium.Tests/EntityTests.cs @@ -1,216 +1,217 @@ using System; +using MfGames.Gallium; + using Xunit; using Xunit.Abstractions; -namespace MfGames.Gallium.Tests +namespace Gallium.Tests; + +public class EntityTests : GalliumTestsBase { - public class EntityTests : GalliumTestsBase + public EntityTests(ITestOutputHelper output) + : base(output) { - public EntityTests(ITestOutputHelper output) - : base(output) - { - } + } - [Fact] - public void AddingComponentOnceWorks() - { - var component1 = new TestComponent1(); - Entity entity1 = new Entity().Add(component1); + [Fact] + public void AddingComponentOnceWorks() + { + var component1 = new TestComponent1(); + Entity entity1 = new Entity().Add(component1); - Assert.Equal(1, entity1.Count); - Assert.True(entity1.Has()); - Assert.Equal(component1, entity1.Get()); - } + Assert.Equal(1, entity1.Count); + Assert.True(entity1.Has()); + Assert.Equal(component1, entity1.Get()); + } - [Fact] - public void AddingComponentViaInterfaceWork() - { - var component1 = new TestComponent3a(); - Entity entity1 = new Entity().Add(component1); + [Fact] + public void AddingComponentViaInterfaceWork() + { + var component1 = new TestComponent3a(); + Entity entity1 = new Entity().Add(component1); - Assert.Equal(1, entity1.Count); - Assert.False(entity1.Has()); - Assert.True(entity1.Has()); - Assert.Equal(component1, entity1.Get()); - } + Assert.Equal(1, entity1.Count); + Assert.False(entity1.Has()); + Assert.True(entity1.Has()); + Assert.Equal(component1, entity1.Get()); + } - [Fact] - public void AddingTwiceThrowsException() - { - var component1 = new TestComponent1(); + [Fact] + public void AddingTwiceThrowsException() + { + var component1 = new TestComponent1(); - Exception exception = Assert.Throws( - () => new Entity() - .Add(component1) - .Add(component1)); + Exception exception = Assert.Throws( + () => new Entity() + .Add(component1) + .Add(component1)); - Assert.Equal( - "An element with the same type " - + "(MfGames.Gallium.Tests.TestComponent1)" - + " already exists. (Parameter 'component')", - exception.Message); - } + Assert.Equal( + "An element with the same type " + + "(MfGames.Gallium.Tests.TestComponent1)" + + " already exists. (Parameter 'component')", + exception.Message); + } - [Fact] - public void CopyEntityAlsoCopiesComponents() - { - var component1 = new TestComponent1(); - Entity entity1 = new Entity().Add(component1); - Entity entity2 = entity1.Copy(); + [Fact] + public void CopyEntityAlsoCopiesComponents() + { + var component1 = new TestComponent1(); + Entity entity1 = new Entity().Add(component1); + Entity entity2 = entity1.Copy(); - Assert.Equal(1, entity2.Count); - Assert.True(entity2.Has()); - Assert.Equal(component1, entity2.Get()); - } + Assert.Equal(1, entity2.Count); + Assert.True(entity2.Has()); + Assert.Equal(component1, entity2.Get()); + } - [Fact] - public void ExactCopyEntityAlsoCopiesComponents() - { - var component1 = new TestComponent1(); - Entity entity1 = new Entity().Add(component1); - Entity entity2 = entity1.ExactCopy(); + [Fact] + public void ExactCopyEntityAlsoCopiesComponents() + { + var component1 = new TestComponent1(); + Entity entity1 = new Entity().Add(component1); + Entity entity2 = entity1.ExactCopy(); - Assert.Equal(1, entity2.Count); - Assert.True(entity2.Has()); - Assert.Equal(component1, entity2.Get()); - } + Assert.Equal(1, entity2.Count); + Assert.True(entity2.Has()); + Assert.Equal(component1, entity2.Get()); + } - [Fact] - public void GetOptionalWorks() - { - var component1 = new TestComponent1(); - Entity entity1 = new Entity().Add(component1); + [Fact] + public void GetOptionalWorks() + { + var component1 = new TestComponent1(); + Entity entity1 = new Entity().Add(component1); - Assert.Equal(component1, entity1.GetOptional()); - Assert.Null(entity1.GetOptional()); - } + Assert.Equal(component1, entity1.GetOptional()); + Assert.Null(entity1.GetOptional()); + } - [Fact] - public void HasReturnsFalseIfNotRegistered() - { - var entity1 = new Entity(); + [Fact] + public void HasReturnsFalseIfNotRegistered() + { + var entity1 = new Entity(); - Assert.False(entity1.Has()); - } + Assert.False(entity1.Has()); + } - [Fact] - public void NewEntityHasNoComponents() - { - var entity1 = new Entity(); + [Fact] + public void NewEntityHasNoComponents() + { + var entity1 = new Entity(); - Assert.Equal(0, entity1.Count); - } + Assert.Equal(0, entity1.Count); + } - [Fact] - public void RemoveAlreadyMissingComponentWorks() - { - var entity1 = new Entity(); + [Fact] + public void RemoveAlreadyMissingComponentWorks() + { + var entity1 = new Entity(); - Assert.Equal(0, entity1.Count); + Assert.Equal(0, entity1.Count); - Entity entity2 = entity1.Remove(); + Entity entity2 = entity1.Remove(); - Assert.Equal(0, entity1.Count); - Assert.Equal(0, entity2.Count); - } + Assert.Equal(0, entity1.Count); + Assert.Equal(0, entity2.Count); + } - [Fact] - public void RemoveComponentDoesNotRemoveFromCopies() - { - var component1 = new TestComponent1(); - Entity entity1 = new Entity().Add(component1); - Entity entity2 = entity1.ExactCopy(); - Entity entity3 = entity1.Copy(); + [Fact] + public void RemoveComponentDoesNotRemoveFromCopies() + { + var component1 = new TestComponent1(); + Entity entity1 = new Entity().Add(component1); + Entity entity2 = entity1.ExactCopy(); + Entity entity3 = entity1.Copy(); - Assert.Equal(1, entity1.Count); - Assert.Equal(1, entity2.Count); - Assert.Equal(1, entity3.Count); + Assert.Equal(1, entity1.Count); + Assert.Equal(1, entity2.Count); + Assert.Equal(1, entity3.Count); - Entity entity1A = entity1.Remove(); + Entity entity1A = entity1.Remove(); - Assert.Equal(1, entity1.Count); - Assert.Equal(0, entity1A.Count); - Assert.Equal(1, entity2.Count); - Assert.Equal(1, entity3.Count); - } + Assert.Equal(1, entity1.Count); + Assert.Equal(0, entity1A.Count); + Assert.Equal(1, entity2.Count); + Assert.Equal(1, entity3.Count); + } - [Fact] - public void RemoveComponentWorks() - { - var component1 = new TestComponent1(); - Entity entity1 = new Entity().Add(component1); + [Fact] + public void RemoveComponentWorks() + { + var component1 = new TestComponent1(); + Entity entity1 = new Entity().Add(component1); - Assert.Equal(1, entity1.Count); + Assert.Equal(1, entity1.Count); - Entity entity2 = entity1.Remove(); + Entity entity2 = entity1.Remove(); - Assert.Equal(1, entity1.Count); - Assert.Equal(0, entity2.Count); - } + Assert.Equal(1, entity1.Count); + Assert.Equal(0, entity2.Count); + } - [Fact] - public void SettingComponentsWorks() - { - var component1 = new TestComponent3a(); - var component2 = new TestComponent3b(); + [Fact] + public void SettingComponentsWorks() + { + var component1 = new TestComponent3a(); + var component2 = new TestComponent3b(); - Entity entity1 = new Entity() - .Set(component1) - .Set(component1) - .Set(component2); + Entity entity1 = new Entity() + .Set(component1) + .Set(component1) + .Set(component2); - Assert.Equal(1, entity1.Count); - Assert.Equal(component2, entity1.Get()); - } + Assert.Equal(1, entity1.Count); + Assert.Equal(component2, entity1.Get()); + } - [Fact] - public void TryGetWorks() - { - var component1 = new TestComponent1(); - Entity entity1 = new Entity().Add(component1); + [Fact] + public void TryGetWorks() + { + var component1 = new TestComponent1(); + Entity entity1 = new Entity().Add(component1); - bool result1 = entity1.TryGet(out TestComponent1 value1); + bool result1 = entity1.TryGet(out TestComponent1 value1); - Assert.True(result1); - Assert.Equal(component1, value1); + Assert.True(result1); + Assert.Equal(component1, value1); - bool result2 = entity1.TryGet(out TestComponent2 _); + bool result2 = entity1.TryGet(out TestComponent2 _); - Assert.False(result2); - } + Assert.False(result2); + } - [Fact] - public void TwoCopiesHaveDifferentIds() - { - var entity1 = new Entity(); - Entity entity2 = entity1.Copy(); + [Fact] + public void TwoCopiesHaveDifferentIds() + { + var entity1 = new Entity(); + Entity entity2 = entity1.Copy(); - Assert.NotEqual(entity1.Id, entity2.Id); - Assert.NotEqual(entity1, entity2); - Assert.False(entity1 == entity2); - } + Assert.NotEqual(entity1.Id, entity2.Id); + Assert.NotEqual(entity1, entity2); + Assert.False(entity1 == entity2); + } - [Fact] - public void TwoEntitiesHaveDifferentIds() - { - var entity1 = new Entity(); - var entity2 = new Entity(); + [Fact] + public void TwoEntitiesHaveDifferentIds() + { + var entity1 = new Entity(); + var entity2 = new Entity(); - Assert.NotEqual(entity1.Id, entity2.Id); - Assert.NotEqual(entity1, entity2); - Assert.False(entity1 == entity2); - } + Assert.NotEqual(entity1.Id, entity2.Id); + Assert.NotEqual(entity1, entity2); + Assert.False(entity1 == entity2); + } - [Fact] - public void TwoExactCopiesHaveDifferentIds() - { - var entity1 = new Entity(); - Entity entity2 = entity1.ExactCopy(); + [Fact] + public void TwoExactCopiesHaveDifferentIds() + { + var entity1 = new Entity(); + Entity entity2 = entity1.ExactCopy(); - Assert.Equal(entity1.Id, entity2.Id); - Assert.Equal(entity1, entity2); - Assert.True(entity1 == entity2); - } + Assert.Equal(entity1.Id, entity2.Id); + Assert.Equal(entity1, entity2); + Assert.True(entity1 == entity2); } } diff --git a/tests/MfGames.Gallium.Tests/EnumerableEntityTests.cs b/tests/MfGames.Gallium.Tests/EnumerableEntityTests.cs index 8a4659d..0ff727b 100644 --- a/tests/MfGames.Gallium.Tests/EnumerableEntityTests.cs +++ b/tests/MfGames.Gallium.Tests/EnumerableEntityTests.cs @@ -1,215 +1,221 @@ using System.Linq; +using MfGames.Gallium; + using Xunit; -namespace MfGames.Gallium.Tests +namespace Gallium.Tests; + +public class EnumerableEntityTests { - public class EnumerableEntityTests + [Fact] + public void ForComponentsC1() { - [Fact] - public void ForComponentsC1() + Entity[] entities = { - Entity[] entities = - { - new Entity().Add("1") - .Add(new TestComponent1()), - new Entity().Add("2") - .Add(new TestComponent2()), - new Entity().Add("3") - .Add(new TestComponent1()), - }; + new Entity().Add("1") + .Add(new TestComponent1()), + new Entity().Add("2") + .Add(new TestComponent2()), + new Entity().Add("3") + .Add(new TestComponent1()) + }; - Assert.Equal( - new[] { "1!", "2", "3!" }, - entities.SelectEntity( - ( - e, - _) => e.Set(e.Get() + "!")) - .Select(x => x.Get()) - .ToArray()); - } + Assert.Equal( + new[] { "1!", "2", "3!" }, + entities.SelectEntity( + ( + e, + _) => e.Set(e.Get() + "!")) + .Select(x => x.Get()) + .ToArray()); + } - [Fact] - public void ForComponentsC2() + [Fact] + public void ForComponentsC2() + { + Entity[] entities = { - Entity[] entities = - { - new Entity().Add("1") - .Add(new TestComponent1()), - new Entity().Add("2") - .Add(new TestComponent2()), - new Entity().Add("3") - .Add(new TestComponent1()) - .Add(new TestComponent2()), - }; + new Entity().Add("1") + .Add(new TestComponent1()), + new Entity().Add("2") + .Add(new TestComponent2()), + new Entity().Add("3") + .Add(new TestComponent1()) + .Add(new TestComponent2()) + }; - Assert.Equal( - new[] { "1", "2", "3!" }, - entities.SelectEntity( - ( - e, - _, - _) => e.Set(e.Get() + "!")) - .Select(x => x.Get()) - .ToArray()); - } + Assert.Equal( + new[] { "1", "2", "3!" }, + entities.SelectEntity( + ( + e, + _, + _) => e.Set(e.Get() + "!")) + .Select(x => x.Get()) + .ToArray()); + } - [Fact] - public void ForComponentsC3() + [Fact] + public void ForComponentsC3() + { + Entity[] entities = { - Entity[] entities = - { - new Entity().Add("1") - .Add(new TestComponent1()), - new Entity().Add("2") - .Add(new TestComponent2()) - .Add(new TestComponent3b()), - new Entity().Add("3") - .Add(new TestComponent3a()) - .Add(new TestComponent1()) - .Add(new TestComponent2()), - }; + new Entity().Add("1") + .Add(new TestComponent1()), + new Entity().Add("2") + .Add(new TestComponent2()) + .Add(new TestComponent3b()), + new Entity().Add("3") + .Add(new TestComponent3a()) + .Add(new TestComponent1()) + .Add(new TestComponent2()) + }; - Assert.Equal( - new[] { "1", "2", "3-TestComponent3a" }, - entities.SelectEntity( - ( - e, - _, - _, - t) => e.Set( - e.Get() - + "-" - + t.GetType() - .Name)) - .Select(x => x.Get()) - .ToArray()); - } + Assert.Equal( + new[] { "1", "2", "3-TestComponent3a" }, + entities + .SelectEntity( + ( + e, + _, + _, + t) => e.Set( + e.Get() + + "-" + + t.GetType() + .Name)) + .Select(x => x.Get()) + .ToArray()); + } - [Fact] - public void HasComponentsC1() + [Fact] + public void HasComponentsC1() + { + Entity[] entities = { - Entity[] entities = - { - new Entity().Add("1") - .Add(new TestComponent1()), - new Entity().Add("2") - .Add(new TestComponent2()), - new Entity().Add("3") - .Add(new TestComponent1()), - }; + new Entity().Add("1") + .Add(new TestComponent1()), + new Entity().Add("2") + .Add(new TestComponent2()), + new Entity().Add("3") + .Add(new TestComponent1()) + }; - Assert.Equal( - new[] { "1", "3" }, - entities.WhereEntityHas() - .Select(x => x.Get()) - .ToArray()); - } + Assert.Equal( + new[] { "1", "3" }, + entities.WhereEntityHas() + .Select(x => x.Get()) + .ToArray()); + } - [Fact] - public void HasComponentsC2() + [Fact] + public void HasComponentsC2() + { + Entity[] entities = { - Entity[] entities = - { - new Entity().Add("1") - .Add(new TestComponent1()), - new Entity().Add("2") - .Add(new TestComponent2()) - .Add(new TestComponent1()), - new Entity().Add("3") - .Add(new TestComponent1()), - }; + new Entity().Add("1") + .Add(new TestComponent1()), + new Entity().Add("2") + .Add(new TestComponent2()) + .Add(new TestComponent1()), + new Entity().Add("3") + .Add(new TestComponent1()) + }; - Assert.Equal( - new[] { "2" }, - entities.WhereEntityHasAll() - .Select(x => x.Get()) - .ToArray()); - } + Assert.Equal( + new[] { "2" }, + entities.WhereEntityHasAll() + .Select(x => x.Get()) + .ToArray()); + } - [Fact] - public void HasComponentsC3() + [Fact] + public void HasComponentsC3() + { + Entity[] entities = { - Entity[] entities = - { - new Entity().Add("1") - .Add(new TestComponent1()) - .Add(new TestComponent3b()) - .Add(new TestComponent2()), - new Entity().Add("2") - .Add(new TestComponent2()) - .Add(new TestComponent1()), - new Entity().Add("3") - .Add(new TestComponent3a()), - }; + new Entity().Add("1") + .Add(new TestComponent1()) + .Add(new TestComponent3b()) + .Add(new TestComponent2()), + new Entity().Add("2") + .Add(new TestComponent2()) + .Add(new TestComponent1()), + new Entity().Add("3") + .Add(new TestComponent3a()) + }; - Assert.Equal( - new[] { "1" }, - entities.WhereEntityHasAll() - .Select(x => x.Get()) - .ToArray()); - } + Assert.Equal( + new[] { "1" }, + entities + .WhereEntityHasAll() + .Select(x => x.Get()) + .ToArray()); + } - [Fact] - public void NotComponentsC1() + [Fact] + public void NotComponentsC1() + { + Entity[] entities = { - Entity[] entities = - { - new Entity().Add("1") - .Add(new TestComponent1()), - new Entity().Add("2") - .Add(new TestComponent2()), - new Entity().Add("3") - .Add(new TestComponent1()), - }; + new Entity().Add("1") + .Add(new TestComponent1()), + new Entity().Add("2") + .Add(new TestComponent2()), + new Entity().Add("3") + .Add(new TestComponent1()) + }; - Assert.Equal( - new[] { "2" }, - entities.WhereEntityNotHas() - .Select(x => x.Get()) - .ToArray()); - } + Assert.Equal( + new[] { "2" }, + entities.WhereEntityNotHas() + .Select(x => x.Get()) + .ToArray()); + } - [Fact] - public void NotComponentsC2() + [Fact] + public void NotComponentsC2() + { + Entity[] entities = { - Entity[] entities = - { - new Entity().Add("1") - .Add(new TestComponent1()), - new Entity().Add("2") - .Add(new TestComponent2()) - .Add(new TestComponent1()), - new Entity().Add("3"), - }; + new Entity().Add("1") + .Add(new TestComponent1()), + new Entity().Add("2") + .Add(new TestComponent2()) + .Add(new TestComponent1()), + new Entity().Add("3") + }; - Assert.Equal( - new[] { "1", "3" }, - entities.WhereEntityNotHasAll() - .Select(x => x.Get()) - .ToArray()); - } + Assert.Equal( + new[] { "1", "3" }, + entities.WhereEntityNotHasAll() + .Select(x => x.Get()) + .ToArray()); + } - [Fact] - public void NotComponentsC3() + [Fact] + public void NotComponentsC3() + { + Entity[] entities = { - Entity[] entities = - { - new Entity().Add("1") - .Add(new TestComponent1()), - new Entity().Add("2") - .Add(new TestComponent1()) - .Add(new TestComponent2()) - .Add(new TestComponent3b()), - new Entity().Add("3") - .Add(new TestComponent3a()), - }; + new Entity().Add("1") + .Add(new TestComponent1()), + new Entity().Add("2") + .Add(new TestComponent1()) + .Add(new TestComponent2()) + .Add(new TestComponent3b()), + new Entity().Add("3") + .Add(new TestComponent3a()) + }; - Assert.Equal( - new string[] { "1", "3" }, - entities.WhereEntityNotHasAll() - .Select(x => x.Get()) - .ToArray()); - } + Assert.Equal( + new[] { "1", "3" }, + entities + .WhereEntityNotHasAll() + .Select(x => x.Get()) + .ToArray()); } } diff --git a/tests/MfGames.Gallium.Tests/GalliumTestsBase.cs b/tests/MfGames.Gallium.Tests/GalliumTestsBase.cs index 757acbe..89cc6cf 100644 --- a/tests/MfGames.Gallium.Tests/GalliumTestsBase.cs +++ b/tests/MfGames.Gallium.Tests/GalliumTestsBase.cs @@ -3,39 +3,38 @@ using Serilog.Core; using Xunit.Abstractions; -namespace MfGames.Gallium.Tests +namespace Gallium.Tests; + +/// +/// Common initialization logic for Gallium-based tests including setting +/// up containers, logging, and Serilog. +/// +public abstract class GalliumTestsBase { - /// - /// Common initialization logic for Gallium-based tests including setting - /// up containers, logging, and Serilog. - /// - public abstract class GalliumTestsBase + protected GalliumTestsBase(ITestOutputHelper output) { - protected GalliumTestsBase(ITestOutputHelper output) - { - this.Output = output; + this.Output = output; - // Set up logging. - const string Template = - "[{Level:u3}] " - + "({SourceContext}) {Message}" - + "{NewLine}{Exception}"; + // Set up logging. + const string Template = + "[{Level:u3}] " + + "({SourceContext}) {Message}" + + "{NewLine}{Exception}"; - this.Logger = new LoggerConfiguration() - .WriteTo.TestOutput( - output, - outputTemplate: Template) - .CreateLogger(); - } - - /// - /// Gets the output for the tests. - /// - public ITestOutputHelper Output { get; } - - /// - /// Gets the logger used to report messages about the test. - /// - protected Logger Logger { get; } + this.Logger = new LoggerConfiguration() + .WriteTo.TestOutput( + output, + outputTemplate: Template) + .CreateLogger(); } + + /// + /// Gets the output for the tests. + /// + public ITestOutputHelper Output { get; } + + /// + /// Gets the logger used to report messages about the test. + /// + protected Logger Logger { get; } } diff --git a/tests/MfGames.Gallium.Tests/ITestComponent3.cs b/tests/MfGames.Gallium.Tests/ITestComponent3.cs index 21648a6..ccc4105 100644 --- a/tests/MfGames.Gallium.Tests/ITestComponent3.cs +++ b/tests/MfGames.Gallium.Tests/ITestComponent3.cs @@ -1,6 +1,5 @@ -namespace MfGames.Gallium.Tests +namespace Gallium.Tests; + +public interface ITestComponent3 { - public interface ITestComponent3 - { - } } diff --git a/tests/MfGames.Gallium.Tests/MfGames.Gallium.Tests.csproj b/tests/MfGames.Gallium.Tests/MfGames.Gallium.Tests.csproj index 4877045..febe6e9 100644 --- a/tests/MfGames.Gallium.Tests/MfGames.Gallium.Tests.csproj +++ b/tests/MfGames.Gallium.Tests/MfGames.Gallium.Tests.csproj @@ -10,10 +10,10 @@ 1.4.0 - - - - + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all @@ -24,6 +24,6 @@ - + diff --git a/tests/MfGames.Gallium.Tests/TestComponent1.cs b/tests/MfGames.Gallium.Tests/TestComponent1.cs index 729038a..8634d8c 100644 --- a/tests/MfGames.Gallium.Tests/TestComponent1.cs +++ b/tests/MfGames.Gallium.Tests/TestComponent1.cs @@ -1,6 +1,5 @@ -namespace MfGames.Gallium.Tests +namespace Gallium.Tests; + +public class TestComponent1 { - public class TestComponent1 - { - } } diff --git a/tests/MfGames.Gallium.Tests/TestComponent2.cs b/tests/MfGames.Gallium.Tests/TestComponent2.cs index b06e69f..1775dd2 100644 --- a/tests/MfGames.Gallium.Tests/TestComponent2.cs +++ b/tests/MfGames.Gallium.Tests/TestComponent2.cs @@ -1,6 +1,5 @@ -namespace MfGames.Gallium.Tests +namespace Gallium.Tests; + +public class TestComponent2 { - public class TestComponent2 - { - } } diff --git a/tests/MfGames.Gallium.Tests/TestComponent3a.cs b/tests/MfGames.Gallium.Tests/TestComponent3a.cs index a6b1c43..29f9a4a 100644 --- a/tests/MfGames.Gallium.Tests/TestComponent3a.cs +++ b/tests/MfGames.Gallium.Tests/TestComponent3a.cs @@ -1,6 +1,5 @@ -namespace MfGames.Gallium.Tests +namespace Gallium.Tests; + +public class TestComponent3a : ITestComponent3 { - public class TestComponent3a : ITestComponent3 - { - } } diff --git a/tests/MfGames.Gallium.Tests/TestComponent3b.cs b/tests/MfGames.Gallium.Tests/TestComponent3b.cs index c886a3b..9631a65 100644 --- a/tests/MfGames.Gallium.Tests/TestComponent3b.cs +++ b/tests/MfGames.Gallium.Tests/TestComponent3b.cs @@ -1,6 +1,5 @@ -namespace MfGames.Gallium.Tests +namespace Gallium.Tests; + +public class TestComponent3b : ITestComponent3 { - public class TestComponent3b : ITestComponent3 - { - } }