forked from fclp/fluent-command-line-parser
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUriTests.cs
157 lines (127 loc) · 5.31 KB
/
UriTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.Linq;
using Fclp.Internals.Extensions;
using NUnit.Framework;
namespace Fclp.Tests
{
/// <summary>
/// Tests for Uris
/// </summary>
[TestFixture]
public class UriTests
{
public class ExampleArgsContainer
{
public string UriAsString { get; set; }
public Uri Uri { get; set; }
}
[Test]
public void GenericFclp_UriAsString()
{
const char shortKey = 'u';
const string longKey = "uri";
const string uri = "http://services.internal/backoffce/service/svc";
var variations = CreateAllKeyVariations(shortKey, longKey, uri).ToList();
foreach (var combination in variations)
{
var fclp = new FluentCommandLineParser<ExampleArgsContainer>();
fclp.Setup(args => args.UriAsString)
.As(shortKey, longKey);
var result = fclp.Parse(combination.Args);
Assert.IsEmpty(result.Errors);
Assert.IsEmpty(result.AdditionalOptionsFound);
Assert.AreEqual(uri, fclp.Object.UriAsString);
}
}
[Test]
public void StandardFclp_UriAsString()
{
const char shortKey = 'u';
const string longKey = "uri";
const string uri = "http://services.internal/backoffce/service/svc";
string uriAsString = null;
var variations = CreateAllKeyVariations(shortKey, longKey, uri).ToList();
foreach (var combination in variations)
{
var fclp = new Fclp.FluentCommandLineParser();
fclp.Setup<string>(shortKey, longKey).Callback(val => uriAsString = val);
var result = fclp.Parse(combination.Args);
Assert.IsEmpty(result.Errors);
Assert.IsEmpty(result.AdditionalOptionsFound);
Assert.AreEqual(uri, uriAsString);
}
}
[Test]
public void GenericFclp_Uri()
{
const char shortKey = 'u';
const string longKey = "uri";
const string uri = "http://services.internal/backoffce/service/svc";
var variations = CreateAllKeyVariations(shortKey, longKey, uri).ToList();
foreach (var combination in variations)
{
var fclp = new FluentCommandLineParser<ExampleArgsContainer>();
fclp.Setup(args => args.Uri)
.As(shortKey, longKey);
var result = fclp.Parse(combination.Args);
Assert.IsEmpty(result.Errors);
Assert.IsEmpty(result.AdditionalOptionsFound);
Assert.AreEqual(uri, fclp.Object.Uri.AbsoluteUri);
}
}
[Test]
public void StandardFclp_Uri()
{
const char shortKey = 'u';
const string longKey = "uri";
const string uri = "http://services.internal/backoffce/service/svc";
Uri actualUri = null;
var variations = CreateAllKeyVariations(shortKey, longKey, uri).ToList();
foreach (var combination in variations)
{
var fclp = new Fclp.FluentCommandLineParser();
fclp.Setup<Uri>(shortKey, longKey).Callback(val => actualUri = val);
var result = fclp.Parse(combination.Args);
Assert.IsEmpty(result.Errors);
Assert.IsEmpty(result.AdditionalOptionsFound);
Assert.AreEqual(uri, actualUri.AbsoluteUri);
}
}
private static IEnumerable<TestArguments> CreateAllKeyVariations(char shortKey, string longKey, string value)
{
return CreateKeyVariations(new[] { "-", "/" }, shortKey.ToString(), value)
.Union(CreateKeyVariations(new[] { "--", "/" }, longKey, value));
}
private static IEnumerable<TestArguments> CreateKeyVariations(IEnumerable<string> keys, string option, string value)
{
var valueIdentifiers = new[] { '=', ':', ' ' };
foreach (string key in keys)
{
foreach (char valueIdentifier in valueIdentifiers)
{
yield return new TestArguments(key, option, valueIdentifier, value);
}
}
}
public class TestArguments
{
public TestArguments(string key, string option, char valueIdentifier, string value)
{
FriendlyArgs = string.Format("{0}{1}{2}{3}", key, option, valueIdentifier, value);
}
public string[] Args { get { return ParseArguments(FriendlyArgs); } }
public string FriendlyArgs { get; set; }
static string[] ParseArguments(string args)
{
args = ReplaceWithDoubleQuotes(args);
return args.SplitOnWhitespace().ToArray();
}
static string ReplaceWithDoubleQuotes(string args)
{
if (args == null) return null;
return args.Replace('\'', '"');
}
}
}
}