Skip to content

Commit 1c885ea

Browse files
authored
Merge pull request #331 - OfType methods should have the option to pass Type without generics
OfType methods should have the option to pass Type without generics
2 parents 341efc7 + e139c8a commit 1c885ea

File tree

25 files changed

+620
-91
lines changed

25 files changed

+620
-91
lines changed

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Attributes/BaseAttributesTestBuilder{TAttributesTestBuilder}.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,26 @@ protected BaseAttributesTestBuilder(ComponentTestContext testContext)
3131
/// <value>Test builder for the attributes.</value>
3232
public abstract TAttributesTestBuilder AttributesTestBuilder { get; }
3333

34-
/// <inheritdoc />
35-
public TAttributesTestBuilder ContainingAttributeOfType<TAttribute>()
36-
where TAttribute : Attribute
34+
///// <inheritdoc />
35+
public TAttributesTestBuilder ContainingAttributeOfType(Type attribute)
3736
{
38-
var expectedAttributeType = typeof(TAttribute);
3937
this.Validations.Add(attrs =>
4038
{
41-
if (attrs.All(a => a.GetType() != expectedAttributeType))
39+
if (attrs.All(a => a.GetType() != attribute))
4240
{
4341
this.ThrowNewAttributeAssertionException(
44-
expectedAttributeType.ToFriendlyTypeName(),
42+
attribute.ToFriendlyTypeName(),
4543
"in fact such was not found");
4644
}
4745
});
4846

4947
return this.AttributesTestBuilder;
5048
}
5149

50+
/// <inheritdoc />
51+
public TAttributesTestBuilder ContainingAttributeOfType<TAttribute>()
52+
where TAttribute : Attribute => this.ContainingAttributeOfType(typeof(TAttribute));
53+
5254
/// <inheritdoc />
5355
public TAttributesTestBuilder PassingFor<TAttribute>(Action<TAttribute> assertions)
5456
where TAttribute : Attribute

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/CaughtExceptions/AggregateExceptionTestBuilder.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,25 @@ public AggregateExceptionTestBuilder(ComponentTestContext testContext)
2525
}
2626

2727
/// <inheritdoc />
28-
public IAndAggregateExceptionTestBuilder ContainingInnerExceptionOfType<TInnerException>()
29-
where TInnerException : Exception
28+
public IAndAggregateExceptionTestBuilder ContainingInnerExceptionOfType(Type innerException)
3029
{
31-
var expectedInnerExceptionType = typeof(TInnerException);
32-
var innerExceptionFound = this.aggregateException.InnerExceptions.Any(e => e.GetType() == expectedInnerExceptionType);
30+
var innerExceptionFound = this.aggregateException.InnerExceptions.Any(e => e.GetType() == innerException);
3331
if (!innerExceptionFound)
3432
{
3533
throw new InvalidExceptionAssertionException(string.Format(
3634
"{0} {1} to contain {2}, but none was found.",
3735
this.TestContext.ExceptionMessagePrefix,
3836
nameof(AggregateException),
39-
expectedInnerExceptionType.ToFriendlyTypeName()));
37+
innerException.ToFriendlyTypeName()));
4038
}
4139

4240
return this;
4341
}
4442

43+
/// <inheritdoc />
44+
public IAndAggregateExceptionTestBuilder ContainingInnerExceptionOfType<TInnerException>()
45+
where TInnerException : Exception => this.ContainingInnerExceptionOfType(typeof(TInnerException));
46+
4547
/// <inheritdoc />
4648
public new IAggregateExceptionTestBuilder AndAlso() => this;
4749
}

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/CaughtExceptions/ExceptionTestBuilder.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,25 @@ public ExceptionTestBuilder(ComponentTestContext testContext)
2222
}
2323

