• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / ark / 13642217050

03 Mar 2025 11:02PM CUT coverage: 99.261% (+0.4%) from 98.883%
13642217050

push

github

web-flow
Add more unit tests (#119)

* test entity pool capacity
* test relations panic
* test table match panics
* test register on locked world
* test cache panic
* test storage panics
* test exchange mask panics

3 of 3 new or added lines in 1 file covered. (100.0%)

5772 of 5815 relevant lines covered (99.26%)

34225.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/ecs/functions.go
1
package ecs
2

3
import "reflect"
4

5
// ComponentID returns the [ID] for a component type via generics.
6
// Registers the type if it is not already registered.
7
//
8
// The number of unique component types per [World] is limited to 256 ([MaskTotalBits]).
9
//
10
// Panics if called on a locked world and the type is not registered yet.
11
//
12
// Note that type aliases are not considered separate component types.
13
// Type re-definitions, however, are separate types.
14
//
15
// ⚠️ Warning: Using IDs that are outside of the range of registered IDs anywhere in [World] or other places will result in undefined behavior!
16
func ComponentID[T any](w *World) ID {
2,623✔
17
        return w.componentID(typeOf[T]())
2,623✔
18
}
2,623✔
19

20
// ComponentIDs returns a list of all registered component IDs.
21
func ComponentIDs(w *World) []ID {
1✔
22
        intIds := w.storage.registry.IDs
1✔
23
        ids := make([]ID, len(intIds))
1✔
24
        for i, iid := range intIds {
3✔
25
                ids[i] = id8(iid)
2✔
26
        }
2✔
27
        return ids
1✔
28
}
29

30
// ComponentInfo returns the [CompInfo] for a component [ID], and whether the ID is assigned.
31
func ComponentInfo(w *World, id ID) (CompInfo, bool) {
2✔
32
        tp, ok := w.storage.registry.ComponentType(id.id)
2✔
33
        if !ok {
3✔
34
                return CompInfo{}, false
1✔
35
        }
1✔
36

37
        return CompInfo{
1✔
38
                ID:         id,
1✔
39
                Type:       tp,
1✔
40
                IsRelation: w.storage.registry.IsRelation[id.id],
1✔
41
        }, true
1✔
42
}
43

44
// TypeID returns the [ID] for a component type.
45
// Registers the type if it is not already registered.
46
//
47
// The number of unique component types per [World] is limited to [MaskTotalBits].
48
func TypeID(w *World, tp reflect.Type) ID {
2✔
49
        return w.componentID(tp)
2✔
50
}
2✔
51

52
// Comp is a helper to pass component types to functions and methods.
53
// Use function [C] to create one.
54
type Comp struct {
55
        tp reflect.Type
56
}
57

58
// C creates a [Comp] instance for the given type.
59
func C[T any]() Comp {
100✔
60
        return Comp{typeOf[T]()}
100✔
61
}
100✔
62

63
// Type returns the reflect.Type of the component.
64
func (c Comp) Type() reflect.Type {
1✔
65
        return c.tp
1✔
66
}
1✔
67

68
// ResourceID returns the [ResID] for a resource type via generics.
69
// Registers the type if it is not already registered.
70
//
71
// The number of resources per [World] is limited to [MaskTotalBits].
72
func ResourceID[T any](w *World) ResID {
9✔
73
        return w.resourceID(typeOf[T]())
9✔
74
}
9✔
75

76
// ResourceIDs returns a list of all registered resource IDs.
77
func ResourceIDs(w *World) []ResID {
1✔
78
        intIds := w.resources.registry.IDs
1✔
79
        ids := make([]ResID, len(intIds))
1✔
80
        for i, iid := range intIds {
3✔
81
                ids[i] = ResID{id: iid}
2✔
82
        }
2✔
83
        return ids
1✔
84
}
85

86
// ResourceType returns the reflect.Type for a resource [ResID], and whether the ID is assigned.
87
func ResourceType(w *World, id ResID) (reflect.Type, bool) {
2✔
88
        return w.resources.registry.ComponentType(id.id)
2✔
89
}
2✔
90

91
// GetResource returns a pointer to the given resource type in the world.
92
// Returns nil if there is no such resource.
93
//
94
// Uses reflection. For more efficient access, use [Resource].
95
// This more than 20 times faster than the GetResource function.
96
//
97
// See also [AddResource].
98
func GetResource[T any](w *World) *T {
1✔
99
        return w.resources.Get(ResourceID[T](w)).(*T)
1✔
100
}
1✔
101

102
// AddResource adds a resource to the world.
103
// Returns the ID for the added resource.
104
//
105
// Panics if there is already such a resource.
106
//
107
// Uses reflection. For more efficient access, use [Resource].
108
//
109
// The number of resources per [World] is limited to [MaskTotalBits].
110
func AddResource[T any](w *World, res *T) ResID {
2✔
111
        id := ResourceID[T](w)
2✔
112
        w.resources.Add(id, res)
2✔
113
        return id
2✔
114
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc