Skip to content

Instantly share code, notes, and snippets.

@ptran123
Created March 15, 2013 18:22
Show Gist options
  • Save ptran123/5171911 to your computer and use it in GitHub Desktop.
Save ptran123/5171911 to your computer and use it in GitHub Desktop.
public class StringBuilder2Test
{
[Fact]
public void ToString_IsEmpty_ReturnEmptyString()
{
var sb = new StringBuilder2();
Assert.Equal("", sb.ToString());
}
[Fact]
public void ToString_HasAValue_RetunsTheValue()
{
var sb = new StringBuilder2();
sb.AppendText("hello");
Assert.Equal("hello", sb.ToString());
}
[Fact]
public void ToString_AppendMultiple_RetunsMultipleValuesAppended()
{
var sb = new StringBuilder2();
sb.AppendText("hello");
sb.AppendText(" world");
Assert.Equal("hello world", sb.ToString());
}
[Fact]
public void ToString_AppendParameterAtTall_ReturnCorrectValue()
{
var sb = new StringBuilder2();
sb.AppendText("hello world {0}", "Peter");
Assert.Equal("hello world Peter", sb.ToString());
}
[Fact]
public void ToString_AppendParameterAtHead_ReturnCorrectValue()
{
var sb = new StringBuilder2();
sb.AppendText("{0} hello world", "Peter");
Assert.Equal("Peter hello world", sb.ToString());
}
[Fact]
public void ToString_AppendParameterAtMiddle_ReturnCorrectValue()
{
var sb = new StringBuilder2();
sb.AppendText("hello {0} world", "Peter");
Assert.Equal("hello Peter world", sb.ToString());
}
[Fact]
public void ToString_AppendTwoParameterAtTail_ReturnCorrectValue()
{
var sb = new StringBuilder2();
sb.AppendText("hello world {0} {1}", "Peter", "Tran");
Assert.Equal("hello world Peter Tran", sb.ToString());
}
[Fact]
public void ToString_AppendThreeParameterAtTail_ReturnCorrectValue()
{
var sb = new StringBuilder2();
sb.AppendText("hello world {0} {1} {2}", "Peter", "Tran", "Foo");
Assert.Equal("hello world Peter Tran Foo", sb.ToString());
}
}
public class StringBuilder2
{
private string result = string.Empty;
public override string ToString()
{
return this.result;
}
internal void AppendText(string value, params string[] parameters)
{
this.result += value;
for (int i = 0; i < parameters.Length; i++)
{
this.result = this.result.Replace("{" + i + "}", parameters[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment