UppercaseCounter Kata
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Sample | |
{ | |
public class UppercaseSearcher | |
{ | |
internal int[] Search(string source) | |
{ | |
var result = new List<int>(); | |
if (source.Length > 0) | |
{ | |
for (int i = 0; i < source.Length; i++) | |
{ | |
var current = source[i]; | |
if (char.IsUpper(source, i)) | |
{ | |
result.Add(i); | |
} | |
} | |
} | |
return result.ToArray(); | |
} | |
private bool IsUpperCase(char current) | |
{ | |
return current.ToString().ToUpper() | |
== current.ToString(); | |
} | |
} | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Xunit; | |
using Xunit.Extensions; | |
namespace Sample | |
{ | |
public class Test | |
{ | |
[Fact] | |
public void ShouldReturnEmptyArrayIfEmptyString() | |
{ | |
//Arrange | |
var source = ""; | |
var searcher = new UppercaseSearcher(); | |
//Act | |
var result = searcher.Search(source); | |
//Assert | |
Assert.Empty(result); | |
} | |
[Fact] | |
public void ShouldReturnValidUppercaseLocation() | |
{ | |
//Arrange | |
var source = "A"; | |
var searcher = new UppercaseSearcher(); | |
//Act | |
var result = searcher.Search(source); | |
//Assert | |
Assert.Equal(1, result.Length); | |
Assert.Equal(0, result[0]); | |
} | |
[Fact] | |
public void ShouldReturnValidUppercaseInSecondPlace() | |
{ | |
//Arrange | |
var source = "bA"; | |
var searcher = new UppercaseSearcher(); | |
var expected = new int[] { 1 }; | |
//Act | |
var result = searcher.Search(source); | |
//Assert | |
Assert.Equal(expected, result); | |
} | |
[Theory] | |
[InlineData("A", new int[] { 0 })] | |
[InlineData("bA", new int[] { 1 })] | |
[InlineData("bbAab", new int[] { 2 })] | |
[InlineData("babC", new int[] { 3 })] | |
//Multiple uppercases | |
[InlineData("bCbC", new int[] {1,3})] | |
//No uppercase | |
[InlineData("qwerty", new int[] { })] | |
public void ShouldBeValidInDifferentSituations(string source, int[] expected) | |
{ | |
//Arrange | |
var searcher = new UppercaseSearcher(); | |
//Act | |
var result = searcher.Search(source); | |
//Assert | |
Assert.Equal(expected, result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment