-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPropertyExists.cs
37 lines (33 loc) · 1.47 KB
/
PropertyExists.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
namespace AssertLibrary
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Globalization;
public static partial class Assert
{
/// <summary>
/// Asserts that the given property exists in the given object.
/// </summary>
/// <param name="value">Object to check</param>
/// <param name="propertyName">Name of the property to check</param>
/// <param name="variableName">Variable name to include in the error message</param>
public static void PropertyExists(object value, string propertyName, string variableName = null)
{
if (value == null)
throw new ArgumentNullException("value");
var type = value.GetType();
var exists = type == typeof(ExpandoObject) ?
((IDictionary<string, object>)value).ContainsKey(propertyName) :
type.GetProperty(propertyName) != null;
Check(exists, PropertyExistsMessage(variableName, propertyName));
}
private static string PropertyExistsMessage(string variableName, string propertyName)
{
return variableName == null ?
string.Format(CultureInfo.CurrentCulture, "\"{0}\" should exist at this point.", propertyName) :
string.Format(CultureInfo.CurrentCulture, "\"{0}\" should exist at this point in \"{1}\".", propertyName, variableName);
}
}
}