Skip to content

Commit

Permalink
Merge pull request #15 from relogiclabs/develop
Browse files Browse the repository at this point in the history
Update Scripting Functionalities
  • Loading branch information
zhossain-info authored Apr 17, 2024
2 parents e5d0b6a + 1a22417 commit ea8304a
Show file tree
Hide file tree
Showing 361 changed files with 9,004 additions and 6,871 deletions.
6 changes: 3 additions & 3 deletions JSchema.Tests/JSchema.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<Description>This project contains test cases for JSchema project.</Description>
<Authors>Relogic Labs</Authors>
<Company>Relogic Labs</Company>
<Version>2.0.0</Version>
<AssemblyVersion>2.0.0</AssemblyVersion>
<Version>2.1.0</Version>
<AssemblyVersion>2.1.0</AssemblyVersion>
<Copyright>Copyright © Relogic Labs. All rights reserved.</Copyright>
<NeutralLanguage>en</NeutralLanguage>
<TargetFrameworks>net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
Expand All @@ -21,7 +21,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" />
<PackageReference Include="coverlet.collector" Version="6.0.1" />
<PackageReference Include="coverlet.collector" Version="6.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using RelogicLabs.JSchema.Functions;
using RelogicLabs.JSchema.Tree;
using RelogicLabs.JSchema.Nodes;

// Functions for negative (error) test cases
Expand All @@ -17,8 +16,6 @@ public bool Odd(JNumber target)

public class ExternalFunctions2 : FunctionProvider
{
public ExternalFunctions2(RuntimeContext runtime) : base(runtime) { }

public void Odd(JNumber target)
{
bool result = target % 2 != 0;
Expand All @@ -28,15 +25,11 @@ public void Odd(JNumber target)

public class ExternalFunctions3 : FunctionProvider
{
public ExternalFunctions3(RuntimeContext runtime) : base(runtime) { }

public bool Odd() => throw new InvalidOperationException();
}

public class ExternalFunctions4 : FunctionProvider
{
public ExternalFunctions4(RuntimeContext runtime) : base(runtime) { }

public bool CanTest(JNumber target)
{
// If you just want to throw any exception without details
Expand All @@ -46,5 +39,5 @@ public bool CanTest(JNumber target)

public class ExternalFunctions5 : FunctionProvider
{
public ExternalFunctions5() : base(null!) { }
public ExternalFunctions5() => throw new Exception("Error occurred");
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using RelogicLabs.JSchema.Exceptions;
using RelogicLabs.JSchema.Functions;
using RelogicLabs.JSchema.Message;
using RelogicLabs.JSchema.Tree;
using RelogicLabs.JSchema.Nodes;
using RelogicLabs.JSchema.Utilities;

Expand All @@ -17,8 +16,6 @@ public class ExternalFunctions : FunctionProvider
public const string SUMEQUAL01 = "SUMEQUAL01";
public const string MINMAX01 = "MINMAX01";

public ExternalFunctions(RuntimeContext runtime) : base(runtime) { }

public bool Even(JNumber target)
{
bool result = target % 2 == 0;
Expand Down Expand Up @@ -84,7 +81,7 @@ public FutureFunction SumEqual(JInteger target, JReceiver receiver)
var expression = values.Join("+");
Console.WriteLine("Target: " + target);
Console.WriteLine("Received values: " + expression);
int result = values.Sum(i => (int) i);
int result = values.Sum(static i => (int) i);
if(result != target)
return Fail(new JsonSchemaException(
new ErrorDetail(SUMEQUAL01, $"Number != sum of {expression} = {result}"),
Expand Down
22 changes: 12 additions & 10 deletions JSchema.Tests/RelogicLabs/JSchema/Tests/Negative/AggregatedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public void When_ExtendedAggregatedTestWithInvalidAccess_ExceptionThrown()
%version: "2.0.0-extended"
%import: RelogicLabs.JSchema.Tests.External.ExternalFunctions,
RelogicLabs.JSchema.Tests

%pragma IgnoreUndefinedProperties: true

%define $post: {
"id": @range(1, 1000) #integer,
"title": @length(10, 100) #string,
Expand All @@ -103,7 +103,7 @@ public void When_ExtendedAggregatedTestWithInvalidAccess_ExceptionThrown()
}
%define $tags: @length(1, 10) #string*($tag) #array
%define $tag: @length(3, 20) @regex("[A-Za-z_]+") #string
%schema:
%schema:
{
"user": {
"id": @range(1, 10000) #integer,
Expand Down Expand Up @@ -243,7 +243,7 @@ public void When_ExtendedAggregatedScriptTestWithInvalidAccess_ExceptionThrown()
var schema = """
%title: "Extended User Profile Dashboard API Response"
%version: "2.0.0-extended"

%pragma DateDataTypeFormat: "DD-MM-YYYY"
%pragma TimeDataTypeFormat: "DD-MM-YYYY hh:mm:ss"
%pragma IgnoreUndefinedProperties: true
Expand All @@ -254,7 +254,7 @@ public void When_ExtendedAggregatedScriptTestWithInvalidAccess_ExceptionThrown()
"content": @length(30, 1000) #string,
"tags": $tags
} #object

%define $product: {
"id": @length(2, 10) @regex("[a-z][a-z0-9]+") #string,
"name": @length(5, 30) #string,
Expand All @@ -267,11 +267,11 @@ public void When_ExtendedAggregatedScriptTestWithInvalidAccess_ExceptionThrown()
"storage": @regex("[0-9]{1,4}GB (SSD|HDD)") #string
} #object #null
}

%define $tags: @length(1, 10) #string*($tag) #array
%define $tag: @length(3, 20) @regex("[A-Za-z_]+") #string
%schema:

%schema:
{
"user": {
"id": @range(1, 10000) #integer,
Expand Down Expand Up @@ -308,10 +308,12 @@ public void When_ExtendedAggregatedScriptTestWithInvalidAccess_ExceptionThrown()
"isCloudy": #boolean
}
}

%script: {
future constraint checkAccess(role) {
if(role[0] == "user" && target > 5) return fail(
// Auto-unpacking turns the single-value '&role' array into the value itself
// 'target' keyword refers to the target JSON value
if(role == "user" && target > 5) return fail(
"ERRACCESS01", "Data access incompatible with 'user' role",
expected("an access at most 5 for 'user' role"),
actual("found access " + target + " which is greater than 5"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void When_ExternalImportInstantiationNotSuccessful_ExceptionThrown()
//JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<ClassInstantiationException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(CLAS05, exception.Code);
Assert.AreEqual(INST01, exception.Code);
Console.WriteLine(exception);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public void When_ReceiveWrongValuesInObject_ExceptionThrown()

%schema:
{
"key1": #integer &relatedData,
"key10": @sumEqual(&relatedData) #integer,
"key1": #integer &relatedData,
"key2": #integer &relatedData,
"key3": #integer &relatedData,
"key4": #integer &relatedData,
Expand All @@ -142,10 +142,10 @@ public void When_ReceiveWrongValuesInObject_ExceptionThrown()
{
"key1": 9,
"key2": 5,
"key10": 100,
"key3": 13,
"key4": 60,
"key5": 12
"key5": 12,
"key10": 100
}
""";
JsonSchema.IsValid(schema, json);
Expand Down
Loading

0 comments on commit ea8304a

Please sign in to comment.