Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Commit 2617550

Browse files
committed
Merge pull request #37 from asbjornu/unit-testing
Unit testing
2 parents 9cc49f1 + 6a8705a commit 2617550

33 files changed

+1861
-603
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ obj/
88
*.manifest
99
.DS_Store
1010
*.suo
11+
/packages/
12+
!/packages/repositories.config

.nuget/NuGet.Config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<solution>
4+
<add key="disableSourceControlIntegration" value="true" />
5+
</solution>
6+
</configuration>

.nuget/NuGet.exe

1.54 MB
Binary file not shown.

.nuget/NuGet.targets

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>
5+
6+
<!-- Enable the restore command to run before builds -->
7+
<RestorePackages Condition=" '$(RestorePackages)' == '' ">false</RestorePackages>
8+
9+
<!-- Property that enables building a package from a project -->
10+
<BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>
11+
12+
<!-- Determines if package restore consent is required to restore packages -->
13+
<RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>
14+
15+
<!-- Download NuGet.exe if it does not already exist -->
16+
<DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
17+
</PropertyGroup>
18+
19+
<ItemGroup Condition=" '$(PackageSources)' == '' ">
20+
<!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
21+
<!-- The official NuGet package source (https://www.nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
22+
<!--
23+
<PackageSource Include="https://www.nuget.org/api/v2/" />
24+
<PackageSource Include="https://my-nuget-source/nuget/" />
25+
-->
26+
</ItemGroup>
27+
28+
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
29+
<!-- Windows specific commands -->
30+
<NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
31+
<PackagesConfig>$([System.IO.Path]::Combine($(ProjectDir), "packages.config"))</PackagesConfig>
32+
</PropertyGroup>
33+
34+
<PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
35+
<!-- We need to launch nuget.exe with the mono command if we're not on windows -->
36+
<NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
37+
<PackagesConfig>packages.config</PackagesConfig>
38+
</PropertyGroup>
39+
40+
<PropertyGroup>
41+
<!-- NuGet command -->
42+
<NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\NuGet.exe</NuGetExePath>
43+
<PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>
44+
45+
<NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
46+
<NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 $(NuGetExePath)</NuGetCommand>
47+
48+
<PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>
49+
50+
<RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
51+
<NonInteractiveSwitch Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' ">-NonInteractive</NonInteractiveSwitch>
52+
53+
<PaddedSolutionDir Condition=" '$(OS)' == 'Windows_NT'">"$(SolutionDir) "</PaddedSolutionDir>
54+
<PaddedSolutionDir Condition=" '$(OS)' != 'Windows_NT' ">"$(SolutionDir)"</PaddedSolutionDir>
55+
56+
<!-- Commands -->
57+
<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)</RestoreCommand>
58+
<BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand>
59+
60+
<!-- We need to ensure packages are restored prior to assembly resolve -->
61+
<BuildDependsOn Condition="$(RestorePackages) == 'true'">
62+
RestorePackages;
63+
$(BuildDependsOn);
64+
</BuildDependsOn>
65+
66+
<!-- Make the build depend on restore packages -->
67+
<BuildDependsOn Condition="$(BuildPackage) == 'true'">
68+
$(BuildDependsOn);
69+
BuildPackage;
70+
</BuildDependsOn>
71+
</PropertyGroup>
72+
73+
<Target Name="CheckPrerequisites">
74+
<!-- Raise an error if we're unable to locate nuget.exe -->
75+
<Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
76+
<!--
77+
Take advantage of MsBuild's build dependency tracking to make sure that we only ever download nuget.exe once.
78+
This effectively acts as a lock that makes sure that the download operation will only happen once and all
79+
parallel builds will have to wait for it to complete.
80+
-->
81+
<MsBuild Targets="_DownloadNuGet" Projects="$(MSBuildThisFileFullPath)" Properties="Configuration=NOT_IMPORTANT;DownloadNuGetExe=$(DownloadNuGetExe)" />
82+
</Target>
83+
84+
<Target Name="_DownloadNuGet">
85+
<DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
86+
</Target>
87+
88+
<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
89+
<Exec Command="$(RestoreCommand)"
90+
Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />
91+
92+
<Exec Command="$(RestoreCommand)"
93+
LogStandardErrorAsError="true"
94+
Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
95+
</Target>
96+
97+
<Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
98+
<Exec Command="$(BuildCommand)"
99+
Condition=" '$(OS)' != 'Windows_NT' " />
100+
101+
<Exec Command="$(BuildCommand)"
102+
LogStandardErrorAsError="true"
103+
Condition=" '$(OS)' == 'Windows_NT' " />
104+
</Target>
105+
106+
<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
107+
<ParameterGroup>
108+
<OutputFilename ParameterType="System.String" Required="true" />
109+
</ParameterGroup>
110+
<Task>
111+
<Reference Include="System.Core" />
112+
<Using Namespace="System" />
113+
<Using Namespace="System.IO" />
114+
<Using Namespace="System.Net" />
115+
<Using Namespace="Microsoft.Build.Framework" />
116+
<Using Namespace="Microsoft.Build.Utilities" />
117+
<Code Type="Fragment" Language="cs">
118+
<![CDATA[
119+
try {
120+
OutputFilename = Path.GetFullPath(OutputFilename);
121+
122+
Log.LogMessage("Downloading latest version of NuGet.exe...");
123+
WebClient webClient = new WebClient();
124+
webClient.DownloadFile("https://www.nuget.org/nuget.exe", OutputFilename);
125+
126+
return true;
127+
}
128+
catch (Exception ex) {
129+
Log.LogErrorFromException(ex);
130+
return false;
131+
}
132+
]]>
133+
</Code>
134+
</Task>
135+
</UsingTask>
136+
</Project>

