Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System.Text;
using System.Text.RegularExpressions;
using Mockolate.SourceGenerators.Entities;

namespace Mockolate.SourceGenerators.Sources;

#pragma warning disable S3776 // Cognitive Complexity of methods should not be too high
internal static partial class Sources
{
private static readonly Regex InvalidIdentifierChars = new("[^a-zA-Z0-9_]", RegexOptions.Compiled);

public static string ForMockCombinationExtensions(string name, MockClass mockClass,
IEnumerable<Class> distinctAdditionalImplementations)
{
Expand Down Expand Up @@ -75,10 +78,11 @@ private static void AppendAdditionalMockExtensions(StringBuilder sb, Class @clas
{
sb.AppendLine();
int nameSuffix = 1;
string name = @class.ClassName.Replace('.', '_');
string sanitizedClassName = InvalidIdentifierChars.Replace(@class.ClassName, "_");
string name = sanitizedClassName;
while (!usedNames.Add(name))
{
name = $"{@class.ClassName.Replace('.', '_')}__{++nameSuffix}";
name = $"{sanitizedClassName}__{++nameSuffix}";
}

string mockExpression =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private static string ExtractFormDataFromMultipartContent(string rawContent)

if (!string.IsNullOrWhiteSpace(line))
{
sb.AppendLine(line.TrimEnd());
sb.AppendLine(line);
Comment thread
vbreuss marked this conversation as resolved.
}
}

Expand Down
20 changes: 20 additions & 0 deletions Tests/Mockolate.Internal.Tests/WebTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ public async Task HttpContentParameter_MatchesWithNullContent_ShouldReturnFalse(
await That(result).IsFalse();
}

[Fact]
public async Task WhenParameterImplementsIHttpRequestMessagePropertyParameter_ShouldUseThisMatch()
{
ItExtensions.IHttpContentParameter parameter =
Mock.Create<ItExtensions.IHttpContentParameter, IParameter,
IHttpRequestMessagePropertyParameter<HttpContent?>>();
parameter.SetupIParameterMock.Method.Matches(It.IsAny<object?>()).Returns(true);
parameter.SetupIHttpRequestMessagePropertyParameter_HttpContent_Mock.Method
.Matches(It.IsAny<HttpContent?>(), It.IsAny<HttpRequestMessage?>()).Returns(true);

ItExtensions.IStringContentBodyParameter sut = parameter.WithString("foo");

bool result = ((IHttpRequestMessagePropertyParameter<HttpContent?>)sut).Matches(null, null);

await That(result).IsTrue();
await That(parameter.VerifyOnIHttpRequestMessagePropertyParameter_HttpContent_Mock.Invoked
.Matches(It.IsNull<HttpContent?>(), It.IsNull<HttpRequestMessage?>()))
.Once();
}

[Fact]
public async Task WithoutMediaTypeHeader_WhenNoneIsRequired_ShouldReturnTrue()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public sealed class WithFormDataTests
[InlineData(false, "y", "234", "z", "345", "z", "567")]
[InlineData(false, "x", "123", "z", "345")]
[InlineData(false, "x", "123", "y", "234")]
[InlineData(false, "x", "123", "y", "234", "z", "345")]
public async Task Exactly_ShouldOnlyMatchWhenAllParametersAreChecked(
bool expectSuccess, params string[] rawValues)
{
Expand Down Expand Up @@ -53,7 +54,7 @@ await That(result.StatusCode)
}

[Theory]
[InlineData("x<>", "1<2> 3")]
[InlineData("x<>", "1<2> 3 ")]
public async Task ShouldEncodeValues(string key, string value)
{
HttpClient httpClient = Mock.Create<HttpClient>();
Expand Down
Loading