Skip to content

Commit

Permalink
Merge pull request #2480 from greymistcube/refactor/use-libplanet-mocks
Browse files Browse the repository at this point in the history
♻️ 🧹 Use libplanet `Mocks` instead of lib9c's reimplementation
  • Loading branch information
greymistcube authored Mar 25, 2024
2 parents 757b1ca + e514468 commit 10d612f
Show file tree
Hide file tree
Showing 104 changed files with 371 additions and 882 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Lib9c.Tests.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Mocks;
using Libplanet.Types.Assets;
using Nekoyume;
using Nekoyume.Action;
Expand All @@ -30,7 +31,7 @@ public class CreateOrReplaceAvatarTest

public CreateOrReplaceAvatarTest()
{
_initialStates = new World(new MockWorldState());
_initialStates = new World(MockUtil.MockModernWorldState);

#pragma warning disable CS0618
var ncgCurrency = Currency.Legacy("NCG", 2, null);
Expand Down
6 changes: 3 additions & 3 deletions .Lib9c.DevExtensions.Tests/Action/FaucetCurrencyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Lib9c.Tests.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Mocks;
using Libplanet.Types.Assets;
using Nekoyume.Model.State;
using Nekoyume.Module;
Expand Down Expand Up @@ -30,9 +31,8 @@ public FaucetCurrencyTest(ITestOutputHelper outputHelper)
_crystal = Currency.Legacy("CRYSTAL", 18, null);
#pragma warning restore CS0618

_initialState = new World(
new MockWorldState()
.AddBalance(GoldCurrencyState.Address, _ncg * int.MaxValue));
_initialState = new World(MockWorldState.CreateModern()
.SetBalance(GoldCurrencyState.Address, _ncg * int.MaxValue));

var goldCurrencyState = new GoldCurrencyState(_ncg);
_agentAddress = new PrivateKey().Address;
Expand Down
3 changes: 2 additions & 1 deletion .Lib9c.DevExtensions.Tests/Action/FaucetRuneTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Xunit;
using Xunit.Abstractions;
using static Lib9c.SerializeKeys;
using Libplanet.Mocks;

namespace Lib9c.DevExtensions.Tests.Action
{
Expand All @@ -33,7 +34,7 @@ public FaucetRuneTest(ITestOutputHelper outputHelper)
.WriteTo.TestOutput(outputHelper)
.CreateLogger();

_initialState = new World(new MockWorldState());
_initialState = new World(MockUtil.MockModernWorldState);
var sheets = TableSheetsImporter.ImportSheets();
foreach (var (key, value) in sheets)
{
Expand Down
67 changes: 0 additions & 67 deletions .Lib9c.Tests/AccountExtensions.cs

This file was deleted.

7 changes: 4 additions & 3 deletions .Lib9c.Tests/Action/AccountStateDeltaExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Lib9c.Tests.Action
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Mocks;
using Nekoyume.Action;
using Nekoyume.Helper;
using Nekoyume.Model.Coupons;
Expand Down Expand Up @@ -46,7 +47,7 @@ public AccountStateDeltaExtensionsTest()
public void SetWorldBossKillReward(int level, int expectedRune, int expectedCrystal, Type exc)
{
var context = new ActionContext();
IWorld states = new World(new MockWorldState());
IWorld states = new World(MockUtil.MockModernWorldState);
var rewardInfoAddress = new PrivateKey().Address;
var rewardRecord = new WorldBossKillRewardRecord();
for (int i = 0; i < level; i++)
Expand Down Expand Up @@ -106,7 +107,7 @@ public void SetWorldBossKillReward(int level, int expectedRune, int expectedCrys
[Fact]
public void SetCouponWallet()
{
IWorld states = new World(new MockWorldState());
IWorld states = new World(MockUtil.MockModernWorldState);
var guid1 = new Guid("6856AE42-A820-4041-92B0-5D7BAA52F2AA");
var guid2 = new Guid("701BA698-CCB9-4FC7-B88F-7CB8C707D135");
var guid3 = new Guid("910296E7-34E4-45D7-9B4E-778ED61F278B");
Expand Down Expand Up @@ -162,7 +163,7 @@ public void Mead(int agentBalance)
var mead = Currencies.Mead;
var price = RequestPledge.DefaultRefillMead * mead;
ActionContext context = new ActionContext();
IWorld states = new World(new MockWorldState())
IWorld states = new World(MockUtil.MockModernWorldState)
.SetLegacyState(
agentContractAddress,
List.Empty.Add(patron.Serialize()).Add(true.Serialize()))
Expand Down
29 changes: 15 additions & 14 deletions .Lib9c.Tests/Action/AccountStateViewExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Lib9c.Tests.Action
using Lib9c.Tests.Extensions;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Mocks;
using Libplanet.Types.Assets;
using Nekoyume;
using Nekoyume.Action;
Expand Down Expand Up @@ -49,23 +50,23 @@ public AccountStateViewExtensionsTest()
[Fact]
public void TryGetAvatarStateEmptyAddress()
{
var states = new World(new MockWorldState());
var states = new World(MockUtil.MockModernWorldState);

Assert.False(states.TryGetAvatarState(default, default, out _));
}

[Fact]
public void TryGetAvatarStateAddressKeyNotFoundException()
{
var states = new World(new MockWorldState()).SetLegacyState(default, Dictionary.Empty);
var states = new World(MockUtil.MockModernWorldState).SetLegacyState(default, Dictionary.Empty);

Assert.False(states.TryGetAvatarState(default, default, out _));
}

[Fact]
public void TryGetAvatarStateKeyNotFoundException()
{
var states = new World(new MockWorldState())
var states = new World(MockUtil.MockModernWorldState)
.SetLegacyState(
default,
Dictionary.Empty
Expand All @@ -78,23 +79,23 @@ public void TryGetAvatarStateKeyNotFoundException()
[Fact]
public void TryGetAvatarStateInvalidCastException()
{
var states = new World(new MockWorldState()).SetLegacyState(default, default(Text));
var states = new World(MockUtil.MockModernWorldState).SetLegacyState(default, default(Text));

Assert.False(states.TryGetAvatarState(default, default, out _));
}

[Fact]
public void TryGetAvatarStateInvalidAddress()
{
var states = new World(new MockWorldState()).SetAvatarState(default, _avatarState);
var states = new World(MockUtil.MockModernWorldState).SetAvatarState(default, _avatarState);

Assert.False(states.TryGetAvatarState(Addresses.GameConfig, _avatarAddress, out _));
}

[Fact]
public void GetAvatarStateV2()
{
IWorld states = new World(new MockWorldState());
IWorld states = new World(MockUtil.MockModernWorldState);
states = states
.SetAvatarState(_avatarAddress, _avatarState);

Expand All @@ -118,12 +119,12 @@ public void GetAvatarStateV2_Throw_FailedLoadStateException(string account)
_ => throw new ArgumentException(),
};

IWorld states = new World(new MockWorldState());
IWorld states = new World(MockUtil.MockModernWorldState);
states = states
.SetAvatarState(_avatarAddress, _avatarState);
states = states.SetAccount(
accountAddress,
states.GetAccount(accountAddress).SetNull(_avatarAddress));
states.GetAccount(accountAddress).RemoveState(_avatarAddress));
var exc = Assert.Throws<FailedLoadStateException>(() => states.GetAvatarState(_avatarAddress));
Assert.Contains(account, exc.Message);
}
Expand All @@ -133,7 +134,7 @@ public void GetAvatarStateV2_Throw_FailedLoadStateException(string account)
[InlineData(false)]
public void TryGetAvatarState(bool backward)
{
IWorld states = new World(new MockWorldState());
IWorld states = new World(MockUtil.MockModernWorldState);
if (backward)
{
states = states
Expand All @@ -151,7 +152,7 @@ public void TryGetAvatarState(bool backward)
[Fact]
public void GetStatesAsDict()
{
IWorld states = new World(new MockWorldState());
IWorld states = new World(MockUtil.MockModernWorldState);
var dict = new Dictionary<Address, IValue>
{
{ new PrivateKey().Address, Null.Value },
Expand All @@ -178,7 +179,7 @@ public void GetStatesAsDict()
[Fact]
public void GetSheets()
{
IWorld states = new World(new MockWorldState());
IWorld states = new World(MockUtil.MockModernWorldState);
SheetsExtensionsTest.InitSheets(
states,
out _,
Expand All @@ -205,7 +206,7 @@ public void GetSheets()
[InlineData(true)]
public void GetCrystalCostState(bool exist)
{
IWorld states = new World(new MockWorldState());
IWorld states = new World(MockUtil.MockModernWorldState);
int expectedCount = exist ? 1 : 0;
FungibleAssetValue expectedCrystal = exist
? 100 * CrystalCalculator.CRYSTAL
Expand Down Expand Up @@ -238,7 +239,7 @@ public void GetCrystalCostStates(long blockIndex, bool previousWeeklyExist)
Address previousCostAddress = Addresses.GetWeeklyCrystalCostAddress(weeklyIndex - 1);
Address beforePreviousCostAddress = Addresses.GetWeeklyCrystalCostAddress(weeklyIndex - 2);
var crystalCostState = new CrystalCostState(default, 100 * CrystalCalculator.CRYSTAL);
IWorld state = new World(new MockWorldState())
IWorld state = new World(MockUtil.MockModernWorldState)
.SetLegacyState(dailyCostAddress, crystalCostState.Serialize())
.SetLegacyState(weeklyCostAddress, crystalCostState.Serialize())
.SetLegacyState(previousCostAddress, crystalCostState.Serialize())
Expand Down Expand Up @@ -268,7 +269,7 @@ public void GetCrystalCostStates(long blockIndex, bool previousWeeklyExist)
[Fact]
public void GetCouponWallet()
{
IWorld states = new World(new MockWorldState());
IWorld states = new World(MockUtil.MockModernWorldState);
var guid1 = new Guid("6856AE42-A820-4041-92B0-5D7BAA52F2AA");
var guid2 = new Guid("701BA698-CCB9-4FC7-B88F-7CB8C707D135");
var guid3 = new Guid("910296E7-34E4-45D7-9B4E-778ED61F278B");
Expand Down
3 changes: 2 additions & 1 deletion .Lib9c.Tests/Action/ActionContextExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Lib9c.Tests.Action
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Mocks;
using Libplanet.Types.Assets;
using Nekoyume;
using Nekoyume.Action;
Expand Down Expand Up @@ -143,7 +144,7 @@ public static IEnumerable<object[]> IsPreviewNetTestcases()
[MemberData(nameof(IsMainNetTestcases))]
public void IsMainNet(GoldCurrencyState goldCurrencyState, bool expected)
{
var state = new World(new MockWorldState()).SetLegacyState(Addresses.GoldCurrency, goldCurrencyState.Serialize());
var state = new World(MockUtil.MockModernWorldState).SetLegacyState(Addresses.GoldCurrency, goldCurrencyState.Serialize());
IActionContext context = new ActionContext
{
PreviousState = state,
Expand Down
3 changes: 2 additions & 1 deletion .Lib9c.Tests/Action/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Lib9c.Tests.Action
using Lib9c.Formatters;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Mocks;
using Libplanet.Types.Assets;
using MessagePack;
using MessagePack.Resolvers;
Expand Down Expand Up @@ -34,7 +35,7 @@ public ActionEvaluationTest()
#pragma warning restore CS0618
_signer = new PrivateKey().Address;
_sender = new PrivateKey().Address;
_states = new World(new MockWorldState())
_states = new World(MockUtil.MockModernWorldState)
.SetLegacyState(_signer, (Text)"ANYTHING")
.SetLegacyState(default, Dictionary.Empty.Add("key", "value"))
.MintAsset(context, _signer, _currency * 10000);
Expand Down
Loading

0 comments on commit 10d612f

Please sign in to comment.