c# - How to test a directory path that matches a specific pattern/regex? -
i have following test aims ensure file path generated of specific format. using nunit's fluent interfaces, how can go this?
i having trouble regex.
[test] public void assetcontrolpath_shouldhaveformat_basedir_yyyy_mmm_yyyymmmdd() { //arrange var basedir = "c:\\basedir"; var fpbuilder = new filepathbuilder(new datetime(2010,10,10), basedir ); //act var destinationpath = fpbuilder.assetcontrolpath(); //assert // destinationpath = c:\basdir\2010\oct\20101010 assert.that(destinationpath, is.stringmatching(@"c:\\basedir\\d{4}\\[a-za-z]{3}\\d{8}")); }
the unit test error xxx.filepathbuildertests.assetcontrolpath_shouldhaveformat_basedir_yyyy_mmm_yyyymmmdd: expected: string matching "c:\\basedir\\d{4}\\[a-za-z]{3}\\d{8}" was: "c:\basedir\2010\oct\20101010"
edit: have switched test use @chrisf's approach. question still stands.
@"c:\\basedir\\d{4}\\[a-za-z]{3}]\\d{8}" // /\ bracket
also have problem \
escaping, need \\\d{4}
, \\\d{8}
, want match xxx\20101010
, not xxx20101010
. following fix matches correct:
var str = @"c:\basedir\2010\oct\20101010"; var re = new regex(@"c:\\basedir\\\d{4}\\[a-za-z]{3}\\\d{8}", regexoptions.ignorecase); var result = re.ismatch(str); // true
Comments
Post a Comment