-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'development' into test/improve-exception-test
- Loading branch information
Showing
69 changed files
with
10,929 additions
and
4,256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
namespace Lib9c.Tests.Action | ||
{ | ||
using System; | ||
using System.Linq; | ||
using Libplanet.Action.State; | ||
using Libplanet.Crypto; | ||
using Libplanet.Mocks; | ||
using Nekoyume; | ||
using Nekoyume.Action; | ||
using Nekoyume.Model.Item; | ||
using Nekoyume.Model.State; | ||
using Nekoyume.Module; | ||
using Xunit; | ||
|
||
public class ClaimGiftsTest | ||
{ | ||
private readonly TableSheets _tableSheets; | ||
private readonly IWorld _state; | ||
|
||
public ClaimGiftsTest() | ||
{ | ||
_state = new World(MockUtil.MockModernWorldState); | ||
|
||
var tableCsv = TableSheetsImporter.ImportSheets(); | ||
foreach (var (key, value) in tableCsv) | ||
{ | ||
_state = _state.SetLegacyState(Addresses.GetSheetAddress(key), value.Serialize()); | ||
} | ||
|
||
_tableSheets = new TableSheets(tableCsv); | ||
} | ||
|
||
[Theory] | ||
[InlineData(1)] | ||
[InlineData(300)] | ||
[InlineData(600)] | ||
[InlineData(1200)] | ||
public void Execute_Success(long blockIndex) | ||
{ | ||
var agentAddress = new PrivateKey().Address; | ||
var avatarAddress = Addresses.GetAvatarAddress(agentAddress, 0); | ||
|
||
var avatarState = AvatarState.Create( | ||
avatarAddress, | ||
agentAddress, | ||
0, | ||
_tableSheets.GetAvatarSheets(), | ||
default); | ||
var state = _state.SetAvatarState(avatarAddress, avatarState); | ||
|
||
if (!_tableSheets.ClaimableGiftsSheet.TryFindRowByBlockIndex(blockIndex, out var row)) | ||
{ | ||
throw new Exception(); | ||
} | ||
|
||
Execute( | ||
state, | ||
avatarAddress, | ||
agentAddress, | ||
row.Id, | ||
blockIndex, | ||
row.Items.ToArray() | ||
); | ||
} | ||
|
||
[Fact] | ||
public void Execute_ClaimableGiftsNotAvailableException() | ||
{ | ||
var agentAddress = new PrivateKey().Address; | ||
var avatarAddress = Addresses.GetAvatarAddress(agentAddress, 0); | ||
|
||
var avatarState = AvatarState.Create( | ||
avatarAddress, | ||
agentAddress, | ||
0, | ||
_tableSheets.GetAvatarSheets(), | ||
default); | ||
var state = _state.SetAvatarState(avatarAddress, avatarState); | ||
var sheet = _tableSheets.ClaimableGiftsSheet; | ||
|
||
Assert.Throws<ClaimableGiftsNotAvailableException>(() => | ||
{ | ||
var row = sheet.Values.OrderBy(row => row.StartedBlockIndex).First(); | ||
Execute( | ||
state, | ||
avatarAddress, | ||
agentAddress, | ||
row.Id, | ||
row.StartedBlockIndex - 1, | ||
row.Items.ToArray() | ||
); | ||
}); | ||
Assert.Throws<ClaimableGiftsNotAvailableException>(() => | ||
{ | ||
var row = sheet.Values.OrderByDescending(row => row.EndedBlockIndex).First(); | ||
Execute( | ||
state, | ||
avatarAddress, | ||
agentAddress, | ||
row.Id, | ||
row.EndedBlockIndex + 1, | ||
row.Items.ToArray() | ||
); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Execute_AlreadyClaimedGiftsException() | ||
{ | ||
var agentAddress = new PrivateKey().Address; | ||
var avatarAddress = Addresses.GetAvatarAddress(agentAddress, 0); | ||
|
||
var avatarState = AvatarState.Create( | ||
avatarAddress, | ||
agentAddress, | ||
0, | ||
_tableSheets.GetAvatarSheets(), | ||
default); | ||
var state = _state.SetAvatarState(avatarAddress, avatarState); | ||
|
||
var row = _tableSheets.ClaimableGiftsSheet.Values.First(); | ||
var blockIndex = row.StartedBlockIndex; | ||
|
||
var nextState = Execute( | ||
state, | ||
avatarAddress, | ||
agentAddress, | ||
row.Id, | ||
blockIndex, | ||
row.Items.ToArray() | ||
); | ||
Assert.Throws<AlreadyClaimedGiftsException>(() => | ||
{ | ||
Execute( | ||
nextState, | ||
avatarAddress, | ||
agentAddress, | ||
row.Id, | ||
blockIndex + 1, | ||
row.Items.ToArray() | ||
); | ||
}); | ||
} | ||
|
||
private IWorld Execute( | ||
IWorld previousState, | ||
Address avatarAddress, | ||
Address agentAddress, | ||
int giftId, | ||
long blockIndex, | ||
(int itemId, int quantity, bool tradable)[] expected) | ||
{ | ||
var prevClaimedGifts = _state.GetClaimedGifts(avatarAddress); | ||
|
||
var action = new ClaimGifts(avatarAddress, giftId); | ||
var actionContext = new ActionContext | ||
{ | ||
PreviousState = previousState, | ||
Signer = agentAddress, | ||
BlockIndex = blockIndex, | ||
}; | ||
|
||
var nextState = action.Execute(actionContext); | ||
|
||
// Check claimed gifts. | ||
var nextClaimedGifts = nextState.GetClaimedGifts(avatarAddress); | ||
Assert.Equal(prevClaimedGifts.Count + 1, nextClaimedGifts.Count); | ||
|
||
// Check Inventory. | ||
var inventory = nextState.GetInventoryV2(avatarAddress); | ||
foreach (var (itemId, quantity, tradable) in expected) | ||
{ | ||
Assert.True(inventory.TryGetItem(itemId, out var inventoryItem)); | ||
Assert.Equal(quantity, inventoryItem.count); | ||
Assert.Equal(tradable, inventoryItem.item is ITradableItem); | ||
} | ||
|
||
return nextState; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace Lib9c.Tests.TableData.Event | ||
{ | ||
using Nekoyume.TableData; | ||
using Xunit; | ||
|
||
public class ClaimableGiftsSheetTest | ||
{ | ||
[Fact] | ||
public void Set() | ||
{ | ||
const string csv = @"id,started_block_index,ended_block_index,item_1_id,item_1_quantity,item_1_tradable,item_2_id,item_2_quantity,item_2_tradable,item_3_id,item_3_quantity,item_3_tradable,item_4_id,item_4_quantity,item_4_tradable,item_5_id,item_5_quantity,item_5_tradable | ||
1,1,250,600402,5,true,,,,,,,,,,,, | ||
2,251,500,40100030,1,true,,,,,,,,,,,, | ||
3,501,1000,49900022,1,true,,,,,,,,,,,, | ||
4,1001,1500,40100028,1,true,,,,,,,,,,,, | ||
5,1501,2000,400000,5,false,,,,,,,,,,,,"; | ||
|
||
var sheet = new ClaimableGiftsSheet(); | ||
sheet.Set(csv); | ||
Assert.Equal(5, sheet.Count); | ||
Assert.NotNull(sheet.First); | ||
Assert.NotNull(sheet.Last); | ||
var row = sheet.First; | ||
Assert.Equal(1, row.Id); | ||
Assert.Equal(1, row.StartedBlockIndex); | ||
Assert.Equal(250, row.EndedBlockIndex); | ||
Assert.Single(row.Items); | ||
Assert.Equal(600402, row.Items[0].itemId); | ||
Assert.Equal(5, row.Items[0].quantity); | ||
Assert.True(row.Items[0].tradable); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule .Libplanet
deleted from
46cff6
Oops, something went wrong.