-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathCommandLineTests.cs
More file actions
85 lines (71 loc) · 2.42 KB
/
CommandLineTests.cs
File metadata and controls
85 lines (71 loc) · 2.42 KB
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
using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Macross.CommandLine.Tests
{
[TestClass]
public class CommandLineTests
{
[TestMethod]
public void CommandParsedTest()
{
CommandLineArguments Expected = new CommandLineArguments
{
Command = "push"
};
CommandLineArguments Actual = CommandLineArgumentParser.Parse(ParseCommandLine("push"));
Assert.AreEqual(Expected.Command, Actual.Command);
}
[TestMethod]
public void CommandAndParametersParsedTest()
{
CommandLineArguments Expected = new CommandLineArguments
{
Command = "push"
};
Expected.Parameters.Add("param1");
Expected.Parameters.Add("param2");
CommandLineArguments Actual = CommandLineArgumentParser.Parse(ParseCommandLine("push param1 param2"));
Assert.AreEqual(Expected.Command, Actual.Command);
Assert.IsTrue(Expected.Parameters.SequenceEqual(Actual.Parameters));
}
[TestMethod]
public void OptionsAndSwitchesParsedTest()
{
CommandLineArguments Expected = new CommandLineArguments();
Expected.Options.Add("option1", "value1");
Expected.Options.Add("option2", "value with spaces");
Expected.Options.Add("option3", "value3");
Expected.Switches.Add("switch1");
Expected.Switches.Add("switch2");
CommandLineArguments Actual = CommandLineArgumentParser.Parse(ParseCommandLine("-switch1 -option1 value1 --switch2 --option2=\"value with spaces\" -option3=value3"));
Assert.AreEqual(Expected.Command, Actual.Command);
Assert.IsTrue(Expected.Options.SequenceEqual(Actual.Options));
Assert.IsTrue(Expected.Switches.SequenceEqual(Actual.Switches));
}
/// <remarks>
/// Adapted from code posted by <a href="https://stackoverflow.com/questions/298830/split-string-containing-command-line-parameters-into-string-in-c-sharp/298990#298990">Daniel Earwicker </a>.
/// </remarks>
private static string[] ParseCommandLine(string commandLine)
{
bool inQuotes = false;
bool isEscaping = false;
return commandLine
.Split(c =>
{
if (c == '\\' && !isEscaping)
{
isEscaping = true;
return false;
}
if (c == '\"' && !isEscaping)
inQuotes = !inQuotes;
isEscaping = false;
return !inQuotes && char.IsWhiteSpace(c);
})
.Select(arg => arg.Trim().TrimBookendings('\"').Replace("\\\"", "\"", StringComparison.OrdinalIgnoreCase))
.Where(arg => !string.IsNullOrEmpty(arg))
.ToArray();
}
}
}