Skip to content

Commit

Permalink
Merge pull request #388 from mcavigelli/Standard-Docs
Browse files Browse the repository at this point in the history
#385 Standard docs: make examples executable with NUnit
  • Loading branch information
mcavigelli committed Mar 17, 2021
2 parents 2c1f3b3 + 6f40e72 commit a25d078
Show file tree
Hide file tree
Showing 84 changed files with 620 additions and 6,040 deletions.
Binary file removed FileHelpers.Examples/Demos.ico
Binary file not shown.
77 changes: 77 additions & 0 deletions FileHelpers.Examples/ExampleBase.cs
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 FileHelpers.Examples/Examples/60.Sorting/30.SortBigFilesString2.cs

This file was deleted.

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 not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using FileHelpers;
using System.Collections;
using System.Collections.Generic;

namespace ExamplesFx
namespace FileHelpers.Examples.Advanced.MultiRecordEngine
{
//-> Name: Multi Record Processing
//-> Description: Read or write a file with many different layouts
Expand Down Expand Up @@ -110,11 +111,11 @@ public override string ToString()

//-> To work with this engine you must create one instance of it in this way:

public override void Run()
protected override void Run()
{
//-> File:RunEngine.cs

var engine = new MultiRecordEngine(typeof (Orders),
var engine = new FileHelpers.MultiRecordEngine(typeof (Orders),
typeof (Customer),
typeof (SampleType));

Expand All @@ -132,7 +133,7 @@ public override void Run()
//-> This is the selector that determines the record type based on whatever criteria you write

//-> File:Selector.cs
private Type CustomSelector(MultiRecordEngine engine, string recordLine)
private Type CustomSelector(FileHelpers.MultiRecordEngine engine, string recordLine)
{
if (recordLine.Length == 0)
return null;
Expand Down

0 comments on commit a25d078

Please sign in to comment.