Skip to content

Commit e7a3fcd

Browse files
committed
Add int, double resolvers tests
1 parent 50c638f commit e7a3fcd

6 files changed

Lines changed: 184 additions & 83 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using MatthiWare.CommandLine;
5+
using Xunit;
6+
7+
namespace MatthiWare.CommandLineParser.Tests
8+
{
9+
public class CommandLineParserTests
10+
{
11+
[Fact]
12+
public void ParseTests()
13+
{
14+
15+
var parser = new CommandLineParser<Options>();
16+
17+
parser.Configure(opt => opt.Message)
18+
.ShortName("-m")
19+
.LongName("--message")
20+
.Default("Default message")
21+
.Required();
22+
23+
var parsed = parser.Parse(new string[] { "app.exe", "-m", "test" });
24+
25+
Assert.NotNull(parsed);
26+
27+
Assert.Equal("test", parsed.Result.Message);
28+
}
29+
30+
[Fact]
31+
public void ConfigureTests()
32+
{
33+
var parser = new CommandLineParser<Options>();
34+
35+
parser.Configure(opt => opt.Message)
36+
.ShortName("-m")
37+
.LongName("--message")
38+
.Default("Default message")
39+
.Required();
40+
41+
parser.Configure(opt => opt.Option2)
42+
.ShortName("-x")
43+
.LongName("--xsomething")
44+
.Required();
45+
46+
Assert.Equal(2, parser.Options.Count);
47+
48+
var message = parser.Options[0];
49+
var option = parser.Options[1];
50+
51+
Assert.NotNull(message);
52+
Assert.NotNull(option);
53+
54+
Assert.Equal("-m", message.ShortName);
55+
Assert.Equal("--message", message.LongName);
56+
Assert.True(message.HasDefault);
57+
58+
Assert.Equal("-x", option.ShortName);
59+
Assert.Equal("--xsomething", option.LongName);
60+
Assert.False(option.HasDefault);
61+
}
62+
63+
private class Options
64+
{
65+
public string Message { get; set; }
66+
public bool Option2 { get; set; }
67+
}
68+
}
69+
}

CommandLineParser.Tests/ExtensionsTests.cs

Lines changed: 24 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -9,87 +9,29 @@ namespace MatthiWare.CommandLineParser.Tests
99
{
1010
public class ExtensionsTests
1111
{
12-
[Fact]
13-
public void Test1()
14-
{
15-
16-
var parser = new CommandLineParser<Options>();
17-
18-
parser.Configure(opt => opt.Message)
19-
.ShortName("-m")
20-
.LongName("--message")
21-
.Default("Default message")
22-
.Required();
23-
24-
var parsed = parser.Parse(new string[] { "app.exe", "-m", "test" });
25-
26-
Assert.NotNull(parsed);
27-
28-
Assert.Equal("test", parsed.Result.Message);
29-
}
30-
31-
[Fact]
32-
public void Test2()
33-
{
34-
var parser = new CommandLineParser<Options>();
35-
36-
parser.Configure(opt => opt.Message)
37-
.ShortName("-m")
38-
.LongName("--message")
39-
.Default("Default message")
40-
.Required();
41-
42-
parser.Configure(opt => opt.Option2)
43-
.ShortName("-x")
44-
.LongName("--xsomething")
45-
.Required();
46-
47-
Assert.Equal(2, parser.Options.Count);
48-
49-
var message = parser.Options[0];
50-
var option = parser.Options[1];
51-
52-
Assert.NotNull(message);
53-
Assert.NotNull(option);
54-
55-
Assert.Equal("-m", message.ShortName);
56-
Assert.Equal("--message", message.LongName);
57-
Assert.True(message.HasDefault);
58-
59-
Assert.Equal("-x", option.ShortName);
60-
Assert.Equal("--xsomething", option.LongName);
61-
Assert.False(option.HasDefault);
62-
}
63-
64-
private class Options
65-
{
66-
public string Message { get; set; }
67-
public bool Option2 { get; set; }
68-
}
69-
70-
[Fact]
71-
public void TestV1()
72-
{
73-
var testString = "this is my test string \"with some quotes\" the end. '\"Here is some literal\"' ";
74-
75-
var resultArr = new string[] { "this", "is", "my", "test", "string", "with some quotes", "the", "end.", "\"Here is some literal\"", " " };
76-
77-
int i = 0;
78-
foreach (var token in Extensions.SplitOnWhitespace(testString))
79-
{
80-
Assert.Equal(resultArr[i++], token);
81-
}
82-
83-
Assert.Equal(resultArr.Length, i);
84-
}
85-
86-
[Theory]
87-
[InlineData("\"with some quotes\"", "with some quotes")]
88-
[InlineData("'\"with some quotes\"'", "\"with some quotes\"")]
89-
[InlineData("test", "test")]
90-
public void TestRemoveLiteralAndDoubleQuotes(string input, string result)
91-
{
92-
Assert.Equal(result, input.AsSpan().RemoveLiteralsAndQuotes());
93-
}
12+
//[Fact]
13+
//public void TestV1()
14+
//{
15+
// var testString = "this is my test string \"with some quotes\" the end. '\"Here is some literal\"' ";
16+
17+
// var resultArr = new string[] { "this", "is", "my", "test", "string", "with some quotes", "the", "end.", "\"Here is some literal\"", " " };
18+
19+
// int i = 0;
20+
// foreach (var token in Extensions.SplitOnWhitespace(testString))
21+
// {
22+
// Assert.Equal(resultArr[i++], token);
23+
// }
24+
25+
// Assert.Equal(resultArr.Length, i);
26+
//}
27+
28+
//[Theory]
29+
//[InlineData("\"with some quotes\"", "with some quotes")]
30+
//[InlineData("'\"with some quotes\"'", "\"with some quotes\"")]
31+
//[InlineData("test", "test")]
32+
//public void TestRemoveLiteralAndDoubleQuotes(string input, string result)
33+
//{
34+
// Assert.Equal(result, input.AsSpan().RemoveLiteralsAndQuotes());
35+
//}
9436
}
9537
}

