Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 787821f

Browse files
committed
Add HasAttributeOf + AllAttributesLazy test
1 parent 9a11551 commit 787821f

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/ServiceStack.Text.Tests/ReflectionExtensionTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,50 @@ public void Can_use_HasAttributeCached()
146146
Assert.That(typeof(CharEnum).HasAttributeCached<EnumAsCharAttribute>());
147147
Assert.That(typeof(CharEnum).HasAttribute<EnumAsCharAttribute>());
148148
}
149+
150+
[Test]
151+
public void Can_use_lazy_HasAttributeOf_APIs()
152+
{
153+
var props = typeof(DeclarativeValidationTest).GetPublicProperties();
154+
Assert.That(props.Length, Is.EqualTo(3));
155+
156+
var locationsProp = props.FirstOrDefault(x =>
157+
x.PropertyType != typeof(string) && x.PropertyType.GetTypeWithGenericInterfaceOf(typeof(IEnumerable<>)) != null);
158+
159+
Assert.That(locationsProp.Name, Is.EqualTo(nameof(DeclarativeValidationTest.Locations)));
160+
161+
var genericDef = locationsProp.PropertyType.GetTypeWithGenericInterfaceOf(typeof(IEnumerable<>));
162+
var elementType = genericDef.GetGenericArguments()[0];
163+
var elementProps = elementType.GetPublicProperties();
164+
var hasAnyChildValidators = elementProps
165+
.Any(elProp => elProp.HasAttributeOf<ValidateAttribute>());
166+
Assert.That(hasAnyChildValidators);
167+
}
149168
}
150169

151170
public class GenericType<T> { }
152171
public class GenericType<T1, T2> { }
153172
public class GenericType<T1, T2, T3> { }
173+
174+
public class Location
175+
{
176+
public string Name { get; set; }
177+
[ValidateMaximumLength(20)]
178+
public string Value { get; set; }
179+
}
180+
181+
public class DeclarativeValidationTest : IReturn<EmptyResponse>
182+
{
183+
[ValidateNotEmpty]
184+
[ValidateMaximumLength(20)]
185+
public string Site { get; set; }
186+
public List<Location> Locations { get; set; } // **** here's the example
187+
public List<NoValidators> NoValidators { get; set; }
188+
}
189+
190+
public class NoValidators
191+
{
192+
public string Name { get; set; }
193+
public string Value { get; set; }
194+
}
154195
}

0 commit comments

Comments
 (0)