Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions Libplanet.SDK.Action.Tests/BarAction.cs

This file was deleted.

31 changes: 0 additions & 31 deletions Libplanet.SDK.Action.Tests/FooAction.cs

This file was deleted.

92 changes: 38 additions & 54 deletions Libplanet.SDK.Action.Tests/MockActionContext.cs
Original file line number Diff line number Diff line change
@@ -1,76 +1,60 @@
using System.Collections.Generic;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Evidence;
using Libplanet.Types.Tx;

namespace Libplanet.SDK.Tests;

public class MockActionContext : IActionContext
namespace Libplanet.SDK.Action.Tests
{
public Address Signer
public class MockActionContext : IActionContext
{
get;
}
public MockActionContext(
Address signer,
Address miner,
IWorld previousState)
{
Signer = signer;
Miner = miner;
PreviousState = previousState;
}

public TxId? TxId
{
get;
}
public Address Signer { get; }

public Address Miner
{
get;
}
public TxId? TxId =>
throw new NotSupportedException();

public long BlockIndex
{
get;
}
public Address Miner { get; }

public int BlockProtocolVersion
{
get;
}
public long BlockIndex =>
throw new NotSupportedException();

public IWorld PreviousState
{
get;
set;
}
public int BlockProtocolVersion =>
throw new NotSupportedException();

public int RandomSeed
{
get;
}
public IWorld PreviousState { get; }

public bool IsPolicyAction
{
get;
}
public int RandomSeed =>
throw new NotSupportedException();

public IReadOnlyList<ITransaction> Txs
{
get;
}
public bool IsPolicyAction =>
throw new NotSupportedException();

public IReadOnlyList<EvidenceBase> Evidence
{
get;
}
public IReadOnlyList<ITransaction> Txs =>
throw new NotSupportedException();

public void UseGas(long gas)
{
throw new NotSupportedException();
}
public IReadOnlyList<EvidenceBase> Evidence =>
throw new NotSupportedException();

public void UseGas(long gas) =>
throw new NotSupportedException();

public IRandom GetRandom() =>
throw new NotSupportedException();
public IRandom GetRandom() =>
throw new NotSupportedException();

public long GasUsed() =>
throw new NotSupportedException();
public long GasUsed() =>
throw new NotSupportedException();

public long GasLimit() =>
throw new NotSupportedException();
public long GasLimit() =>
throw new NotSupportedException();
}
}
47 changes: 47 additions & 0 deletions Libplanet.SDK.Action.Tests/Sample/Actions/NumberAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Crypto;
using Libplanet.SDK.Action.Attributes;

namespace Libplanet.SDK.Action.Tests.Sample.Actions
{
[ActionType("Number")]
public class NumberAction : ActionBase
{
public override Address StorageAddress =>
new Address("0x1000000000000000000000000000000000000001");

[Executable]
public void Add(IValue args)
{
Integer operand = (Integer)args;
Integer stored = GetState(Signer) is IValue value
? (Integer)value
: new Integer(0);
Call<NumberLogAction>(nameof(NumberLogAction.Add), new object?[] { operand });
SetState(Signer, new Integer(stored.Value + operand.Value));
}

[Executable]
public void Subtract(IValue args)
{
Integer operand = (Integer)args;
Integer stored = GetState(Signer) is IValue value
? (Integer)value
: new Integer(0);
Call<NumberLogAction>(nameof(NumberLogAction.Subtract), new object?[] { operand });
SetState(Signer, new Integer(stored.Value - operand.Value));
}

[Executable]
public void Multiply(IValue args)
{
Integer operand = (Integer)args;
Integer stored = GetState(Signer) is IValue value
? (Integer)value
: new Integer(1);
Call<NumberLogAction>(nameof(NumberLogAction.Multiply), new object?[] { operand });
SetState(Signer, new Integer(stored.Value * operand.Value));
}
}
}
57 changes: 57 additions & 0 deletions Libplanet.SDK.Action.Tests/Sample/Actions/NumberLogAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Bencodex.Types;
using Libplanet.Crypto;
using Libplanet.SDK.Action.Attributes;

namespace Libplanet.SDK.Action.Tests.Sample.Actions
{
public class NumberLogAction : ActionBase
{
public override Address StorageAddress =>
new Address("0x1000000000000000000000000000000000000002");

[Callable]
public void Add(Integer operand)
{
Text stored = GetState(Signer) is IValue value
? (Text)value
: new Text(string.Empty);
Text formatted = operand.Value >= 0
? new Text($"{operand.Value}")
: new Text($"({operand.Value})");
formatted = stored.Value.Length == 0
? new Text(stored.Value + $"{formatted.Value}")
: new Text(stored.Value + $" + {formatted.Value}");
SetState(Signer, formatted);
}

[Callable]
public void Subtract(Integer operand)
{
Text stored = GetState(Signer) is IValue value
? (Text)value
: new Text(string.Empty);
Text formatted = operand.Value >= 0
? new Text($"{operand.Value}")
: new Text($"({operand.Value})");
formatted = stored.Value.Length == 0
? new Text(stored.Value + $"{formatted.Value}")
: new Text(stored.Value + $" - {formatted.Value}");
SetState(Signer, formatted);
}

// This is without Callable attribute on purpose for testing.
public void Multiply(Integer operand)
{
Text stored = GetState(Signer) is IValue value
? (Text)value
: new Text(string.Empty);
Text formatted = operand.Value >= 0
? new Text($"{operand.Value}")
: new Text($"({operand.Value})");
formatted = stored.Value.Length == 0
? new Text($"{formatted.Value}")
: new Text(stored.Value + $" * {formatted.Value}");
SetState(Signer, formatted);
}
}
}
24 changes: 24 additions & 0 deletions Libplanet.SDK.Action.Tests/Sample/Actions/TextAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Bencodex.Types;
using Libplanet.Action;
using Libplanet.Crypto;
using Libplanet.SDK.Action.Attributes;

namespace Libplanet.SDK.Action.Tests
{
[ActionType("Text")]
public class TextAction : ActionBase
{
public override Address StorageAddress =>
new Address("0x1000000000000000000000000000000000000003");

[Executable]
public void Append(IValue args)
{
Text operand = (Text)args;
Text stored = GetState(Signer) is IValue value
? (Text)value
: new Text(string.Empty);
SetState(Signer, new Text(stored.Value + operand.Value));
}
}
}
Loading