Skip to content

Commit

Permalink
Support creating ParsedFiles using a string as existingSeparator.
Browse files Browse the repository at this point in the history
In fact, internal implementation has changed to use strings.
Closes #3
  • Loading branch information
eduherminio committed Mar 9, 2018
1 parent c21f8ca commit c50930d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
7 changes: 6 additions & 1 deletion FileParserSolution/FileParser/Implementations/ParsedFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public ParsedFile(Queue<Queue<string>> parsedFile)
_value = parsedFile;
}

public ParsedFile(string path, char[] existingSeparator = null, string lineSeparatorToAdd = null)
public ParsedFile(string path, char[] existingSeparator, string lineSeparatorToAdd = null)
: this(path, new string(existingSeparator), lineSeparatorToAdd)
{
}

public ParsedFile(string path, string existingSeparator = null, string lineSeparatorToAdd = null)
{
_value = FileReader.ParseFile(path, existingSeparator);
}
Expand Down
8 changes: 4 additions & 4 deletions FileParserSolution/FileParser/Utils/FileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static internal class FileReader
/// <param name="existingSeparator"></param>
/// <param name="lineSeparatorToAdd"></param>
/// <returns></returns>
static public Queue<Queue<string>> ParseFile(string path, char[] existingSeparator = null)
static public Queue<Queue<string>> ParseFile(string path, string existingSeparator = null)
{
Queue<Queue<string>> parsedFile = new Queue<Queue<string>>();

Expand Down Expand Up @@ -59,7 +59,7 @@ static public Queue<Queue<string>> ParseFile(string path, char[] existingSeparat
/// <param name="path"></param>
/// <param name="separator"></param>
/// <returns></returns>
static public ICollection<string> ParseLine(string path, char[] separator = null)
static public ICollection<string> ParseLine(string path, string separator = null)
{
try
{
Expand Down Expand Up @@ -149,10 +149,10 @@ static public T Peek<T>(Queue<string> wordsInLine)
return StringConverter.Convert<T>(stringToConvert);
}

static private ICollection<string> ProcessLine(string original_line, char[] separator)
static private ICollection<string> ProcessLine(string original_line, string separator)
{
List<string> wordsInLine = original_line
.Split(separator)
.Split(separator?.ToCharArray())
.Select(str => str.Trim()).ToList();

wordsInLine.RemoveAll(string.IsNullOrWhiteSpace); // Probably not needed, but just in case
Expand Down
12 changes: 10 additions & 2 deletions FileParserSolution/FileParserTest/ParsedFile.ToListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,36 @@ public void SameFileDifferentSeparators()
{
ICollection<string> sampleSpaces = new ParsedFile(_sampleFolderPath + "Sample_spaces.txt").ToList<string>();
ICollection<string> sampleCommas = new ParsedFile(_sampleFolderPath + "Sample_commas.txt", new char[] { ',' }).ToList<string>();
ICollection<string> sampleCommas2 = new ParsedFile(_sampleFolderPath + "Sample_commas.txt", ",").ToList<string>();
ICollection<string> sampleSlashes = new ParsedFile(_sampleFolderPath + "Sample_doubleslashes.txt", new char[] { '/', '/' }).ToList<string>();
ICollection<string> sampleSlashes2 = new ParsedFile(_sampleFolderPath + "Sample_doubleslashes.txt", "//").ToList<string>();

Assert.True(sampleSpaces.Count > 1);

Assert.True(Enumerable.SequenceEqual(sampleSpaces, sampleCommas));
Assert.True(Enumerable.SequenceEqual(sampleSpaces, sampleCommas2));
Assert.True(Enumerable.SequenceEqual(sampleSpaces, sampleSlashes));
Assert.True(Enumerable.SequenceEqual(sampleSpaces, sampleSlashes2));
}

[Fact]
public void DifferentFilesSameSeparators()
{
ICollection<string> sampleSlashes = new ParsedFile(_sampleFolderPath + "Sample_doubleslashes.txt", new char[] { '/', '/' }).ToList<string>();
ICollection<string> modififedSampleSlashes = new ParsedFile(_sampleFolderPath + "SlightlyModified_Sample_doubleslashes.txt", new char[] { '/', '/' }).ToList<string>();
ICollection<string> modififedSampleSlashes2 = new ParsedFile(_sampleFolderPath + "SlightlyModified_Sample_doubleslashes.txt", "//").ToList<string>();

Assert.True(sampleSlashes.Count > 1);

Assert.NotEqual(sampleSlashes, modififedSampleSlashes);
Assert.NotEqual(sampleSlashes, modififedSampleSlashes2);
}

[Fact]
public void ListOfStrings()
{
string fileName = "Sample_ListOfints.txt";
char[] separator = "$$$$$$$".ToCharArray();
string separator = "$$$$$$$";
string sampleContent = " $$$$$$$one$$$$$$$two$$$$$$$three$$$$$$$four$$$$$$$ ";

StreamWriter writer = new StreamWriter(fileName);
Expand All @@ -48,8 +54,10 @@ public void ListOfStrings()
writer.WriteLine(sampleContent);
}

List<string> parsedArray = new ParsedFile(fileName, separator).ToList<string>();
List<string> parsedArray = new ParsedFile(fileName, separator.ToCharArray()).ToList<string>();
List<string> parsedArray2 = new ParsedFile(fileName, separator).ToList<string>();
Assert.Equal(new List<string>() { "one", "two", "three", "four" }, parsedArray);
Assert.Equal(new List<string>() { "one", "two", "three", "four" }, parsedArray2);
}

[Fact]
Expand Down

0 comments on commit c50930d

Please sign in to comment.