Skip to content
Merged
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
8 changes: 7 additions & 1 deletion src/WireMock.Net.Minimal/Serialization/MappingConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,15 @@ public string ToCSharpCode(IMapping mapping, MappingConverterSettings? settings
{
Comment thread
Bafyn marked this conversation as resolved.
sb.AppendLine($" .InScenario({ToCSharpStringLiteral(mapping.Scenario)})");
}
if (!string.IsNullOrEmpty(mapping.ExecutionConditionState))
{
sb.AppendLine($" .WhenStateIs({ToCSharpStringLiteral(mapping.ExecutionConditionState)})");
}
if (!string.IsNullOrEmpty(mapping.NextState))
{
sb.AppendLine($" .WhenStateIs({ToCSharpStringLiteral(mapping.NextState)}, {ToCSharpIntLiteral(mapping.TimesInSameState)})");
sb.AppendLine(mapping.TimesInSameState > 1
Comment thread
Bafyn marked this conversation as resolved.
Outdated
? $" .WillSetStateTo({ToCSharpStringLiteral(mapping.NextState)}, {ToCSharpIntLiteral(mapping.TimesInSameState)})"
: $" .WillSetStateTo({ToCSharpStringLiteral(mapping.NextState)})");
}

if (mapping.Probability != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ builder
)
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931005")
.InScenario("To do list")
.WhenStateIs("TodoList State Started", 2)
.WillSetStateTo("TodoList State Started", 2)
.RespondWith(Response.Create()
.WithBody("Buy milk")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ server
)
.WithGuid("98fae52e-76df-47d9-876f-2ee32e931005")
.InScenario("To do list")
.WhenStateIs("TodoList State Started", 2)
.WillSetStateTo("TodoList State Started", 2)
.RespondWith(Response.Create()
.WithBody("Buy milk")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,67 @@ public Task ToCSharpCode_With_Server_And_AddStartIsFalse()
return Verify(code, VerifySettings);
}

[Fact]
public Task ToCSharpCode_With_ScenarioAndState_Emits_WhenStateIs_And_WillSetStateTo()
{
// Assign: a stateful mapping that gates on state "Begin" and moves to "End" after 3 hits.
var mapping = CreateScenarioMapping("Ordering", executionConditionState: "Begin", nextState: "End", timesInSameState: 3);

// Act
var code = _sut.ToCSharpCode(mapping, new MappingConverterSettings
{
AddStart = false,
ConverterType = MappingConverterType.Server
});

// Verify
return Verify(code, VerifySettings);
}

[Fact]
public Task ToCSharpCode_With_NextStateOnly_Emits_WillSetStateTo_Without_Times()
{
// Assign: a start-state mapping that only sets the next state (no execution condition, default times).
var mapping = CreateScenarioMapping("Ordering", executionConditionState: null, nextState: "Begin", timesInSameState: null);

// Act
var code = _sut.ToCSharpCode(mapping, new MappingConverterSettings
{
AddStart = false,
ConverterType = MappingConverterType.Server
});

// Verify
return Verify(code, VerifySettings);
}

private Mapping CreateScenarioMapping(string scenario, string? executionConditionState, string? nextState, int? timesInSameState)
{
var request = Request.Create().UsingGet().WithPath("/test_path");
var response = Response.Create().WithSuccess();

return new Mapping
Comment thread
Bafyn marked this conversation as resolved.
(
new Guid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc"),
_updatedAt,
string.Empty,
string.Empty,
null,
_settings,
request,
response,
0,
scenario,
executionConditionState,
nextState,
timesInSameState,
null,
false,
null,
data: null
);
}

private IMapping CreateMapping()
{
var guid = new Guid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
server
.Given(Request.Create()
.UsingMethod("GET")
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/test_path", false, WireMock.Matchers.MatchOperator.Or))
)
.WithGuid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc")
.InScenario("Ordering")
.WillSetStateTo("Begin")
.RespondWith(Response.Create()
.WithStatusCode(200)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server
.Given(Request.Create()
.UsingMethod("GET")
.WithPath(new WildcardMatcher(WireMock.Matchers.MatchBehaviour.AcceptOnMatch, "/test_path", false, WireMock.Matchers.MatchOperator.Or))
)
.WithGuid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc")
.InScenario("Ordering")
.WhenStateIs("Begin")
.WillSetStateTo("End", 3)
.RespondWith(Response.Create()
.WithStatusCode(200)
);