2424
/// <inheritdoc />
25-
public IAndExceptionTestBuilder OfType<TException>()
25+
public IAndExceptionTestBuilder OfType(Type exceptionType)
2626
{
27-
var expectedExceptionType = typeof(TException);
2827
var actualExceptionType = this.TestContext.CaughtException.GetType();
29-
if (Reflection.AreDifferentTypes(expectedExceptionType, actualExceptionType))
28+
if (Reflection.AreDifferentTypes(exceptionType, actualExceptionType))
3029
{
3130
throw new InvalidExceptionAssertionException(string.Format(
3231
"{0} {1}, but instead received {2}.",
3332
this.TestContext.ExceptionMessagePrefix,
34-
expectedExceptionType.ToFriendlyTypeName(),
33+
exceptionType.ToFriendlyTypeName(),
3534
actualExceptionType.ToFriendlyTypeName()));
3635
}
3736

3837
return this;
3938
}
4039

4140
/// <inheritdoc />
41+
public IAndExceptionTestBuilder OfType<TException>()
42+
=> OfType(typeof(TException));
43+
4244
public IExceptionMessageTestBuilder WithMessage()
4345
{
4446
return new ExceptionMessageTestBuilder(

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/Attributes/IBaseAttributesTestBuilder.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ public interface IBaseAttributesTestBuilder<TAttributesTestBuilder>
1717
TAttributesTestBuilder ContainingAttributeOfType<TAttribute>()
1818
where TAttribute : Attribute;
1919

20+
/// <summary>
21+
/// Tests whether the collected attributes contain the provided attribute type.
22+
/// </summary>
23+
/// <param name="attributeType"></param>
24+
/// <returns></returns>
25+
TAttributesTestBuilder ContainingAttributeOfType(Type attributeType);
26+
2027
/// <summary>
2128
/// Tests whether the collected attributes contain the provided attribute type passing the given assertions.
2229
/// </summary>

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/CaughtExceptions/IAggregateExceptionTestBuilder.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,12 @@ public interface IAggregateExceptionTestBuilder : IBaseExceptionTestBuilder
1414
/// <returns>The same <see cref="IAndAggregateExceptionTestBuilder"/>.</returns>
1515
IAndAggregateExceptionTestBuilder ContainingInnerExceptionOfType<TInnerException>()
1616
where TInnerException : Exception;
17+
18+
/// <summary>
19+
/// Tests whether <see cref="AggregateException"/> contains inner exception of the provided type.
20+
/// </summary>
21+
/// <param name="innerExeption"></param>
22+
/// <returns></returns>
23+
IAndAggregateExceptionTestBuilder ContainingInnerExceptionOfType(Type innerExeption);
1724
}
1825
}

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Contracts/CaughtExceptions/IExceptionTestBuilder.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.CaughtExceptions
22
{
3+
using System;
4+
35
/// <summary>
46
/// Used for testing expected <see cref="System.Exception"/>.
57
/// </summary>
@@ -11,5 +13,11 @@ public interface IExceptionTestBuilder : IBaseExceptionTestBuilder
1113
/// <typeparam name="TException">Type of the expected <see cref="System.Exception"/>.</typeparam>
1214
/// <returns>The same <see cref="IAndExceptionTestBuilder"/>.</returns>
1315
IAndExceptionTestBuilder OfType<TException>();
16+
17+
/// <summary>
18+
/// Tests whether certain type of <see cref="System.Exception"/> is thrown from the invoked method.
19+
/// </summary>
20+
/// <returns>The same <see cref="IAndExceptionTestBuilder"/>.</returns>
21+
IAndExceptionTestBuilder OfType(Type exceptionType);
1422
}
1523
}

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Data/BaseDataProviderTestBuilder.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace MyTested.AspNetCore.Mvc.Builders.Data
22
{
3+
using System;
34
using System.Collections.Generic;
45
using Base;
56
using Exceptions;
@@ -79,11 +80,20 @@ protected void ValidateContainingEntryWithValue<TValue>(TValue value)
7980
value,
8081
this.ThrowNewDataProviderAssertionException);
8182

83+
/// <summary>
84+
/// Validates whether the data provider contains entry value with the given type.
85+
/// </summary>
86+
protected void ValidateContainingEntryOfType(Type valueType)
87+
=> DictionaryValidator.ValidateValueOfType(
88+
this.DataProviderName,
89+
this.DataProvider,
90+
this.ThrowNewDataProviderAssertionException,valueType);
91+
8292
/// <summary>
8393
/// Validates whether the data provider contains entry value with the given type.
8494
/// </summary>
8595
/// <typeparam name="TValue">Type of the value.</typeparam>
86-
protected void ValidateContainingEntryOfType<TValue>()
96+
protected void ValidateContainingEntryOfType<TValue>()
8797
=> DictionaryValidator.ValidateValueOfType<TValue>(
8898
this.DataProviderName,
8999
this.DataProvider,
@@ -101,6 +111,18 @@ protected void ValidateContainingEntryOfType<TValue>(string key)
101111
key,
102112
this.ThrowNewDataProviderAssertionException);
103113

