Skip to content

Commit

Permalink
Add wholesome test to check JSON names (#1609)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed May 7, 2019
1 parent bde171a commit d9661af
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/StripeTests/Wholesome/JsonNamesAreSnakeCase.cs
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

0 comments on commit d9661af

Please sign in to comment.