Skip to content

Instantly share code, notes, and snippets.

@ptran123
Created March 15, 2013 18:22

Revisions

  1. ptran123 created this gist Mar 15, 2013.
    78 changes: 78 additions & 0 deletions gistfile1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    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());
    }

    }
    20 changes: 20 additions & 0 deletions gistfile2.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    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]);
    }

    }
    }