114+
/// <summary>
115+
/// Validates whether the data provider contains entry value with the given type and corresponding key.
116+
/// </summary>
117+
/// <param name="key"></param>
118+
/// <param name="valueType"></param>
119+
protected void ValidateContainingEntryOfType(string key, Type valueType)
120+
=> DictionaryValidator.ValidateStringKeyAndValueOfType(
121+
this.DataProviderName,
122+
this.DataProvider,
123+
key,
124+
this.ThrowNewDataProviderAssertionException,valueType);
125+
104126
/// <summary>
105127
/// Validates whether the data provider contains entry with the given key and corresponding value.
106128
/// </summary>

src/MyTested.AspNetCore.Mvc.Abstractions/Builders/Data/BaseDataProviderWithStringKeyTestBuilder.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,28 @@ public TDataProviderTestBuilder ContainingEntryWithValue<TValue>(TValue value)
4747
/// <inheritdoc />
4848
public TDataProviderTestBuilder ContainingEntryOfType<TValue>()
4949
{
50-
this.ValidateContainingEntryOfType<TValue>();
50+
this.ContainingEntryOfType(typeof(TValue));
51+
return this.DataProviderTestBuilder;
52+
}
53+
54+
/// <inheritdoc />
55+
public TDataProviderTestBuilder ContainingEntryOfType(Type valueType)
56+
{
57+
this.ValidateContainingEntryOfType(valueType);
5158
return this.DataProviderTestBuilder;
5259
}
5360

5461
/// <inheritdoc />
5562
public TDataProviderTestBuilder ContainingEntryOfType<TValue>(string key)
5663
{
57-
this.ValidateContainingEntryOfType<TValue>(key);
64+
this.ContainingEntryOfType(key,typeof(TValue));
65+
return this.DataProviderTestBuilder;
66+
}
67+
68+
/// <inheritdoc />
69+
public TDataProviderTestBuilder ContainingEntryOfType(string key, Type valueType)
70+
{
71+
this.ValidateContainingEntryOfType(key, valueType);
5872
return this.DataProviderTestBuilder;
5973
}
6074

src/MyTested.AspNetCore.Mvc.Abstractions/Utilities/Validators/DictionaryValidator.cs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,34 +49,52 @@ public static void ValidateValueOfType<TValue>(
4949
ValidateValueOfType<TValue>(name, dictionary.Values, failedValidationAction);
5050
}
5151

52+
public static void ValidateValueOfType(
53+
string name,
54+
IDictionary<string, object> dictionary,
55+
Action<string, string, string> failedValidationAction, Type valueType)
56+
=> ValidateValueOfType(name, dictionary.Values, failedValidationAction,valueType);
57+
5258
public static void ValidateValueOfType<TValue>(
5359
string name,
5460
IDictionary<object, object> dictionary,
5561
Action<string, string, string> failedValidationAction)
56-
{
57-
ValidateValueOfType<TValue>(name, dictionary.Values, failedValidationAction);
58-
}
59-
60-
public static void ValidateStringKeyAndValueOfType<TValue>(
62+
=> ValidateValueOfType<TValue>(name, dictionary.Values, failedValidationAction);
63+
64+
public static void ValidateValueOfType(
65+
string name,
66+
IDictionary<object, object> dictionary,
67+
Action<string, string, string> failedValidationAction,
68+
Type valueType)
69+
=> ValidateValueOfType(name, dictionary.Values, failedValidationAction, valueType);
70+
71+
public static void ValidateStringKeyAndValueOfType(
6172
string name,
6273
IDictionary<string, object> dictionary,
6374
string key,
64-
Action<string, string, string> failedValidationAction)
75+
Action<string, string, string> failedValidationAction,
76+
Type valueType)
6577
{
6678
var entryExists = dictionary.ContainsKey(key);
6779
var actualValue = entryExists ? dictionary[key] : null;
6880

69-
var expectedType = typeof(TValue);
7081
var actualType = actualValue?.GetType();
71-
if (!entryExists || Reflection.AreDifferentTypes(expectedType, actualType))
82+
if (!entryExists || Reflection.AreDifferentTypes(valueType, actualType))
7283
{
7384
failedValidationAction(
7485
name,
75-
$"to have entry with '{key}' key and value of {expectedType.ToFriendlyTypeName()} type",
86+
$"to have entry with '{key}' key and value of {valueType.ToFriendlyTypeName()} type",
7687
$"{(entryExists ? $"in fact found {actualType.ToFriendlyTypeName()}" : "such was not found")}");
7788
}
7889
}
7990

