Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions YamlDotNet.Test/Serialization/NamingConventionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.Collections.Generic;
using FluentAssertions;
using Xunit;
using YamlDotNet.Serialization;
Expand Down Expand Up @@ -77,6 +78,22 @@ public void AppliesLowerCaseConvention(string expectedName, string input)
ShouldApplyConventionGiven(input, expectedName, LowerCaseNamingConvention.Instance);
}

public static IEnumerable<object[]> AllNamingConventions()
{
yield return new object[] { CamelCaseNamingConvention.Instance };
yield return new object[] { PascalCaseNamingConvention.Instance };
yield return new object[] { HyphenatedNamingConvention.Instance };
yield return new object[] { UnderscoredNamingConvention.Instance };
yield return new object[] { LowerCaseNamingConvention.Instance };
}

[Theory]
[MemberData(nameof(AllNamingConventions))]
public void ApplyingConventionToEmptyStringReturnsEmptyString(INamingConvention convention)
{
convention.Apply(string.Empty).Should().Be(string.Empty);
}

private void ShouldApplyConventionGiven(string input, string expectedName, INamingConvention convention)
{
convention.Apply(input).Should().Be(expectedName);
Expand Down
10 changes: 10 additions & 0 deletions YamlDotNet/Serialization/Utilities/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ internal static class StringExtensions
{
private static string ToCamelOrPascalCase(string str, Func<char, char> firstLetterTransform)
{
if (string.IsNullOrEmpty(str))
{
return str;
}

var text = Regex.Replace(str, "([_\\-])(?<char>[a-z])", match => match.Groups["char"].Value.ToUpperInvariant(), RegexOptions.IgnoreCase);
return firstLetterTransform(text[0]) + text.Substring(1);
}
Expand Down Expand Up @@ -70,6 +75,11 @@ public static string ToPascalCase(this string str)
/// <returns>Converted string</returns>
public static string FromCamelCase(this string str, string separator)
{
if (string.IsNullOrEmpty(str))
{
return str;
}

// Ensure first letter is always lowercase
str = char.ToLower(str[0], CultureInfo.InvariantCulture) + str.Substring(1);

Expand Down