CommandLineParser.Tests/Parsing/ResolverFactoryTest.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ namespace MatthiWare.CommandLineParser.Tests.Parsing
1212
public class ResolverFactoryTest
1313
{
1414

15+
private class RandomType { }
16+
1517
[Fact]
1618
public void ContainsWork()
1719
{
1820
var factory = new ResolverFactory();
1921

2022
Assert.True(factory.Contains<string>());
21-
Assert.False(factory.Contains<int>());
23+
Assert.True(factory.Contains<int>());
24+
Assert.True(factory.Contains<int>());
25+
26+
Assert.False(factory.Contains<RandomType>());
2227
}
2328

2429
[Fact]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Text;
5+
using MatthiWare.CommandLine.Abstractions.Models;
6+
using MatthiWare.CommandLine.Core.Parsing.Resolvers;
7+
using Xunit;
8+
9+
namespace MatthiWare.CommandLineParser.Tests.Parsing.Resolvers
10+
{
11+
12+
public class DoubleResolverTests
13+
{
14+
[Theory]
15+
[InlineData(true, "6E-14")]
16+
[InlineData(false, "false")]
17+
public void TestCanResolve(bool expected, string value)
18+
{
19+
var resolver = new DoubleResolver();
20+
var model = new ArgumentModel("key", value);
21+
22+
Assert.Equal(expected, resolver.CanResolve(model));
23+
}
24+
25+
[Theory]
26+
[InlineData(5.2, "5.2")]
27+
[InlineData(6E-14, "6E-14")]
28+
[InlineData(0.84551240822557006, "0.84551240822557006")]
29+
[InlineData(0.84551240822557, "0.84551240822557")]
30+
[InlineData(4.2, "4.2000000000000002")]
31+
[InlineData(4.2, "4.2")]
32+
[InlineData(double.NaN, "NaN")]
33+
[InlineData(double.NegativeInfinity, "-Infinity")]
34+
[InlineData(double.PositiveInfinity, "Infinity")]
35+
public void TestResolve(double expected, string value)
36+
{
37+
var resolver = new DoubleResolver();
38+
var model = new ArgumentModel("key", value);
39+
40+
Assert.Equal(expected, resolver.Resolve(model));
41+
}
42+
43+
44+
}
45+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using MatthiWare.CommandLine.Abstractions.Models;
5+
using MatthiWare.CommandLine.Core.Parsing.Resolvers;
6+
using Xunit;
7+
8+
namespace MatthiWare.CommandLineParser.Tests.Parsing.Resolvers
9+
{
10+
11+
public class IntResolverTests
12+
{
13+
14+
[Theory]
15+
[InlineData(true, "-m", "5")]
16+
[InlineData(false, "-m", "false")]
17+
public void TestCanResolve(bool expected, string key, string value)
18+
{
19+
var resolver = new IntResolver();
20+
var model = new ArgumentModel(key, value);
21+
22+
Assert.Equal(expected, resolver.CanResolve(model));
23+
}
24+
25+
[Theory]
26+
[InlineData(5, "-m", "5")]
27+
[InlineData(-5, "-m", "-5")]
28+
public void TestResolve(int expected, string key, string value)
29+
{
30+
var resolver = new IntResolver();
31+
var model = new ArgumentModel(key, value);
32+
33+
Assert.Equal(expected, resolver.Resolve(model));
34+
}
35+
36+
37+
}
38+
}

CommandLineParser.Tests/Parsing/Resolvers/StringResovlerTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class StringResovlerTests
1212

1313
[Theory]
1414
[InlineData(true, "-m", "test")]
15+
[InlineData(true, "-m", "my string")]
1516
public void TestCanResolve(bool expected, string key, string value)
1617
{
1718
var resolver = new StringResolver();
@@ -22,6 +23,7 @@ public void TestCanResolve(bool expected, string key, string value)
2223

2324
[Theory]
2425
[InlineData("test", "-m", "test")]
26+
[InlineData("my string", "-m", "my string")]
2527
public void TestResolve(string expected, string key, string value)
2628
{
2729
var resolver = new StringResolver();

0 commit comments

Comments
 (0)