Skip to content

Commit

Permalink
Cleaned up code to prepare for 1.0. (#17)
Browse files Browse the repository at this point in the history
Provided consistent formatting.
Updated for some C# 6.0 syntax.
Reduced several public APIs to internal APIs.
Bumped version.
  • Loading branch information
joncloud committed May 22, 2016
1 parent 7815375 commit 6a39399
Show file tree
Hide file tree
Showing 31 changed files with 1,002 additions and 879 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Install-Package ThorNet
Or update `project.json` to include a dependency on

```json
"ThorNet": "0.4.0"
"ThorNet": "1.0.0"
```

## Usage
Expand Down
4 changes: 2 additions & 2 deletions src/ThorNet.Sample/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
"type": "platform",
"version": "1.0.0-rc2-3002702"
},
"ThorNet": "0.4.0"
"ThorNet": "1.0.0"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
},
"version": "0.4.0"
"version": "1.0.0"
}
199 changes: 104 additions & 95 deletions src/ThorNet.UnitTests/MethodInfoWrapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,106 +3,115 @@
using System.Reflection;
using Xunit;

namespace ThorNet.UnitTests {
public class MethodInfoWrapperTests {
namespace ThorNet.UnitTests
{
public class MethodInfoWrapperTests
{

[InlineData(nameof(Helper.HasLongDescription), "", "", "LongDescription", typeof(void))]
[InlineData(nameof(Helper.Int), "", "", "", typeof(int))]
[InlineData(nameof(Helper.String), "", "", "", typeof(string))]
[InlineData(nameof(Helper.Void), "", "", "", typeof(void))]
[InlineData(nameof(Helper.Void_WithDescription), "Lorem", "", "", typeof(void))]
[InlineData(nameof(Helper.Void_WithDescriptionAndExample), "Ipsum", "Dolor", "", typeof(void))]
[InlineData(nameof(Helper.Void_WithExample), "", "Sit", "", typeof(void))]
[Theory]
public void Ctor_Tests(string name, string description, string example, string longDescription, Type returnType) {
MethodInfoWrapper target = Create(name);

Assert.Equal(name, target.Name);
Assert.Equal(description, target.Description);
Assert.Equal(example, target.Example);
[InlineData(nameof(Helper.String), "", "", "", typeof(string))]
[InlineData(nameof(Helper.Void), "", "", "", typeof(void))]
[InlineData(nameof(Helper.Void_WithDescription), "Lorem", "", "", typeof(void))]
[InlineData(nameof(Helper.Void_WithDescriptionAndExample), "Ipsum", "Dolor", "", typeof(void))]
[InlineData(nameof(Helper.Void_WithExample), "", "Sit", "", typeof(void))]
[Theory]
public void Ctor_Tests(string name, string description, string example, string longDescription, Type returnType)
{
MethodInfoWrapper target = Create(name);

Assert.Equal(name, target.Name);
Assert.Equal(description, target.Description);
Assert.Equal(example, target.Example);
Assert.Equal(longDescription, target.LongDescription);
Assert.Equal(returnType, target.ReturnType);
}

[InlineData("Int", 0)]
[InlineData("Int", 1)]
[InlineData("Int", 2)]
[InlineData("String", null)]
[InlineData("String", "a")]
[InlineData("String", "abc")]
[InlineData("Void", null)]
public void Invoke_Tests(string name, object expected) {
MethodInfoWrapper target = Create(name);

Helper helper = new Helper(expected);

object actual = target.Invoke(helper, new object[0]);

Assert.Equal(expected, actual);
}

[InlineData("Void_WithMethodOption", "alpha,omega", "a,o", "beginning,ending")]
public void Options_Tests(string name, string namesList, string aliasesList, string descriptionsList) {
MethodInfoWrapper target = Create(name);

string[] actualNames = target.Options.Select(o => o.Name).ToArray();
string[] actualAliases = target.Options.Select(o => o.Alias).ToArray();
string[] actualDescriptions = target.Options.Select(o => o.Description).ToArray();

string[] expectedNames = Utility.ToArray(namesList);
string[] expectedAliases = Utility.ToArray(aliasesList);
string[] expectedDescriptions = Utility.ToArray(descriptionsList);

Assert.Equal(expectedNames, actualNames);
Assert.Equal(expectedAliases, actualAliases);
Assert.Equal(expectedDescriptions, actualDescriptions);
}

[InlineData("Void", "")]
[InlineData("Void_WithParametersX", "x")]
[InlineData("Void_WithParametersXY", "x,y")]
[InlineData("Void_WithParametersXYZ", "x,y,z")]
public void Parameters_Tests(string name, string parametersList) {
MethodInfoWrapper target = Create(name);

Assert.True(target.Parameters.All(p => p is ParameterInfoWrapper), "MethodInfoWrapper does not use ParameterInfoWrapper.");

string[] actual = target.Parameters.Select(p => p.Name).ToArray();
string[] expected = Utility.ToArray(parametersList);

Assert.Equal(expected, actual);
}

private static MethodInfoWrapper Create(string name) {
MethodInfo method = typeof(Helper).GetMethod(name);

return new MethodInfoWrapper(method);
}

public class Helper {
private object _result;
public Helper(object result) {
_result = result;
}
Assert.Equal(returnType, target.ReturnType);
}

[InlineData("Int", 0)]
[InlineData("Int", 1)]
[InlineData("Int", 2)]
[InlineData("String", null)]
[InlineData("String", "a")]
[InlineData("String", "abc")]
[InlineData("Void", null)]
public void Invoke_Tests(string name, object expected)
{
MethodInfoWrapper target = Create(name);

Helper helper = new Helper(expected);

object actual = target.Invoke(helper, new object[0]);

Assert.Equal(expected, actual);
}

[InlineData("Void_WithMethodOption", "alpha,omega", "a,o", "beginning,ending")]
public void Options_Tests(string name, string namesList, string aliasesList, string descriptionsList)
{
MethodInfoWrapper target = Create(name);

string[] actualNames = target.Options.Select(o => o.Name).ToArray();
string[] actualAliases = target.Options.Select(o => o.Alias).ToArray();
string[] actualDescriptions = target.Options.Select(o => o.Description).ToArray();

string[] expectedNames = Utility.ToArray(namesList);
string[] expectedAliases = Utility.ToArray(aliasesList);
string[] expectedDescriptions = Utility.ToArray(descriptionsList);

Assert.Equal(expectedNames, actualNames);
Assert.Equal(expectedAliases, actualAliases);
Assert.Equal(expectedDescriptions, actualDescriptions);
}

[InlineData("Void", "")]
[InlineData("Void_WithParametersX", "x")]
[InlineData("Void_WithParametersXY", "x,y")]
[InlineData("Void_WithParametersXYZ", "x,y,z")]
public void Parameters_Tests(string name, string parametersList)
{
MethodInfoWrapper target = Create(name);

Assert.True(target.Parameters.All(p => p is ParameterInfoWrapper), "MethodInfoWrapper does not use ParameterInfoWrapper.");

string[] actual = target.Parameters.Select(p => p.Name).ToArray();
string[] expected = Utility.ToArray(parametersList);

Assert.Equal(expected, actual);
}

private static MethodInfoWrapper Create(string name)
{
MethodInfo method = typeof(Helper).GetMethod(name);

return new MethodInfoWrapper(method);
}

public class Helper
{
private object _result;
public Helper(object result)
{
_result = result;
}

[LongDesc("LongDescription")]
public void HasLongDescription() { }
public int Int() { return (int)_result; }
public string String() { return (string)_result; }
public void Void() { }
[Desc(null, "Lorem")]
public void Void_WithDescription() { }
[Desc("Dolor", "Ipsum")]
public void Void_WithDescriptionAndExample() { }
[Desc("Sit", null)]
public void Void_WithExample() { }
[Option("alpha", "a", "beginning")]
[Option("omega", "o", "ending")]
public void Void_WithMethodOption() { }
public void Void_WithParametersX(int x) { }
public void Void_WithParametersXY(int x, int y) { }
public void Void_WithParametersXYZ(int x, int y, int z) { }
}
}
public int Int() { return (int)_result; }
public string String() { return (string)_result; }
public void Void() { }
[Desc(null, "Lorem")]
public void Void_WithDescription() { }
[Desc("Dolor", "Ipsum")]
public void Void_WithDescriptionAndExample() { }
[Desc("Sit", null)]
public void Void_WithExample() { }

[Option("alpha", "a", "beginning")]
[Option("omega", "o", "ending")]
public void Void_WithMethodOption() { }
public void Void_WithParametersX(int x) { }
public void Void_WithParametersXY(int x, int y) { }
public void Void_WithParametersXYZ(int x, int y, int z) { }
}
}
}
115 changes: 62 additions & 53 deletions src/ThorNet.UnitTests/OptionSubstitutorTests.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace ThorNet.UnitTests {
public class OptionSubstitutorTests {
[InlineData("-1", "-1Single", "Single", "", false)]
[InlineData("--one", "--one=Single", "Single", "", false)]
[InlineData("-m,-m,-m", "-m1,-m2,-m3", "1,2,3", "", false)]
[InlineData("-1,-2,-3", "-1one,-2two,-3three", "one,two,three", "", false)]
[InlineData("--one,--two,--three", "--one=one,--two=two,--three=three", "one,two,three", "", false)]
[InlineData("-1", "a,b,c,-1one", "one", "a b c", false)]
[InlineData("--one", "--one", "", "", true)]
namespace ThorNet.UnitTests
{
public class OptionSubstitutorTests
{
[InlineData("-1", "-1Single", "Single", "", false)]
[InlineData("--one", "--one=Single", "Single", "", false)]
[InlineData("-m,-m,-m", "-m1,-m2,-m3", "1,2,3", "", false)]
[InlineData("-1,-2,-3", "-1one,-2two,-3three", "one,two,three", "", false)]
[InlineData("--one,--two,--three", "--one=one,--two=two,--three=three", "one,two,three", "", false)]
[InlineData("-1", "a,b,c,-1one", "one", "a b c", false)]
[InlineData("--one", "--one", "", "", true)]
[Theory]
public void Substitute_Tests(string optionsList, string textArgsList, string valuesList, string remainingArgs, bool allowFlag) {
OptionSubstitutor target = new OptionSubstitutor();

List<string> textArgs = Utility.ToArray(textArgsList).ToList();

Dictionary<string, Option> options = new Dictionary<string, Option>();
string[] optionsArray = Utility.ToArray(optionsList);
for (int i = 0; i < optionsArray.Length; i++) {
string option = optionsArray[i];
if (!options.ContainsKey(option)) {
options.Add(option, new Option(option) { AllowFlag = allowFlag });
}
}

Helper host = new Helper();
target.Substitute(host, options, textArgs);

string[] values = valuesList.Split(',');
int j = host.OptionValues.Count;
for (int i = 0; i < values.Length; i++) {
string value = values[i];
if (value != "") {
Option option = host.OptionValues[--j];
Assert.Equal(optionsArray[i], option.Alias);
Assert.Equal(value, option.Value);
}
}

string args = string.Join(" ", textArgs);
Assert.Equal(remainingArgs, args);
}

public class Helper : IThor {
public Helper() {
OptionValues = new List<Option>();
}
public void Substitute_Tests(string optionsList, string textArgsList, string valuesList, string remainingArgs, bool allowFlag)
{
OptionSubstitutor target = new OptionSubstitutor();

List<string> textArgs = Utility.ToArray(textArgsList).ToList();

Dictionary<string, Option> options = new Dictionary<string, Option>();
string[] optionsArray = Utility.ToArray(optionsList);
for (int i = 0; i < optionsArray.Length; i++)
{
string option = optionsArray[i];
if (!options.ContainsKey(option))
{
options.Add(option, new Option(option) { AllowFlag = allowFlag });
}
}

Helper host = new Helper();
target.Substitute(host, options, textArgs);

string[] values = valuesList.Split(',');
int j = host.OptionValues.Count;
for (int i = 0; i < values.Length; i++)
{
string value = values[i];
if (value != "")
{
Option option = host.OptionValues[--j];
Assert.Equal(optionsArray[i], option.Alias);
Assert.Equal(value, option.Value);
}
}

string args = string.Join(" ", textArgs);
Assert.Equal(remainingArgs, args);
}

internal class Helper : IThor
{
public Helper()
{
OptionValues = new List<Option>();
}

public IEnumerable<OptionAttribute> Options => new OptionAttribute[0];
public List<Option> OptionValues { get; }

public void AddOption(string name, string value) {
OptionValues.Add(new Option(name) { Value = value });
}
public bool HasOption(string name) { return false; }
}
}

public void AddOption(string name, string value)
{
OptionValues.Add(new Option(name) { Value = value });
}
public bool HasOption(string name) { return false; }
}
}
}
6 changes: 1 addition & 5 deletions src/ThorNet.UnitTests/OptionTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Xunit;

namespace ThorNet.UnitTests
{
Expand Down
Loading

0 comments on commit 6a39399

Please sign in to comment.