-
Notifications
You must be signed in to change notification settings - Fork 571
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add wholesome test to check JSON names (#1609)
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#if NETCOREAPP | ||
namespace StripeTests | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text.RegularExpressions; | ||
using Newtonsoft.Json; | ||
using Stripe; | ||
using Xunit; | ||
|
||
/// <summary> | ||
/// This wholesome test ensures that no entity or options class reuses the same name in | ||
/// different `JsonProperty` attributes. | ||
/// </summary> | ||
public class JsonNamesAreSnakeCase : WholesomeTest | ||
{ | ||
private const string AssertionMessage = | ||
"Found at least one invalid JsonProperty name."; | ||
|
||
[Fact] | ||
public void Check() | ||
{ | ||
List<string> results = new List<string>(); | ||
|
||
// Get all classes that derive from StripeEntity or implement INestedOptions | ||
var stripeClasses = GetSubclassesOf(typeof(StripeEntity)); | ||
stripeClasses.AddRange(GetClassesWithInterface(typeof(INestedOptions))); | ||
|
||
foreach (Type stripeClass in stripeClasses) | ||
{ | ||
foreach (PropertyInfo property in stripeClass.GetProperties()) | ||
{ | ||
var propType = property.PropertyType; | ||
|
||
// Skip properties that don't have a `JsonProperty` attribute | ||
var attribute = property.GetCustomAttribute<JsonPropertyAttribute>(); | ||
if (attribute == null) | ||
{ | ||
continue; | ||
} | ||
|
||
var match = Regex.Match(attribute.PropertyName, "^[a-z0-9][a-z0-9_]*$"); | ||
|
||
if (!match.Success) | ||
{ | ||
results.Add($"{stripeClass.Name}.{property.Name}"); | ||
} | ||
} | ||
} | ||
|
||
AssertEmpty(results, AssertionMessage); | ||
} | ||
} | ||
} | ||
#endif |