typestar

Theories in C#

One test body, many cases.

using Xunit;

public class FizzTests
{
    static string Fizz(int n) => (n % 3, n % 5) switch
    {
        (0, 0) => "FizzBuzz", (0, _) => "Fizz",
        (_, 0) => "Buzz", _ => n.ToString(),
    };

    [Theory]
    [InlineData(3, "Fizz")]
    [InlineData(10, "Buzz")]
    [InlineData(15, "FizzBuzz")]
    [InlineData(7, "7")]
    public void Maps_the_classics(int n, string expected)
    {
        Assert.Equal(expected, Fizz(n));
    }
}

How it works

  1. [Theory] plus [InlineData] runs the body per row.
  2. The tuple-pattern switch is the code under test, in the same file.
  3. Four rows cover Fizz, Buzz, both and neither.

Keywords and builtins used here

The run, in numbers

Lines
20
Characters to type
373
Tokens
90
Three-star pace
85 tpm

At the three-star pace of 85 tokens a minute, this run takes about 64 seconds.

Type this snippet

Step 2 of 3 in Testing, step 11 of 16 in The open ecosystem.

← Previous Next →