Skip to content

Commit

Permalink
Merge pull request #2914 from eugene-doobu/feature/apply-rule-test
Browse files Browse the repository at this point in the history
Feature/apply rule test
  • Loading branch information
eugene-doobu authored Oct 22, 2024
2 parents 55b6e9e + 097a13b commit 8bea553
Show file tree
Hide file tree
Showing 248 changed files with 9,052 additions and 7,747 deletions.
12 changes: 7 additions & 5 deletions .Lib9c.Tests/Action/AccountStateDeltaExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void SetWorldBossKillReward(int level, int expectedRune, int expectedCrys
IWorld states = new World(MockUtil.MockModernWorldState);
var rewardInfoAddress = new PrivateKey().Address;
var rewardRecord = new WorldBossKillRewardRecord();
for (int i = 0; i < level; i++)
for (var i = 0; i < level; i++)
{
rewardRecord[i] = false;
}
Expand All @@ -67,11 +67,13 @@ public void SetWorldBossKillReward(int level, int expectedRune, int expectedCrys
);
var bossId = bossState.Id;
var runeWeightSheet = new RuneWeightSheet();
runeWeightSheet.Set($@"id,boss_id,rank,rune_id,weight
runeWeightSheet.Set(
$@"id,boss_id,rank,rune_id,weight
1,{bossId},0,10001,100
");
var killRewardSheet = new WorldBossKillRewardSheet();
killRewardSheet.Set($@"id,boss_id,rank,rune_min,rune_max,crystal,circle
killRewardSheet.Set(
$@"id,boss_id,rank,rune_min,rune_max,crystal,circle
1,{bossId},0,1,1,100,0
");

Expand Down Expand Up @@ -176,8 +178,8 @@ public void Mead(int agentBalance)
var agentContractAddress = _agentAddress.GetPledgeAddress();
var mead = Currencies.Mead;
var price = RequestPledge.DefaultRefillMead * mead;
ActionContext context = new ActionContext();
IWorld states = new World(MockUtil.MockModernWorldState)
var context = new ActionContext();
var states = new World(MockUtil.MockModernWorldState)
.SetLegacyState(
agentContractAddress,
List.Empty.Add(patron.Serialize()).Add(true.Serialize()))
Expand Down
32 changes: 16 additions & 16 deletions .Lib9c.Tests/Action/AccountStateViewExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ public void TryGetAvatarStateKeyNotFoundException()
{
var states = new World(MockUtil.MockModernWorldState)
.SetLegacyState(
default,
Dictionary.Empty
.Add("agentAddress", default(Address).Serialize())
);
default,
Dictionary.Empty
.Add("agentAddress", default(Address).Serialize())
);

Assert.False(states.TryGetAvatarState(default, default, out _));
}
Expand Down Expand Up @@ -110,7 +110,7 @@ public void GetAvatarStateV2()
[InlineData("questList")]
public void GetAvatarStateV2_Throw_FailedLoadStateException(string account)
{
Address accountAddress = account switch
var accountAddress = account switch
{
"inventory" => Addresses.Inventory,
"worldInformation" => Addresses.WorldInformation,
Expand All @@ -131,7 +131,7 @@ public void GetAvatarStateV2_Throw_FailedLoadStateException(string account)
[Fact]
public void TryGetAvatarState()
{
IWorld states = new World(MockUtil.MockModernWorldState).SetAvatarState(_avatarAddress, _avatarState);
var states = new World(MockUtil.MockModernWorldState).SetAvatarState(_avatarAddress, _avatarState);
Assert.True(states.TryGetAvatarState(_agentAddress, _avatarAddress, out _));
}

Expand Down Expand Up @@ -193,8 +193,8 @@ public void GetSheets()
public void GetCrystalCostState(bool exist)
{
IWorld states = new World(MockUtil.MockModernWorldState);
int expectedCount = exist ? 1 : 0;
FungibleAssetValue expectedCrystal = exist
var expectedCount = exist ? 1 : 0;
var expectedCrystal = exist
? 100 * CrystalCalculator.CRYSTAL
: 0 * CrystalCalculator.CRYSTAL;
Address address = default;
Expand All @@ -205,7 +205,7 @@ public void GetCrystalCostState(bool exist)
states = states.SetLegacyState(address, crystalCostState.Serialize());
}

CrystalCostState actual = states.GetCrystalCostState(address);
var actual = states.GetCrystalCostState(address);
Assert.Equal(expectedCount, actual.Count);
Assert.Equal(expectedCrystal, actual.CRYSTAL);
}
Expand All @@ -217,15 +217,15 @@ public void GetCrystalCostState(bool exist)
[InlineData(151_200L, true)]
public void GetCrystalCostStates(long blockIndex, bool previousWeeklyExist)
{
long interval = _tableSheets.CrystalFluctuationSheet.Values.First(r => r.Type == CrystalFluctuationSheet.ServiceType.Combination).BlockInterval;
var interval = _tableSheets.CrystalFluctuationSheet.Values.First(r => r.Type == CrystalFluctuationSheet.ServiceType.Combination).BlockInterval;
var weeklyIndex = (int)(blockIndex / interval);
Address dailyCostAddress =
var dailyCostAddress =
Addresses.GetDailyCrystalCostAddress((int)(blockIndex / CrystalCostState.DailyIntervalIndex));
Address weeklyCostAddress = Addresses.GetWeeklyCrystalCostAddress(weeklyIndex);
Address previousCostAddress = Addresses.GetWeeklyCrystalCostAddress(weeklyIndex - 1);
Address beforePreviousCostAddress = Addresses.GetWeeklyCrystalCostAddress(weeklyIndex - 2);
var weeklyCostAddress = Addresses.GetWeeklyCrystalCostAddress(weeklyIndex);
var previousCostAddress = Addresses.GetWeeklyCrystalCostAddress(weeklyIndex - 1);
var beforePreviousCostAddress = Addresses.GetWeeklyCrystalCostAddress(weeklyIndex - 2);
var crystalCostState = new CrystalCostState(default, 100 * CrystalCalculator.CRYSTAL);
IWorld state = new World(MockUtil.MockModernWorldState)
var state = new World(MockUtil.MockModernWorldState)
.SetLegacyState(dailyCostAddress, crystalCostState.Serialize())
.SetLegacyState(weeklyCostAddress, crystalCostState.Serialize())
.SetLegacyState(previousCostAddress, crystalCostState.Serialize())
Expand Down Expand Up @@ -311,7 +311,7 @@ public void GetCouponWallet()

states = states.SetLegacyState(
agentAddress1.Derive(CouponWalletKey),
(Bencodex.Types.Binary)new byte[] { 0x00 });
(Bencodex.Types.Binary)new byte[] { 0x00, });

Assert.Throws<InvalidCastException>(() => states.GetCouponWallet(agentAddress1));
Assert.Throws<InvalidCastException>(() => states.GetCouponWallet(agentAddress2));
Expand Down
15 changes: 12 additions & 3 deletions .Lib9c.Tests/Action/ActionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,20 @@ public void UseGas(long gas)
_gasUsed += gas;
}

public IRandom GetRandom() => _random ?? new TestRandom(RandomSeed);
public IRandom GetRandom()
{
return _random ?? new TestRandom(RandomSeed);
}

public long GasUsed() => _gasUsed;
public long GasUsed()
{
return _gasUsed;
}

public long GasLimit() => 0;
public long GasLimit()
{
return 0;
}

// FIXME: Temporary measure to allow inheriting already mutated IRandom.
public void SetRandom(IRandom random)
Expand Down
33 changes: 18 additions & 15 deletions .Lib9c.Tests/Action/ActionContextExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static IEnumerable<object[]> IsMainNetTestcases()
Currency.Legacy("BTC", 2, new Address("47d082a115c63e7b58b1532d20e631538eafadde"))),
false,
};
#pragma warning restore CS0618
#pragma warning restore CS0618
}

public static IEnumerable<object[]> IsPreviewNetTestcases()
Expand Down Expand Up @@ -156,20 +156,23 @@ public void IsMainNet(GoldCurrencyState goldCurrencyState, bool expected)
[Fact]
public void Since()
{
Assert.True(new ActionContext
{
BlockIndex = 1001,
}.Since(1000));

Assert.True(new ActionContext
{
BlockIndex = 0,
}.Since(0));

Assert.False(new ActionContext
{
BlockIndex = 0,
}.Since(1));
Assert.True(
new ActionContext
{
BlockIndex = 1001,
}.Since(1000));

Assert.True(
new ActionContext
{
BlockIndex = 0,
}.Since(0));

Assert.False(
new ActionContext
{
BlockIndex = 0,
}.Since(1));
}
}
}
18 changes: 10 additions & 8 deletions .Lib9c.Tests/Action/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private ActionBase GetAction(Type type)
new PrivateKey().Address,
ItemSubType.Armor,
#pragma warning disable CS0618
// Use of obsolete method Currency.Legacy(): https://github.com/planetarium/lib9c/discussions/1319
// Use of obsolete method Currency.Legacy(): https://github.com/planetarium/lib9c/discussions/1319
Currency.Legacy("NCG", 2, null) * 10
#pragma warning restore CS0618
),
Expand All @@ -181,7 +181,7 @@ private ActionBase GetAction(Type type)
CombinationConsumable _ => new CombinationConsumable(),
CombinationEquipment _ => new CombinationEquipment(),
CreatePendingActivation _ => new CreatePendingActivation(
new PendingActivationState(new byte[] { 0x0 }, new PrivateKey().PublicKey)
new PendingActivationState(new byte[] { 0x0, }, new PrivateKey().PublicKey)
),
DailyReward _ => new DailyReward(),
InitializeStates _ => new InitializeStates
Expand Down Expand Up @@ -437,13 +437,15 @@ private ActionBase GetAction(Type type)
CreatePledge _ => new CreatePledge
{
PatronAddress = new PrivateKey().Address,
AgentAddresses = new[] { (new PrivateKey().Address, new PrivateKey().Address) },
AgentAddresses = new[] { (new PrivateKey().Address, new PrivateKey().Address), },
Mead = 4,
},
TransferAssets _ => new TransferAssets(_sender, new List<(Address, FungibleAssetValue)>
{
(_signer, 1 * _currency),
}),
TransferAssets _ => new TransferAssets(
_sender,
new List<(Address, FungibleAssetValue)>
{
(_signer, 1 * _currency),
}),
RuneSummon _ => new RuneSummon
{
AvatarAddress = _sender,
Expand Down Expand Up @@ -476,7 +478,7 @@ private ActionBase GetAction(Type type)
),
},
},
RetrieveAvatarAssets _ => new RetrieveAvatarAssets(avatarAddress: new PrivateKey().Address),
RetrieveAvatarAssets _ => new RetrieveAvatarAssets(new PrivateKey().Address),
MigrateFee _ => new MigrateFee
{
TransferData = new List<(Address sender, Address recipient, BigInteger amount)>
Expand Down
54 changes: 30 additions & 24 deletions .Lib9c.Tests/Action/ActivateCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public ActivateCollectionTest()
{
var sheets = TableSheetsImporter.ImportSheets();
// Fix csv data for test
sheets[nameof(CollectionSheet)] = @"id,item_id,count,level,skill,item_id,count,level,skill,item_id,count,level,skill,item_id,count,level,skill,item_id,count,level,skill,item_id,count,level,skill,stat_type,modify_type,modify_value,stat_type,modify_type,modify_value,stat_type,modify_type,modify_value
sheets[nameof(CollectionSheet)] =
@"id,item_id,count,level,skill,item_id,count,level,skill,item_id,count,level,skill,item_id,count,level,skill,item_id,count,level,skill,item_id,count,level,skill,stat_type,modify_type,modify_value,stat_type,modify_type,modify_value,stat_type,modify_type,modify_value
1,10110000,1,0,,302000,2,,,200000,2,,,40100000,1,,,,,,,,,,,ATK,Add,1,,,,,,
2,10110000,1,0,,,,,,,,,,,,,,,,,,,,,,ATK,Percentage,1,,,,,,";

Expand Down Expand Up @@ -76,38 +77,41 @@ public void Execute()
{
var item = ItemFactory.CreateItem(itemRow, random);
avatarState.inventory.AddItem(item, material.Count);
materials.Add(new FungibleCollectionMaterial
{
ItemId = item.Id,
ItemCount = material.Count,
});
materials.Add(
new FungibleCollectionMaterial
{
ItemId = item.Id,
ItemCount = material.Count,
});
}
else
{
for (int i = 0; i < material.Count; i++)
for (var i = 0; i < material.Count; i++)
{
var item = ItemFactory.CreateItem(itemRow, random);
var nonFungibleId = ((INonFungibleItem)item).NonFungibleId;
avatarState.inventory.AddItem(item);
if (item.ItemType != ItemType.Consumable)
{
materials.Add(new NonFungibleCollectionMaterial
{
ItemId = item.Id,
NonFungibleId = nonFungibleId,
SkillContains = material.SkillContains,
});
materials.Add(
new NonFungibleCollectionMaterial
{
ItemId = item.Id,
NonFungibleId = nonFungibleId,
SkillContains = material.SkillContains,
});
}
else
{
// Add consumable material only one.
if (i == 0)
{
materials.Add(new FungibleCollectionMaterial
{
ItemId = item.Id,
ItemCount = material.Count,
});
materials.Add(
new FungibleCollectionMaterial
{
ItemId = item.Id,
ItemCount = material.Count,
});
}
}
}
Expand All @@ -120,7 +124,7 @@ public void Execute()
PreviousState = state,
Signer = _agentAddress,
};
ActivateCollection activateCollection = new ActivateCollection()
var activateCollection = new ActivateCollection()
{
AvatarAddress = _avatarAddress,
CollectionData =
Expand All @@ -136,11 +140,13 @@ public void Execute()
var nextAvatarState = nextState.GetAvatarState(_avatarAddress);
Assert.Empty(nextAvatarState.inventory.Items);

Assert.Throws<AlreadyActivatedException>(() => activateCollection.Execute(new ActionContext
{
PreviousState = nextState,
Signer = _agentAddress,
}));
Assert.Throws<AlreadyActivatedException>(
() => activateCollection.Execute(
new ActionContext
{
PreviousState = nextState,
Signer = _agentAddress,
}));
}
}
}
Loading

0 comments on commit 8bea553

Please sign in to comment.