summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/tests/MonoGame.Extended.Entities.Tests/ComponentMapperTests.cs
blob: 54fab047660f04857c5f72c67f9922a302e9784a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using Xunit;

namespace MonoGame.Extended.Entities.Tests
{
    public class ComponentMapperTests
    {
        [Fact]
        public void CreateComponentMapper()
        {
            var mapper = new ComponentMapper<object>(0, _ => {});

            Assert.Equal(typeof(object), mapper.ComponentType);
            Assert.Empty(mapper.Components);
        }

        [Fact]
        public void OnPut()
        {
            const int entityId = 3;

            var mapper = new ComponentMapper<Transform2>(1, _ => { });
            var component = new Transform2();

            mapper.OnPut += (entId) =>
            {
                Assert.Equal(entityId, entId);
                Assert.Same(component, mapper.Get(entityId));
            };

            mapper.Put(entityId, component);
        }

        [Fact]
        public void PutAndGetComponent()
        {
            const int entityId = 3;

            var mapper = new ComponentMapper<Transform2>(1, _ => { });
            var component = new Transform2();

            mapper.Put(entityId, component);

            Assert.Equal(typeof(Transform2), mapper.ComponentType);
            Assert.True(mapper.Components.Count >= 1);
            Assert.Same(component, mapper.Get(entityId));
        }

        [Fact]
        public void OnDelete()
        {
            const int entityId = 1;

            var mapper = new ComponentMapper<Transform2>(2, _ => { });
            var component = new Transform2();

            mapper.OnDelete += (entId) =>
            {
                Assert.Equal(entityId, entId);
                Assert.False(mapper.Has(entityId));
            };

            mapper.Put(entityId, component);
            mapper.Delete(entityId);
        }

        [Fact]
        public void DeleteComponent()
        {
            const int entityId = 1;

            var mapper = new ComponentMapper<Transform2>(2, _ => { });
            var component = new Transform2();

            mapper.Put(entityId, component);
            mapper.Delete(entityId);

            Assert.False(mapper.Has(entityId));
        }

        [Fact]
        public void HasComponent()
        {
            const int entityId = 0;

            var mapper = new ComponentMapper<Transform2>(3, _ => { });
            var component = new Transform2();

            Assert.False(mapper.Has(entityId));

            mapper.Put(entityId, component);

            Assert.True(mapper.Has(entityId));
        }
    }
}