Skip to content

Commit 3f9c647

Browse files
authored
Add tests (#454)
* Added tests. Full coverage for the base package. * Added tests for EF Core package.
1 parent 604959c commit 3f9c647

37 files changed

+1078
-495
lines changed

src/Ardalis.Specification.EntityFrameworkCore/Evaluators/SpecificationEvaluator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public class SpecificationEvaluator : ISpecificationEvaluator
1010
/// </summary>
1111
public static SpecificationEvaluator Default { get; } = new SpecificationEvaluator();
1212

13-
protected List<IEvaluator> Evaluators { get; } = new List<IEvaluator>();
13+
protected List<IEvaluator> Evaluators { get; }
1414

1515
public SpecificationEvaluator()
1616
{
17-
Evaluators.AddRange(new IEvaluator[]
18-
{
17+
Evaluators =
18+
[
1919
WhereEvaluator.Instance,
2020
SearchEvaluator.Instance,
2121
IncludeStringEvaluator.Instance,
@@ -29,12 +29,12 @@ public SpecificationEvaluator()
2929
IgnoreAutoIncludesEvaluator.Instance,
3030
AsSplitQueryEvaluator.Instance,
3131
TagWithEvaluator.Instance,
32-
});
32+
];
3333
}
3434

3535
public SpecificationEvaluator(IEnumerable<IEvaluator> evaluators)
3636
{
37-
Evaluators.AddRange(evaluators);
37+
Evaluators = evaluators.ToList();
3838
}
3939

4040
/// <inheritdoc/>

src/Ardalis.Specification/Evaluators/InMemorySpecificationEvaluator.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ public class InMemorySpecificationEvaluator : IInMemorySpecificationEvaluator
55
// Will use singleton for default configuration. Yet, it can be instantiated if necessary, with default or provided evaluators.
66
public static InMemorySpecificationEvaluator Default { get; } = new InMemorySpecificationEvaluator();
77

8-
protected List<IInMemoryEvaluator> Evaluators { get; } = new List<IInMemoryEvaluator>();
8+
protected List<IInMemoryEvaluator> Evaluators { get; }
99

1010
public InMemorySpecificationEvaluator()
1111
{
12-
Evaluators.AddRange(new IInMemoryEvaluator[]
13-
{
12+
Evaluators =
13+
[
1414
WhereEvaluator.Instance,
1515
SearchMemoryEvaluator.Instance,
1616
OrderEvaluator.Instance,
1717
PaginationEvaluator.Instance
18-
});
18+
];
1919
}
20+
2021
public InMemorySpecificationEvaluator(IEnumerable<IInMemoryEvaluator> evaluators)
2122
{
22-
Evaluators.AddRange(evaluators);
23+
Evaluators = evaluators.ToList();
2324
}
2425

2526
public virtual IEnumerable<TResult> Evaluate<T, TResult>(IEnumerable<T> source, ISpecification<T, TResult> specification)

src/Ardalis.Specification/Evaluators/Iterator.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ public Iterator<TSource> GetEnumerator()
2121
public abstract bool MoveNext();
2222

2323
public TSource Current => _current;
24-
object? IEnumerator.Current => Current;
2524
IEnumerator<TSource> IEnumerable<TSource>.GetEnumerator() => GetEnumerator();
25+
26+
[ExcludeFromCodeCoverage]
27+
object? IEnumerator.Current => Current;
28+
29+
[ExcludeFromCodeCoverage]
2630
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
2731

2832
[ExcludeFromCodeCoverage]

src/Ardalis.Specification/Exceptions/ConcurrentSelectorsException.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
public class ConcurrentSelectorsException : Exception
44
{
5-
private const string _message = "Concurrent specification selector transforms defined. Ensure only one of the Select() or SelectMany() transforms is used in the same specification!";
5+
private const string MESSAGE = "Concurrent specification selector transforms defined. Ensure only one of the Select() or SelectMany() transforms is used in the same specification!";
66

77
public ConcurrentSelectorsException()
8-
: base(_message)
8+
: base(MESSAGE)
99
{
1010
}
1111

1212
public ConcurrentSelectorsException(Exception innerException)
13-
: base(_message, innerException)
13+
: base(MESSAGE, innerException)
1414
{
1515
}
1616
}

src/Ardalis.Specification/Exceptions/DuplicateOrderChainException.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
public class DuplicateOrderChainException : Exception
44
{
5-
private const string _message = "The specification contains more than one Order chain!";
5+
private const string MESSAGE = "The specification contains more than one Order chain!";
66

77
public DuplicateOrderChainException()
8-
: base(_message)
8+
: base(MESSAGE)
99
{
1010
}
1111

1212
public DuplicateOrderChainException(Exception innerException)
13-
: base(_message, innerException)
13+
: base(MESSAGE, innerException)
1414
{
1515
}
1616
}

src/Ardalis.Specification/Exceptions/DuplicateSkipException.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
public class DuplicateSkipException : Exception
44
{
5-
private const string _message = "Duplicate use of Skip(). Ensure you don't use Skip() more than once in the same specification!";
5+
private const string MESSAGE = "Duplicate use of Skip(). Ensure you don't use Skip() more than once in the same specification!";
66

77
public DuplicateSkipException()
8-
: base(_message)
8+
: base(MESSAGE)
99
{
1010
}
1111

1212
public DuplicateSkipException(Exception innerException)
13-
: base(_message, innerException)
13+
: base(MESSAGE, innerException)
1414
{
1515
}
1616
}

src/Ardalis.Specification/Exceptions/DuplicateTakeException.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
public class DuplicateTakeException : Exception
44
{
5-
private const string _message = "Duplicate use of Take(). Ensure you don't use Take() more than once in the same specification!";
5+
private const string MESSAGE = "Duplicate use of Take(). Ensure you don't use Take() more than once in the same specification!";
66

77
public DuplicateTakeException()
8-
: base(_message)
8+
: base(MESSAGE)
99
{
1010
}
1111

1212
public DuplicateTakeException(Exception innerException)
13-
: base(_message, innerException)
13+
: base(MESSAGE, innerException)
1414
{
1515
}
1616
}

src/Ardalis.Specification/Exceptions/InvalidSearchPatternException.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
public class InvalidSearchPatternException : Exception
44
{
5-
private const string _message = "Invalid search pattern: ";
5+
private const string MESSAGE = "Invalid search pattern: ";
66

77
public InvalidSearchPatternException(string searchPattern)
8-
: base($"{_message}{searchPattern}")
8+
: base($"{MESSAGE}{searchPattern}")
99
{
1010
}
1111

1212
public InvalidSearchPatternException(string searchPattern, Exception innerException)
13-
: base($"{_message}{searchPattern}", innerException)
13+
: base($"{MESSAGE}{searchPattern}", innerException)
1414
{
1515
}
1616
}

src/Ardalis.Specification/Exceptions/SelectorNotFoundException.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
public class SelectorNotFoundException : Exception
44
{
5-
private const string _message = "The specification must have a selector transform defined. Ensure either Select() or SelectMany() is used in the specification!";
5+
private const string MESSAGE = "The specification must have a selector transform defined. Ensure either Select() or SelectMany() is used in the specification!";
66

77
public SelectorNotFoundException()
8-
: base(_message)
8+
: base(MESSAGE)
99
{
1010
}
1111

1212
public SelectorNotFoundException(Exception innerException)
13-
: base(_message, innerException)
13+
: base(MESSAGE, innerException)
1414
{
1515
}
1616
}

src/Ardalis.Specification/Validators/SpecificationValidator.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,25 @@ public class SpecificationValidator : ISpecificationValidator
55
// Will use singleton for default configuration. Yet, it can be instantiated if necessary, with default or provided validators.
66
public static SpecificationValidator Default { get; } = new SpecificationValidator();
77

8-
private readonly List<IValidator> _validators = new();
8+
protected List<IValidator> Validators { get; }
99

1010
public SpecificationValidator()
1111
{
12-
_validators.AddRange(new IValidator[]
13-
{
12+
Validators =
13+
[
1414
WhereValidator.Instance,
1515
SearchValidator.Instance
16-
});
16+
];
1717
}
18+
1819
public SpecificationValidator(IEnumerable<IValidator> validators)
1920
{
20-
_validators.AddRange(validators);
21+
Validators = validators.ToList();
2122
}
2223

2324
public virtual bool IsValid<T>(T entity, ISpecification<T> specification)
2425
{
25-
foreach (var partialValidator in _validators)
26+
foreach (var partialValidator in Validators)
2627
{
2728
if (partialValidator.IsValid(entity, specification) == false) return false;
2829
}

0 commit comments

Comments
 (0)