Skip to content

Commit b39a10a

Browse files
committed
Fix: Some more tests and their correct implementation
1 parent 41ab63e commit b39a10a

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

src/CodeOfChaos.Extensions/StringCaseExtensions.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public static partial class StringCaseExtensions {
1616
// -----------------------------------------------------------------------------------------------------------------
1717
// Convert to PascalCase
1818
public static string ToPascalCase(this string input) {
19-
if (string.IsNullOrEmpty(input))
20-
return input;
19+
if (input.IsNullOrWhiteSpace()) return input;
2120

2221
string[] words = NonAlphanumericRegex.Split(input);
2322

@@ -33,10 +32,8 @@ public static string ToPascalCase(this string input) {
3332

3433
// Convert to camelCase
3534
public static string ToCamelCase(this string input) {
36-
if (string.IsNullOrEmpty(input))
37-
return input;
38-
39-
35+
if (input.IsNullOrWhiteSpace()) return input;
36+
4037
string[] words = NonAlphanumericRegex.Split(input);
4138

4239
for (int i = 0; i < words.Length; i++) {
@@ -56,10 +53,9 @@ public static string ToCamelCase(this string input) {
5653

5754
// Convert to kebab-case
5855
public static string ToKebabCase(this string input) {
59-
if (string.IsNullOrEmpty(input))
60-
return input;
56+
if (input.IsNullOrWhiteSpace()) return input;
6157

62-
string[] words = NonAlphanumericRegex.Split(input);
58+
Span<string> words = NonAlphanumericRegex.Split(input);
6359
for (int i = 0; i < words.Length; i++) {
6460
words[i] = words[i].ToLower();
6561
}
@@ -69,8 +65,7 @@ public static string ToKebabCase(this string input) {
6965

7066
// Convert to snake_case
7167
public static string ToSnakeCase(this string input) {
72-
if (string.IsNullOrEmpty(input))
73-
return input;
68+
if (input.IsNullOrWhiteSpace()) return input;
7469

7570
string[] words = NonAlphanumericRegex.Split(input);
7671
for (int i = 0; i < words.Length; i++) {
@@ -82,8 +77,7 @@ public static string ToSnakeCase(this string input) {
8277

8378
// Convert to period.separated.case
8479
public static string ToPeriodSeparatedCase(this string input) {
85-
if (string.IsNullOrEmpty(input))
86-
return input;
80+
if (input.IsNullOrWhiteSpace()) return input;
8781

8882
string[] words = NonAlphanumericRegex.Split(input);
8983
for (int i = 0; i < words.Length; i++) {

tests/Tests.CodeOfChaos.Extensions/StringCaseExtensionTests.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public class StringCaseExtensionTests {
1212

1313
// Tests for ToCamelCase
1414
[Test]
15+
[Arguments("", "")]
16+
[Arguments(null, null)]
17+
[Arguments(" ", " ")]
1518
[Arguments("a", "a")]
1619
[Arguments("A", "a")]
1720
[Arguments("aA", "aA")]
@@ -30,18 +33,21 @@ public class StringCaseExtensionTests {
3033
[Arguments("kebab-case", "kebabCase")]
3134
[Arguments("kebab-case-with-numbers-123", "kebabCaseWithNumbers123")]
3235
[Arguments("kebab-case-with-numbers-123-and-more", "kebabCaseWithNumbers123AndMore")]
33-
public async Task ToCamelCase_ShouldWork(string input, string expected) {
36+
public async Task ToCamelCase_ShouldWork(string? input, string? expected) {
3437
// Arrange
3538

3639
// Act
37-
var result = input.ToCamelCase();
40+
string? result = input?.ToCamelCase();
3841

3942
// Assert
4043
await Assert.That(result).IsEqualTo(expected);
4144
}
4245

4346
// Tests for ToPascalCase
4447
[Test]
48+
[Arguments("", "")]
49+
[Arguments(null, null)]
50+
[Arguments(" ", " ")]
4551
[Arguments("a", "A")]
4652
[Arguments("A", "A")]
4753
[Arguments("aA", "AA")]
@@ -60,18 +66,21 @@ public async Task ToCamelCase_ShouldWork(string input, string expected) {
6066
[Arguments("kebab-case", "KebabCase")]
6167
[Arguments("kebab-case-with-numbers-123", "KebabCaseWithNumbers123")]
6268
[Arguments("kebab-case-with-numbers-123-and-more", "KebabCaseWithNumbers123AndMore")]
63-
public async Task ToPascalCase_ShouldWork(string input, string expected) {
69+
public async Task ToPascalCase_ShouldWork(string? input, string? expected) {
6470
// Arrange
6571

6672
// Act
67-
var result = input.ToPascalCase();
73+
string? result = input?.ToPascalCase();
6874

6975
// Assert
7076
await Assert.That(result).IsEqualTo(expected);
7177
}
7278

7379
// Tests for ToKebabCase
7480
[Test]
81+
[Arguments("", "")]
82+
[Arguments(null, null)]
83+
[Arguments(" ", " ")]
7584
[Arguments("a", "a")]
7685
[Arguments("A", "a")]
7786
[Arguments("aA", "a-a")]
@@ -90,18 +99,21 @@ public async Task ToPascalCase_ShouldWork(string input, string expected) {
9099
[Arguments("kebab-case", "kebab-case")]
91100
[Arguments("kebab-case-with-numbers-123", "kebab-case-with-numbers-123")]
92101
[Arguments("kebab-case-with-numbers-123-and-more", "kebab-case-with-numbers-123-and-more")]
93-
public async Task ToKebabCase_ShouldWork(string input, string expected) {
102+
public async Task ToKebabCase_ShouldWork(string? input, string? expected) {
94103
// Arrange
95104

96105
// Act
97-
var result = input.ToKebabCase();
106+
string? result = input?.ToKebabCase();
98107

99108
// Assert
100109
await Assert.That(result).IsEqualTo(expected);
101110
}
102111

103112
// Tests for ToSnakeCase
104113
[Test]
114+
[Arguments("", "")]
115+
[Arguments(null, null)]
116+
[Arguments(" ", " ")]
105117
[Arguments("a", "a")]
106118
[Arguments("A", "a")]
107119
[Arguments("aA", "a_a")]
@@ -120,18 +132,21 @@ public async Task ToKebabCase_ShouldWork(string input, string expected) {
120132
[Arguments("kebab-case", "kebab_case")]
121133
[Arguments("kebab-case-with-numbers-123", "kebab_case_with_numbers_123")]
122134
[Arguments("kebab-case-with-numbers-123-and-more", "kebab_case_with_numbers_123_and_more")]
123-
public async Task ToSnakeCase_ShouldWork(string input, string expected) {
135+
public async Task ToSnakeCase_ShouldWork(string? input, string? expected) {
124136
// Arrange
125137

126138
// Act
127-
var result = input.ToSnakeCase();
139+
string? result = input?.ToSnakeCase();
128140

129141
// Assert
130142
await Assert.That(result).IsEqualTo(expected);
131143
}
132144

133145
// Tests for ToPeriodSeparatedCase
134146
[Test]
147+
[Arguments("", "")]
148+
[Arguments(null, null)]
149+
[Arguments(" ", " ")]
135150
[Arguments("a", "a")]
136151
[Arguments("A", "a")]
137152
[Arguments("aA", "a.a")]
@@ -150,11 +165,11 @@ public async Task ToSnakeCase_ShouldWork(string input, string expected) {
150165
[Arguments("kebab-case", "kebab.case")]
151166
[Arguments("kebab-case-with-numbers-123", "kebab.case.with.numbers.123")]
152167
[Arguments("kebab-case-with-numbers-123-and-more", "kebab.case.with.numbers.123.and.more")]
153-
public async Task ToPeriodSeparatedCase_ShouldWork(string input, string expected) {
168+
public async Task ToPeriodSeparatedCase_ShouldWork(string? input, string? expected) {
154169
// Arrange
155170

156171
// Act
157-
var result = input.ToPeriodSeparatedCase();
172+
string? result = input?.ToPeriodSeparatedCase();
158173

159174
// Assert
160175
await Assert.That(result).IsEqualTo(expected);

0 commit comments

Comments
 (0)