Skip to content

Commit

Permalink
Benchmark cleanup, updated documentation and validation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sakari-malkki committed Sep 11, 2024
1 parent 1d2485b commit a22f172
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal sealed class MetadataSyntaxValidationBenchmark : FileBenchmark

internal override string Description => "Benchmarks the metadata syntax validation of Px.Utils/Validation/SyntaxValidator.";

private SyntaxValidator validator;
private Encoding encoding = Encoding.Default;

internal MetadataSyntaxValidationBenchmark()
{
Expand All @@ -25,23 +25,22 @@ protected override void OneTimeBenchmarkSetup()
{
base.OneTimeBenchmarkSetup();

Stream stream = new FileStream(TestFilePath, FileMode.Open, FileAccess.Read);
using Stream stream = new FileStream(TestFilePath, FileMode.Open, FileAccess.Read);
PxFileMetadataReader reader = new();
Encoding encoding = reader.GetEncoding(stream);
validator = new(stream, encoding, TestFilePath, leaveStreamOpen: true);
encoding = reader.GetEncoding(stream);
}

private void SyntaxValidationBenchmark()
{
using Stream stream = new FileStream(TestFilePath, FileMode.Open, FileAccess.Read);
stream.Position = 0;
SyntaxValidator validator = new(stream, encoding, TestFilePath, leaveStreamOpen: true);
validator.Validate();
}

private async Task SyntaxValidationBenchmarkAsync()
{
using Stream stream = new FileStream(TestFilePath, FileMode.Open, FileAccess.Read);
stream.Position = 0;
SyntaxValidator validator = new(stream, encoding, TestFilePath, leaveStreamOpen: true);
await validator.ValidateAsync();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ internal static class SyntaxValidationFixtures
];

internal static List<ValidationKeyValuePair> KEYVALUEPAIR_WITH_ILLEGAL_SYMBOLS_IN_SPECIFIER_SECTIONS => [
new("foo", new KeyValuePair<string, string>("FOO([\"first_specifier\"], [\"second_specifier\"])", "YES\n"), 0, [], 0)
new("foo", new KeyValuePair<string, string>("FOO([\"first_specifier\"], [\"second_specifier\"])", "YES\n"), 0, [], 0),
new("foo", new KeyValuePair<string, string>("FOO(\"first_specifier\",, \"second_specifier\")", "YES\n"), 1, [], 0),
new("foo", new KeyValuePair<string, string>("FOO(\"first_specifier\"-\"second_specifier\")", "YES\n"), 2, [], 0)
];

private const string multilineStringValue = "\"dis parturient montes nascetur ridiculus mus\"\n" +
Expand Down Expand Up @@ -171,7 +173,7 @@ internal static class SyntaxValidationFixtures
];

private readonly static ValidationStructuredEntryKey illegalSpecifier = new("FOO", "fi", "first\"specifier");
internal static List<ValidationStructuredEntry> STRUCTIRED_ENTRIES_WITH_ILLEGAL_CHARACTERS_IN_SPECIFIERS => [
internal static List<ValidationStructuredEntry> STRUCTIRED_ENTRIES_WITH_ILLEGAL_CHARACTERS_IN_SPECIFIER_PARTS => [
new("foo", illegalSpecifier, "foo", 0, [], 0, Utils.Validation.ValueType.StringValue)
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,22 @@ public void ValidateObjectsCalledWithKvpWithIllegalSymbolsInLanguageParamReturns

Assert.AreEqual(1, feedback.Count);
Assert.AreEqual(3, feedback.First().Value.Count);
Assert.AreEqual(ValidationFeedbackRule.IllegalCharactersInLanguageParameter, feedback.First().Key.Rule);
Assert.AreEqual(ValidationFeedbackRule.IllegalCharactersInLanguageSection, feedback.First().Key.Rule);
}

[TestMethod]
public void ValidateObjectsCalledWithKvpWithIllegalSymbolsInSpecifierParamReturnsWithErrors()
{
// Arrange
List<ValidationKeyValuePair> keyValuePairs = SyntaxValidationFixtures.KEYVALUEPAIR_WITH_ILLEGAL_SYMBOLS_IN_SPECIFIER_SECTIONS;
List<KeyValuePairValidationFunction> functions = [SyntaxValidationFunctions.IllegalSymbolsInSpecifierParamSection];
List<KeyValuePairValidationFunction> functions = [SyntaxValidationFunctions.IllegalCharactersInSpecifierSection];

// Act
feedback = kvpValidationMethod?.Invoke(null, [keyValuePairs, functions, syntaxConf]) as ValidationFeedback ?? [];

Assert.AreEqual(1, feedback.Count);
Assert.AreEqual(ValidationFeedbackRule.IllegalCharactersInSpecifierParameter, feedback.First().Key.Rule);
Assert.AreEqual(3, feedback.First().Value.Count);
Assert.AreEqual(ValidationFeedbackRule.IllegalCharactersInSpecifierSection, feedback.First().Key.Rule);
}

[TestMethod]
Expand Down Expand Up @@ -280,14 +281,16 @@ public void ValidateObjectsCalledWithStructuredEntriesWithInvalidKeywordsReturns
// Arrange
List<ValidationStructuredEntry> structuredEntries = SyntaxValidationFixtures.STRUCTURED_ENTRIES_WITH_INVALID_KEYWORDS;
List<StructuredValidationFunction> functions = [SyntaxValidationFunctions.KeywordDoesntStartWithALetter, SyntaxValidationFunctions.KeywordContainsIllegalCharacters];
ValidationFeedbackKey startWithletterFeedbackKey = new(ValidationFeedbackLevel.Error, ValidationFeedbackRule.KeywordDoesntStartWithALetter);
ValidationFeedbackKey illegalCharactersFeedbackKey = new(ValidationFeedbackLevel.Error, ValidationFeedbackRule.IllegalCharactersInKeyword);

// Act
feedback = structuredValidationMethod?.Invoke(null, [structuredEntries, functions, syntaxConf]) as ValidationFeedback ?? [];

Assert.AreEqual(2, feedback.Count);
Assert.AreEqual(2, feedback.ElementAt(1).Value.Count);
Assert.AreEqual(ValidationFeedbackRule.KeywordDoesntStartWithALetter, feedback.First().Key.Rule);
Assert.AreEqual(ValidationFeedbackRule.IllegalCharactersInKeyword, feedback.ElementAt(1).Key.Rule);
Assert.IsTrue(feedback.ContainsKey(startWithletterFeedbackKey));
Assert.IsTrue(feedback.ContainsKey(illegalCharactersFeedbackKey));
Assert.AreEqual(2, feedback[illegalCharactersFeedbackKey].Count);
}

