-
Notifications
You must be signed in to change notification settings - Fork 554
[RGen] Add a new type to track versions. #23838
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
70 changes: 70 additions & 0 deletions
70
src/rgen/Microsoft.Macios.Generator/Availability/PlatformSupportVersion.cs
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,70 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.Macios.Generator.Availability; | ||
|
|
||
| /// <summary> | ||
| /// Represents a platform support version, combining a version number and a support kind. | ||
| /// </summary> | ||
| public readonly record struct PlatformSupportVersion { | ||
| /// <summary> | ||
| /// Gets the version number. | ||
| /// </summary> | ||
| public Version Version { get; init; } | ||
| /// <summary> | ||
| /// Gets the kind of support (e.g., explicit, implicit). | ||
| /// </summary> | ||
| public SupportKind Kind { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets a default platform support version with an implicit kind. | ||
| /// </summary> | ||
| public static PlatformSupportVersion ImplicitDefault { get; } = new () { | ||
| Version = new (), | ||
| Kind = SupportKind.Implicit | ||
| }; | ||
|
|
||
| /// <summary> | ||
| /// Gets a default platform support version with an explicit kind. | ||
| /// </summary> | ||
| public static PlatformSupportVersion ExplicitDefault { get; } = new () { | ||
| Version = new (), | ||
| Kind = SupportKind.Explicit | ||
| }; | ||
|
|
||
| /// <summary> | ||
| /// Returns the platform support version with the highest precedence. | ||
| /// </summary> | ||
| /// <param name="v1">The first platform support version to compare.</param> | ||
| /// <param name="v2">The second platform support version to compare.</param> | ||
| /// <returns> | ||
| /// The platform support version with the highest precedence. If the kinds are the same, it returns the one with the greater version. | ||
| /// If the kinds are different, it returns the one with the higher kind value. | ||
| /// </returns> | ||
| public static PlatformSupportVersion Max (PlatformSupportVersion v1, PlatformSupportVersion v2) | ||
| { | ||
| if (v1.Kind == v2.Kind) { | ||
| return v1.Version >= v2.Version ? v1 : v2; | ||
| } | ||
| return (int) v1.Kind > (int) v2.Kind ? v1 : v2; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the platform support version with the lowest version if the kinds are the same, otherwise returns the one with the highest precedence kind. | ||
| /// </summary> | ||
| /// <param name="v1">The first platform support version to compare.</param> | ||
| /// <param name="v2">The second platform support version to compare.</param> | ||
| /// <returns> | ||
| /// The platform support version with the lowest version if the kinds are the same. | ||
| /// If the kinds are different, it returns the one with the higher kind value. | ||
| /// </returns> | ||
| public static PlatformSupportVersion Min (PlatformSupportVersion v1, PlatformSupportVersion v2) | ||
| { | ||
| if (v1.Kind == v2.Kind) { | ||
| return v1.Version <= v2.Version ? v1 : v2; | ||
| } | ||
| return (int) v1.Kind > (int) v2.Kind ? v1 : v2; | ||
| } | ||
| } | ||
9 changes: 9 additions & 0 deletions
9
src/rgen/Microsoft.Macios.Generator/Availability/SupportKind.cs
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,9 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Macios.Generator.Availability; | ||
|
|
||
| public enum SupportKind { | ||
| Implicit, | ||
| Explicit | ||
| } |
160 changes: 160 additions & 0 deletions
160
tests/rgen/Microsoft.Macios.Generator.Tests/Availability/PlatformSupportVersionTests.cs
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,160 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using Microsoft.Macios.Generator.Availability; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Macios.Generator.Tests.Availability; | ||
|
|
||
| public class PlatformSupportVersionTests { | ||
| [Fact] | ||
| public void ImplicitDefaultTest () | ||
| { | ||
| var implicitDefault = PlatformSupportVersion.ImplicitDefault; | ||
| Assert.Equal (new Version (0, 0), implicitDefault.Version); | ||
| Assert.Equal (SupportKind.Implicit, implicitDefault.Kind); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ExplicitDefaultTest () | ||
| { | ||
| var explicitDefault = PlatformSupportVersion.ExplicitDefault; | ||
| Assert.Equal (new Version (0, 0), explicitDefault.Version); | ||
| Assert.Equal (SupportKind.Explicit, explicitDefault.Kind); | ||
| } | ||
|
|
||
| public static TheoryData<PlatformSupportVersion, PlatformSupportVersion, PlatformSupportVersion> MaxTestData => | ||
| new () { | ||
| // Same kind, v1 > v2 | ||
| { | ||
| new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit } | ||
| }, | ||
| // Same kind, v2 > v1 | ||
| { | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit } | ||
| }, | ||
| // Same kind, v1 == v2 | ||
| { | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit } | ||
| }, | ||
| // Different kind, v1.Kind > v2.Kind | ||
| { | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Explicit }, | ||
| new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Explicit } | ||
| }, | ||
| // Different kind, v2.Kind > v1.Kind | ||
| { | ||
| new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Explicit }, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Explicit } | ||
| }, | ||
| // ExplicitDefault vs ImplicitDefault | ||
| { | ||
| PlatformSupportVersion.ExplicitDefault, | ||
| PlatformSupportVersion.ImplicitDefault, | ||
| PlatformSupportVersion.ExplicitDefault | ||
| }, | ||
| // ExplicitDefault vs other Implicit | ||
| { | ||
| PlatformSupportVersion.ExplicitDefault, | ||
| new PlatformSupportVersion { Version = new Version (10, 0), Kind = SupportKind.Implicit }, | ||
| PlatformSupportVersion.ExplicitDefault | ||
| }, | ||
| // ImplicitDefault vs other Implicit | ||
| { | ||
| PlatformSupportVersion.ImplicitDefault, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit } | ||
| }, | ||
| // ExplicitDefault vs other Explicit | ||
| { | ||
| PlatformSupportVersion.ExplicitDefault, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Explicit }, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Explicit } | ||
| }, | ||
| }; | ||
|
|
||
| [Theory] | ||
| [MemberData (nameof (MaxTestData))] | ||
| public void MaxTests (PlatformSupportVersion v1, PlatformSupportVersion v2, PlatformSupportVersion expected) | ||
| { | ||
| Assert.Equal (expected, PlatformSupportVersion.Max (v1, v2)); | ||
| // Test commutativity | ||
| Assert.Equal (expected, PlatformSupportVersion.Max (v2, v1)); | ||
| } | ||
|
|
||
| public static TheoryData<PlatformSupportVersion, PlatformSupportVersion, PlatformSupportVersion> MinTestData => | ||
| new () { | ||
| // Same kind, v1 < v2 | ||
| { | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit } | ||
| }, | ||
| // Same kind, v2 < v1 | ||
| { | ||
| new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit } | ||
| }, | ||
| // Same kind, v1 == v2 | ||
| { | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit } | ||
| }, | ||
| // Different kind, v1.Kind > v2.Kind | ||
| { | ||
| new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Explicit }, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Explicit } | ||
| }, | ||
| // Different kind, v2.Kind > v1.Kind | ||
| { | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }, | ||
| new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Explicit }, | ||
| new PlatformSupportVersion { Version = new Version (2, 0), Kind = SupportKind.Explicit } | ||
| }, | ||
| // ExplicitDefault vs ImplicitDefault | ||
| { | ||
| PlatformSupportVersion.ExplicitDefault, | ||
| PlatformSupportVersion.ImplicitDefault, | ||
| PlatformSupportVersion.ExplicitDefault | ||
| }, | ||
| // ExplicitDefault vs other Implicit | ||
| { | ||
| PlatformSupportVersion.ExplicitDefault, | ||
| new PlatformSupportVersion { Version = new Version (10, 0), Kind = SupportKind.Implicit }, | ||
| PlatformSupportVersion.ExplicitDefault | ||
| }, | ||
| // ImplicitDefault vs other Implicit | ||
| { | ||
| PlatformSupportVersion.ImplicitDefault, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Implicit }, | ||
| PlatformSupportVersion.ImplicitDefault | ||
| }, | ||
| // ExplicitDefault vs other Explicit | ||
| { | ||
| PlatformSupportVersion.ExplicitDefault, | ||
| new PlatformSupportVersion { Version = new Version (1, 0), Kind = SupportKind.Explicit }, | ||
| PlatformSupportVersion.ExplicitDefault | ||
| }, | ||
| }; | ||
|
|
||
| [Theory] | ||
| [MemberData (nameof (MinTestData))] | ||
| public void MinTests (PlatformSupportVersion v1, PlatformSupportVersion v2, PlatformSupportVersion expected) | ||
| { | ||
| Assert.Equal (expected, PlatformSupportVersion.Min (v1, v2)); | ||
| // Test commutativity | ||
| Assert.Equal (expected, PlatformSupportVersion.Min (v2, v1)); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
new Version()constructor creates a version of0.0.0.0, but this may be unclear. Consider usingnew Version(0, 0)to make the intent explicit and match the test expectations.