From 31e4296a80e8c8c5c301ed1d01770088b5b78cbe Mon Sep 17 00:00:00 2001 From: Eddy Nakamura Date: Fri, 16 Apr 2021 11:42:12 -0300 Subject: [PATCH 1/2] Exposing ResultLevelKind --- src/Sarif/Core/ResultLevelKind.cs | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/Sarif/Core/ResultLevelKind.cs diff --git a/src/Sarif/Core/ResultLevelKind.cs b/src/Sarif/Core/ResultLevelKind.cs new file mode 100644 index 000000000..b681d151f --- /dev/null +++ b/src/Sarif/Core/ResultLevelKind.cs @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.CodeAnalysis.Sarif +{ + public class ResultLevelKind + { + private ResultKind? resultKind; + + public ResultKind Kind + { + get + { + // If kind is absent, it SHALL default to "fail". + if (!resultKind.HasValue) + { + return ResultKind.Fail; + } + + // If level has any value other than "none" and kind is present, then kind SHALL have the value "fail". + if (Level != FailureLevel.None) + { + return ResultKind.Fail; + } + + return resultKind.Value; + } + set { resultKind = value; } + } + + public FailureLevel Level { get; set; } + } +} From 897eb40f5838e4400d3596b82885a35a20d919fc Mon Sep 17 00:00:00 2001 From: Eddy Nakamura Date: Fri, 16 Apr 2021 18:13:31 -0300 Subject: [PATCH 2/2] Addressing PR feedback --- src/Sarif/Core/ResultLevelKind.cs | 28 +++++++++++++++-- .../Core/ResultLevelKindTests.cs | 30 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/Test.UnitTests.Sarif/Core/ResultLevelKindTests.cs diff --git a/src/Sarif/Core/ResultLevelKind.cs b/src/Sarif/Core/ResultLevelKind.cs index b681d151f..5a06fd8d3 100644 --- a/src/Sarif/Core/ResultLevelKind.cs +++ b/src/Sarif/Core/ResultLevelKind.cs @@ -6,6 +6,7 @@ namespace Microsoft.CodeAnalysis.Sarif public class ResultLevelKind { private ResultKind? resultKind; + private FailureLevel level; public ResultKind Kind { @@ -25,9 +26,32 @@ public ResultKind Kind return resultKind.Value; } - set { resultKind = value; } + set + { + if (value != ResultKind.Fail) + { + Level = FailureLevel.None; + } + + resultKind = value; + } } - public FailureLevel Level { get; set; } + public FailureLevel Level + { + get + { + return level; + } + set + { + if (value != FailureLevel.None) + { + resultKind = ResultKind.Fail; + } + + level = value; + } + } } } diff --git a/src/Test.UnitTests.Sarif/Core/ResultLevelKindTests.cs b/src/Test.UnitTests.Sarif/Core/ResultLevelKindTests.cs new file mode 100644 index 000000000..1eddcbc17 --- /dev/null +++ b/src/Test.UnitTests.Sarif/Core/ResultLevelKindTests.cs @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using FluentAssertions; + +using Microsoft.CodeAnalysis.Sarif; + +using Xunit; + +namespace Microsoft.CodeAnalysis.Test.UnitTests.Sarif.Core +{ + public class ResultLevelKindTests + { + [Fact] + public void ResultLevelKind_ValidateConditions() + { + var obj = new ResultLevelKind(); + obj.Kind.Should().Be(ResultKind.Fail); + obj.Level.Should().Be(FailureLevel.None); + + obj.Kind = ResultKind.Informational; + obj.Kind.Should().Be(ResultKind.Informational); + obj.Level.Should().Be(FailureLevel.None); + + obj.Level = FailureLevel.Warning; + obj.Kind.Should().Be(ResultKind.Fail); + obj.Level.Should().Be(FailureLevel.Warning); + } + } +}