Skip to content

Commit

Permalink
fix: Avoid throwing an exception when the length of the params list i…
Browse files Browse the repository at this point in the history
…s zero
  • Loading branch information
MrDave1999 committed Jan 12, 2024
1 parent a023593 commit a9e5b39
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
24 changes: 14 additions & 10 deletions src/Loader/YeSqlLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,22 @@ public partial class YeSqlLoader
/// </summary>
/// <param name="sqlFiles">The SQL files to load.</param>
/// <returns>A collection containing the tags with their associated SQL statements.</returns>
/// <exception cref="ArgumentNullException"><c>sqlFiles</c> is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">
/// <c>sqlFiles</c> is <c>null</c>.
/// </exception>
/// <exception cref="ArgumentException">
/// One or more files in <paramref name="sqlFiles"/> is null, empty or consists only of white-space characters.
/// -or-
/// The length of the <paramref name="sqlFiles"/> list is zero.
/// </exception>
/// <exception cref="AggregateException">If the parser and/or loader encounters one or more errors.</exception>
/// <exception cref="AggregateException">
/// If the parser and/or loader encounters one or more errors.
/// </exception>
public IYeSqlCollection LoadFromFiles(params string[] sqlFiles)
{
if (sqlFiles is null)
throw new ArgumentNullException(nameof(sqlFiles));

if (sqlFiles.IsEmpty())
throw new ArgumentException(ExceptionMessages.LengthOfParamsListIsZero);
return _parser.SqlStatements;

if (sqlFiles.ContainsNullOrWhiteSpace())
throw new ArgumentException(string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, nameof(sqlFiles)));
Expand All @@ -67,20 +69,22 @@ public IYeSqlCollection LoadFromFiles(params string[] sqlFiles)
/// </summary>
/// <param name="directories">A set of directories where the SQL files are located.</param>
/// <returns>A collection containing the tags with their associated SQL statements.</returns>
/// <exception cref="ArgumentNullException"><c>directories</c> is <c>null</c>.</exception>
/// <exception cref="ArgumentNullException">
/// <c>directories</c> is <c>null</c>.
/// </exception>
/// <exception cref="ArgumentException">
/// One or more directories in <paramref name="directories"/> is null, empty or consists only of white-space characters.
/// -or-
/// The length of the <paramref name="directories"/> list is zero.
/// </exception>
/// <exception cref="AggregateException">If the parser and/or loader encounters one or more errors.</exception>
/// <exception cref="AggregateException">
/// If the parser and/or loader encounters one or more errors.
/// </exception>
public IYeSqlCollection LoadFromDirectories(params string[] directories)
{
if (directories is null)
throw new ArgumentNullException(nameof(directories));

if (directories.IsEmpty())
throw new ArgumentException(ExceptionMessages.LengthOfParamsListIsZero);
return _parser.SqlStatements;

if(directories.ContainsNullOrWhiteSpace())
throw new ArgumentException(string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, nameof(directories)));
Expand Down
1 change: 0 additions & 1 deletion src/Models/ExceptionMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ internal class ExceptionMessages
public const string DuplicateTagName = "The given tag '{0}' is duplicated.";
public const string TagIsEmptyOrWhitespace = "The tag name is empty.";
public const string LineIsNotAssociatedWithAnyTag = "'{0}' line is not associated with any tag.";
public const string LengthOfParamsListIsZero = "The length of the params list is zero.";
}
9 changes: 3 additions & 6 deletions tests/Loader/YeSqlLoaderTests.Directories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,15 @@ public void LoadFromDirectories_WhenDirectoryNotExists_ShouldThrowAggregateExcep
}

[Test]
public void LoadFromDirectories_WhenParamsListIsZero_ShouldThrowArgumentException()
public void LoadFromDirectories_WhenParamsListIsZero_ShouldNotThrowException()
{
// Arrange
var loader = new YeSqlLoader();
var expectedMessage = ExceptionMessages.LengthOfParamsListIsZero;

// Act
Action action = () => loader.LoadFromDirectories();
var sqlStatements = loader.LoadFromDirectories();

// Assert
action.Should()
.Throw<ArgumentException>()
.WithMessage(expectedMessage);
sqlStatements.Should().BeEmpty();
}
}
9 changes: 3 additions & 6 deletions tests/Loader/YeSqlLoaderTests.Files.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,15 @@ public void LoadFromFiles_WhenCollectionHasNullValueOrOnlyWhitespace_ShouldThrow
}

[Test]
public void LoadFromFiles_WhenParamsListIsZero_ShouldThrowArgumentException()
public void LoadFromFiles_WhenParamsListIsZero_ShouldNotThrowException()
{
// Arrange
var loader = new YeSqlLoader();
var expectedMessage = ExceptionMessages.LengthOfParamsListIsZero;

// Act
Action action = () => loader.LoadFromFiles();
var sqlStatements = loader.LoadFromFiles();

// Assert
action.Should()
.Throw<ArgumentException>()
.WithMessage(expectedMessage);
sqlStatements.Should().BeEmpty();
}
}

0 comments on commit a9e5b39

Please sign in to comment.