Skip to content

Commit 8c44f07

Browse files
authored
Feature/0.4.3 1 (#3)
* Fix assets
1 parent 3b60798 commit 8c44f07

15 files changed

Lines changed: 357 additions & 69 deletions

.github/workflows/dotnet.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Run tests with coverage
2626
run: |
2727
cd ./src/Hexecs.Tests/
28-
dotnet test -c Release --no-build /p:CollectCoverage=true /p:CoverletOutput=TestResults/ /p:CoverletOutputFormat=opencover
28+
dotnet test -c Release --no-build /p:CollectCoverage=true /p:CoverletOutput=TestResults/ /p:CoverletOutputFormat=opencover /p:Exclude="[*Tests*]*|[*Benchmarks*]*|[*Demo*]*"
2929
- name: Publish coverage report
3030
if: matrix.os == 'ubuntu-latest' && matrix.dotnet-version == '10.0.x'
3131
uses: codecov/codecov-action@v3
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using Hexecs.Tests.Mocks;
2+
3+
namespace Hexecs.Tests.Assets;
4+
5+
public sealed class AssetFilter1Should(AssetTestFixture fixture) : IClassFixture<AssetTestFixture>
6+
{
7+
[Fact(DisplayName = "Фильтр ассетов должен содержать все созданные ассеты")]
8+
public void ContainsAllAssets()
9+
{
10+
// arrange
11+
var assetIds = new List<uint>();
12+
13+
var (context, world) = fixture.CreateAssetContext(loader =>
14+
{
15+
for (int i = 1; i < 100; i++)
16+
{
17+
var asset = loader.CreateAsset(new CarAsset(i, i));
18+
assetIds.Add(asset.Id);
19+
}
20+
});
21+
22+
var expectedAssets = assetIds.Select(id => context.GetAsset(id)).ToArray();
23+
24+
// act
25+
26+
var filter = context.Filter<CarAsset>();
27+
var actualActors = filter.ToArray();
28+
29+
// assert
30+
31+
actualActors
32+
.Should()
33+
.Contain(expectedAssets);
34+
35+
world.Dispose();
36+
}
37+
38+
[Fact(DisplayName = "Фильтр ассетов можно перебирать как AssetRef")]
39+
public void AssetFilterShouldEnumerable()
40+
{
41+
// arrange
42+
var expectedIds = new Dictionary<uint, CarAsset>();
43+
44+
var (context, world) = fixture.CreateAssetContext(loader =>
45+
{
46+
for (var i = 0; i < 100; i++)
47+
{
48+
var component = new CarAsset(i, i);
49+
var asset = loader.CreateAsset(component);
50+
51+
expectedIds.Add(asset.Id, component);
52+
}
53+
});
54+
55+
// act
56+
57+
var filter = context.Filter<CarAsset>();
58+
59+
// assert
60+
61+
var actualIds = new List<uint>();
62+
foreach (var asset in filter)
63+
{
64+
actualIds.Add(asset.Id);
65+
asset
66+
.Component1
67+
.Should().Be(expectedIds[asset.Id]);
68+
}
69+
70+
filter.Length
71+
.Should().Be(expectedIds.Count);
72+
73+
actualIds
74+
.Should()
75+
.HaveCount(expectedIds.Count);
76+
77+
actualIds
78+
.Should()
79+
.Contain(expectedIds.Keys);
80+
81+
world.Dispose();
82+
}
83+
}

src/Hexecs.Tests/Hexecs.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
<PropertyGroup>
44
<IsPackable>false</IsPackable>
55
<IsTestProject>true</IsTestProject>
6+
<CollectCoverage>true</CollectCoverage>
7+
<CoverletOutputFormat>opencover</CoverletOutputFormat>
8+
<Exclude>[*Tests*]*,[*Benchmarks*]*,[*Demo*]*</Exclude>
69
</PropertyGroup>
710

811
<ItemGroup>

src/Hexecs.Tests/Mocks/CarAsset.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Hexecs.Assets;
2+
3+
namespace Hexecs.Tests.Mocks;
4+
5+
public readonly struct CarAsset(int price, int speed) : IAssetComponent
6+
{
7+
public const string Alias1 = "Alias1";
8+
public const string Alias2 = "Alias2";
9+
10+
public readonly int Price = price;
11+
public readonly int Speed = speed;
12+
}

src/Hexecs/Actors/ActorContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public Actor Clone(uint actorId, bool withParent = true)
171171
[MethodImpl(MethodImplOptions.AggressiveInlining)]
172172
public Actor CreateActor(uint? expectedId = null)
173173
{
174-
if (expectedId == Actor.EmptyId) ActorError.WrongId();
174+
if (expectedId == Actor.EmptyId) ActorError.InvalidId();
175175
var actorId = expectedId ?? GetNextActorId();
176176

177177
AddEntry(actorId);

src/Hexecs/Actors/ActorError.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ public static T QueueNotFound<T>()
142142
throw new Exception($"System {TypeOf<T>.GetTypeName()} isn't found");
143143
}
144144

145+
[DoesNotReturn]
146+
public static void InvalidId()
147+
{
148+
throw new Exception("Invalid actor id");
149+
}
150+
145151
/// <summary>
146152
/// Генерирует исключение, когда актёр с указанным ключом не найден.
147153
/// </summary>
@@ -255,10 +261,4 @@ public static void ValueNotFound(string name, Type type)
255261
{
256262
throw new Exception($"Value '{name}' (type {TypeOf.GetTypeName(type)}) isn't found");
257263
}
258-
259-
[DoesNotReturn]
260-
public static void WrongId()
261-
{
262-
throw new Exception("Wrong actor id");
263-
}
264264
}