91+
public static void ValidateStringKeyAndValueOfType<TValue>(
92+
string name,
93+
IDictionary<string, object> dictionary,
94+
string key,
95+
Action<string, string, string> failedValidationAction)
96+
=> ValidateStringKeyAndValueOfType(name, dictionary, key, failedValidationAction, typeof(TValue));
97+
8098
public static void ValidateValue<TDictionaryKey, TValue>(
8199
string name,
82100
IDictionary<TDictionaryKey, object> dictionary,
@@ -122,21 +140,27 @@ public static void ValidateValues(
122140
failedValidationAction));
123141
}
124142

125-
private static void ValidateValueOfType<TValue>(
126-
string name,
127-
ICollection<object> values,
128-
Action<string, string, string> failedValidationAction)
143+
private static void ValidateValueOfType(
144+
string name,
145+
ICollection<object> values,
146+
Action<string, string, string> failedValidationAction,
147+
Type valueType)
129148
{
130-
var expectedType = typeof(TValue);
131-
var entryOfSameType = values.FirstOrDefault(arg => arg.GetType() == expectedType);
149+
var entryOfSameType = values.FirstOrDefault(arg => arg.GetType() == valueType);
132150

133151
if (entryOfSameType == null)
134152
{
135153
failedValidationAction(
136154
name,
137-
$"to have at least one entry of {expectedType.ToFriendlyTypeName()} type",
155+
$"to have at least one entry of {valueType.ToFriendlyTypeName()} type",
138156
"none was found");
139157
}
140158
}
159+
160+
private static void ValidateValueOfType<TValue>(
161+
string name,
162+
ICollection<object> values,
163+
Action<string, string, string> failedValidationAction)
164+
=> ValidateValueOfType(name, values, failedValidationAction, typeof(TValue));
141165
}
142166
}

src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/MemoryCache/IMemoryCacheTestBuilder.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ public interface IMemoryCacheTestBuilder
3131
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
3232
IAndMemoryCacheTestBuilder ContainingEntryOfType<TValue>();
3333

34+
/// <summary>
35+
/// Tests whether the <see cref="IMemoryCache"/> contains entry with value of the provided type.
36+
/// </summary>
37+
/// <param name="valueType"></param>
38+
/// <returns></returns>
39+
IAndMemoryCacheTestBuilder ContainingEntryOfType(Type valueType);
40+
3441
/// <summary>
3542
/// Tests whether the <see cref="IMemoryCache"/> contains entry with the provided key and corresponding value.
3643
/// </summary>
@@ -47,6 +54,14 @@ public interface IMemoryCacheTestBuilder
4754
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
4855
IAndMemoryCacheTestBuilder ContainingEntryOfType<TValue>(object key);
4956

57+
/// <summary>
58+
/// Tests whether the <see cref="IMemoryCache"/> contains entry with value of the provided type and the given key.
59+
/// </summary>
60+
/// <param name="key"></param>
61+
/// <param name="valueType"></param>
62+
/// <returns></returns>
63+
IAndMemoryCacheTestBuilder ContainingEntryOfType(object key, Type valueType);
64+
5065
/// <summary>
5166
/// Tests whether the <see cref="IMemoryCache"/> contains entry with the provided key, corresponding value and the deeply equal options to the given ones.
5267
/// </summary>

0 commit comments

Comments
 (0)