This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The contract System.Runtime.InteropServices.RuntimeInformation provides RuntimeInformation type that has methods to query for underlying OS information at runtime. The OS information is expressed by the OSPlatform type. Reference issue: dotnet\corefx#1017, API Review done on 2015\06\09.
- Loading branch information
Lakshmi Priya Sekar
committed
Jun 11, 2015
1 parent
8d8d3bc
commit e84ac58
Showing
15 changed files
with
185 additions
and
154 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
78 changes: 0 additions & 78 deletions
78
src/System.Runtime.Environment/tests/CheckPlatformTests.cs
This file was deleted.
Oops, something went wrong.
This file contains 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
90 changes: 90 additions & 0 deletions
90
src/System.Runtime.InteropServices.RuntimeInformation/src/OSPlatform.cs
This file contains 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,90 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace System.Runtime.InteropServices | ||
{ | ||
public struct OSPlatform : IEquatable<OSPlatform> | ||
{ | ||
private readonly string _osPlatform; | ||
|
||
private const string WindowsName = "WINDOWS"; | ||
private const string LinuxName = "LINUX"; | ||
private const string OSXName = "OSX"; | ||
|
||
private static readonly OSPlatform s_windows = new OSPlatform(WindowsName); | ||
private static readonly OSPlatform s_linux = new OSPlatform(LinuxName); | ||
private static readonly OSPlatform s_osx = new OSPlatform(OSXName); | ||
|
||
public static OSPlatform Windows | ||
{ | ||
get | ||
{ | ||
return s_windows; | ||
} | ||
} | ||
|
||
public static OSPlatform Linux | ||
{ | ||
get | ||
{ | ||
return s_linux; | ||
} | ||
} | ||
|
||
public static OSPlatform OSX | ||
{ | ||
get | ||
{ | ||
return s_osx; | ||
} | ||
} | ||
|
||
private OSPlatform(string osPlatform) | ||
{ | ||
if (osPlatform == null) throw new ArgumentNullException("name"); | ||
if (osPlatform.Length == 0) throw new ArgumentException(SR.Argument_EmptyValue, "name"); | ||
|
||
_osPlatform = osPlatform; | ||
} | ||
|
||
public static OSPlatform Create(string osPlatform) | ||
{ | ||
return new OSPlatform(osPlatform); | ||
} | ||
|
||
public bool Equals(OSPlatform other) | ||
{ | ||
return string.Equals(other._osPlatform, _osPlatform, StringComparison.Ordinal); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (obj is OSPlatform) | ||
{ | ||
return Equals((OSPlatform)obj); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return _osPlatform == null ? 0 : _osPlatform.GetHashCode(); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return _osPlatform ?? string.Empty; | ||
} | ||
|
||
public static bool operator ==(OSPlatform left, OSPlatform right) | ||
{ | ||
return left.Equals(right); | ||
} | ||
|
||
public static bool operator !=(OSPlatform left, OSPlatform right) | ||
{ | ||
return !(left == right); | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains 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 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 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 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
File renamed without changes.
File renamed without changes.
80 changes: 80 additions & 0 deletions
80
src/System.Runtime.InteropServices.RuntimeInformation/tests/CheckPlatformTests.cs
This file contains 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,80 @@ | ||
using System.Runtime.InteropServices; | ||
using Xunit; | ||
|
||
namespace System.Runtime.InteropServices.RuntimeInformationTests | ||
{ | ||
public class CheckPlatformTests | ||
{ | ||
[Fact, PlatformSpecific(PlatformID.Windows)] | ||
public void CheckWindows() | ||
{ | ||
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); | ||
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("WINDOWS"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("windows"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("Windows NT"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)); | ||
} | ||
|
||
[Fact, PlatformSpecific(PlatformID.Linux)] | ||
public void CheckLinux() | ||
{ | ||
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); | ||
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("LINUX"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("linux"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("UNIX"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("DARWIN"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("ubuntu"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)); | ||
} | ||
|
||
[Fact, PlatformSpecific(PlatformID.OSX)] | ||
public void CheckOSX() | ||
{ | ||
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)); | ||
Assert.True(RuntimeInformation.IsOSPlatform(OSPlatform.Create("OSX"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("osx"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("mac"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("DARWIN"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Create("MACOSX"))); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)); | ||
Assert.False(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); | ||
} | ||
|
||
[Fact] | ||
public void CheckOSPlatform() | ||
{ | ||
OSPlatform winObj = OSPlatform.Create("WINDOWS"); | ||
OSPlatform winProp = OSPlatform.Windows; | ||
OSPlatform randomObj = OSPlatform.Create("random"); | ||
OSPlatform defaultObj = default(OSPlatform); | ||
OSPlatform conObj = new OSPlatform(); | ||
Assert.Throws<ArgumentNullException>(() => { OSPlatform nullObj = OSPlatform.Create(null); }); | ||
Assert.Throws<ArgumentException>(() => { OSPlatform emptyObj = OSPlatform.Create(""); }); | ||
|
||
Assert.True(winObj == winProp); | ||
Assert.True(winObj != randomObj); | ||
Assert.True(defaultObj == conObj); | ||
Assert.False(winObj == defaultObj); | ||
Assert.False(winObj == randomObj); | ||
Assert.False(winObj != winProp); | ||
|
||
Assert.True(winObj.Equals(winProp)); | ||
Assert.True(conObj.Equals(defaultObj)); | ||
Assert.False(defaultObj.Equals(winProp)); | ||
Assert.False(winObj.Equals(null)); | ||
Assert.False(winObj.Equals("something")); | ||
|
||
Assert.Equal("WINDOWS", winObj.ToString()); | ||
Assert.Equal("WINDOWS", winProp.ToString()); | ||
Assert.Equal("", defaultObj.ToString()); | ||
Assert.Equal("", conObj.ToString()); | ||
Assert.Equal("random", randomObj.ToString()); | ||
|
||
Assert.Equal(winObj.GetHashCode(), winProp.GetHashCode()); | ||
Assert.Equal(0, defaultObj.GetHashCode()); | ||
Assert.Equal(defaultObj.GetHashCode(), conObj.GetHashCode()); | ||
} | ||
} | ||
} |
Oops, something went wrong.