Adds ScopedOwner class, that disposes owned values when garbage collected.

This commit is contained in:
2021-01-15 16:53:32 +01:00
parent 7e31bba05e
commit 7807ee9676
18 changed files with 195 additions and 57 deletions

View File

@@ -16,12 +16,12 @@ namespace PkmnLibSharpTests.Battling.BattleTests
{
var lib = BattleLibraryHelper.GetLibrary();
var battle = new BattleBuilder(lib, true, 2, 1).Build();
Assert.AreEqual(lib, battle.Library);
Assert.AreEqual(true, battle.CanFlee);
Assert.AreEqual(2, battle.SidesCount);
Assert.AreEqual(false, battle.HasEnded);
Assert.AreEqual(0, battle.PartiesCount);
battle.Dispose();
Assert.AreEqual(lib, battle.Value.Library);
Assert.AreEqual(true, battle.Value.CanFlee);
Assert.AreEqual(2, battle.Value.SidesCount);
Assert.AreEqual(false, battle.Value.HasEnded);
Assert.AreEqual(0, battle.Value.PartiesCount);
battle.Value.Dispose();
}
private static PokemonParty BuildTestParty(BattleLibrary lib)
@@ -45,10 +45,10 @@ namespace PkmnLibSharpTests.Battling.BattleTests
.WithPartyOnPositions(p1, new BattlePosition(0, 0))
.WithPartyOnPositions(p2, new BattlePosition(1, 0))
.Build();
Assert.AreEqual(2, battle.PartiesCount);
Assert.AreEqual(p1, battle.Parties[0].Party);
Assert.AreEqual(p2, battle.Parties[1].Party);
battle.Dispose();
Assert.AreEqual(2, battle.Value.PartiesCount);
Assert.AreEqual(p1, battle.Value.Parties[0].Party);
Assert.AreEqual(p2, battle.Value.Parties[1].Party);
battle.Value.Dispose();
}
[Test]
@@ -63,19 +63,19 @@ namespace PkmnLibSharpTests.Battling.BattleTests
.WithPartyOnPositions(p2, new BattlePosition(1, 0))
.Build();
battle.SwitchPokemon(0, 0, p1.GetAtIndex(0));
battle.SwitchPokemon(1, 0, p2.GetAtIndex(0));
Assert.AreEqual(0, battle.CurrentTurn);
battle.Value.SwitchPokemon(0, 0, p1.GetAtIndex(0));
battle.Value.SwitchPokemon(1, 0, p2.GetAtIndex(0));
Assert.AreEqual(0, battle.Value.CurrentTurn);
var moveTurn1 = new MoveTurnChoice(p1.GetAtIndex(0), p1.GetAtIndex(0).Moves[0], 1, 0 );
var moveTurn2 = new MoveTurnChoice(p2.GetAtIndex(0), p2.GetAtIndex(0).Moves[0], 0, 0 );
Assert.That(battle.TrySetChoice(moveTurn1));
Assert.That(battle.TrySetChoice(moveTurn2));
Assert.That(battle.Value.TrySetChoice(moveTurn1));
Assert.That(battle.Value.TrySetChoice(moveTurn2));
Assert.AreEqual(battle.CurrentTurn, 1);
Assert.AreEqual(battle.Value.CurrentTurn, 1);
battle.Dispose();
battle.Value.Dispose();
}
[Test]
@@ -97,18 +97,18 @@ namespace PkmnLibSharpTests.Battling.BattleTests
evts.Add(evt);
return Task.CompletedTask;
});
battle.RegisterEventListener(eventListener);
battle.Value.RegisterEventListener(eventListener);
battle.SwitchPokemon(0, 0, p1.GetAtIndex(0));
battle.SwitchPokemon(1, 0, p2.GetAtIndex(0));
Assert.AreEqual(0, battle.CurrentTurn);
battle.Value.SwitchPokemon(0, 0, p1.GetAtIndex(0));
battle.Value.SwitchPokemon(1, 0, p2.GetAtIndex(0));
Assert.AreEqual(0, battle.Value.CurrentTurn);
var moveTurn1 = new MoveTurnChoice(p1.GetAtIndex(0), p1.GetAtIndex(0).Moves[0], 1, 0 );
var moveTurn2 = new MoveTurnChoice(p2.GetAtIndex(0), p2.GetAtIndex(0).Moves[0], 0, 0 );
Assert.That(battle.TrySetChoice(moveTurn1));
Assert.That(battle.TrySetChoice(moveTurn2));
Assert.AreEqual(1, battle.CurrentTurn);
Assert.That(battle.Value.TrySetChoice(moveTurn1));
Assert.That(battle.Value.TrySetChoice(moveTurn2));
Assert.AreEqual(1, battle.Value.CurrentTurn);
eventListener.EnsureFinishedListening();
@@ -117,7 +117,7 @@ namespace PkmnLibSharpTests.Battling.BattleTests
Assert.AreEqual(damageEvents[0].Pokemon, p2.GetAtIndex(0));
Assert.AreEqual(damageEvents[1].Pokemon, p1.GetAtIndex(0));
battle.Dispose();
battle.Value.Dispose();
}
[Test]
@@ -140,21 +140,21 @@ namespace PkmnLibSharpTests.Battling.BattleTests
evts.Add(evt);
return Task.CompletedTask;
});
battle.RegisterEventListener(eventListener);
battle.Value.RegisterEventListener(eventListener);
battle.SwitchPokemon(0, 0, p1.GetAtIndex(0));
battle.SwitchPokemon(1, 0, p2.GetAtIndex(0));
Assert.AreEqual(0, battle.CurrentTurn);
battle.Value.SwitchPokemon(0, 0, p1.GetAtIndex(0));
battle.Value.SwitchPokemon(1, 0, p2.GetAtIndex(0));
Assert.AreEqual(0, battle.Value.CurrentTurn);
var moveTurn1 = new MoveTurnChoice(p1.GetAtIndex(0), p1.GetAtIndex(0).Moves[0], 1, 0 );
var moveTurn2 = new MoveTurnChoice(p2.GetAtIndex(0), p2.GetAtIndex(0).Moves[0], 0, 0 );
Assert.That(battle.TrySetChoice(moveTurn1));
Assert.That(battle.TrySetChoice(moveTurn2));
Assert.That(battle.Value.TrySetChoice(moveTurn1));
Assert.That(battle.Value.TrySetChoice(moveTurn2));
Assert.AreEqual(battle.CurrentTurn, 1);
Assert.AreEqual(battle.Value.CurrentTurn, 1);
battle.Dispose();
battle.Value.Dispose();
mon1.Heal(1000, true);
mon2.Heal(1000, true);