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

Commit cf6c92e

Browse files
Add more tests for match expressions
1 parent 5625bf8 commit cf6c92e

1 file changed

Lines changed: 229 additions & 0 deletions

File tree

PatternMatching.Tests/MatchTests.cs

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,195 @@ public void StrictMatchStatementShouldThrowIfNoMatchFound(Func<string, bool> pre
212212
}
213213
}
214214

215+
[Property(Arbitrary = new[] { typeof(Generators) })]
216+
public Property MatchExpressionToFunctionShouldMatchPatternsCorrectly(
217+
Func<string, bool> predicate,
218+
string value)
219+
{
220+
var pattern = new SimplePattern<string>(predicate);
221+
222+
bool matchSuccessful = Match.Create<string, bool>()
223+
.Case(pattern, _ => true)
224+
.Case(Pattern.Any<string>(), _ => false)
225+
.ToFunction()(value);
226+
227+
return (matchSuccessful == pattern.Match(value).IsSome).ToProperty();
228+
}
229+
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+
246+
[Property(Arbitrary = new[] { typeof(Generators) })]
247+
public Property NonStrictMatchExpressionToFunctionShouldMatchPatternsCorrectly(
248+
Func<string, bool> predicate,
249+
string value)
250+
{
251+
var pattern = new SimplePattern<string>(predicate);
252+
253+
bool matchSuccessful = Match.Create<string, bool>()
254+
.Case(pattern, _ => true)
255+
.Case(Pattern.Any<string>(), _ => false)
256+
.ToNonStrictFunction()(value)
257+
.IfNoneUnsafe(() => false);
258+
259+
return (matchSuccessful == pattern.Match(value).IsSome).ToProperty();
260+
}
261+
262+
[Property(Arbitrary = new[] { typeof(Generators) })]
263+
public Property NonStrictMatchExpressionToFunctionShouldMatchPatternsCorrectlyWithNull(
264+
Func<string, bool> predicate,
265+
string value)
266+
{
267+
var pattern = new SimplePattern<string>(predicate);
268+
269+
var matchSuccessful = Match.Create<string, bool?>()
270+
.Case(pattern, _ => true)
271+
.Case(Pattern.Any<string>(), _ => null)
272+
.ToNonStrictFunction()(value)
273+
.IfNoneUnsafe(() => false);
274+
275+
return (matchSuccessful == true == pattern.Match(value).IsSome).ToProperty();
276+
}
277+
278+
[Property(Arbitrary = new[] { typeof(Generators) })]
279+
public Property NonStrictMatchExpressionToFunctionShouldMatchPatternsCorrectlyWithNullable(
280+
Func<string, bool> predicate,
281+
string value)
282+
{
283+
var pattern = new SimplePattern<string>(predicate);
284+
285+
var matchSuccessful = Match.Create<string, bool?>()
286+
.Case(pattern, _ => true)
287+
.Case(Pattern.Any<string>(), _ => false)
288+
.ToNonStrictFunction()(value)
289+
.IfNoneUnsafe(() => null);
290+
291+
return (matchSuccessful == pattern.Match(value).IsSome).ToProperty();
292+
}
293+
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+
310+
[Property(Arbitrary = new[] { typeof(Generators) })]
311+
public Property NonStrictMatchExpressionToFunctionShouldReturnNothingIfNoMatchFound(
312+
Func<string, bool> predicate,
313+
string value)
314+
{
315+
var pattern = new SimplePattern<string>(predicate);
316+
317+
var result = Match.Create<string, bool>()
318+
.Case(pattern, _ => true)
319+
.ToNonStrictFunction()(value);
320+
321+
return (result.IsSome == pattern.Match(value).IsSome).ToProperty();
322+
}
323+
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+
338+
[Property(Arbitrary = new[] { typeof(Generators) })]
339+
public void MatchExpressionToFunctionShouldThrowIfNoMatchFound(Func<string, bool> predicate, string value)
340+
{
341+
var pattern = new SimplePattern<string>(predicate);
342+
343+
Action action = () =>
344+
Match.Create<string, bool>()
345+
.Case(pattern, _ => true)
346+
.ToFunction()(value);
347+
348+
if (pattern.Match(value).IsSome)
349+
{
350+
action.Should().NotThrow<MatchException>();
351+
} else
352+
{
353+
action.Should().Throw<MatchException>();
354+
}
355+
}
356+
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+
370+
[Property(Arbitrary = new[] { typeof(Generators) })]
371+
public void NonStrictMatchExpressionToFunctionShouldNotThrowIfNoMatchFound(
372+
Func<string, bool> predicate,
373+
string value)
374+
{
375+
var pattern = new SimplePattern<string>(predicate);
376+
377+
Action action = () =>
378+
Match.Create<string, bool>()
379+
.Case(pattern, _ => true)
380+
.ToNonStrictFunction()(value);
381+
382+
action.Should().NotThrow<MatchException>();
383+
}
384+
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+
215404
[Fact]
216405
public void MatchExpressionShouldThrowIfPatternIsNull()
217406
{
@@ -256,6 +445,26 @@ public void MatchStatementShouldThrowIfCaseFunctionIsNull(Func<string, bool> pre
256445
action.Should().Throw<ArgumentNullException>();
257446
}
258447

448+
[Fact]
449+
public void MatchExpressionShouldThrowIfCaseTypeFunctionIsNull()
450+
{
451+
Action action = () =>
452+
Match.Create<string, bool>()
453+
.Case<string>(null);
454+
455+
action.Should().Throw<ArgumentNullException>();
456+
}
457+
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+
259468
[Theory]
260469
[InlineData(true)]
261470
[InlineData(false)]
@@ -307,5 +516,25 @@ public void MatchStatementShouldThrowIfCaseFunctionWithFallthroughIsNull(
307516

308517
action.Should().Throw<ArgumentNullException>();
309518
}
519+
520+
[Property(Arbitrary = new[] { typeof(Generators) })]
521+
public void MatchExpressionShouldThrowIfCaseTypeFunctionWithFallthroughIsNull(bool fallthrough)
522+
{
523+
Action action = () =>
524+
Match.Create<string, bool>()
525+
.Case<string>(fallthrough, null);
526+
527+
action.Should().Throw<ArgumentNullException>();
528+
}
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+
}
310539
}
311540
}

0 commit comments

Comments
 (0)