예제 #1
0
// Test slice filtering works correctly
func TestFilter(t *testing.T) {
	sl := entity.NewSlice()

	e0, e1, e2 := MockEntity{0, entity.EntityType("a")}, MockEntity{1, entity.EntityType("a")}, MockEntity{2, entity.EntityType("b")}

	sl.Add(e0)
	sl.Add(e1)
	sl.Add(e2)

	var subset *entity.Slice
	subset = sl.Filter("a")
	compareLists(t, "filtered", subset, []MockEntity{e0, e1})
	subset = sl.Filter("b")
	compareLists(t, "filtered", subset, []MockEntity{e2})
}
예제 #2
0
// Simulates a few entity add/remove/update cycles
func TestContainer(t *testing.T) {
	col := entity.NewCollection()

	e0, e1, e2 := MockEntity{0, entity.EntityType("a")}, MockEntity{1, entity.EntityType("a")}, MockEntity{2, entity.EntityType("b")}

	tcs := []struct {
		add      []MockEntity
		entities []MockEntity
		remove   []MockEntity
	}{
		{
			add:      []MockEntity{e0},
			entities: []MockEntity{e0},
			remove:   []MockEntity{},
		},
		{
			add:      []MockEntity{e1},
			entities: []MockEntity{e0, e1},
			remove:   []MockEntity{},
		},
		{
			add:      []MockEntity{},
			entities: []MockEntity{e0, e1},
			remove:   []MockEntity{},
		},
		{
			add:      []MockEntity{e2},
			entities: []MockEntity{e1, e2},
			remove:   []MockEntity{e0},
		},
		{
			add:      []MockEntity{},
			entities: []MockEntity{e2},
			remove:   []MockEntity{e1},
		},
		{
			add:      []MockEntity{},
			entities: []MockEntity{},
			remove:   []MockEntity{e2},
		},
		{
			add:      []MockEntity{},
			entities: []MockEntity{},
			remove:   []MockEntity{},
		},
	}

	for i, tc := range tcs {
		t.Logf("cycle %v", i)
		col.Update()

		for _, e := range tc.add {
			col.Add(e)
		}

		for _, e := range tc.remove {
			col.Remove(e)
		}

		compareLists(t, "adding", col.Adding(), tc.add)
		compareLists(t, "removing", col.Removing(), tc.remove)
		compareLists(t, "entities", col.Entities(), tc.entities)
	}
}