Skip to content

Commit 9090fa8

Browse files
author
Simon Curtis
committed
All green tests!
1 parent ed7b0df commit 9090fa8

File tree

14 files changed

+231
-174
lines changed

14 files changed

+231
-174
lines changed

Clap.Net.Tests/Tests.cs

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public void LongFlag_IsTrue_WhenPresent()
3333
args.AsT0.All.ShouldBeTrue();
3434
}
3535

36+
[Fact]
37+
public void LongFlag_IsFalse_WhenNegated()
38+
{
39+
var args = LongFlagDefaultTrueApp.TryParse(["--no-all"]);
40+
args.AsT0.All.ShouldBeFalse();
41+
}
42+
3643
[Fact]
3744
public void LongFlag_IsFalse_WhenNotPresent()
3845
{
@@ -84,7 +91,6 @@ public void OptionWithValue_CanBeParsed_UsingShortAndLong()
8491
longResult.IsT0.ShouldBeTrue();
8592
longResult.AsT0.Name.ShouldBe("Bob");
8693

87-
// TODO: Split '=' expression this into two tokens
8894
var eqResult = OptionWithValueApp.TryParse(new[] { "--name=Carol" });
8995
eqResult.IsT0.ShouldBeTrue();
9096
eqResult.AsT0.Name.ShouldBe("Carol");
@@ -93,12 +99,13 @@ public void OptionWithValue_CanBeParsed_UsingShortAndLong()
9399
[Fact]
94100
public void AppendOption_CollectsMultipleValues()
95101
{
96-
var result = AppendOptionApp.TryParse(new[]
97-
{
98-
"-t", "one",
99-
"--tag", "two",
100-
"-t", "three"
101-
});
102+
var result = AppendOptionApp.TryParse(
103+
new[]
104+
{
105+
"-t", "one",
106+
"--tag", "two",
107+
"-t", "three"
108+
});
102109
result.IsT0.ShouldBeTrue();
103110
result.AsT0.Tags.ShouldBeSubsetOf(["one", "two", "three"]);
104111
}
@@ -112,7 +119,7 @@ public void CountFlag_IncrementsCorrectly()
112119
}
113120

114121
[Fact]
115-
public void HelpFlag_ReturnsShowHelp()
122+
public void HelpFlag_IsTrue_WhenPresent()
116123
{
117124
var result = HelpApp.TryParse(new[] { "--help" });
118125
result.IsT1.ShouldBeTrue("Expected ShowHelp on help flag");
@@ -125,18 +132,46 @@ public void MissingRequiredOption_ReturnsParseError()
125132
result.IsT3.ShouldBeTrue("Expected ParseError when required missing");
126133
result.AsT3.Message.ShouldContain("required", Case.Sensitive, "Error message should mention required");
127134
}
135+
136+
[Fact]
137+
public void UnexpectedCompoundFlag_ReturnsParseError()
138+
{
139+
var result = CountFlagApp.TryParse(["-pp"]);
140+
result.IsT3.ShouldBeTrue("Expected ParseError when required missing");
141+
result.AsT3.Message.ShouldBe("Unexpected flag supplied in compound flags 'p'");
142+
}
143+
144+
[Fact]
145+
public void TrailingParams_AreCollected_WhenPresent()
146+
{
147+
var result = TrailingArgsApp.TryParse(["example", "example1", "example2"]);
148+
result.IsT0.ShouldBeTrue();
149+
result.AsT0.FirstWord.ShouldBe("example");
150+
result.AsT0.RestOfTheWords.Length.ShouldBe(2);
151+
result.AsT0.RestOfTheWords[0].ShouldBe("example1");
152+
result.AsT0.RestOfTheWords[1].ShouldBe("example2");
153+
}
128154
}
129155

130156
[Command]
131157
public partial class ShortFlagApp
132158
{
133-
[Arg(Short = 'a')] public bool All { get; init; }
159+
[Arg(Short = 'a')]
160+
public bool All { get; init; }
134161
}
135162

136163
[Command]
137164
public partial class LongFlagApp
138165
{
139-
[Arg(Long = "all")] public bool All { get; init; }
166+
[Arg(Long = "all")]
167+
public bool All { get; init; }
168+
}
169+
170+
[Command]
171+
public partial class LongFlagDefaultTrueApp
172+
{
173+
[Arg(Long = "all", Negation = true)]
174+
public bool All { get; init; } = true;
140175
}
141176

142177
[Command]
@@ -155,7 +190,8 @@ public partial class DoubleRequiredPositionalApp
155190
[Command]
156191
public partial class SubCommandApp
157192
{
158-
[Command(Subcommand = true)] public required SubCommandCommand Command { get; init; }
193+
[Command(Subcommand = true)]
194+
public required SubCommandCommand Command { get; init; }
159195
}
160196

161197
[SubCommand]
@@ -171,8 +207,11 @@ public partial class Two : SubCommandCommand;
171207
[Command]
172208
public partial class CombinedShortFlagsApp
173209
{
174-
[Arg(Short = 'a')] public bool All { get; init; }
175-
[Arg(Short = 'b')] public bool Bold { get; init; }
210+
[Arg(Short = 'a')]
211+
public bool All { get; init; }
212+
213+
[Arg(Short = 'b')]
214+
public bool Bold { get; init; }
176215
}
177216

178217
[Command]
@@ -206,21 +245,26 @@ public partial class HelpApp
206245
[Command]
207246
public partial class RequiredOptionApp
208247
{
209-
[Arg(Short = 'r', Long = "req")] public required int Req { get; init; }
248+
[Arg(Short = 'r', Long = "req")]
249+
public required int Req { get; init; }
210250
}
211251

212252
[Command]
213253
public partial class FlagFollowedByBoolArgsApp
214254
{
215-
[Arg(Short = 't')] public bool Trim { get; init; }
255+
[Arg(Short = 't')]
256+
public bool Trim { get; init; }
216257

217-
[Arg] public required string Word { get; init; }
258+
[Arg]
259+
public required string Word { get; init; }
218260
}
219261

220262
[Command]
221263
public partial class TrailingArgsApp
222264
{
223-
[Arg] public required string FirstWord { get; init; }
265+
[Arg]
266+
public required string FirstWord { get; init; }
224267

225-
[Arg] public required string[] RestOfTheWords { get; init; }
268+
[Arg]
269+
public required string[] RestOfTheWords { get; init; }
226270
}

Clap.Net/Attributes/ArgAttribute.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public enum ArgAction
3636
SetFalse,
3737
Count,
3838
Help,
39-
HelpShort,
40-
HelpLong,
4139
Version,
4240
}
4341

0 commit comments

Comments
 (0)