Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -815,4 +815,124 @@ public void TestMethod()

await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}

[TestMethod]
public async Task WhenOSPlatformCreateWithKnownPlatform_Diagnostic()
{
// OSPlatform.Create("Windows") exercises the Create() branch in TryGetOSPlatformFromIsOSPlatformCall.
// The platform name "Windows" maps to OperatingSystems.Windows, so the fixer applies.
string code = """
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MyTestClass
{
[TestMethod]
public void TestMethod()
{
[|if (!RuntimeInformation.IsOSPlatform(OSPlatform.Create("Windows")))
{
return;
}|]
}
}
""";

string fixedCode = """
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MyTestClass
{
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void TestMethod()
{
}
}
""";

await VerifyCS.VerifyCodeFixAsync(code, fixedCode);
}

[TestMethod]
public async Task WhenOSPlatformCreateWithUnknownPlatform_Diagnostic()
{
// OSPlatform.Create("CustomOS") exercises the Create() branch but with a platform name
// that has no matching OperatingSystems enum value. The analyzer fires but the fixer
// cannot produce a code fix (no matching OperatingSystems member), so the code is unchanged.
string code = """
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MyTestClass
{
[TestMethod]
public void TestMethod()
{
[|if (!RuntimeInformation.IsOSPlatform(OSPlatform.Create("CustomOS")))
{
return;
}|]
}
}
""";

await VerifyCS.VerifyAnalyzerAsync(code);
Comment thread
Evangelink marked this conversation as resolved.
Resolved
}

[TestMethod]
public async Task WhenOperatingSystemIsIOS_Diagnostic()
{
// OperatingSystem.IsIOS() maps to OS platform "iOS", which has no corresponding
// OperatingSystems enum value. The analyzer fires but the fixer cannot produce a code fix.
string code = """
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MyTestClass
{
[TestMethod]
public void TestMethod()
{
[|if (!OperatingSystem.IsIOS())
{
return;
}|]
}
}
""";

await VerifyCS.VerifyAnalyzerAsync(code);
Comment thread
Evangelink marked this conversation as resolved.
Resolved
}

[TestMethod]
public async Task WhenOperatingSystemIsAndroid_Diagnostic()
{
// OperatingSystem.IsAndroid() maps to OS platform "Android", which has no corresponding
// OperatingSystems enum value. The analyzer fires but the fixer cannot produce a code fix.
string code = """
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MyTestClass
{
[TestMethod]
public void TestMethod()
{
[|if (!OperatingSystem.IsAndroid())
{
return;
}|]
}
}
""";

await VerifyCS.VerifyAnalyzerAsync(code);
Comment thread
Evangelink marked this conversation as resolved.
Resolved
}
}
Loading