Skip to content

Commit

Permalink
Added custom rules unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nat Van Gulck authored and Nat Van Gulck committed Oct 16, 2023
1 parent 1e3e6db commit f77660d
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 12 deletions.
8 changes: 4 additions & 4 deletions DocsExamples/Example-rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@
},
{
"name": "Give visible pages meaningful names",
"description": "Checks for visible pages with a default 'Page x' display name.",
"description": "Returns an array of visible page names with a default 'Page x' display name.",
"disabled": false,
"logType": "warning",
"path": "$.sections[*]",
Expand Down Expand Up @@ -392,8 +392,8 @@
]
},
{
"name": "Check Deneb charts properties - In progress",
"description": "Check that drillvar operator can read deneb properties",
"name": "Check Deneb charts properties - work in progress",
"description": "Checks that the drillvar custom rule can read deneb custom visual nested jsonspec properties. This is an example in progress that demonstrates the used of the drillvar custom rule but doesn't yet do anything useful.",
"disabled": true,
"logType": "error",
"path": "$.sections[*]",
Expand Down Expand Up @@ -429,7 +429,7 @@
},
{
"name": "Check for visuals overlap with a 5px margin",
"description": "Returns names of visuals that overlap.",
"description": "Returns names of visuals that overlap while inflacting visuals rectangle area by 5px left, right, top and bottom. Currently this does not check for overlap with the sides of report page itself. This rule does not currently work with visual groups.",
"forEachPath": "$.sections[*]",
"forEachPathName": "$.name",
"forEachPathDisplayName": "$.displayName",
Expand Down
2 changes: 1 addition & 1 deletion PBIXInspectorLibrary/CustomRules/CountRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CountRule : Json.Logic.Rule
{
internal Json.Logic.Rule Input { get; }

internal CountRule(Json.Logic.Rule input)
public CountRule(Json.Logic.Rule input)
{
Input = input;
}
Expand Down
2 changes: 1 addition & 1 deletion PBIXInspectorLibrary/CustomRules/StringContains.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class StringContains : Json.Logic.Rule
internal Json.Logic.Rule SearchString { get; }
internal Json.Logic.Rule ContainsString { get; }

internal StringContains(Json.Logic.Rule searchString, Json.Logic.Rule containsString)
public StringContains(Json.Logic.Rule searchString, Json.Logic.Rule containsString)
{
SearchString = searchString;
ContainsString = containsString;
Expand Down
2 changes: 1 addition & 1 deletion PBIXInspectorLibrary/CustomRules/ToString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ToString : Json.Logic.Rule
{
internal Json.Logic.Rule InputString { get; }

internal ToString(Json.Logic.Rule inputString)
public ToString(Json.Logic.Rule inputString)
{
InputString = inputString;
}
Expand Down
4 changes: 2 additions & 2 deletions PBIXInspectorLibrary/CustomRules/isNullOrEmptyRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
namespace PBIXInspectorLibrary.CustomRules
{
/// <summary>
/// Handles the `count` operation.
/// Handles the `isnullorempty` operation.
/// </summary>
[Operator("isnullorempty")]
[JsonConverter(typeof(IsNullOrEmptyJsonConverter))]
public class IsNullOrEmptyRule : Json.Logic.Rule
{
internal Json.Logic.Rule Value { get; }

internal IsNullOrEmptyRule(Json.Logic.Rule value)
public IsNullOrEmptyRule(Json.Logic.Rule value)
{
Value = value;
}
Expand Down
2 changes: 1 addition & 1 deletion PBIXInspectorLibrary/Inspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ private void AddCustomRulesToRegistry()
q.ToList().ForEach(t1 => Json.Logic.RuleRegistry.AddRule<t1>());
*/

Json.Logic.RuleRegistry.AddRule<CustomRules.CountRule>();
Json.Logic.RuleRegistry.AddRule<CustomRules.IsNullOrEmptyRule>();
Json.Logic.RuleRegistry.AddRule<CustomRules.CountRule>();
Json.Logic.RuleRegistry.AddRule<CustomRules.StringContains>();
Json.Logic.RuleRegistry.AddRule<CustomRules.ToString>();
Json.Logic.RuleRegistry.AddRule<CustomRules.ToRecordRule>();
Expand Down
80 changes: 80 additions & 0 deletions PBIXInspectorTests/CustomRulesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Text.Json.Nodes;
using PBIXInspectorLibrary.CustomRules;

namespace PBIXInspectorTests
{
[TestFixture]
public class CustomRulesTests
{
#pragma warning disable CS8602
[Test]
public void IsNullOrEmptyRuleTest()
{
var json = "";
var rule = new IsNullOrEmptyRule(json);
var result = rule.Apply(null);
Assert.That((bool)result.AsValue(), Is.True);
}

[Test]
public void IsNullOrEmptyRuleTest2()
{
string json = null;
var rule = new IsNullOrEmptyRule(json);
var result = rule.Apply(null);
Assert.That((bool)result.AsValue(), Is.True);
}

[Test]
public void IsNullOrEmptyRuleTest3()
{
string json = "Hello";
var rule = new IsNullOrEmptyRule(json);
var result = rule.Apply(null);
Assert.That((bool)result.AsValue(), Is.False);
}

[Test]
public void ToStringTest()
{
var json = "{\"message\":\"Hello, world!\"}";

var rule = new ToString(JsonNode.Parse(json));
var result = rule.Apply(null);
Assert.That(result?.ToString(), Is.EqualTo(json));
}

[Test]
public void CountRuleTest()
{
var json = "[\"a\",\"b\",\"c\"]";

var rule = new CountRule(JsonNode.Parse(json));
var result = rule.Apply(null);
JsonAssert.AreEquivalent(result, 3);
}

[Test]
public void StringContainsMatchTest()
{
var searchString = "Hello, world! More text";
var patternString = "^[a-zA-Z]+, world!";

var rule = new StringContains(searchString, patternString);
var result = rule.Apply(null);
Assert.That((int)result.AsValue(), Is.GreaterThan(0));
}

[Test]
public void StringContainsNoMatchTest()
{
var searchString = "Hello, world! More text";
var patternString = "^[a-zA-Z]+, world!$";

var rule = new StringContains(searchString, patternString);
var result = rule.Apply(null);
Assert.That((int)result.AsValue(), Is.EqualTo(0));
}
#pragma warning restore CS8602
}
}
54 changes: 54 additions & 0 deletions PBIXInspectorTests/JsonAssert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//MIT License

//Copyright (c) 2022 Greg Dennis

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

using System.Text.Json.Nodes;
using Json.More;

namespace PBIXInspectorTests
{
public static class JsonAssert
{
public static void AreEquivalent(JsonNode? expected, JsonNode? actual)
{
if (!expected.IsEquivalentTo(actual))
Assert.Fail($"Expected: {expected.AsJsonString()}\nActual: {actual.AsJsonString()}");
}

public static void IsNull(JsonNode? actual)
{
if (actual != null)
Assert.Fail($"Expected: null\nActual: {actual.AsJsonString()}");
}

public static void IsTrue(JsonNode? actual)
{
if (actual is not JsonValue value || !value.TryGetValue(out bool b) || !b)
Assert.Fail($"Expected: true\nActual: {actual.AsJsonString()}");
}

public static void IsFalse(JsonNode? actual)
{
if (actual is not JsonValue value || !value.TryGetValue(out bool b) || b)
Assert.Fail($"Expected: true\nActual: {actual.AsJsonString()}");
}
}
}
24 changes: 23 additions & 1 deletion PBIXInspectorTests/JsonLogicTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
using System.Text.Encodings.Web;
//MIT License

//Copyright (c) 2022 Greg Dennis

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
Expand Down
24 changes: 23 additions & 1 deletion PBIXInspectorTests/JsonLogicTestSuite.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
using System.Text.Json;
//MIT License

//Copyright (c) 2022 Greg Dennis

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.

using System.Text.Json;
using System.Text.Json.Serialization;


Expand Down

0 comments on commit f77660d

Please sign in to comment.