Set up unit test project

This commit is contained in:
Paul Pacheco 2018-07-15 12:29:21 -05:00
parent f42cd36e69
commit 7e1505d8ac
6 changed files with 194 additions and 0 deletions

4
.gitignore vendored
View File

@ -31,6 +31,7 @@ Source/Source.sln
Output/ Output/
bin/ bin/
.vs/ .vs/
Unity-Technologies-networking/packages
# ============ # # ============ #
# OS generated # # OS generated #
@ -42,3 +43,6 @@ bin/
.Trashes .Trashes
ehthumbs.db ehthumbs.db
Thumbs.db Thumbs.db

View File

@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnityEngine.Networking-Edit
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UNetWeaver", "Weaver\UNetWeaver.csproj", "{709222FD-15C2-497D-8B31-366ADCC074CD}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UNetWeaver", "Weaver\UNetWeaver.csproj", "{709222FD-15C2-497D-8B31-366ADCC074CD}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnityEngine.Networking.Tests", "UnityEngine.Networking.Tests\UnityEngine.Networking.Tests.csproj", "{F57702C3-86F0-421C-8B29-AE00F0B84BAF}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -40,6 +42,12 @@ Global
{F8AD1DD0-E73F-419B-97BB-3C708BCA79C8}.Release|Any CPU.Build.0 = Release|Any CPU {F8AD1DD0-E73F-419B-97BB-3C708BCA79C8}.Release|Any CPU.Build.0 = Release|Any CPU
{F8AD1DD0-E73F-419B-97BB-3C708BCA79C8}.Testing|Any CPU.ActiveCfg = Release|Any CPU {F8AD1DD0-E73F-419B-97BB-3C708BCA79C8}.Testing|Any CPU.ActiveCfg = Release|Any CPU
{F8AD1DD0-E73F-419B-97BB-3C708BCA79C8}.Testing|Any CPU.Build.0 = Release|Any CPU {F8AD1DD0-E73F-419B-97BB-3C708BCA79C8}.Testing|Any CPU.Build.0 = Release|Any CPU
{F57702C3-86F0-421C-8B29-AE00F0B84BAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F57702C3-86F0-421C-8B29-AE00F0B84BAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F57702C3-86F0-421C-8B29-AE00F0B84BAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F57702C3-86F0-421C-8B29-AE00F0B84BAF}.Release|Any CPU.Build.0 = Release|Any CPU
{F57702C3-86F0-421C-8B29-AE00F0B84BAF}.Testing|Any CPU.ActiveCfg = Debug|Any CPU
{F57702C3-86F0-421C-8B29-AE00F0B84BAF}.Testing|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -0,0 +1,125 @@
using NUnit.Framework;
using System;
using UnityEngine;
namespace UnityEngine.Networking.Tests
{
[TestFixture()]
public class NetworkWriterTest
{
[Test]
public void TestWritingSmallMessage()
{
// try serializing <32kb and see what happens
NetworkWriter writer = new NetworkWriter();
for (int i = 0; i < 30000 / 4; ++i)
writer.Write(i);
Assert.That(writer.Position, Is.EqualTo(30000));
}
[Test]
public void TestWritingLargeMessage()
{
// try serializing <32kb and see what happens
NetworkWriter writer = new NetworkWriter();
for (int i = 0; i < 40000 / 4; ++i)
writer.Write(i);
Assert.That(writer.Position, Is.EqualTo(40000));
}
[Test]
public void TestResetting()
{
NetworkWriter writer = new NetworkWriter();
writer.StartMessage((short)1337);
writer.Write(1);
writer.Write(2);
writer.Write(3);
writer.Write(4);
writer.FinishMessage();
// try SeekZero and reset afterwards
int messageSize = writer.Position;
writer.SeekZero();
Assert.That(writer.Position, Is.Zero);
writer.Position = messageSize;
// check if .ToArray() returns array until .Position
writer.Position = 4;
Assert.That(writer.ToArray().Length, Is.EqualTo(4));
}
[Test]
public void TestWritingAndReading()
{
// write all simple types once
NetworkWriter writer = new NetworkWriter();
writer.StartMessage((short)1337);
writer.Write((char)1);
writer.Write((byte)2);
writer.Write((sbyte)3);
writer.Write((bool)true);
writer.Write((short)4);
writer.Write((ushort)5);
writer.Write((int)6);
writer.Write((uint)7);
writer.Write((long)8L);
writer.Write((ulong)9L);
writer.Write((float)10);
writer.Write((double)11);
writer.Write((decimal)12);
writer.Write((string)null);
writer.Write((string)"");
writer.Write((string)"13");
writer.Write(new byte[] { 14, 15 }, 0, 2); // just the byte array, no size info etc.
writer.WriteBytesAndSize((byte[])null); // [SyncVar] struct values can have uninitialized byte arrays, null needs to be supported
writer.WriteBytesAndSize(new byte[] { 17, 18 }, 0, 2); // buffer, no-offset, count
writer.WriteBytesAndSize(new byte[] { 19, 20, 21 }, 1, 2); // buffer, offset, count
writer.WriteBytesAndSize(new byte[] { 22, 23 }, 0, 2); // size, buffer
writer.FinishMessage();
byte[] data = writer.ToArray();
// read them
NetworkReader reader = new NetworkReader(writer.ToArray());
Assert.That(reader.ReadInt16(), Is.EqualTo(data.Length - sizeof(ushort) * 2)); // msgType
Assert.That(reader.ReadUInt16(), Is.EqualTo(1337)); // contentSize (messasge.size - 4 bytes header)
Assert.That(reader.ReadChar(), Is.EqualTo(1));
Assert.That(reader.ReadByte(), Is.EqualTo(2));
Assert.That(reader.ReadSByte(), Is.EqualTo(3));
Assert.That(reader.ReadBoolean(), Is.True);
Assert.That(reader.ReadInt16(), Is.EqualTo(4));
Assert.That(reader.ReadUInt16(), Is.EqualTo(5));
Assert.That(reader.ReadInt32(), Is.EqualTo(6));
Assert.That(reader.ReadUInt32(), Is.EqualTo(7));
Assert.That(reader.ReadInt64(), Is.EqualTo(8));
Assert.That(reader.ReadUInt64(), Is.EqualTo(9));
Assert.That(reader.ReadSingle(), Is.EqualTo(10));
Assert.That(reader.ReadDouble(), Is.EqualTo(11));
Assert.That(reader.ReadDecimal(), Is.EqualTo(12));
Assert.That(reader.ReadString(), Is.Null); // writing null string should write null in HLAPI Pro ("" in original HLAPI)
Assert.That(reader.ReadString(), Is.EqualTo(""));
Assert.That(reader.ReadString(), Is.EqualTo("13"));
Assert.That(reader.ReadBytes(2), Is.EqualTo(new byte[] { 14, 15 }));
Assert.That(reader.ReadBytesAndSize(), Is.Null);
Assert.That(reader.ReadBytesAndSize(), Is.EqualTo(new byte[] { 17, 18 }));
Assert.That(reader.ReadBytesAndSize(), Is.EqualTo(new byte[] { 20, 21 }));
Assert.That(reader.ReadBytesAndSize(), Is.EqualTo(new byte[] { 22, 23 }));
reader.SeekZero();
Assert.That(reader.Position, Is.Zero);
}
}
}

View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\NUnit.3.10.1\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{F57702C3-86F0-421C-8B29-AE00F0B84BAF}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>UnityEngine.Networking.Tests</RootNamespace>
<AssemblyName>UnityEngine.Networking.Tests</AssemblyName>
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.3.10.1\lib\net45\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="UnityEngine">
<HintPath>..\lib\UnityEngine.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="NetworkWriterTest.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Runtime\UnityEngine.Networking.csproj">
<Project>{5F1B8F9B-4500-4D09-808B-F43E8337DF05}</Project>
<Name>UnityEngine.Networking</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="3.10.1" targetFramework="net47" />
</packages>

View File

@ -1,5 +1,8 @@
version: '{branch}-{build}' version: '{branch}-{build}'
before_build:
- nuget restore Unity-Technologies-networking/Networking.sln
build: build:
project: Unity-Technologies-networking/Networking.sln project: Unity-Technologies-networking/Networking.sln
verbosity: minimal verbosity: minimal