Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for RuleDictionaryConverter #270

Merged
1 commit merged into from May 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/Sarif.UnitTests/JsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,47 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

using System.IO;
using System.Text;
using Microsoft.CodeAnalysis.Sarif.Readers;

using Microsoft.CodeAnalysis.Sarif.Writers;
using Newtonsoft.Json;

namespace Microsoft.CodeAnalysis.Sarif
{
public class JsonTests
public abstract class JsonTests
{
public JsonTests()
private const SarifVersion CurrentVersion = SarifVersion.OneZeroZeroBetaFive;

protected static readonly string SarifSchemaUri =
SarifUtilities.ConvertToSchemaUri(CurrentVersion).OriginalString;

protected static readonly string SarifFormatVersion =
SarifUtilities.ConvertToText(CurrentVersion);

protected static readonly Run DefaultRun = new Run();
protected static readonly Tool DefaultTool = new Tool();
protected static readonly Result DefaultResult = new Result();

protected JsonTests()
{
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
ContractResolver = SarifContractResolver.Instance
};
}

protected static string GetJson(Action<ResultLogJsonWriter> testContent)
{
StringBuilder result = new StringBuilder();
using (var str = new StringWriter(result))
using (var json = new JsonTextWriter(str) { Formatting = Formatting.Indented })
using (var uut = new ResultLogJsonWriter(json))
{
testContent(uut);
}

return result.ToString();
}
}
}
54 changes: 28 additions & 26 deletions src/Sarif.UnitTests/Readers/AlgorithmKindConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using Microsoft.CodeAnalysis.Sarif.Writers;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Newtonsoft.Json;
Expand All @@ -17,23 +13,6 @@ namespace Microsoft.CodeAnalysis.Sarif.Readers
[TestClass]
public class AlgorithmKindConverterTests : JsonTests
{
private static readonly Run s_defaultRun = new Run();
private static readonly Tool s_defaultTool = new Tool();
private static readonly Result s_defaultResult = new Result();

private static string GetJson(Action<ResultLogJsonWriter> testContent)
{
StringBuilder result = new StringBuilder();
using (var str = new StringWriter(result))
using (var json = new JsonTextWriter(str))
using (var uut = new ResultLogJsonWriter(json))
{
testContent(uut);
}

return result.ToString();
}

[TestMethod]
public void AlgorithmKind_AllMembers()
{
Expand Down Expand Up @@ -81,17 +60,40 @@ public void AlgorithmKind_AllMembers()
foreach (var testTuple in testTuples)
{
string expected =
"{\"$schema\":\"http://json.schemastore.org/sarif-1.0.0-beta.5\",\"version\":\"1.0.0-beta.5\",\"runs\":[{\"tool\":{\"name\":null},\"files\":{\"http://abc/\":[{\"hashes\":[{\"value\":null,\"algorithm\":\"" +
testTuple.Item2 +
"\"}]}]},\"results\":[{}]}]}";
@"{
""$schema"": """ + SarifSchemaUri + @""",
""version"": """ + SarifFormatVersion + @""",
""runs"": [
{
""tool"": {
""name"": null
},
""files"": {
""http://abc/"": [
{
""hashes"": [
{
""value"": null,
""algorithm"": """ + testTuple.Item2 + @"""
}
]
}
]
},
""results"": [
{}
]
}
]
}";

string actual = GetJson(uut =>
{
var run = new Run();

uut.Initialize(id: null, correlationId: null);

uut.WriteTool(s_defaultTool);
uut.WriteTool(DefaultTool);

var files = new Dictionary<string, IList<FileData>>
{
Expand All @@ -112,7 +114,7 @@ public void AlgorithmKind_AllMembers()

uut.WriteFiles(files);

uut.WriteResults(new[] { s_defaultResult });
uut.WriteResults(new[] { DefaultResult });
});
Assert.AreEqual(expected, actual);

Expand Down
79 changes: 52 additions & 27 deletions src/Sarif.UnitTests/Readers/IsSuppressedInSourceConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;
using System.Text;

using Microsoft.CodeAnalysis.Sarif.Writers;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Newtonsoft.Json;
Expand All @@ -15,33 +10,33 @@ namespace Microsoft.CodeAnalysis.Sarif.Readers
[TestClass]
public class InSourceSuppressionConverterTests : JsonTests
{
private static readonly Run s_defaultRun = new Run();
private static readonly Tool s_defaultTool = new Tool();

private static string GetJson(Action<ResultLogJsonWriter> testContent)
{
StringBuilder result = new StringBuilder();
using (var str = new StringWriter(result))
using (var json = new JsonTextWriter(str))
using (var uut = new ResultLogJsonWriter(json))
{
testContent(uut);
}

return result.ToString();
}

[TestMethod]
public void SuppressionStatus_SuppressedInSource()
{
string expected = "{\"$schema\":\"http://json.schemastore.org/sarif-1.0.0-beta.5\",\"version\":\"1.0.0-beta.5\",\"runs\":[{\"tool\":{\"name\":null},\"results\":[{\"suppressionStates\":[\"suppressedInSource\"]}]}]}";
string expected =
@"{
""$schema"": """ + SarifSchemaUri + @""",
""version"": """ + SarifFormatVersion + @""",
""runs"": [
{
""tool"": {
""name"": null
},
""results"": [
{
""suppressionStates"": [""suppressedInSource""]
}
]
}
]
}";
string actual = GetJson(uut =>
{
var run = new Run();

uut.Initialize(id: null, correlationId: null);

uut.WriteTool(s_defaultTool);
uut.WriteTool(DefaultTool);

uut.WriteResults(new[] { new Result
{
Expand All @@ -58,12 +53,26 @@ public void SuppressionStatus_SuppressedInSource()
[TestMethod]
public void BaselineState_None()
{
string expected = "{\"$schema\":\"http://json.schemastore.org/sarif-1.0.0-beta.5\",\"version\":\"1.0.0-beta.5\",\"runs\":[{\"tool\":{\"name\":null},\"results\":[{}]}]}";
string expected =
@"{
""$schema"": """ + SarifSchemaUri + @""",
""version"": """ + SarifFormatVersion + @""",
""runs"": [
{
""tool"": {
""name"": null
},
""results"": [
{}
]
}
]
}";
string actual = GetJson(uut =>
{
var run = new Run();
uut.Initialize(id: null, correlationId: null);
uut.WriteTool(s_defaultTool);
uut.WriteTool(DefaultTool);

uut.WriteResults(new[] { new Result
{
Expand All @@ -81,14 +90,30 @@ public void BaselineState_None()
[TestMethod]
public void BaselineState_Existing()
{
string expected = "{\"$schema\":\"http://json.schemastore.org/sarif-1.0.0-beta.5\",\"version\":\"1.0.0-beta.5\",\"runs\":[{\"tool\":{\"name\":null},\"results\":[{\"baselineState\":\"existing\"}]}]}";
string expected =
@"{
""$schema"": """ + SarifSchemaUri + @""",
""version"": """ + SarifFormatVersion + @""",
""runs"": [
{
""tool"": {
""name"": null
},
""results"": [
{
""baselineState"": ""existing""
}
]
}
]
}";
string actual = GetJson(uut =>
{
var run = new Run();

uut.Initialize(id: null, correlationId: null);

uut.WriteTool(s_defaultTool);
uut.WriteTool(DefaultTool);

uut.WriteResults(new[] { new Result
{
Expand Down
53 changes: 53 additions & 0 deletions src/Sarif.UnitTests/Readers/RuleDictionaryConverterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;

namespace Microsoft.CodeAnalysis.Sarif.Readers
{
[TestClass]
public class RuleDictionaryConverterTests : JsonTests
{
[TestMethod]
public void RuleDictionaryConverter_WritesRule()
{
string expected =
@"{
""$schema"": ""http://json.schemastore.org/sarif-1.0.0-beta.5"",
""version"": ""1.0.0-beta.5"",
""runs"": [
{
""tool"": {
""name"": null
},
""rules"": {
""CA1000.1"": {
""id"": ""CA1000""
}
}
}
]
}";
string actual = GetJson(uut =>
{
var run = new Run();

uut.Initialize(id: null, correlationId: null);

uut.WriteTool(DefaultTool);

uut.WriteRules(new Dictionary<string, IRule>
{
["CA1000.1"] = new Rule { Id = "CA1000" }
});
});

Assert.AreEqual(expected, actual);

var sarifLog = JsonConvert.DeserializeObject<SarifLog>(actual);
Assert.AreEqual("CA1000", sarifLog.Runs[0].Rules["CA1000.1"].Id);
}
}
}
1 change: 1 addition & 0 deletions src/Sarif.UnitTests/Sarif.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
<Compile Include="Readers\IsSuppressedInSourceConverterTests.cs" />
<Compile Include="Readers\PropertyBagConverterTests.cs" />
<Compile Include="Readers\ResultDiffingVisitorTests.cs" />
<Compile Include="Readers\RuleDictionaryConverterTests.cs" />
<Compile Include="ResultLogObjectWriter.cs" />
<Compile Include="SarifExtensionsTests.cs" />
<Compile Include="SparseReaderDispatchTableTests.cs" />
Expand Down
Loading