Skip to content

Commit

Permalink
Merge pull request #9 from relogiclabs/develop
Browse files Browse the repository at this point in the history
Add New Date and Time Functions
  • Loading branch information
zhossain-info authored Nov 14, 2023
2 parents ff8d2da + 3038c1d commit 0cf55af
Show file tree
Hide file tree
Showing 71 changed files with 5,714 additions and 593 deletions.
6 changes: 3 additions & 3 deletions JsonSchema.Tests/JsonSchema.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 JsonSchema project.</Description>
<Authors>Relogic Labs</Authors>
<Company>Relogic Labs</Company>
<Version>1.7.0</Version>
<AssemblyVersion>1.7.0</AssemblyVersion>
<Version>1.8.0</Version>
<AssemblyVersion>1.8.0</AssemblyVersion>
<Copyright>Copyright © Relogic Labs. All rights reserved.</Copyright>
<NeutralLanguage>en</NeutralLanguage>
<TargetFrameworks>net5.0;net6.0;net7.0</TargetFrameworks>
Expand All @@ -28,4 +28,4 @@
<ProjectReference Include="..\JsonSchema\JsonSchema.csproj" />
</ItemGroup>

</Project>
</Project>
105 changes: 105 additions & 0 deletions JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,109 @@ public void When_EmptyArrayInObject_ExceptionThrown()
Assert.AreEqual(NEMT02, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_JsonWrongLengthInObject_ExceptionThrown()
{
var schema =
"""
@length*(2) #array* #object
""";
var json =
"""
{
"key1": [10, 20],
"key2": [10]
}
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(ALEN01, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_JsonWrongMinimumLengthInObject_ExceptionThrown()
{
var schema =
"""
@length*(2, 4) #array* #object
""";
var json =
"""
{
"key1": [10, 20],
"key2": [10]
}
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(ALEN02, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_JsonWrongMaximumLengthInObject_ExceptionThrown()
{
var schema =
"""
@length*(2, 4) #array* #object
""";
var json =
"""
{
"key1": [10, 20],
"key2": [10, 20, 30, 40, 50]
}
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(ALEN03, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_JsonWrongMinimumLengthWithUndefinedInObject_ExceptionThrown()
{
var schema =
"""
@length*(2, !) #array* #object
""";
var json =
"""
{
"key1": [10, 20],
"key2": [10]
}
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(ALEN04, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_JsonWrongMaximumLengthWithUndefinedInObject_ExceptionThrown()
{
var schema =
"""
@length*(!, 4) #array* #object
""";
var json =
"""
{
"key1": [10, 20],
"key2": [10, 20, 30, 40, 50]
}
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(ALEN05, exception.Code);
Console.WriteLine(exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -467,4 +467,218 @@ public void When_InvalidTimePatternCauseLexerError_ExceptionThrown()
Assert.AreEqual(DLEX01, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_JsonDateNotValidWithBothRange_ExceptionThrown()
{
var schema =
"""
@range*("2010-01-01", "2010-12-31") #date* #array
""";
var json =
"""
[
"2010-01-01",
"2010-02-01",
"2010-06-30",
"2009-12-31",
"2011-01-01"
]
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(DRNG01, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_JsonTimeNotValidWithBothRange_ExceptionThrown()
{
var schema =
"""
@range*("2010-01-01T00:00:00.000Z",
"2010-12-31T23:59:59.999Z")
#time* #array
""";
var json =
"""
[
"2010-01-01T00:00:00.000Z",
"2010-02-01T01:30:45.1Z",
"2010-06-30T12:01:07.999999Z",
"2009-12-31T22:39:50.0-04:00",
"2011-01-01T02:10:00.0+06:00",
"2009-12-31T22:59:59.000Z",
"2011-01-01T00:00:00.000Z"
]
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(DRNG02, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_JsonDateNotValidWithStart_ExceptionThrown()
{
var schema =
"""
@start*("2010-01-01") #date* #array
""";
var json =
"""
[
"2010-01-01",
"2010-02-01",
"2011-06-30",
"2050-11-01",
"2009-12-31"
]
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(STRT01, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_JsonDateNotValidWithEnd_ExceptionThrown()
{
var schema =
"""
@end*("2010-12-31") #date* #array
""";
var json =
"""
[
"1930-01-01",
"2000-02-01",
"2005-06-30",
"2010-12-31",
"2011-01-01"
]
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(ENDE01, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_JsonDateNotValidWithBefore_ExceptionThrown()
{
var schema =
"""
@before*("2011-01-01") #date* #array
""";
var json =
"""
[
"2010-01-01",
"2010-06-30",
"2010-12-31",
"2011-01-01",
"2023-11-15"
]
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(BFOR01, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_JsonDateNotValidWithAfter_ExceptionThrown()
{
var schema =
"""
@after*("2009-12-31") #date* #array
""";
var json =
"""
[
"2010-01-01",
"2010-02-10",
"2010-12-31",
"2009-12-31",
"1980-11-19"
]
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(AFTR01, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_JsonTimeNotValidWithEndInFraction_ExceptionThrown()
{
var schema =
"""
@end*("1939-09-02T02:12:12.555Z") #time* #array
""";
var json =
"""
[
"1939-09-02T02:12:12.554Z",
"1939-09-02T02:12:12.555Z",
"1939-09-02T02:12:12.556Z"
]
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(ENDE02, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_SchemaDateNotValidWithBefore_ExceptionThrown()
{
var schema =
"""
@before*("01-01-2011") #date* #array
""";
var json =
"""
[
"1900-01-01",
"2010-06-30",
"2010-12-31"
]
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(DYAR01, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_SchemaDateNotValidWithAfter_ExceptionThrown()
{
var schema =
"""
@after*("12-31-2009") #date* #array
""";
var json =
"""
[
"2010-01-01",
"2010-02-10",
"2050-12-31"
]
""";
JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(DYAR01, exception.Code);
Console.WriteLine(exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void When_NestedRangeWithJsonWrongIntegerInObject_ExceptionThrown()
}

[TestMethod]
public void When_NestedRangeWithUndefinedAndWrongIntegerInArray_ExceptionThrown()
public void When_NestedRangeWithMinUndefinedAndWrongIntegerInArray_ExceptionThrown()
{
var schema =
"""
Expand All @@ -204,6 +204,25 @@ public void When_NestedRangeWithUndefinedAndWrongIntegerInArray_ExceptionThrown(
Console.WriteLine(exception);
}

[TestMethod]
public void When_NestedRangeWithMaxUndefinedAndWrongIntegerInArray_ExceptionThrown()
{
var schema =
"""
@range*(1000, !) #integer*
""";
var json =
"""
[2000, 1000, 900]
""";

JsonSchema.IsValid(schema, json);
var exception = Assert.ThrowsException<JsonSchemaException>(
() => JsonAssert.IsValid(schema, json));
Assert.AreEqual(RANG03, exception.Code);
Console.WriteLine(exception);
}

[TestMethod]
public void When_NestedPositiveWithWrongIntegerInArray_ExceptionThrown()
{
Expand Down
Loading

0 comments on commit 0cf55af

Please sign in to comment.