SharpRaven.CaptureTest/Program.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ static void setup()
2424
ravenClient.Logger = "C#";
2525
ravenClient.LogScrubber = new Logging.LogScrubber();
2626

27-
PrintInfo("Sentry Uri: " + ravenClient.CurrentDSN.SentryURI);
28-
PrintInfo("Port: " + ravenClient.CurrentDSN.Port);
29-
PrintInfo("Public Key: " + ravenClient.CurrentDSN.PublicKey);
30-
PrintInfo("Private Key: " + ravenClient.CurrentDSN.PrivateKey);
31-
PrintInfo("Project ID: " + ravenClient.CurrentDSN.ProjectID);
27+
PrintInfo("Sentry Uri: " + ravenClient.CurrentDsn.SentryUri);
28+
PrintInfo("Port: " + ravenClient.CurrentDsn.Port);
29+
PrintInfo("Public Key: " + ravenClient.CurrentDsn.PublicKey);
30+
PrintInfo("Private Key: " + ravenClient.CurrentDsn.PrivateKey);
31+
PrintInfo("Project ID: " + ravenClient.CurrentDsn.ProjectID);
3232
}
3333

3434
static void testWithoutStacktrace()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("SharpRaven.UnitTests")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("SharpRaven.UnitTests")]
13+
[assembly: AssemblyCopyright("Copyright © 2013")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("cf1c285c-42e3-46f3-a8b1-9339c3f1f8ae")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Reflection;
6+
7+
using Newtonsoft.Json;
8+
using Newtonsoft.Json.Linq;
9+
using Newtonsoft.Json.Schema;
10+
11+
namespace SharpRaven.UnitTests
12+
{
13+
public static class SchemaHelper
14+
{
15+
public static string GetSchemaPath()
16+
{
17+
var directory = new DirectoryInfo(Environment.CurrentDirectory);
18+
FileInfo file = null;
19+
20+
while (directory != null && directory.Exists &&
21+
(file = directory.EnumerateFiles("*.json")
22+
.FirstOrDefault(f => f.FullName.EndsWith("schema.json"))) == null)
23+
{
24+
directory = directory.Parent;
25+
}
26+
27+
return file != null ? file.FullName : null;
28+
}
29+
30+
31+
public static JsonSchema GetSchema()
32+
{
33+
var stream = typeof(SerializationTests).Assembly
34+
.GetManifestResourceStream(typeof(SerializationTests),
35+
"schema.json");
36+
37+
if (stream == null)
38+
{
39+
return null;
40+
}
41+
42+
using (StreamReader reader = new StreamReader(stream))
43+
{
44+
return JsonSchema.Parse(reader.ReadToEnd());
45+
}
46+
}
47+
48+
49+
public static JsonSchema GenerateSchema(Type type)
50+
{
51+
var schemaGenerator = new JsonSchemaGenerator();
52+
var schema = schemaGenerator.Generate(type);
53+
schema.Title = type.Name;
54+
return schema.MapSchemaTypes(type);
55+
}
56+
57+
58+
private static PropertyInfo GetProperty(Type type, KeyValuePair<string, JsonSchema> jsProperty)
59+
{
60+
if (type.IsArray)
61+
// Unwrap the type contained in an array.
62+
type = type.GetElementType();
63+
64+
foreach (var p in type.GetProperties())
65+
{
66+
var jsonPropertyAttribute = p.GetCustomAttribute<JsonPropertyAttribute>();
67+
68+
if ((jsonPropertyAttribute == null || jsonPropertyAttribute.PropertyName != jsProperty.Key) &&
69+
(p.Name != jsProperty.Key))
70+
continue;
71+
72+
if (jsonPropertyAttribute != null && jsonPropertyAttribute.Required == Required.Always)
73+
jsProperty.Value.Required = true;
74+
75+
return p;
76+
}
77+
78+
throw new KeyNotFoundException("Property not found: " + jsProperty.Key);
79+
}
80+
81+
82+
/// <summary>
83+
/// Maps types from the specified <paramref name="type"/> to the <paramref name="schema"/> and
84+
/// returns the modified schema.
85+
/// </summary>
86+
/// <param name="schema">The schema.</param>
87+
/// <param name="type">The type.</param>
88+
/// <returns>
89+
/// The modified schema.
90+
/// </returns>
91+
/// <remarks>
92+
/// Shamefully stolen from <a href="https://json.codeplex.com/discussions/245604">CodePlex</a>.
93+
/// </remarks>
94+
/// <exception cref="System.ArgumentNullException">
95+
/// schema
96+
/// or
97+
/// type
98+
/// </exception>
99+
private static JsonSchema MapSchemaTypes(this JsonSchema schema, Type type)
100+
{
101+
if (schema == null)
102+
throw new ArgumentNullException("schema");
103+
104+
if (type == null)
105+
throw new ArgumentNullException("type");
106+
107+
if (schema.Properties == null)
108+
return schema;
109+
110+
foreach (var jsProperty in schema.Properties)
111+
{
112+
PropertyInfo property = GetProperty(type, jsProperty);
113+
Type propertyType = property.PropertyType;
114+
115+
if (propertyType.IsGenericType
116+
&& propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
117+
{
118+
Type underlyingType = Nullable.GetUnderlyingType(propertyType);
119+
120+
if (underlyingType == typeof(DateTime))
121+
jsProperty.Value.Format = "date-time";
122+
else if (underlyingType.BaseType == typeof(Enum))
123+
{
124+
jsProperty.Value.Type = JsonSchemaType.String;
125+
jsProperty.Value.Enum = new JArray(GetEnumValues(underlyingType, property));
126+
}
127+
}
128+
else if (propertyType == typeof(DateTime))
129+
{
130+
jsProperty.Value.Format = "date-time";
131+
}
132+
else if (propertyType.BaseType == typeof(Enum))
133+
{
134+
jsProperty.Value.Type = JsonSchemaType.String;
135+
jsProperty.Value.Enum = new JArray(GetEnumValues(propertyType, property));
136+
}
137+
else if (jsProperty.Value.Items != null && jsProperty.Value.Items.Any())
138+
{
139+
foreach (var item in jsProperty.Value.Items)
140+
{
141+
var arg = propertyType.GetGenericArguments();
142+
if (arg.Any())
143+
propertyType = arg[0];
144+
145+
item.MapSchemaTypes(propertyType);
146+
}
147+
}
148+
else if (jsProperty.Value.Properties != null && jsProperty.Value.Properties.Any())
149+
{
150+
jsProperty.Value.MapSchemaTypes(propertyType);
151+
}
152+
}
153+
154+
return schema;
155+
}
156+
157+
158+
private static IEnumerable<object> GetEnumValues(Type enumType, PropertyInfo property)
159+
{
160+
var converterAttribute = property.GetCustomAttribute<JsonConverterAttribute>();
161+
162+
if (converterAttribute != null)
163+
{
164+
var converter = (JsonConverter) Activator.CreateInstance(converterAttribute.ConverterType);
165+
if (converter.CanConvert(enumType))
166+
{
167+
foreach (var value in Enum.GetValues(enumType))
168+
{
169+
using (var stringWriter = new StringWriter())
170+
{
171+
using (var jsonWriter = new JsonTextWriter(stringWriter))
172+
{
173+
converter.WriteJson(jsonWriter, value, new JsonSerializer());
174+
}
175+
176+
yield return stringWriter.ToString().Replace("\"", String.Empty);
177+
;
178+
}
179+
}
180+
}
181+
}
182+
else
183+
foreach (var name in Enum.GetNames(enumType))
184+
yield return name;
185+
}
186+
}
187+
}

0 commit comments

Comments
 (0)