src/Hexecs/Assets/AssetContext.Dictionary.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public int Length
1212

1313
private ref Entry AddEntry(uint id)
1414
{
15+
if (id == Asset.EmptyId) AssetError.InvalidId();
1516
ref var entry = ref CollectionsMarshal.GetValueRefOrAddDefault(_entries, id, out var exists);
1617
if (exists) AssetError.AlreadyExists(id);
1718
return ref entry;

src/Hexecs/Assets/AssetContext.Entry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public void Add(ushort componentId)
1212
ArrayUtils.InsertOrCreate(ref _array, _length, componentId);
1313
_length++;
1414
}
15-
15+
1616
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1717
public readonly ReadOnlySpan<ushort> AsReadOnlySpan() => _length == 0
1818
? ReadOnlySpan<ushort>.Empty

src/Hexecs/Assets/AssetContext.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@ public Asset<T1> GetAsset<T1>()
7676
where T1 : struct, IAssetComponent
7777
{
7878
var pool = GetComponentPool<T1>();
79-
if (pool is { Length: > 0 }) return pool.First();
79+
if (pool != null)
80+
{
81+
var firstId = pool.FirstId();
82+
if (firstId != Asset.EmptyId)
83+
{
84+
return new Asset<T1>(this, firstId);
85+
}
86+
}
8087

8188
AssetError.NotFound<T1>();
8289
return Asset<T1>.Empty;
@@ -92,8 +99,6 @@ public Asset<T1> GetAsset<T1>()
9299
public Asset<T1> GetAsset<T1>(uint assetId)
93100
where T1 : struct, IAssetComponent
94101
{
95-
Debug.Assert(ExistsAsset(assetId), $"Asset {assetId} isn't found");
96-
97102
var pool = GetComponentPool<T1>();
98103
if (pool == null || !pool.Has(assetId)) AssetError.ComponentNotFound<T1>(assetId);
99104

src/Hexecs/Assets/AssetError.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public static void ConstraintExists<T>()
9797
throw new Exception($"Constraint for {TypeOf<T>.GetTypeName()} already exists");
9898
}
9999

100+
[DoesNotReturn]
101+
public static void InvalidId()
102+
{
103+
throw new Exception("Invalid asset id");
104+
}
105+
100106
/// <summary>
101107
/// Генерирует исключение при попытке использовать загрузчик ассетов, который уже освобожден.
102108
/// </summary>

0 commit comments

Comments
 (0)