Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow logic blocks to be tested #11

Merged
merged 1 commit into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 7 additions & 7 deletions Chickensoft.LogicBlocks.Example/VendingMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public record TransactionTimedOut : Input;
public record VendingCompleted : Input;
}

public abstract record State(Context Context) : StateLogic(Context) {
public abstract record State(IContext Context) : StateLogic(Context) {
public record Idle : State,
IGet<Input.SelectionEntered>, IGet<Input.PaymentReceived> {
public Idle(Context context) : base(context) {
public Idle(IContext context) : base(context) {
OnEnter<Idle>((previous) => context.Output(
new Output.ClearTransactionTimeOutTimer()
));
Expand Down Expand Up @@ -49,7 +49,7 @@ public abstract record TransactionActive : State,
public int AmountReceived { get; }

public TransactionActive(
Context context, ItemType type, int price, int amountReceived
IContext context, ItemType type, int price, int amountReceived
) : base(context) {
Type = type;
Price = price;
Expand Down Expand Up @@ -95,7 +95,7 @@ public State On(Input.TransactionTimedOut input) {
public record Started : TransactionActive,
IGet<Input.SelectionEntered> {
public Started(
Context context, ItemType type, int price, int amountReceived
IContext context, ItemType type, int price, int amountReceived
) : base(context, type, price, amountReceived) {
OnEnter<Started>(
(previous) => context.Output(new Output.TransactionStarted())
Expand All @@ -115,15 +115,15 @@ public State On(Input.SelectionEntered input) {
}

public record PaymentPending(
Context Context, ItemType Type, int Price, int AmountReceived
IContext Context, ItemType Type, int Price, int AmountReceived
) : TransactionActive(Context, Type, Price, AmountReceived);
}

public record Vending : State, IGet<Input.VendingCompleted> {
public ItemType Type { get; }
public int Price { get; }

public Vending(Context context, ItemType type, int price) :
public Vending(IContext context, ItemType type, int price) :
base(context) {
Type = type;
Price = price;
Expand Down Expand Up @@ -171,7 +171,7 @@ public VendingMachine(VendingMachineStock stock) {
Set(stock);
}

public override State GetInitialState(Context context)
public override State GetInitialState(IContext context)
=> new State.Idle(context);
}

Expand Down
10 changes: 5 additions & 5 deletions Chickensoft.LogicBlocks.Generator.Tests/test_cases/Heater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Heater(ITemperatureSensor tempSensor) {
Set(tempSensor);
}

public override State GetInitialState(Context context) =>
public override State GetInitialState(IContext context) =>
new State.Off(context, 72.0);

public abstract record Input {
Expand All @@ -31,23 +31,23 @@ public record TargetTempChanged(double Temp) : Input;
public record AirTempSensorChanged(double AirTemp) : Input;
}

public abstract record State(Context Context, double TargetTemp)
public abstract record State(IContext Context, double TargetTemp)
: StateLogic(Context) {
public record Off(
Context Context, double TargetTemp
IContext Context, double TargetTemp
) : State(Context, TargetTemp), IGet<Input.TurnOn> {
State IGet<Input.TurnOn>.On(Input.TurnOn input) =>
new Heating(Context, TargetTemp);
}

public record Idle(Context Context, double TargetTemp) :
public record Idle(IContext Context, double TargetTemp) :
State(Context, TargetTemp);

public record Heating : State,
IGet<Input.TurnOff>,
IGet<Input.AirTempSensorChanged>,
IGet<Input.TargetTempChanged> {
public Heating(Context context, double targetTemp) : base(
public Heating(IContext context, double targetTemp) : base(
context, targetTemp
) {
var tempSensor = context.Get<ITemperatureSensor>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ namespace Chickensoft.LogicBlocks.Generator.Tests;

[StateMachine]
public class LightSwitch : LogicBlock<LightSwitch.Input, LightSwitch.State, LightSwitch.Output> {
public override State GetInitialState(Context context) =>
public override State GetInitialState(IContext context) =>
new State.Off(context);

public abstract record Input {
public record Toggle : Input;
}

public abstract record State(Context Context) : StateLogic(Context) {
public record On(Context Context) : State(Context), IGet<Input.Toggle> {
public abstract record State(IContext Context) : StateLogic(Context) {
public record On(IContext Context) : State(Context), IGet<Input.Toggle> {
State IGet<Input.Toggle>.On(Input.Toggle input) => new Off(Context);
}

public record Off(Context Context) : State(Context), IGet<Input.Toggle> {
public record Off(IContext Context) : State(Context), IGet<Input.Toggle> {
State IGet<Input.Toggle>.On(Input.Toggle input) => new On(Context);
}
}
Expand Down
10 changes: 5 additions & 5 deletions Chickensoft.LogicBlocks.Generator.Tests/test_cases/Patterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ namespace Chickensoft.LogicBlocks.Generator.Tests;
public class Patterns : LogicBlock<Patterns.Input, Patterns.State, LightSwitch.Output> {
public enum Mode { One, Two, Three }

public override State GetInitialState(Context context) =>
public override State GetInitialState(IContext context) =>
new State.One(context);

public abstract record Input {
public record SetMode(Mode Mode) : Input;
}

public abstract record State(Context Context) : StateLogic(Context), IGet<Input.SetMode> {
public abstract record State(IContext Context) : StateLogic(Context), IGet<Input.SetMode> {
public State On(Input.SetMode input) => input.Mode switch {
Mode.One => new One(Context),
Mode.Two => new Two(Context),
Expand All @@ -21,9 +21,9 @@ public abstract record State(Context Context) : StateLogic(Context), IGet<Input.
},
_ => throw new NotImplementedException()
};
public record One(Context Context) : State(Context);
public record Two(Context Context) : State(Context);
public record Three(Context Context) : State(Context);
public record One(IContext Context) : State(Context);
public record Two(IContext Context) : State(Context);
public record Three(IContext Context) : State(Context);
}

public abstract record Output { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ namespace Chickensoft.LogicBlocks.Generator.Tests;
[StateMachine]
public class SingleState :
LogicBlock<SingleState.Input, SingleState.State, SingleState.Output> {
public override State GetInitialState(Context context) => new(Context);
public override State GetInitialState(IContext context) => new(Context);

public abstract record Input {
public record MyInput : Input { }
}
public record State : StateLogic, IGet<Input.MyInput> {
public State(Context context) : base(context) {
public State(IContext context) : base(context) {
OnEnter<State>((previous) => Context.Output(new Output.MyOutput()));
OnExit<State>((next) => Context.Output(new Output.MyOutput()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Chickensoft.LogicBlocks.Generator.Tests;
[StateMachine]
public class ToasterOven :
LogicBlock<ToasterOven.Input, ToasterOven.State, ToasterOven.Output> {
public override State GetInitialState(Context context) =>
public override State GetInitialState(IContext context) =>
new State.Toasting(context, 0);

public record Input {
Expand All @@ -12,9 +12,9 @@ public record StartBaking(int Temperature) : Input;
public record StartToasting(int ToastColor) : Input;
}

public abstract record State(Context Context) : StateLogic(Context) {
public abstract record State(IContext Context) : StateLogic(Context) {
public record Heating : State, IGet<Input.OpenDoor> {
public Heating(Context context) : base(context) {
public Heating(IContext context) : base(context) {
OnEnter<Heating>(
(previous) => Context.Output(new Output.TurnHeaterOn())
);
Expand All @@ -29,7 +29,7 @@ public Heating(Context context) : base(context) {
public record Toasting : Heating, IGet<Input.StartBaking> {
public int ToastColor { get; }

public Toasting(Context context, int toastColor) : base(context) {
public Toasting(IContext context, int toastColor) : base(context) {
ToastColor = toastColor;

OnEnter<Toasting>(
Expand All @@ -48,7 +48,7 @@ public Toasting(Context context, int toastColor) : base(context) {
public record Baking : Heating, IGet<Input.StartToasting> {
public int Temperature { get; }

public Baking(Context context, int temperature) : base(context) {
public Baking(IContext context, int temperature) : base(context) {
Temperature = temperature;

OnEnter<Baking>(
Expand All @@ -65,7 +65,7 @@ public Baking(Context context, int temperature) : base(context) {
}

public record DoorOpen : State, IGet<Input.CloseDoor> {
public DoorOpen(Context context) : base(context) {
public DoorOpen(IContext context) : base(context) {
OnEnter<DoorOpen>(
(previous) => Context.Output(new Output.TurnLampOn())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace Chickensoft.LogicBlocks.Generator.Tests;
[StateMachine]
public partial class PartialLogic :
LogicBlock<PartialLogic.Input, PartialLogic.State, PartialLogic.Output> {
public override State GetInitialState(Context context) =>
public override State GetInitialState(IContext context) =>
throw new NotImplementedException();

public abstract record Input {
public record One : Input;
}
public abstract partial record State(Context Context) : StateLogic(Context);
public abstract partial record State(IContext Context) : StateLogic(Context);
public abstract record Output {
public record OutputA : Output;
public record OutputEnterA : Output;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public partial class PartialLogic :
LogicBlock<PartialLogic.Input, PartialLogic.State, PartialLogic.Output> {
public abstract partial record State : StateLogic {
public partial record A : State, IGet<Input.One> {
public A(Context context) : base(context) {
public A(IContext context) : base(context) {
OnEnter<A>(
(previous) => Context.Output(new Output.OutputEnterA())
);
Expand All @@ -14,6 +14,6 @@ public A(Context context) : base(context) {
}
}

public record B(Context Context) : State(Context);
public record B(IContext Context) : State(Context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<NoWarn>NU5128</NoWarn>

<Title>LogicBlocks Generator</Title>
<Version>2.2.0</Version>
<Version>2.3.0</Version>
<Description></Description>
<Copyright>© 2023 Chickensoft Games</Copyright>
<Authors>Chickensoft</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Constants {
public static int SPACES_PER_INDENT = 2;

public const string LOGIC_BLOCK_GET_INITIAL_STATE = "GetInitialState";
public const string LOGIC_BLOCK_CONTEXT_ID = "global::Chickensoft.LogicBlocks.Logic.Context";
public const string LOGIC_BLOCK_CONTEXT_ID = "global::Chickensoft.LogicBlocks.Logic.IContext";
public const string LOGIC_BLOCK_CONTEXT_OUTPUT = "Output";
public const string LOGIC_BLOCK_STATE_LOGIC_ON_ENTER = "OnEnter";
public const string LOGIC_BLOCK_STATE_LOGIC_ON_EXIT = "OnExit";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
24 changes: 12 additions & 12 deletions Chickensoft.LogicBlocks.Tests/test/fixtures/FakeLogicBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public record NoNewState() : Input;
public record SelfInput(Input Input) : Input;
public record InputCallback(
Action Callback,
Func<Context, State> Next
Func<IContext, State> Next
) : Input;
public record Custom(Func<Context, State> Next) : Input;
public record Custom(Func<IContext, State> Next) : Input;
}

public abstract record State(Context Context) : StateLogic(Context),
public abstract record State(IContext Context) : StateLogic(Context),
IGet<Input.InputOne>,
IGet<Input.InputTwo>,
IGet<Input.InputThree>,
Expand Down Expand Up @@ -67,26 +67,26 @@ public State On(Input.InputCallback input) {

public State On(Input.SelfInput input) => Context.Input(input.Input);

public record StateA(Context Context, int Value1, int Value2) :
public record StateA(IContext Context, int Value1, int Value2) :
State(Context);
public record StateB(Context Context, string Value1, string Value2) :
public record StateB(IContext Context, string Value1, string Value2) :
State(Context);
public record StateC(Context Context, string Value) :
public record StateC(IContext Context, string Value) :
State(Context);
public record StateD(Context Context, string Value1, string Value2) :
public record StateD(IContext Context, string Value1, string Value2) :
State(Context);

public record NothingState(Context Context) : State(Context);
public record NothingState(IContext Context) : State(Context);

public record Custom : State {
public Custom(Context context, Action<Context> setupCallback) :
public Custom(IContext context, Action<IContext> setupCallback) :
base(context) {
setupCallback(context);
}
}

public record OnEnterState : State {
public OnEnterState(Context context, Action<State> onEnter) :
public OnEnterState(IContext context, Action<State> onEnter) :
base(context) {
OnEnter<OnEnterState>(onEnter);
}
Expand All @@ -103,13 +103,13 @@ public partial class FakeLogicBlock
: LogicBlock<
FakeLogicBlock.Input, FakeLogicBlock.State, FakeLogicBlock.Output
> {
public Func<Context, State>? InitialState { get; init; }
public Func<IContext, State>? InitialState { get; init; }

public List<Exception> Exceptions { get; } = new();

public void PublicSet<T>(T value) where T : notnull => Set(value);

public override State GetInitialState(Context context) =>
public override State GetInitialState(IContext context) =>
InitialState?.Invoke(context) ?? new State.StateA(context, 1, 2);

private readonly Action<Exception>? _onError;
Expand Down
22 changes: 11 additions & 11 deletions Chickensoft.LogicBlocks.Tests/test/fixtures/FakeLogicBlockAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public record NoNewState() : Input;
public record SelfInput(Input Input) : Input;
public record InputCallback(
Action Callback,
Func<Context, State> Next
Func<IContext, State> Next
) : Input;
public record Custom(Func<Context, State> Next) : Input;
public record Custom(Func<IContext, State> Next) : Input;
}

public abstract record State(Context Context) : StateLogic(Context),
public abstract record State(IContext Context) : StateLogic(Context),
IGet<Input.InputOne>,
IGet<Input.InputTwo>,
IGet<Input.InputThree>,
Expand Down Expand Up @@ -70,23 +70,23 @@ public async Task<State> On(Input.SelfInput input) {
return this;
}

public record StateA(Context Context, int Value1, int Value2) :
public record StateA(IContext Context, int Value1, int Value2) :
State(Context);
public record StateB(Context Context, string Value1, string Value2) :
public record StateB(IContext Context, string Value1, string Value2) :
State(Context);
public record StateC(Context Context, string Value) :
public record StateC(IContext Context, string Value) :
State(Context);
public record StateD(Context Context, string Value1, string Value2) :
public record StateD(IContext Context, string Value1, string Value2) :
State(Context);
public record Custom : State {
public Custom(Context context, Action<Context> setupCallback) :
public Custom(IContext context, Action<IContext> setupCallback) :
base(context) {
setupCallback(context);
}
}

public record OnEnterState : State {
public OnEnterState(Context context, Func<State, Task> onEnter) :
public OnEnterState(IContext context, Func<State, Task> onEnter) :
base(context) {
OnEnter<OnEnterState>(onEnter);
}
Expand All @@ -103,11 +103,11 @@ public partial class FakeLogicBlockAsync
: LogicBlockAsync<
FakeLogicBlockAsync.Input, FakeLogicBlockAsync.State, FakeLogicBlockAsync.Output
> {
public Func<Context, State>? InitialState { get; init; }
public Func<IContext, State>? InitialState { get; init; }

public List<Exception> Exceptions { get; } = new();

public override State GetInitialState(Context context) =>
public override State GetInitialState(IContext context) =>
InitialState?.Invoke(context) ?? new State.StateA(context, 1, 2);

private readonly Action<Exception>? _onError;
Expand Down
Loading