Skip to content
This repository was archived by the owner on Jun 7, 2025. It is now read-only.

Commit 9f83743

Browse files
Refactor tests
Divide tests for match expressions and statements into separate classes. Also, upgrade dependencies.
1 parent cf6c92e commit 9f83743

3 files changed

Lines changed: 253 additions & 237 deletions

File tree

PatternMatching.Tests/MatchTests.cs renamed to PatternMatching.Tests/MatchExpressionTests.cs

Lines changed: 3 additions & 233 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,22 @@
99

1010
namespace PatternMatching
1111
{
12-
public class MatchTests
12+
public class MatchExpressionTests
1313
{
1414
[Fact]
1515
public void MatchCreateShouldNeverReturnNull()
16-
{
17-
Match.Create<int, string>()
18-
.Should()
19-
.NotBeNull();
20-
21-
Match.Create<int>()
16+
=> Match.Create<int, string>()
2217
.Should()
2318
.NotBeNull();
24-
}
2519

2620
[Theory]
2721
[InlineData(true)]
2822
[InlineData(false)]
2923
public void MatchCreateWithFallthroughShouldNeverReturnNull(bool fallthroughByDefault)
30-
{
31-
Match.Create<int, string>(fallthroughByDefault)
24+
=> Match.Create<int, string>(fallthroughByDefault)
3225
.Should()
3326
.NotBeNull();
3427

35-
Match.Create<int>(fallthroughByDefault)
36-
.Should()
37-
.NotBeNull();
38-
}
39-
4028
[Property(Arbitrary = new[] { typeof(Generators) })]
4129
public Property MatchExpressionShouldMatchPatternsCorrectly(Func<string, bool> predicate, string value)
4230
{
@@ -50,20 +38,6 @@ public Property MatchExpressionShouldMatchPatternsCorrectly(Func<string, bool> p
5038
return (matchSuccessful == pattern.Match(value).IsSome).ToProperty();
5139
}
5240

53-
[Property(Arbitrary = new[] { typeof(Generators) })]
54-
public Property MatchStatementShouldMatchPatternsCorrectly(Func<string, bool> predicate, string value)
55-
{
56-
var pattern = new SimplePattern<string>(predicate);
57-
bool matchSuccessful = false;
58-
59-
Match.Create<string>()
60-
.Case(pattern, _ => matchSuccessful = true)
61-
.Case(Pattern.Any<string>(), _ => matchSuccessful = false)
62-
.ExecuteOn(value);
63-
64-
return (matchSuccessful == pattern.Match(value).IsSome).ToProperty();
65-
}
66-
6741
[Property(Arbitrary = new[] { typeof(Generators) })]
6842
public Property NonStrictMatchExpressionShouldMatchPatternsCorrectly(Func<string, bool> predicate, string value)
6943
{
@@ -110,20 +84,6 @@ public Property NonStrictMatchExpressionShouldMatchPatternsCorrectlyWithNullable
11084
return (matchSuccessful == pattern.Match(value).IsSome).ToProperty();
11185
}
11286

113-
[Property(Arbitrary = new[] { typeof(Generators) })]
114-
public Property StrictMatchStatementShouldMatchPatternsCorrectly(Func<string, bool> predicate, string value)
115-
{
116-
var pattern = new SimplePattern<string>(predicate);
117-
bool matchSuccessful = false;
118-
119-
Match.Create<string>()
120-
.Case(pattern, _ => matchSuccessful = true)
121-
.Case(Pattern.Any<string>(), _ => matchSuccessful = false)
122-
.ExecuteStrict(value);
123-
124-
return (matchSuccessful == pattern.Match(value).IsSome).ToProperty();
125-
}
126-
12787
[Property(Arbitrary = new[] { typeof(Generators) })]
12888
public Property NonStrictMatchExpressionShouldReturnNothingIfNoMatchFound(Func<string, bool> predicate, string value)
12989
{
@@ -136,18 +96,6 @@ public Property NonStrictMatchExpressionShouldReturnNothingIfNoMatchFound(Func<s
13696
return (result.IsSome == pattern.Match(value).IsSome).ToProperty();
13797
}
13898

139-
[Property(Arbitrary = new[] { typeof(Generators) })]
140-
public Property MatchStatementShouldReturnFalseIfNoMatchFound(Func<string, bool> predicate, string value)
141-
{
142-
var pattern = new SimplePattern<string>(predicate);
143-
144-
bool matched = Match.Create<string>()
145-
.Case(pattern, _ => { })
146-
.ExecuteOn(value);
147-
148-
return (matched == pattern.Match(value).IsSome).ToProperty();
149-
}
150-
15199
[Property(Arbitrary = new[] { typeof(Generators) })]
152100
public void MatchExpressionShouldThrowIfNoMatchFound(Func<string, bool> predicate, string value)
153101
{
@@ -167,19 +115,6 @@ public void MatchExpressionShouldThrowIfNoMatchFound(Func<string, bool> predicat
167115
}
168116
}
169117

170-
[Property(Arbitrary = new[] { typeof(Generators) })]
171-
public void MatchStatementShouldNotThrowIfNoMatchFound(Func<string, bool> predicate, string value)
172-
{
173-
var pattern = new SimplePattern<string>(predicate);
174-
175-
Action action = () =>
176-
Match.Create<string>()
177-
.Case(pattern, _ => { })
178-
.ExecuteOn(value);
179-
180-
action.Should().NotThrow<MatchException>();
181-
}
182-
183118
[Property(Arbitrary = new[] { typeof(Generators) })]
184119
public void NonStrictMatchExpressionShouldNotThrowIfNoMatchFound(Func<string, bool> predicate, string value)
185120
{
@@ -193,25 +128,6 @@ public void NonStrictMatchExpressionShouldNotThrowIfNoMatchFound(Func<string, bo
193128
action.Should().NotThrow<MatchException>();
194129
}
195130

196-
[Property(Arbitrary = new[] { typeof(Generators) })]
197-
public void StrictMatchStatementShouldThrowIfNoMatchFound(Func<string, bool> predicate, string value)
198-
{
199-
var pattern = new SimplePattern<string>(predicate);
200-
201-
Action action = () =>
202-
Match.Create<string>()
203-
.Case(pattern, _ => { })
204-
.ExecuteStrict(value);
205-
206-
if (pattern.Match(value).IsSome)
207-
{
208-
action.Should().NotThrow<MatchException>();
209-
} else
210-
{
211-
action.Should().Throw<MatchException>();
212-
}
213-
}
214-
215131
[Property(Arbitrary = new[] { typeof(Generators) })]
216132
public Property MatchExpressionToFunctionShouldMatchPatternsCorrectly(
217133
Func<string, bool> predicate,
@@ -227,22 +143,6 @@ public Property MatchExpressionToFunctionShouldMatchPatternsCorrectly(
227143
return (matchSuccessful == pattern.Match(value).IsSome).ToProperty();
228144
}
229145

230-
[Property(Arbitrary = new[] { typeof(Generators) })]
231-
public Property MatchStatementToFunctionShouldMatchPatternsCorrectly(
232-
Func<string, bool> predicate,
233-
string value)
234-
{
235-
var pattern = new SimplePattern<string>(predicate);
236-
bool matchSuccessful = false;
237-
238-
Match.Create<string>()
239-
.Case(pattern, _ => matchSuccessful = true)
240-
.Case(Pattern.Any<string>(), _ => matchSuccessful = false)
241-
.ToFunction()(value);
242-
243-
return (matchSuccessful == pattern.Match(value).IsSome).ToProperty();
244-
}
245-
246146
[Property(Arbitrary = new[] { typeof(Generators) })]
247147
public Property NonStrictMatchExpressionToFunctionShouldMatchPatternsCorrectly(
248148
Func<string, bool> predicate,
@@ -291,22 +191,6 @@ public Property NonStrictMatchExpressionToFunctionShouldMatchPatternsCorrectlyWi
291191
return (matchSuccessful == pattern.Match(value).IsSome).ToProperty();
292192
}
293193

294-
[Property(Arbitrary = new[] { typeof(Generators) })]
295-
public Property StrictMatchStatementToFunctionShouldMatchPatternsCorrectly(
296-
Func<string, bool> predicate,
297-
string value)
298-
{
299-
var pattern = new SimplePattern<string>(predicate);
300-
bool matchSuccessful = false;
301-
302-
Match.Create<string>()
303-
.Case(pattern, _ => matchSuccessful = true)
304-
.Case(Pattern.Any<string>(), _ => matchSuccessful = false)
305-
.ToStrictFunction()(value);
306-
307-
return (matchSuccessful == pattern.Match(value).IsSome).ToProperty();
308-
}
309-
310194
[Property(Arbitrary = new[] { typeof(Generators) })]
311195
public Property NonStrictMatchExpressionToFunctionShouldReturnNothingIfNoMatchFound(
312196
Func<string, bool> predicate,
@@ -321,20 +205,6 @@ public Property NonStrictMatchExpressionToFunctionShouldReturnNothingIfNoMatchFo
321205
return (result.IsSome == pattern.Match(value).IsSome).ToProperty();
322206
}
323207

324-
[Property(Arbitrary = new[] { typeof(Generators) })]
325-
public Property MatchStatementToFunctionShouldReturnFalseIfNoMatchFound(
326-
Func<string, bool> predicate,
327-
string value)
328-
{
329-
var pattern = new SimplePattern<string>(predicate);
330-
331-
bool matched = Match.Create<string>()
332-
.Case(pattern, _ => { })
333-
.ToFunction()(value);
334-
335-
return (matched == pattern.Match(value).IsSome).ToProperty();
336-
}
337-
338208
[Property(Arbitrary = new[] { typeof(Generators) })]
339209
public void MatchExpressionToFunctionShouldThrowIfNoMatchFound(Func<string, bool> predicate, string value)
340210
{
@@ -354,19 +224,6 @@ public void MatchExpressionToFunctionShouldThrowIfNoMatchFound(Func<string, bool
354224
}
355225
}
356226

357-
[Property(Arbitrary = new[] { typeof(Generators) })]
358-
public void MatchStatementToFunctionShouldNotThrowIfNoMatchFound(Func<string, bool> predicate, string value)
359-
{
360-
var pattern = new SimplePattern<string>(predicate);
361-
362-
Action action = () =>
363-
Match.Create<string>()
364-
.Case(pattern, _ => { })
365-
.ToFunction()(value);
366-
367-
action.Should().NotThrow<MatchException>();
368-
}
369-
370227
[Property(Arbitrary = new[] { typeof(Generators) })]
371228
public void NonStrictMatchExpressionToFunctionShouldNotThrowIfNoMatchFound(
372229
Func<string, bool> predicate,
@@ -382,25 +239,6 @@ public void NonStrictMatchExpressionToFunctionShouldNotThrowIfNoMatchFound(
382239
action.Should().NotThrow<MatchException>();
383240
}
384241

385-
[Property(Arbitrary = new[] { typeof(Generators) })]
386-
public void StrictMatchStatementToFunctionShouldThrowIfNoMatchFound(Func<string, bool> predicate, string value)
387-
{
388-
var pattern = new SimplePattern<string>(predicate);
389-
390-
Action action = () =>
391-
Match.Create<string>()
392-
.Case(pattern, _ => { })
393-
.ToStrictFunction()(value);
394-
395-
if (pattern.Match(value).IsSome)
396-
{
397-
action.Should().NotThrow<MatchException>();
398-
} else
399-
{
400-
action.Should().Throw<MatchException>();
401-
}
402-
}
403-
404242
[Fact]
405243
public void MatchExpressionShouldThrowIfPatternIsNull()
406244
{
@@ -411,16 +249,6 @@ public void MatchExpressionShouldThrowIfPatternIsNull()
411249
action.Should().Throw<ArgumentNullException>();
412250
}
413251

414-
[Fact]
415-
public void MatchStatementShouldThrowIfPatternIsNull()
416-
{
417-
Action action = () =>
418-
Match.Create<string>()
419-
.Case<string>(null, _ => { });
420-
421-
action.Should().Throw<ArgumentNullException>();
422-
}
423-
424252
[Property(Arbitrary = new[] { typeof(Generators) })]
425253
public void MatchExpressionShouldThrowIfCaseFunctionIsNull(Func<string, bool> predicate)
426254
{
@@ -433,18 +261,6 @@ public void MatchExpressionShouldThrowIfCaseFunctionIsNull(Func<string, bool> pr
433261
action.Should().Throw<ArgumentNullException>();
434262
}
435263

436-
[Property(Arbitrary = new[] { typeof(Generators) })]
437-
public void MatchStatementShouldThrowIfCaseFunctionIsNull(Func<string, bool> predicate)
438-
{
439-
var pattern = new SimplePattern<string>(predicate);
440-
441-
Action action = () =>
442-
Match.Create<string>()
443-
.Case(pattern, null);
444-
445-
action.Should().Throw<ArgumentNullException>();
446-
}
447-
448264
[Fact]
449265
public void MatchExpressionShouldThrowIfCaseTypeFunctionIsNull()
450266
{
@@ -455,16 +271,6 @@ public void MatchExpressionShouldThrowIfCaseTypeFunctionIsNull()
455271
action.Should().Throw<ArgumentNullException>();
456272
}
457273

458-
[Fact]
459-
public void MatchStatementShouldThrowIfCaseTypeFunctionIsNull()
460-
{
461-
Action action = () =>
462-
Match.Create<string>()
463-
.Case<string>(null);
464-
465-
action.Should().Throw<ArgumentNullException>();
466-
}
467-
468274
[Theory]
469275
[InlineData(true)]
470276
[InlineData(false)]
@@ -477,18 +283,6 @@ public void MatchExpressionShouldThrowIfPatternWithFallthroughIsNull(bool fallth
477283
action.Should().Throw<ArgumentNullException>();
478284
}
479285

480-
[Theory]
481-
[InlineData(true)]
482-
[InlineData(false)]
483-
public void MatchStatementShouldThrowIfPatternWithFallthroughIsNull(bool fallthrough)
484-
{
485-
Action action = () =>
486-
Match.Create<string>()
487-
.Case<string>(null, fallthrough, _ => { });
488-
489-
action.Should().Throw<ArgumentNullException>();
490-
}
491-
492286
[Property(Arbitrary = new[] { typeof(Generators) })]
493287
public void MatchExpressionShouldThrowIfCaseFunctionWithFallthroughIsNull(
494288
Func<string, bool> predicate,
@@ -503,20 +297,6 @@ public void MatchExpressionShouldThrowIfCaseFunctionWithFallthroughIsNull(
503297
action.Should().Throw<ArgumentNullException>();
504298
}
505299

506-
[Property(Arbitrary = new[] { typeof(Generators) })]
507-
public void MatchStatementShouldThrowIfCaseFunctionWithFallthroughIsNull(
508-
Func<string, bool> predicate,
509-
bool fallthrough)
510-
{
511-
var pattern = new SimplePattern<string>(predicate);
512-
513-
Action action = () =>
514-
Match.Create<string>()
515-
.Case(pattern, fallthrough, null);
516-
517-
action.Should().Throw<ArgumentNullException>();
518-
}
519-
520300
[Property(Arbitrary = new[] { typeof(Generators) })]
521301
public void MatchExpressionShouldThrowIfCaseTypeFunctionWithFallthroughIsNull(bool fallthrough)
522302
{
@@ -526,15 +306,5 @@ public void MatchExpressionShouldThrowIfCaseTypeFunctionWithFallthroughIsNull(bo
526306

527307
action.Should().Throw<ArgumentNullException>();
528308
}
529-
530-
[Property(Arbitrary = new[] { typeof(Generators) })]
531-
public void MatchStatementShouldThrowIfCaseTypeFunctionWithFallthroughIsNull(bool fallthrough)
532-
{
533-
Action action = () =>
534-
Match.Create<string>()
535-
.Case<string>(fallthrough, null);
536-
537-
action.Should().Throw<ArgumentNullException>();
538-
}
539309
}
540310
}

0 commit comments

Comments
 (0)