Skip to content

Instantly share code, notes, and snippets.

@scottmuc
Created January 1, 2012 00:06
Show Gist options
  • Save scottmuc/1545719 to your computer and use it in GitHub Desktop.
Save scottmuc/1545719 to your computer and use it in GitHub Desktop.
BLOG: Handy Web Path Concatenation Code
using System.Text;
namespace CBC.Radio3.Commons.Web
{
public class WebPath
{
/// <summary>
/// Takes a parameter list of strings and returns the strings concatenated with a
/// preceding '/' character. If the string has a trailing/preceding slash it will
/// be removed. Any slashes in the middle of the string will remain.
/// </summary>
/// <param name="paths"></param>
/// <returns>Contatenation of paths seperated by directory seperator</returns>
public static string Combine(params string[] paths)
{
var sb = new StringBuilder();
foreach (var path in paths)
sb.AppendFormat("/{0}", path.TrimEnd('/').TrimStart('/'));
return sb.ToString();
}
}
}
using bddunit.core;
using CBC.Radio3.Commons.Specs;
using Observation = MbUnit.Framework.TestAttribute;
using SUT = CBC.Radio3.Commons.Web.WebPath;
namespace CBC.Radio3.Commons.Web
{
[Concern(typeof (WebPath))]
public class when_WebPath_Combines_a_bunch_of_strings : ContextSpecification
{
private string result;
protected override void establish_context()
{
}
protected override void because()
{
result = SUT.Combine("/slashesonbothsides/", "//doubleslash//", "rightslash/", "/leftslash", "file.jpg");
}
[Observation]
public void should_return_an_absolute_web_path()
{
result.should_be_equal_to("/slashesonbothsides/doubleslash/rightslash/leftslash/file.jpg");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment