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
@@ -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
};
Comment on lines +24 to +27
Copy link

Copilot AI Sep 17, 2025

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 of 0.0.0.0, but this may be unclear. Consider using new Version(0, 0) to make the intent explicit and match the test expectations.

Copilot uses AI. Check for mistakes.

/// <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;
}
}
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
}
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));
}
}