summaryrefslogtreecommitdiff
path: root/core/src/test/java/coffee/liz/ecs/model/EntityTest.java
blob: 5be4cdb7efad7c6a3ea2688774658b2248f04d8d (plain) (blame)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package coffee.liz.ecs.model;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import coffee.liz.ecs.events.ComponentAdded;
import coffee.liz.ecs.events.ComponentRemoved;
import coffee.liz.ecs.events.EntityEvent;

import lombok.RequiredArgsConstructor;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Stream;

final class EntityTest {
    @ParameterizedTest
    @MethodSource("componentCombinationProvider")
    public void hasAllReportsPresenceForComponentSets(final Collection<Class<? extends Component>> query,
            final boolean expected) {
        final Entity entity = Entity.builder().id(7).build();
        entity.add(new AlphaComponent("first"));
        entity.add(new BetaComponent(3));
        entity.add(new GammaKeyedComponent());

        assertEquals(expected, entity.hasAll(query));
    }

    private static Stream<Arguments> componentCombinationProvider() {
        return Stream
                .of(Arguments.of(List.of(AlphaComponent.class), true), Arguments.of(List.of(BetaComponent.class), true),
                        Arguments.of(List.of(AlphaComponent.class, BetaComponent.class, GammaComponent.class), true),
                        Arguments.of(List.of(AlphaComponent.class, BetaComponent.class, GammaKeyedComponent.class),
                                false),
                        Arguments.of(List.of(GammaComponent.class), true),
                        Arguments.of(List.of(GammaKeyedComponent.class), false));
    }

    @Test
    public void getThrowsForMissingComponentsWithHelpfulMessage() {
        final Entity entity = Entity.builder().id(99).build();

        final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class,
                () -> entity.get(AlphaComponent.class));

        assertTrue(thrown.getMessage().contains("AlphaComponent"));
        assertTrue(thrown.getMessage().contains("99"));
    }

    @Test
    public void addReplacesExistingComponentInstance() {
        final Entity entity = Entity.builder().id(17).build();
        final AlphaComponent initial = new AlphaComponent("initial");
        entity.add(initial);

        final AlphaComponent replacement = new AlphaComponent("replacement");
        entity.add(replacement);

        assertSame(replacement, entity.get(AlphaComponent.class));
    }

    @Test
    public void replacingExistingComponentDoesNotEmitAddedEvent() {
        final Entity entity = Entity.builder().id(18).build();
        final AtomicInteger addedCount = new AtomicInteger(0);
        final Consumer<EntityEvent> hook = entity.subscribe(event -> {
            if (event instanceof ComponentAdded) {
                addedCount.incrementAndGet();
            }
        });

        entity.add(new AlphaComponent("initial"));
        entity.add(new AlphaComponent("replacement"));

        assertEquals(1, addedCount.get());
        entity.unsubscribe(hook);
    }

    @Test
    public void removeClearsComponentPresence() {
        final Entity entity = Entity.builder().id(45).build();
        entity.add(new BetaComponent(2));
        assertTrue(entity.has(BetaComponent.class));

        entity.remove(BetaComponent.class);

        assertFalse(entity.has(BetaComponent.class));
        assertTrue(entity.componentTypes().isEmpty());
    }

    @Test
    public void subscribeReceivesEmittedEvents() {
        final Entity entity = Entity.builder().id(51).build();
        final AtomicInteger addedCount = new AtomicInteger(0);
        final AtomicInteger removedCount = new AtomicInteger(0);

        final Consumer<EntityEvent> hook = entity.subscribe(event -> {
            if (event instanceof ComponentAdded) {
                addedCount.incrementAndGet();
            }
            if (event instanceof ComponentRemoved) {
                removedCount.incrementAndGet();
            }
        });

        entity.add(new AlphaComponent("a"));
        entity.remove(AlphaComponent.class);

        assertEquals(1, addedCount.get());
        assertEquals(1, removedCount.get());

        entity.unsubscribe(hook);
    }

    private record AlphaComponent(String name) implements Component {
    }

    private record BetaComponent(int level) implements Component {
    }

    @RequiredArgsConstructor
    private class GammaComponent implements Component {
        @Override
        public Class<? extends Component> getKey() {
            return GammaComponent.class;
        }
    }

    private class GammaKeyedComponent extends GammaComponent {
    }

    private record ContextualComponent(int ownerId) implements Component {
    }
}