[TestMethod]
Expand All @@ -314,14 +317,14 @@ public void ValidateObjectsCalledWithStructuredEntriesWithInvalidLanguagesReturn
feedback = structuredValidationMethod?.Invoke(null, [structuredEntries, functions, syntaxConf]) as ValidationFeedback ?? [];

Assert.AreEqual(1, feedback.Count);
Assert.AreEqual(ValidationFeedbackRule.IllegalCharactersInLanguageParameter, feedback.First().Key.Rule);
Assert.AreEqual(ValidationFeedbackRule.IllegalCharactersInLanguageSection, feedback.First().Key.Rule);
}

[TestMethod]
public void ValidateObjectsCalledWithStructuredEntriesWithIllegalCharactersInSpecifiersReturnsWithErrors()
{
// Arrange
List<ValidationStructuredEntry> structuredEntries = SyntaxValidationFixtures.STRUCTIRED_ENTRIES_WITH_ILLEGAL_CHARACTERS_IN_SPECIFIERS;
List<ValidationStructuredEntry> structuredEntries = SyntaxValidationFixtures.STRUCTIRED_ENTRIES_WITH_ILLEGAL_CHARACTERS_IN_SPECIFIER_PARTS;
List<StructuredValidationFunction> functions = [SyntaxValidationFunctions.IllegalCharactersInSpecifierParts];

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Px.Utils.Validation.ContentValidation
/// <summary>
/// Represents the result of a px file metadata content validation operation. Contains a validation report and information about the expected dimensions of the data section.
/// </summary>
/// <param name="feedbackItems">An array of <see cref="ValidationFeedbackItem"/> objects gathered during the metadata content validation process.</param>
/// <param name="feedbackItems">A <see cref="ValidationFeedback"/> object containing information about rule violations gathered during the metadata content validation process.</param>
/// <param name="dataRowLength">Expected length of each data row.</param>
/// <param name="dataRowAmount">Expected data column length/amount of data rows.</param>
public sealed class ContentValidationResult(ValidationFeedback feedbackItems, int dataRowLength, int dataRowAmount) : ValidationResult(feedbackItems)
Expand Down
2 changes: 1 addition & 1 deletion Px.Utils/Validation/ContentValidation/ContentValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ ValidationResult IPxFileValidator.Validate()

private int GetProductOfDimensionValues(Dictionary<string, string[]> dimensions)
{
string? lang = _defaultLanguage ?? _availableLanguages?[0];
string? lang = _defaultLanguage ?? _availableLanguages?[0] ?? string.Empty;
if (lang is null || dimensions.Count == 0)
{
return 0;
Expand Down
7 changes: 4 additions & 3 deletions Px.Utils/Validation/DataValidation/DataValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class DataValidator(Stream stream, int rowLen, int numOfRows, string file
/// </summary>
/// <returns>
/// <see cref="ValidationResult"/> object that contains a collection of
/// ValidationFeedbackItem objects representing the feedback for the data validation.
/// validation feedback key value pairs representing the feedback for the data validation.
/// </returns>
public ValidationResult Validate()
{
Expand Down Expand Up @@ -72,7 +72,7 @@ public ValidationResult Validate()
/// Assumes that the stream is at the start of the data section (after 'DATA='-keyword) at the first data item.
/// <returns>
/// <see cref="ValidationResult"/> object that contains a collection of
/// ValidationFeedbackItem objects representing the feedback for the data validation.
/// validation feedback key value pairs representing the feedback for the data validation.
/// </returns>
public async Task<ValidationResult> ValidateAsync(CancellationToken cancellationToken = default)
{
Expand Down Expand Up @@ -221,7 +221,8 @@ private void HandleNonSeparatorType(ref ValidationFeedback validationFeedbacks)
validationFeedbacks.Add(new(
new (ValidationFeedbackLevel.Error,
ValidationFeedbackRule.DataValidationFeedbackInvalidRowLength),
new(filename, _lineNumber + startRow, _charPosition))
new(filename, _lineNumber + startRow, _charPosition,
$"Expected {rowLen}, got row length of {_currentRowLength}."))
);
}
_lineNumber++;
Expand Down
4 changes: 2 additions & 2 deletions Px.Utils/Validation/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public enum ValidationFeedbackRule
IllegalCharactersInKeyword = 13,
KeywordDoesntStartWithALetter = 14,
RegexTimeout = 15,
IllegalCharactersInLanguageParameter = 17,
IllegalCharactersInSpecifierParameter = 18,
IllegalCharactersInLanguageSection = 17,
IllegalCharactersInSpecifierSection = 18,
EntryWithoutValue = 19,
IncompliantLanguage = 20,
KeywordContainsUnderscore = 21,
Expand Down
Loading

0 comments on commit a22f172

Please sign in to comment.