diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2017-11-16 13:38:56 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2017-11-16 13:38:56 +0200 |
| commit | 914f4db513477d9aff726546bac47545195a3e37 (patch) | |
| tree | d2ff190fd84b1dfaa03eec76563c431592ece7ff /Software/Visual_Studio/Tango.UnitTesting | |
| parent | 65d01ff549d80fbe13ff5e966df216c9f7c03653 (diff) | |
| download | Tango-914f4db513477d9aff726546bac47545195a3e37.tar.gz Tango-914f4db513477d9aff726546bac47545195a3e37.zip | |
Rename "Visual Studio" to "Visual_Studio"
Rename "External Repositories" to "External_Repositories".
Diffstat (limited to 'Software/Visual_Studio/Tango.UnitTesting')
5 files changed, 330 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.UnitTesting/Helper.cs b/Software/Visual_Studio/Tango.UnitTesting/Helper.cs new file mode 100644 index 000000000..ece74d3f3 --- /dev/null +++ b/Software/Visual_Studio/Tango.UnitTesting/Helper.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; +using Tango.Logging; + +namespace Tango.UnitTesting +{ + /// <summary> + /// Contains several unit testing helper methods. + /// </summary> + public static class Helper + { + /// <summary> + /// Gets the absolute path to the specified file name in the solution 'Resources' folder. + /// </summary> + /// <param name="fileName">Name of the file.</param> + /// <returns></returns> + public static String GetResourcePath(String fileName) + { + return Path.GetFullPath(@"..\..\Resources\" + fileName); + } + + /// <summary> + /// Gets the PMR (Protobuf Messages Repository) path. + /// </summary> + /// <returns></returns> + public static String GetPMRPath() + { + return Path.GetFullPath(@"..\..\..\PMR\Messages\"); + } + + /// <summary> + /// Initializes the logging manager. + /// </summary> + public static ConsoleLogger InitializeLogging(bool useConsole = false, [CallerMemberName] string testName = null) + { + LogManager.RegisterLogger(new FileLogger(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "logs", "Unit Testing", testName + ".txt"))); + if (useConsole) + { + var consoleLogger = new ConsoleLogger(testName); + LogManager.RegisterLogger(consoleLogger); + return consoleLogger; + } + + return null; + } + + /// <summary> + /// Creates a temporary folder and returns it's path. + /// </summary> + /// <returns></returns> + public static String GetTempFolderPathAppend(String customFolder, [CallerMemberName] string testName = null) + { + String tempDirectory = Path.Combine(Path.GetTempPath(), "Twine", "Unit Testing", testName, customFolder); + Directory.CreateDirectory(tempDirectory); + return tempDirectory; + } + + /// <summary> + /// Creates a temporary folder and returns it's path. + /// </summary> + /// <returns></returns> + public static String GetTempFolderPath([CallerMemberName] string testName = null) + { + String tempDirectory = Path.Combine(Path.GetTempPath(), "Twine", "Unit Testing", testName, Path.GetRandomFileName()); + Directory.CreateDirectory(tempDirectory); + return tempDirectory; + } + + /// <summary> + /// Tries to delete folder. + /// </summary> + /// <param name="path">The path.</param> + /// <returns></returns> + public static bool TryDeleteFolder(String path) + { + try + { + Directory.Delete(path, true); + return true; + } + catch + { + return false; + } + } + + /// <summary> + /// Shows the file in explorer. + /// </summary> + /// <param name="path">Name of the file/folder.</param> + public static void ShowInExplorer(String path) + { + Process.Start("explorer.exe", string.Format("/select,\"{0}\"", path)); + } + } +} diff --git a/Software/Visual_Studio/Tango.UnitTesting/Properties/AssemblyInfo.cs b/Software/Visual_Studio/Tango.UnitTesting/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..0551f8f77 --- /dev/null +++ b/Software/Visual_Studio/Tango.UnitTesting/Properties/AssemblyInfo.cs @@ -0,0 +1,6 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("Tango - Unit Testing")] +[assembly: ComVisible(false)]
\ No newline at end of file diff --git a/Software/Visual_Studio/Tango.UnitTesting/Protobuf_TST.cs b/Software/Visual_Studio/Tango.UnitTesting/Protobuf_TST.cs new file mode 100644 index 000000000..ed3bfbb7d --- /dev/null +++ b/Software/Visual_Studio/Tango.UnitTesting/Protobuf_TST.cs @@ -0,0 +1,115 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Tango.Protobuf; +using System.Threading; +using System.Collections.Generic; +using System.Linq; +using System.IO; +using Google.Protobuf; +using Tango.PMR.Jobs; +using Tango.PMR.Common; +using Tango.PMR; +using Tango.Logging; +using Newtonsoft.Json; +using System.Runtime.InteropServices; +using Tango.Core.Helpers; +using System.Text; +using Tango.PMR.Stubs; + +namespace Tango.UnitTesting +{ + [TestClass] + [TestCategory("Protobuf")] + public class Protobuf_TST + { + [DllImport("Tango.ProtoTest.dll", CallingConvention = CallingConvention.Cdecl)] + public static extern int Calculate(IntPtr data, int size, ref IntPtr output); + + /// <summary> + /// Compiles all the whole PMR using all available compilers. + /// </summary> + [TestMethod] + public void Compile_All_PMR() + { + var console = Helper.InitializeLogging(true); + + String pmrFolder = Helper.GetPMRPath(); + + List<String> tempFolders = new List<string>(); + + foreach (var compiler in CompilerFactory.GetAvailableCompilers()) + { + CompilerFolderResult result = compiler.CompileFolder(pmrFolder); + String tmpFolder = Helper.GetTempFolderPathAppend(compiler.Language.ToString()); + result.Save(tmpFolder); + tempFolders.Add(tmpFolder); + } + + Helper.ShowInExplorer(Directory.GetParent(tempFolders.First()).FullName); + + console.WaitForConsoleExit().Wait(); + + foreach (var folder in tempFolders) + { + Helper.TryDeleteFolder(folder); + } + } + + /// <summary> + /// Writes and reads a proto message then compares. + /// </summary> + [TestMethod] + public void Read_Write_Message() + { + var console = Helper.InitializeLogging(true); + + TangoMessage<Job> container = MessageFactory.CreateTangoMessage<Job>(); + + container.Message.Name = "Test Job"; + + container.Message.Segments.Add(new Segment() + { + Color = new RGB() { R = 1, G = 2, B = 3 }, + Length = 10, + Name = "Segment 1" + }); + + container.Message.Segments.Add(new Segment() + { + Color = new RGB() { R = 10, G = 20, B = 30 }, + Length = 100, + Name = "Segment 2" + }); + + LogManager.Log("Write Message:" + Environment.NewLine + JsonConvert.SerializeObject(container.Message, Formatting.Indented)); + + var bytes = container.ToBytes(); + + var parsed = MessageFactory.ParseTangoMessage<Job>(bytes); + + LogManager.Log("Read Message:" + Environment.NewLine + JsonConvert.SerializeObject(parsed.Message, Formatting.Indented)); + + Assert.AreEqual(container.Message, parsed.Message); + + LogManager.Log("Test Passed!"); + + console.WaitForConsoleExit().Wait(); + } + + /// <summary> + /// Calls a C++ native library and get a result. + /// </summary> + [TestMethod] + public void Call_CPP_And_Get_Result() + { + CalculateRequest request = new CalculateRequest(); + request.A = 10; + request.B = 5; + + NativePMR<CalculateRequest, CalculateResponse> nativePMR = new NativePMR<CalculateRequest, CalculateResponse>(Calculate); + CalculateResponse response = nativePMR.Invoke(request); + + Assert.AreEqual(response.Sum, request.A + request.B); + } + } +} diff --git a/Software/Visual_Studio/Tango.UnitTesting/Tango.UnitTesting.csproj b/Software/Visual_Studio/Tango.UnitTesting/Tango.UnitTesting.csproj new file mode 100644 index 000000000..a377179b9 --- /dev/null +++ b/Software/Visual_Studio/Tango.UnitTesting/Tango.UnitTesting.csproj @@ -0,0 +1,100 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{FB82AA6B-1652-452C-8235-4FB2E524FBC0}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>Tango.UnitTesting</RootNamespace> + <AssemblyName>Tango.UnitTesting</AssemblyName> + <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion> + <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> + <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath> + <IsCodedUITest>False</IsCodedUITest> + <TestProjectType>UnitTest</TestProjectType> + <NuGetPackageImportStamp> + </NuGetPackageImportStamp> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>..\Build\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <AllowUnsafeBlocks>true</AllowUnsafeBlocks> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="Google.Protobuf, Version=3.4.1.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604, processorArchitecture=MSIL"> + <HintPath>..\packages\Google.Protobuf.3.4.1\lib\net45\Google.Protobuf.dll</HintPath> + </Reference> + <Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> + <HintPath>..\packages\MSTest.TestFramework.1.1.11\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath> + </Reference> + <Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> + <HintPath>..\packages\MSTest.TestFramework.1.1.11\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath> + </Reference> + <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> + <HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath> + </Reference> + <Reference Include="System" /> + <Reference Include="System.Core" /> + </ItemGroup> + <ItemGroup> + <Compile Include="..\Versioning\GlobalVersionInfo.cs"> + <Link>GlobalVersionInfo.cs</Link> + </Compile> + <Compile Include="Helper.cs" /> + <Compile Include="Protobuf_TST.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Tango.Core\Tango.Core.csproj"> + <Project>{a34ee0f0-649d-41c8-8489-b6f1cc6924ee}</Project> + <Name>Tango.Core</Name> + </ProjectReference> + <ProjectReference Include="..\Tango.Logging\Tango.Logging.csproj"> + <Project>{bc932dbd-7cdb-488c-99e4-f02cf441f55e}</Project> + <Name>Tango.Logging</Name> + </ProjectReference> + <ProjectReference Include="..\Tango.PMR\Tango.PMR.csproj"> + <Project>{e4927038-348d-4295-aaf4-861c58cb3943}</Project> + <Name>Tango.PMR</Name> + </ProjectReference> + <ProjectReference Include="..\Tango.Protobuf\Tango.Protobuf.csproj"> + <Project>{40073806-914e-4e78-97ab-fa9639308ebe}</Project> + <Name>Tango.Protobuf</Name> + </ProjectReference> + <ProjectReference Include="..\Tango.SharedUI\Tango.SharedUI.csproj"> + <Project>{ac489889-6e50-4f16-9dba-ff4c6f9ec72b}</Project> + <Name>Tango.SharedUI</Name> + </ProjectReference> + </ItemGroup> + <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" /> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> + <PropertyGroup> + <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText> + </PropertyGroup> + <Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.props'))" /> + <Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets'))" /> + </Target> + <Import Project="..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.1.11\build\net45\MSTest.TestAdapter.targets')" /> +</Project>
\ No newline at end of file diff --git a/Software/Visual_Studio/Tango.UnitTesting/packages.config b/Software/Visual_Studio/Tango.UnitTesting/packages.config new file mode 100644 index 000000000..d3cd9d043 --- /dev/null +++ b/Software/Visual_Studio/Tango.UnitTesting/packages.config @@ -0,0 +1,7 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="Google.Protobuf" version="3.4.1" targetFramework="net45" /> + <package id="MSTest.TestAdapter" version="1.1.11" targetFramework="net45" /> + <package id="MSTest.TestFramework" version="1.1.11" targetFramework="net45" /> + <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" /> +</packages>
\ No newline at end of file |
