• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2018 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #endregion
18 
19 using System.Runtime.InteropServices;  // For NETCORE tests.
20 using Microsoft.Build.Framework;
21 using Moq;
22 using NUnit.Framework;
23 
24 namespace Grpc.Tools.Tests
25 {
26     public class ProtoToolsPlatformTaskTest
27     {
28         ProtoToolsPlatform _task;
29         int _cpuMatched, _osMatched;
30 
31         [OneTimeSetUp]
SetUp()32         public void SetUp()
33         {
34             var mockEng = new Mock<IBuildEngine>();
35             _task = new ProtoToolsPlatform() { BuildEngine = mockEng.Object };
36             _task.Execute();
37         }
38 
39         [OneTimeTearDown]
TearDown()40         public void TearDown()
41         {
42             Assert.AreEqual(1, _cpuMatched, "CPU type detection failed");
43             Assert.AreEqual(1, _osMatched, "OS detection failed");
44         }
45 
46 #if NETCORE
47         // PlatformAttribute not yet available in NUnit, coming soon:
48         // https://github.com/nunit/nunit/pull/3003.
49         // Use same test case names as under the full framework.
50         [Test]
CpuIsX86()51         public void CpuIsX86()
52         {
53             if (RuntimeInformation.OSArchitecture == Architecture.X86)
54             {
55                 _cpuMatched++;
56                 Assert.AreEqual("x86", _task.Cpu);
57             }
58         }
59 
60         [Test]
CpuIsX64()61         public void CpuIsX64()
62         {
63             if (RuntimeInformation.OSArchitecture == Architecture.X64)
64             {
65                 _cpuMatched++;
66                 Assert.AreEqual("x64", _task.Cpu);
67             }
68         }
69 
70         [Test]
OsIsWindows()71         public void OsIsWindows()
72         {
73             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
74             {
75                 _osMatched++;
76                 Assert.AreEqual("windows", _task.Os);
77             }
78         }
79 
80         [Test]
OsIsLinux()81         public void OsIsLinux()
82         {
83             if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
84             {
85                 _osMatched++;
86                 Assert.AreEqual("linux", _task.Os);
87             }
88         }
89 
90         [Test]
OsIsMacOsX()91         public void OsIsMacOsX()
92         {
93             if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
94             {
95                 _osMatched++;
96                 Assert.AreEqual("macosx", _task.Os);
97             }
98         }
99 
100 #else  // !NETCORE, i.e. full framework.
101 
102         [Test, Platform("32-Bit")]
CpuIsX86()103         public void CpuIsX86()
104         {
105             _cpuMatched++;
106             Assert.AreEqual("x86", _task.Cpu);
107         }
108 
109         [Test, Platform("64-Bit")]
CpuIsX64()110         public void CpuIsX64()
111         {
112             _cpuMatched++;
113             Assert.AreEqual("x64", _task.Cpu);
114         }
115 
116         [Test, Platform("Win")]
OsIsWindows()117         public void OsIsWindows()
118         {
119             _osMatched++;
120             Assert.AreEqual("windows", _task.Os);
121         }
122 
123         [Test, Platform("Linux")]
OsIsLinux()124         public void OsIsLinux()
125         {
126             _osMatched++;
127             Assert.AreEqual("linux", _task.Os);
128         }
129 
130         [Test, Platform("MacOSX")]
OsIsMacOsX()131         public void OsIsMacOsX()
132         {
133             _osMatched++;
134             Assert.AreEqual("macosx", _task.Os);
135         }
136 
137 #endif  // NETCORE
138     };
139 }
140