Friday, October 19, 2007

In it's Weekly Source Code, Scott Hanselman presents a new CodePlex project, NDepend.Helpers.FileDirectoryPath from Patrick Smacchia. Nice, better path handling should have been part of the BCL a while ago. 

Path stuff is hard

Path normalization and parsing is not an easy task so when Patrick Smacchia mentions that his code "100% unit-tested", I decided to see if Pex could not find a little bug over there.

A dumb parameterized unit test

So I added wrote the following parameterized unit test, which 'simply' calls the constructor of FilePathRelative. Under the hood, there is some string manipulation done by the library to normalize the path, it should be interresting to see what comes out of this. I also added calls to PexComment.XXX to log the input/output values (Pex will build a table out of this):

    [PexMethod]
    public void FilePathRelativeCtor(string path) {
        PexComment.Parameters(path);
        FilePath result = new FilePathRelative(path);
        PexComment.Value("result", result.FileName);
    }

Ooops

So Pex starts running and soon enough an assert pops up. Pex had just found a neat little path that broke an assertion in the library:

[Test]
public void FilePathRelativeCtor_String_71019_003302_0_05() {
    this.FilePathRelativeCtor("/");
}

Popping up the reports, I went for the parameter table (remember the PexComment calls) that shows one row for each generated test. In fact, the 5-th test that Pex generated was triggering the assert:

Note that "//" also triggers the bug which seems to indicate that any path finishing by "/" will have this behavior.

The path condition

Lastly, I took a quick look at the path condition that Pex solved to discover the bug (see red below). Luckily this one is fairly easy and one can clearly see 'path[0] == '/' in there.

 What did we learn today?

Handling paths is hard :)

Also, we saw that a dumb parameterized unit test (just calling a ctor), could find a bugs. If you use assertions, it will help Pex look for bugs in your code.

Friday, October 19, 2007 4:26:09 PM UTC
Nice to see Pex shines here. But when can it be available to the rest of the world?
Friday, October 19, 2007 4:40:54 PM UTC
We are working hard towards making Pex available. I cannot give any timeline when this will happen.
Saturday, October 20, 2007 4:15:42 PM UTC
Indeed, it looks impressing. So with NStatic from Wesner Moise , it seems that the community has potentially 2 big automatic correctness tools not yet available.

But still, a "/" or @"\" are not valid relative or absolute paths and the library has a correct behavior :o)
Btw, I added following unit tests:

string reason;
Assert.IsFalse(PathHelper.IsValidAbsolutePath("/", out reason));
Assert.IsFalse(PathHelper.IsValidRelativePath("/", out reason));
Assert.IsFalse(PathHelper.IsValidAbsolutePath(@"\", out reason));
Assert.IsFalse(PathHelper.IsValidRelativePath(@"\", out reason));
Assert.IsTrue(PathHelper.IsValidAbsolutePath(@"C:\Lib/", out reason));
Comments are closed.