-
-
Notifications
You must be signed in to change notification settings - Fork 128
fix(mocks): emit 'where T : default' for interface-constrained generic methods #6458
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,62 @@ | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.Kiota.Abstractions; | ||
|
|
||
| namespace TUnit.Mocks.SourceGenerator.Tests; | ||
|
|
||
| /// <summary> | ||
| /// Regression tests for #6456: mocking Microsoft.Kiota.Abstractions.IRequestAdapter, whose | ||
| /// generic methods carry an interface constraint (where ModelType : IParsable). Explicit | ||
| /// interface implementations inherit constraints, but T? in them is parsed as Nullable<T> | ||
| /// unless a 'where T : default' clause is emitted — which is required whenever T is not known | ||
| /// to be a reference or value type, not just when T is fully unconstrained. | ||
| /// </summary> | ||
| public class KiotaMockTests : SnapshotTestBase | ||
| { | ||
| private const string Source = """ | ||
| using Microsoft.Kiota.Abstractions; | ||
| using TUnit.Mocks; | ||
|
|
||
| [assembly: GenerateMock(typeof(IRequestAdapter))] | ||
| """; | ||
|
|
||
| private static IEnumerable<MetadataReference> KiotaReferences() => | ||
| [MetadataReference.CreateFromFile(typeof(IRequestAdapter).Assembly.Location)]; | ||
|
|
||
| [Test] | ||
| public void KiotaIRequestAdapter_ConstrainedGenericMethod_EmitsDefaultConstraint() | ||
| { | ||
| var output = string.Join("\n", RunGenerator(Source, KiotaReferences())); | ||
|
|
||
| // SendAsync<ModelType> has 'where ModelType : IParsable' and returns Task<ModelType?>; | ||
| // its explicit implementation must carry 'where ModelType : default'. | ||
| AssertContains(output, | ||
| "global::Microsoft.Kiota.Abstractions.IRequestAdapter.SendAsync<ModelType>"); | ||
| AssertContains(output, "where ModelType : default"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void KiotaIRequestAdapter_GeneratedMock_HasNoConstraintErrors() | ||
| { | ||
| var errors = GetGeneratedCompilationErrors(Source, KiotaReferences()); | ||
|
|
||
| // The exact errors reported in #6456. Other diagnostics (e.g. CS1520/CS1513 from C# 14 | ||
| // extension() blocks that the test-pinned Roslyn cannot parse) are infra limitations | ||
| // and intentionally not asserted here — see SnapshotTestBase. | ||
| foreach (var id in (string[])["CS0539", "CS0535", "CS0453", "CS0314"]) | ||
| { | ||
| var match = errors.FirstOrDefault(e => string.Equals(e.Id, id, StringComparison.Ordinal)); | ||
| if (match is not null) | ||
| { | ||
| throw new InvalidOperationException($"Generated code produced {id}: {match}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void AssertContains(string text, string expected) | ||
| { | ||
| if (!text.Contains(expected, StringComparison.Ordinal)) | ||
| { | ||
| throw new InvalidOperationException($"Expected generated output to contain: {expected}"); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,51 @@ | ||
| using Microsoft.Kiota.Abstractions; | ||
| using Microsoft.Kiota.Abstractions.Serialization; | ||
| using TUnit.Mocks; | ||
|
|
||
| namespace TUnit.Mocks.Tests; | ||
|
|
||
| /// <summary> | ||
| /// Regression tests for #6456: Microsoft.Kiota.Abstractions.IRequestAdapter could not be mocked | ||
| /// because its generic methods carry 'where ModelType : IParsable'. The generated explicit | ||
| /// interface implementations omitted the 'where ModelType : default' clause, so the compiler | ||
| /// parsed Task<ModelType?> as Task<Nullable<ModelType>> (CS0453/CS0539/CS0535/CS0314). | ||
| /// </summary> | ||
| public class Issue6456Tests | ||
| { | ||
| private sealed class TestModel : IParsable | ||
| { | ||
| public IDictionary<string, Action<IParseNode>> GetFieldDeserializers() => | ||
| new Dictionary<string, Action<IParseNode>>(); | ||
|
|
||
| public void Serialize(ISerializationWriter writer) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task SendNoContentAsync_Can_Be_Mocked_And_Verified() | ||
| { | ||
| var mock = IRequestAdapter.Mock(); | ||
| var requestInfo = new RequestInformation(); | ||
|
|
||
| await mock.Object.SendNoContentAsync(requestInfo); | ||
|
|
||
| mock.SendNoContentAsync(Any(), Any(), Any()).WasCalled(Times.Once); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task SendAsync_With_Constrained_Generic_Returns_Configured_Value() | ||
| { | ||
| var mock = IRequestAdapter.Mock(); | ||
| var expected = new TestModel(); | ||
| mock.SendAsync<TestModel>(Any(), Any(), Any(), Any()).Returns(expected); | ||
|
|
||
| var result = await mock.Object.SendAsync( | ||
| new RequestInformation(), | ||
| static _ => new TestModel(), | ||
| errorMapping: null, | ||
| CancellationToken.None); | ||
|
|
||
| await Assert.That(result).IsSameReferenceAs(expected); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.