Skip to content

Commit edd1131

Browse files
committed
Add and update test cases for script
1 parent 101f6b2 commit edd1131

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+3410
-207
lines changed

JSchema.Tests/RelogicLabs/JSchema/Tests/External/ExternalErrorTest.cs renamed to JSchema.Tests/RelogicLabs/JSchema/Tests/External/ExternalFunctionErrors.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using RelogicLabs.JsonSchema.Functions;
2-
using RelogicLabs.JsonSchema.Tree;
3-
using RelogicLabs.JsonSchema.Types;
1+
using RelogicLabs.JSchema.Functions;
2+
using RelogicLabs.JSchema.Tree;
3+
using RelogicLabs.JSchema.Nodes;
44

55
// Functions for negative (error) test cases
6-
namespace RelogicLabs.JsonSchema.Tests.External;
6+
namespace RelogicLabs.JSchema.Tests.External;
77

88
public class ExternalFunctions1
99
{
@@ -15,7 +15,7 @@ public bool Odd(JNumber target)
1515
}
1616
}
1717

18-
public class ExternalFunctions2 : FunctionBase
18+
public class ExternalFunctions2 : FunctionProvider
1919
{
2020
public ExternalFunctions2(RuntimeContext runtime) : base(runtime) { }
2121

@@ -26,25 +26,25 @@ public void Odd(JNumber target)
2626
}
2727
}
2828

