Skip to content

Commit

Permalink
new class for converter testing created
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton92nd committed Nov 9, 2014
1 parent c4346d4 commit a14af4a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 56 deletions.
57 changes: 1 addition & 56 deletions Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,16 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;

namespace Mark
{
class Converter
{
static public readonly string[] LineEndings = { "\r\n", "\r", "\n" };

private static string[] GetParagraphsFromText(string[] text)
{
var result = new List<string>();
while (text.Length > 0)
{
text = text.SkipWhile(x => x.Trim().Length == 0).ToArray();
var paragraphLines = text.TakeWhile(x => x.Trim().Length > 0);
text = text.SkipWhile(x => x.Trim().Length > 0).ToArray();
var paragraph = String.Join(" ", paragraphLines);
if (paragraph.Trim().Length > 0)
result.Add("<p>\n" + paragraph + "\n</p>");
}
return result.ToArray();
}

static public string ConvertString(string content)
{
var paragraphs = GetParagraphsFromText(content.Split(LineEndings, StringSplitOptions.None));
string result = "<html>";
foreach (var i in paragraphs)
{
result += '\n' + i;
}
string result = "<html>\n" + content;
return result + "\n</html>\n";
}

Expand All @@ -54,38 +33,4 @@ static void Main(string[] args)
ConvertFile(fileName);
}
}

[TestFixture]
class Converter_should
{
[Test]
public void throw_exception_if_no_such_file()
{
Assert.Throws<FileNotFoundException>(() => Converter.ConvertFile("no_such_file.txt"));
}

[Test]
public void create_file_with_html_extension()
{
Converter.ConvertFile("sample.txt");
Assert.True(File.Exists("sample.html"));
}

[Test]
public void convert_empty_file_into_file_with_html_tag()
{
Converter.ConvertFile("empty.txt");
var result = new StreamReader("empty.html").ReadToEnd().Split(Converter.LineEndings, StringSplitOptions.RemoveEmptyEntries);
var expected = new StreamReader("emptyResult.txt").ReadToEnd().Split(Converter.LineEndings, StringSplitOptions.RemoveEmptyEntries);
Assert.AreEqual(expected, result);
}

[Test]
public void add_p_tags_around_paragraphs()
{
string text = "This\nis\nfirst paragraph\n \nThis is\nsecond\n";
string result = Converter.ConvertString(text);
Assert.AreEqual("<html>\n<p>\nThis is first paragraph\n</p>\n<p>\nThis is second\n</p>\n</html>\n", result);
}
}
}
44 changes: 44 additions & 0 deletions Converter_should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;

namespace Mark
{
[TestFixture]
class Converter_should
{
[Test]
public void throw_exception_if_no_such_file()
{
Assert.Throws<FileNotFoundException>(() => Converter.ConvertFile("no_such_file.txt"));
}

[Test]
public void create_file_with_html_extension()
{
Converter.ConvertFile("sample.txt");
Assert.True(File.Exists("sample.html"));
}

[Test]
public void convert_empty_file_into_file_with_html_tag()
{
Converter.ConvertFile("empty.txt");
var result = new StreamReader("empty.html").ReadToEnd().Split(Converter.LineEndings, StringSplitOptions.RemoveEmptyEntries);
var expected = new StreamReader("emptyResult.txt").ReadToEnd().Split(Converter.LineEndings, StringSplitOptions.RemoveEmptyEntries);
Assert.AreEqual(expected, result);
}

[Test]
public void add_p_tags_around_paragraphs()
{
string text = "This\nis\nfirst paragraph\n \nThis is\nsecond\n";
string result = Converter.ConvertString(text);
Assert.AreEqual("<html>\n<p>\nThis is first paragraph\n</p>\n<p>\nThis is second\n</p>\n</html>\n", result);
}
}
}
1 change: 1 addition & 0 deletions Mark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Converter.cs" />
<Compile Include="Converter_should.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit a14af4a

Please sign in to comment.