Skip to content

Commit

Permalink
extracting HtmlDocument's File IO into Actions that can be stubbed in…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
Sharon Cichelli committed May 20, 2011
1 parent ec48dcd commit 64a657f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
36 changes: 36 additions & 0 deletions src/HtmlTags.Testing/HtmlDocumentTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,41 @@ public void the_last_property()

document.Last.TagName().ShouldEqual("p");
}

[Test]
public void write_to_file_sends_the_document_to_the_file_writer()
{
string actualContent = null;
document.FileWriter = (path, content) =>
{
actualContent = content;
};

document.WriteToFile("myfile.htm");

actualContent.ShouldStartWith("<!DOCTYPE");
}

[Test]
public void open_in_browser_writes_a_temp_file_then_opens_it()
{
string actualPath = null;
string actualContent = null;
string actualTempFileName = null;
document.FileWriter = (path, content) =>
{
actualPath = path;
actualContent = content;
};
document.FileOpener = (filename) =>
{
actualTempFileName = filename;
};

document.OpenInBrowser();

actualPath.ShouldEqual(actualTempFileName);
actualContent.ShouldStartWith("<!DOCTYPE");
}
}
}
28 changes: 19 additions & 9 deletions src/HtmlTags/HtmlDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,19 @@ public HtmlDocument()
public string Title { get { return _title.Text(); } set { _title.Text(value); } }
public HtmlTag Current { get { return _currentStack.Count == 0 ? _body : _currentStack.Peek(); } }
public HtmlTag Last { get; private set; }
public Action<string, string> FileWriter = writeToFile;
public Action<string> FileOpener = openFile;

public void WriteToFile(string fileName)
{
ensureFolderExists(fileName);

using (var writer = new StreamWriter(fileName))
{
writer.WriteLine(ToString());
}
FileWriter(fileName, ToString());
}


public void OpenInBrowser()
{
var filename = getPath();
WriteToFile(filename);

Process.Start(filename);
FileOpener(filename);
}

public HtmlTag Body
Expand All @@ -64,6 +59,16 @@ protected virtual string getPath()
return Path.GetTempFileName() + ".htm";
}

private static void writeToFile(string fileName, string fileContents)
{
ensureFolderExists(fileName);

using (var writer = new StreamWriter(fileName))
{
writer.WriteLine(fileContents);
}
}

private static void ensureFolderExists(string fileName)
{
var folder = Path.GetDirectoryName(fileName);
Expand All @@ -79,6 +84,11 @@ private static void ensureFolderExists(string fileName)
}
}

private static void openFile(string fileName)
{
Process.Start(fileName);
}

public HtmlTag Add(string tagName)
{
Last = Current.Add(tagName);
Expand Down

0 comments on commit 64a657f

Please sign in to comment.