29-
public class ExternalFunctions3 : FunctionBase
29+
public class ExternalFunctions3 : FunctionProvider
3030
{
3131
public ExternalFunctions3(RuntimeContext runtime) : base(runtime) { }
3232

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

36-
public class ExternalFunctions4 : FunctionBase
36+
public class ExternalFunctions4 : FunctionProvider
3737
{
3838
public ExternalFunctions4(RuntimeContext runtime) : base(runtime) { }
3939

4040
public bool CanTest(JNumber target)
4141
{
4242
// If you just want to throw any exception without details
43-
return FailWith(new Exception("something went wrong"));
43+
return Fail(new Exception("something went wrong"));
4444
}
4545
}
4646

47-
public class ExternalFunctions5 : FunctionBase
47+
public class ExternalFunctions5 : FunctionProvider
4848
{
4949
public ExternalFunctions5() : base(null!) { }
5050
}

JSchema.Tests/RelogicLabs/JSchema/Tests/External/ExternalFunctions.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using RelogicLabs.JsonSchema.Exceptions;
2-
using RelogicLabs.JsonSchema.Functions;
3-
using RelogicLabs.JsonSchema.Message;
4-
using RelogicLabs.JsonSchema.Tree;
5-
using RelogicLabs.JsonSchema.Types;
6-
using RelogicLabs.JsonSchema.Utilities;
1+
using RelogicLabs.JSchema.Exceptions;
2+
using RelogicLabs.JSchema.Functions;
3+
using RelogicLabs.JSchema.Message;
4+
using RelogicLabs.JSchema.Tree;
5+
using RelogicLabs.JSchema.Nodes;
6+
using RelogicLabs.JSchema.Utilities;
77

88
// Functions for positive (valid) test cases
9-
namespace RelogicLabs.JsonSchema.Tests.External;
9+
namespace RelogicLabs.JSchema.Tests.External;
1010

11-
public class ExternalFunctions : FunctionBase
11+
public class ExternalFunctions : FunctionProvider
1212
{
1313
public const string EVENFUNC01 = "EVENFUNC01";
1414
public const string ERRACCESS01 = "ERRACCESS01";
@@ -22,9 +22,9 @@ public ExternalFunctions(RuntimeContext runtime) : base(runtime) { }
2222
public bool Even(JNumber target)
2323
{
2424
bool result = target % 2 == 0;
25-
if(!result) return FailWith(new JsonSchemaException(
25+
if(!result) return Fail(new JsonSchemaException(
2626
new ErrorDetail(EVENFUNC01, "Number is not even"),
27-
new ExpectedDetail(Function, "even number"),
27+
new ExpectedDetail(Caller, "even number"),
2828
new ActualDetail(target, $"number {target} is odd")));
2929
return true;
3030
}
@@ -41,9 +41,9 @@ public bool CanTest(JNumber target, JString str1, JBoolean bool1, params JNumber
4141
public bool CheckAccess(JInteger target, JReceiver userRole)
4242
{
4343
string role = userRole.GetValueNode<JString>();
44-
if(role == "user" && target > 5) return FailWith(new JsonSchemaException(
44+
if(role == "user" && target > 5) return Fail(new JsonSchemaException(
4545
new ErrorDetail(ERRACCESS01, "Data access incompatible with 'user' role"),
46-
new ExpectedDetail(Function, "an access at most 5 for 'user' role"),
46+
new ExpectedDetail(Caller, "an access at most 5 for 'user' role"),
4747
new ActualDetail(target, $"found access {target} which is greater than 5")));
4848
return true;
4949
}
@@ -53,9 +53,9 @@ public bool Condition(JInteger target, JReceiver receiver)
5353
var threshold = receiver.GetValueNode<JInteger>();
5454
Console.WriteLine("Received threshold: " + threshold);
5555
bool result = threshold < target;
56-
if(!result) return FailWith(new JsonSchemaException(
56+
if(!result) return Fail(new JsonSchemaException(
5757
new ErrorDetail(CONDFUNC01, "Number does not satisfy the condition"),
58-
new ExpectedDetail(Function, $"a number > {threshold} of '{receiver.Name}'"),
58+
new ExpectedDetail(Caller, $"a number > {threshold} of '{receiver.Name}'"),
5959
new ActualDetail(target, $"found number {target} <= {threshold}")));
6060
return result;
6161
}
@@ -67,17 +67,17 @@ public bool ConditionAll(JInteger target, JReceiver receiver)
6767
Console.WriteLine("Target: " + target);
6868
Console.WriteLine("Received integers: " + values);
6969
bool result = list.All(i => i < target);
70-
if(!result) return FailWith(new JsonSchemaException(
70+
if(!result) return Fail(new JsonSchemaException(
7171
new ErrorDetail(CONDFUNC02, "Number does not satisfy the condition"),
72-
new ExpectedDetail(Function, $"a number > any of {values} of '{receiver.Name}'"),
72+
new ExpectedDetail(Caller, $"a number > any of {values} of '{receiver.Name}'"),
7373
new ActualDetail(target, $"found number {target} <= some of {values}")));
7474
return true;
7575
}
7676

77-
public FutureValidator SumEqual(JInteger target, JReceiver receiver)
77+
public FutureFunction SumEqual(JInteger target, JReceiver receiver)
7878
{
79-
// Capture the current value of the function for future lambda
80-
var caller = Function;
79+
// Capture the current value of the caller
80+
var current = Caller;
8181
return () =>
8282
{
8383
var values = receiver.GetValueNodes<JInteger>();
@@ -86,18 +86,18 @@ public FutureValidator SumEqual(JInteger target, JReceiver receiver)
8686
Console.WriteLine("Received values: " + expression);
8787
int result = values.Sum(i => (int) i);
8888
if(result != target)
89-
return FailWith(new JsonSchemaException(
89+
return Fail(new JsonSchemaException(
9090
new ErrorDetail(SUMEQUAL01, $"Number != sum of {expression} = {result}"),
91-
new ExpectedDetail(caller, $"a number = sum of numbers {result}"),
91+
new ExpectedDetail(current, $"a number = sum of numbers {result}"),
9292
new ActualDetail(target, $"found number {target} != {result}")));
9393
return true;
9494
};
9595
}
9696

97-
public FutureValidator Minmax(JInteger target, JReceiver min, JReceiver max)
97+
public FutureFunction Minmax(JInteger target, JReceiver min, JReceiver max)
9898
{
99-
// Capture the current value of the function for future lambda
100-
var caller = Function;
99+
// Capture the current value of the caller
100+
var current = Caller;
101101
return () =>
102102
{
103103
var intMin = min.GetValueNode<JInteger>();
@@ -106,9 +106,9 @@ public FutureValidator Minmax(JInteger target, JReceiver min, JReceiver max)
106106
Console.WriteLine($"Received min: {intMin}, max: {intMax}");
107107
bool result = target >= intMin && target <= intMax;
108108
if(!result)
109-
return FailWith(new JsonSchemaException(
109+
return Fail(new JsonSchemaException(
110110
new ErrorDetail(MINMAX01, "Number is outside of range"),
111-
new ExpectedDetail(caller, $"a number in range [{intMin}, {intMax}]"),
111+
new ExpectedDetail(current, $"a number in range [{intMin}, {intMax}]"),
112112
new ActualDetail(target, $"found number {target} not in range")));
113113
return true;
114114
};

0 commit comments

Comments
 (0)