Created
January 1, 2012 00:06
-
-
Save scottmuc/1545719 to your computer and use it in GitHub Desktop.
BLOG: Handy Web Path Concatenation Code
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.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(); | |
} | |
} | |
} |
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 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