Skip to content

Commit

Permalink
Test contexts types
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-garcia committed May 22, 2018
1 parent 7c26d80 commit 8355dd8
Show file tree
Hide file tree
Showing 11 changed files with 384 additions and 7 deletions.
4 changes: 0 additions & 4 deletions samples/Sentry.Samples.Console.Basic/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
using System;
using System.Threading.Tasks;
using static System.Console;

namespace Sentry.Samples.Console.Basic
{
static class Program
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../../src/Sentry/Sentry.csproj" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions src/Sentry/JsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Sentry
{
internal static class JsonSerializer
{
private static readonly StringEnumConverter StringEnumConverter = new StringEnumConverter();

public static string SerializeObject<T>(T @object) => JsonConvert.SerializeObject(@object, StringEnumConverter);
}
}
4 changes: 2 additions & 2 deletions src/Sentry/Sentry.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="Protocol\Context\" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>

</Project>
55 changes: 55 additions & 0 deletions test/Sentry.Tests/Protocol/Contexts/AppTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using Sentry.Protocol;
using Xunit;

namespace Sentry.Tests.Protocol.Contexts
{
public class AppTests
{
[Fact]
public void SerializeObject_AllPropertiesSetToNonDefault_SerializesValidObject()
{
var sut = new App
{
Version = "8b03fd7",
Build = "1.23152",
BuildType = "nightly",
Hash = "93fd0e9a",
Name = "Sentry.Test.App",
StartTime = DateTimeOffset.MaxValue
};

var actual = JsonSerializer.SerializeObject(sut);

Assert.Equal("{\"app_start_time\":\"9999-12-31T23:59:59.9999999+00:00\","
+ "\"device_app_hash\":\"93fd0e9a\","
+ "\"build_type\":\"nightly\","
+ "\"app_name\":\"Sentry.Test.App\","
+ "\"app_version\":\"8b03fd7\","
+ "\"app_build\":\"1.23152\"}",
actual);
}

[Theory]
[MemberData(nameof(TestCases))]
public void SerializeObject_TestCase_SerializesAsExpected((App app, string serialized) @case)
{
var actual = JsonSerializer.SerializeObject(@case.app);

Assert.Equal(@case.serialized, actual);
}

public static IEnumerable<object[]> TestCases()
{
yield return new object[] { (new App(), "{}") };
yield return new object[] { (new App { Name = "some name" }, "{\"app_name\":\"some name\"}") };
yield return new object[] { (new App { Build = "some build" }, "{\"app_build\":\"some build\"}") };
yield return new object[] { (new App { BuildType = "some build type" }, "{\"build_type\":\"some build type\"}") };
yield return new object[] { (new App { Hash = "some hash" }, "{\"device_app_hash\":\"some hash\"}") };
yield return new object[] { (new App { StartTime = DateTimeOffset.MaxValue }, "{\"app_start_time\":\"9999-12-31T23:59:59.9999999+00:00\"}") };
yield return new object[] { (new App { Version = "some version" }, "{\"app_version\":\"some version\"}") };
yield return new object[] { (new App { Identifier = "some identifier" }, "{\"app_identifier\":\"some identifier\"}") };
}
}
}
39 changes: 39 additions & 0 deletions test/Sentry.Tests/Protocol/Contexts/BrowserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using Sentry.Protocol;
using Xunit;

namespace Sentry.Tests.Protocol.Contexts
{
public class BrowserTests
{
[Fact]
public void SerializeObject_AllPropertiesSetToNonDefault_SerializesValidObject()
{
var sut = new Browser
{
Version = "6",
Name = "Internet Explorer",
};

var actual = JsonSerializer.SerializeObject(sut);

Assert.Equal("{\"name\":\"Internet Explorer\",\"version\":\"6\"}", actual);
}

[Theory]
[MemberData(nameof(TestCases))]
public void SerializeObject_TestCase_SerializesAsExpected((Browser browser, string serialized) @case)
{
var actual = JsonSerializer.SerializeObject(@case.browser);

Assert.Equal(@case.serialized, actual);
}

public static IEnumerable<object[]> TestCases()
{
yield return new object[] { (new Browser(), "{}") };
yield return new object[] { (new Browser { Name = "some name" }, "{\"name\":\"some name\"}") };
yield return new object[] { (new Browser { Version = "some version" }, "{\"version\":\"some version\"}") };
}
}
}
77 changes: 77 additions & 0 deletions test/Sentry.Tests/Protocol/Contexts/ContextsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Xunit;

namespace Sentry.Tests.Protocol.Contexts
{
public class ContextsTests
{
[Fact]
public void SerializeObject_NoPropertyFilled_SerializesEmptyObject()
{
var sut = new Sentry.Protocol.Contexts();

var actual = JsonSerializer.SerializeObject(sut);

Assert.Equal("{}", actual);
}

[Fact]
public void SerializeObject_SingleDevicePropertySet_SerializeSingleProperty()
{
var sut = new Sentry.Protocol.Contexts();
sut.Device.Architecture = "x86";
var actual = JsonSerializer.SerializeObject(sut);

Assert.Equal("{\"device\":{\"arch\":\"x86\"}}", actual);
}

[Fact]
public void SerializeObject_SingleAppPropertySet_SerializeSingleProperty()
{
var sut = new Sentry.Protocol.Contexts();
sut.App.Name = "My.App";
var actual = JsonSerializer.SerializeObject(sut);

Assert.Equal("{\"app\":{\"app_name\":\"My.App\"}}", actual);
}

[Fact]
public void SerializeObject_SingleOsPropertySet_SerializeSingleProperty()
{
var sut = new Sentry.Protocol.Contexts();
sut.OperatingSystem.Version = "1.1.1.100";
var actual = JsonSerializer.SerializeObject(sut);

Assert.Equal("{\"os\":{\"version\":\"1.1.1.100\"}}", actual);
}

[Fact]
public void SerializeObject_SingleRuntimePropertySet_SerializeSingleProperty()
{
var sut = new Sentry.Protocol.Contexts();
sut.Runtime.Version = "2.1.1.100";
var actual = JsonSerializer.SerializeObject(sut);

Assert.Equal("{\"runtime\":{\"version\":\"2.1.1.100\"}}", actual);
}

[Fact]
public void Ctor_SingleBrowserPropertySet_SerializeSingleProperty()
{
var contexts = new Sentry.Protocol.Contexts();
contexts.Browser.Name = "Netscape 1";
var actual = JsonSerializer.SerializeObject(contexts);

Assert.Equal("{\"browser\":{\"name\":\"Netscape 1\"}}", actual);
}

[Fact]
public void Ctor_SingleOperatingSystemPropertySet_SerializeSingleProperty()
{
var contexts = new Sentry.Protocol.Contexts();
contexts.OperatingSystem.Name = "BeOS 1";
var actual = JsonSerializer.SerializeObject(contexts);

Assert.Equal("{\"os\":{\"name\":\"BeOS 1\"}}", actual);
}
}
}
97 changes: 97 additions & 0 deletions test/Sentry.Tests/Protocol/Contexts/DeviceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using Sentry.Protocol;
using Xunit;

namespace Sentry.Tests.Protocol.Contexts
{
public class DeviceTests
{
[Fact]
public void Ctor_NoPropertyFilled_SerializesEmptyObject()
{
var sut = new Device();

var actual = JsonSerializer.SerializeObject(sut);

Assert.Equal("{}", actual);
}

[Fact]
public void SerializeObject_AllPropertiesSetToNonDefault_SerializesValidObject()
{
var sut = new Device
{
Name = "testing.sentry.io",
Architecture = "x64",
BatteryLevel = 99,
BootTime = DateTimeOffset.MaxValue,
ExternalFreeStorage = 100_000_000_000_000, // 100 TB
ExternalStorageSize = 1_000_000_000_000_000, // 1 PB
Family = "Windows",
FreeMemory = 200_000_000_000, // 200 GB
MemorySize = 500_000_000_000, // 500 GB
StorageSize = 100_000_000,
FreeStorage = 0,
Model = "Windows Server 2012 R2",
ModelId = "0921309128012",
Orientation = DeviceOrientation.Portrait,
Simulator = false,
Timezone = TimeZoneInfo.Local,
UsableMemory = 100
};

var actual = JsonSerializer.SerializeObject(sut);

Assert.Equal($"{{\"timezone\":\"{TimeZoneInfo.Local.Id}\"," +
"\"name\":\"testing.sentry.io\"," +
"\"family\":\"Windows\"," +
"\"model\":\"Windows Server 2012 R2\"," +
"\"model_id\":\"0921309128012\"," +
"\"arch\":\"x64\"," +
"\"battery_level\":99," +
"\"orientation\":\"portrait\"," +
"\"simulator\":false," +
"\"memory_size\":500000000000," +
"\"free_memory\":200000000000," +
"\"usable_memory\":100," +
"\"storage_size\":100000000," +
"\"free_storage\":0," +
"\"external_storage_size\":1000000000000000," +
"\"external_free_storage\":100000000000000," +
"\"boot_time\":\"9999-12-31T23:59:59.9999999+00:00\"}",
actual);
}

[Theory]
[MemberData(nameof(TestCases))]
public void SerializeObject_TestCase_SerializesAsExpected((Device device, string serialized) @case)
{
var actual = JsonSerializer.SerializeObject(@case.device);

Assert.Equal(@case.serialized, actual);
}

public static IEnumerable<object[]> TestCases()
{
yield return new object[] { (new Device(), "{}") };
yield return new object[] { (new Device { Name = "some name" }, "{\"name\":\"some name\"}") };
yield return new object[] { (new Device { Orientation = DeviceOrientation.Landscape }, "{\"orientation\":\"landscape\"}") };
yield return new object[] { (new Device { Family = "some family" }, "{\"family\":\"some family\"}") };
yield return new object[] { (new Device { Model = "some model" }, "{\"model\":\"some model\"}") };
yield return new object[] { (new Device { ModelId = "some model id" }, "{\"model_id\":\"some model id\"}") };
yield return new object[] { (new Device { Architecture = "some arch" }, "{\"arch\":\"some arch\"}") };
yield return new object[] { (new Device { BatteryLevel = 1 }, "{\"battery_level\":1}") };
yield return new object[] { (new Device { Simulator = false }, "{\"simulator\":false}") };
yield return new object[] { (new Device { MemorySize = 1 }, "{\"memory_size\":1}") };
yield return new object[] { (new Device { FreeMemory = 1 }, "{\"free_memory\":1}") };
yield return new object[] { (new Device { UsableMemory = 1 }, "{\"usable_memory\":1}") };
yield return new object[] { (new Device { StorageSize = 1 }, "{\"storage_size\":1}") };
yield return new object[] { (new Device { FreeStorage = 1 }, "{\"free_storage\":1}") };
yield return new object[] { (new Device { ExternalStorageSize = 1 }, "{\"external_storage_size\":1}") };
yield return new object[] { (new Device { ExternalFreeStorage = 1 }, "{\"external_free_storage\":1}") };
yield return new object[] { (new Device { BootTime = DateTimeOffset.MaxValue }, "{\"boot_time\":\"9999-12-31T23:59:59.9999999+00:00\"}") };
yield return new object[] { (new Device { Timezone = TimeZoneInfo.Utc }, "{\"timezone\":\"UTC\"}") };
}
}
}
52 changes: 52 additions & 0 deletions test/Sentry.Tests/Protocol/Contexts/OperatingSystemTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Collections.Generic;
using Sentry.Protocol;
using Xunit;

namespace Sentry.Tests.Protocol.Contexts
{
public class OperatingSystemTests
{
[Fact]
public void SerializeObject_AllPropertiesSetToNonDefault_SerializesValidObject()
{
var sut = new OperatingSystem
{
Name = "Windows",
KernelVersion = "who knows",
Version = "2016",
RawDescription = "Windows 2016",
Build = "14393",
Rooted = true
};

var actual = JsonSerializer.SerializeObject(sut);

Assert.Equal("{\"name\":\"Windows\","
+ "\"version\":\"2016\","
+ "\"raw_description\":\"Windows 2016\","
+ "\"build\":\"14393\","
+ "\"kernel_version\":\"who knows\","
+ "\"rooted\":true}",
actual);
}

[Theory]
[MemberData(nameof(TestCases))]
public void SerializeObject_TestCase_SerializesAsExpected((OperatingSystem os, string serialized) @case)
{
var actual = JsonSerializer.SerializeObject(@case.os);

Assert.Equal(@case.serialized, actual);
}

public static IEnumerable<object[]> TestCases()
{
yield return new object[] { (new OperatingSystem(), "{}") };
yield return new object[] { (new OperatingSystem { Name = "some name" }, "{\"name\":\"some name\"}") };
yield return new object[] { (new OperatingSystem { RawDescription = "some Name, some version" }, "{\"raw_description\":\"some Name, some version\"}") };
yield return new object[] { (new OperatingSystem { Build = "some build" }, "{\"build\":\"some build\"}") };
yield return new object[] { (new OperatingSystem { KernelVersion = "some kernel version" }, "{\"kernel_version\":\"some kernel version\"}") };
yield return new object[] { (new OperatingSystem { Rooted = false }, "{\"rooted\":false}") };
}
}
}
Loading

0 comments on commit 8355dd8

Please sign in to comment.