using System.Linq; using Xunit; namespace MfGames.Gallium.Tests { public class EnumerableEntityTests { [Fact] public void ForComponentsC1() { Entity[] entities = { 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()); } [Fact] public void ForComponentsC2() { 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()), }; Assert.Equal( new[] { "1", "2", "3!" }, entities.SelectEntity( ( e, _, _) => e.Set(e.Get() + "!")) .Select(x => x.Get()) .ToArray()); } [Fact] public void ForComponentsC3() { 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()), }; 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() { Entity[] entities = { 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()); } [Fact] public void HasComponentsC2() { 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()), }; Assert.Equal( new[] { "2" }, entities.WhereEntityHasAll() .Select(x => x.Get()) .ToArray()); } [Fact] public void HasComponentsC3() { 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()), }; Assert.Equal( new[] { "1" }, entities.WhereEntityHasAll() .Select(x => x.Get()) .ToArray()); } [Fact] public void NotComponentsC1() { Entity[] entities = { 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()); } [Fact] public void NotComponentsC2() { Entity[] entities = { 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()); } [Fact] public void NotComponentsC3() { 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()), }; Assert.Equal( new string[] { "1", "3" }, entities.WhereEntityNotHasAll() .Select(x => x.Get()) .ToArray()); } } }