-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #388 from mcavigelli/Standard-Docs
#385 Standard docs: make examples executable with NUnit
- Loading branch information
Showing
84 changed files
with
620 additions
and
6,040 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using NUnit.Framework; | ||
|
||
namespace FileHelpers | ||
{ | ||
[TestFixture] | ||
public abstract class ExampleBase | ||
{ | ||
/// <summary> | ||
/// This property allows inheritors to call Console.Method() just like the static Console class. | ||
/// This fake console captures the output. The output is used for the documentation generation. | ||
/// </summary> | ||
protected VirtualConsole Console => new VirtualConsole(); | ||
|
||
/// <summary>Before each test, the input in the directory of the test is moved.</summary> | ||
[Test] | ||
public void ExecuteExample() | ||
{ | ||
var binaryDirectory = GetCompileDirectory(); | ||
var projectDirectory = GetProjectDirectory(binaryDirectory); | ||
var testDirectory = GetTestDirectory(projectDirectory); | ||
MoveFile(testDirectory, binaryDirectory); | ||
|
||
Run(); | ||
} | ||
|
||
protected abstract void Run(); | ||
|
||
private static void MoveFile(DirectoryInfo testDirectory, DirectoryInfo binaryDirectory) | ||
{ | ||
const string InputFileName = "input.txt"; | ||
var sourceFullName = Path.Combine(testDirectory.FullName, InputFileName); | ||
var targetFullName = Path.Combine(binaryDirectory.FullName, InputFileName); | ||
|
||
File.Copy(sourceFullName, targetFullName, true); | ||
} | ||
|
||
private DirectoryInfo GetTestDirectory(DirectoryInfo projectDirectory) | ||
{ | ||
const string NamespaceOfProject = "FileHelpers"; | ||
const string NamespaceDelimiter = "."; | ||
var fullNamespace = GetType().Namespace; | ||
Assert.NotNull(fullNamespace); | ||
var relativeNamespace = fullNamespace.Substring(NamespaceOfProject.Length + NamespaceDelimiter.Length); | ||
var relativeFoldersFromProject = relativeNamespace.Replace(NamespaceDelimiter, "/"); | ||
|
||
var testDir = Path.Combine(projectDirectory.FullName, relativeFoldersFromProject); | ||
|
||
return new DirectoryInfo(testDir); | ||
} | ||
|
||
private static DirectoryInfo GetProjectDirectory(DirectoryInfo binaryDirectory) | ||
{ | ||
const string WorkDirRelativeToProject = "bin/Debug/net40"; | ||
var depth = WorkDirRelativeToProject.Split('/').Length; | ||
var projectDirectory = binaryDirectory; | ||
for (var i = 0; i < depth; i++) | ||
{ | ||
var parent = projectDirectory.Parent; | ||
Assert.NotNull(parent); | ||
projectDirectory = parent; | ||
} | ||
|
||
return projectDirectory; | ||
} | ||
|
||
private DirectoryInfo GetCompileDirectory() | ||
{ | ||
var toExecutableThing = GetType().Assembly.Location; | ||
var di = new FileInfo(toExecutableThing).Directory; | ||
return di; | ||
} | ||
} | ||
} |
32 changes: 0 additions & 32 deletions
32
FileHelpers.Examples/Examples/60.Sorting/30.SortBigFilesString2.cs
This file was deleted.
Oops, something went wrong.
122 changes: 62 additions & 60 deletions
122
...es/50.Advanced/05.DynamicChangeOptions.cs → ...amicChangeOptions/DynamicChangeOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,63 @@ | ||
using FileHelpers; | ||
|
||
namespace ExamplesFx | ||
{ | ||
//-> Name: Dynamic Engine Options | ||
//-> Description: Change the options of the engines at run time | ||
|
||
public class EngineOptions | ||
: ExampleBase | ||
{ | ||
//-> FileIn:Input.txt | ||
/*ALFKI|Alfreds Futterkiste|Maria Anders|Sales Representative|Obere Str. 57|Berlin|Germany | ||
ANATR|Emparedados y Helados|Ana Trujillo|Owner|Avda. Constitución 2222|México D.F.|Mexico | ||
ANTON|Antonio Moreno Taquería|Antonio Moreno|Owner|Mataderos 2312|México D.F.|Mexico | ||
BERGS|Berglunds snabbköp|Christina Berglund|Administrator|Berguvsvägen 8|Luleå | ||
BLAUS|Blauer Delikatessen|Hanna Moos|Sales Rep|Forsterstr. 57|Mannheim|Germany | ||
BOLID|Bólido Comidas preparadas|Martín Sommer|Owner|C/ Araquil, 67|Madrid|Spain | ||
*/ | ||
|
||
//-> /File | ||
|
||
//-> File:CustomersVerticalBar.cs | ||
[DelimitedRecord("|")] | ||
public class CustomersVerticalBar | ||
{ | ||
public string CustomerID; | ||
|
||
// Will be excluded at run time | ||
public string DummyField; | ||
|
||
public string CompanyName; | ||
public string ContactName; | ||
public string ContactTitle; | ||
public string Address; | ||
public string City; | ||
public string Country; | ||
} | ||
|
||
//-> /File | ||
|
||
public override void Run() | ||
{ | ||
//-> File:Example.txt | ||
|
||
var engine = new DelimitedFileEngine<CustomersVerticalBar>(); | ||
|
||
engine.Options.Fields[2].TrimMode = TrimMode.Both; | ||
engine.Options.RemoveField("DummyField"); | ||
|
||
// City is optional | ||
engine.Options.Fields[engine.Options.Fields.Count - 1].IsOptional = true; | ||
|
||
engine.ReadFile("Input.txt"); | ||
|
||
//-> /File | ||
|
||
} | ||
|
||
|
||
} | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace FileHelpers.Examples.Advanced.DynamicChangeOptions | ||
{ | ||
//-> Name: Dynamic Engine Options | ||
//-> Description: Change the options of the engines at run time | ||
|
||
public class EngineOptions | ||
: ExampleBase | ||
{ | ||
//-> FileIn:Input.txt | ||
/*ALFKI|Alfreds Futterkiste|Maria Anders|Sales Representative|Obere Str. 57|Berlin|Germany | ||
ANATR|Emparedados y Helados|Ana Trujillo|Owner|Avda. Constitución 2222|México D.F.|Mexico | ||
ANTON|Antonio Moreno Taquería|Antonio Moreno|Owner|Mataderos 2312|México D.F.|Mexico | ||
BERGS|Berglunds snabbköp|Christina Berglund|Administrator|Berguvsvägen 8|Luleå | ||
BLAUS|Blauer Delikatessen|Hanna Moos|Sales Rep|Forsterstr. 57|Mannheim|Germany | ||
BOLID|Bólido Comidas preparadas|Martín Sommer|Owner|C/ Araquil, 67|Madrid|Spain | ||
*/ | ||
|
||
//-> /File | ||
|
||
//-> File:CustomersVerticalBar.cs | ||
[DelimitedRecord("|")] | ||
public class CustomersVerticalBar | ||
{ | ||
public string CustomerID; | ||
|
||
// Will be excluded at run time | ||
public string DummyField; | ||
|
||
public string CompanyName; | ||
public string ContactName; | ||
public string ContactTitle; | ||
public string Address; | ||
public string City; | ||
public string Country; | ||
} | ||
|
||
//-> /File | ||
|
||
protected override void Run() | ||
{ | ||
//-> File:Example.txt | ||
|
||
var engine = new DelimitedFileEngine<CustomersVerticalBar>(); | ||
|
||
engine.Options.Fields[2].TrimMode = TrimMode.Both; | ||
engine.Options.RemoveField("DummyField"); | ||
|
||
// City is optional | ||
engine.Options.Fields[engine.Options.Fields.Count - 1].IsOptional = true; | ||
|
||
engine.ReadFile("Input.txt"); | ||
|
||
//-> /File | ||
|
||
} | ||
|
||
|
||
} | ||
} |
Binary file added
BIN
+1022 Bytes
FileHelpers.Examples/Examples/Advanced/DynamicChangeOptions/Input.txt
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.