TinyGWTParser 1.0.29

dotnet add package TinyGWTParser --version 1.0.29
                    
NuGet\Install-Package TinyGWTParser -Version 1.0.29
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TinyGWTParser" Version="1.0.29" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TinyGWTParser" Version="1.0.29" />
                    
Directory.Packages.props
<PackageReference Include="TinyGWTParser" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TinyGWTParser --version 1.0.29
                    
#r "nuget: TinyGWTParser, 1.0.29"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TinyGWTParser@1.0.29
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TinyGWTParser&version=1.0.29
                    
Install as a Cake Addin
#tool nuget:?package=TinyGWTParser&version=1.0.29
                    
Install as a Cake Tool

TinyGivenWhenThenParser

Build status

Summary

TinyGivenWhenThenParser is a lightweight, fluent Given-When-Then test parser library for .NET. It allows you to build clean, human-readable Behavior-Driven Development (BDD) style scenarios directly within your existing unit tests without the complex setup, heavy installations, or external dependencies required by full frameworks like SpecFlow.

Why Use TinyGivenWhenThenParser?

When writing tests, full-scale BDD frameworks can introduce unwanted overhead, heavy extensions, and rigid tool setups. TinyGivenWhenThenParser provides a simple, regex-pattern-based alternative to map user story strings directly to executable test methods.

Use TinyGivenWhenThenParser when you want to:

  • Parse structured BDD data from strings or multi-line test cases using Regex patterns.
  • Map parsed string values into dynamic properties or construct custom strongly-typed objects on the fly.
  • Extract tabular GWT row data effortlessly into list collections or strongly-typed vertical/horizontal data tables.
  • Avoid installing complex third-party IDE plugins, maintaining seamless portability for anyone pulling your repository.

TinyGivenWhenThenParser gives you complete data extraction power (strings, collections, custom types, and data tables) while maintaining the pure speed of your favorite vanilla unit test runners.

Source Code

https://github.com/wjeon/TinyGivenWhenThenParser

Automatic 'And' Context Swapping

When parsing multi-line scenarios, the parser intelligently replaces leading And tokens with the preceding line's context (Given, When, or Then) automatically. This lets you write natural-looking, flowing text blocks while mapping lines cleanly against uniform regex definitions.

Example Usage:

Initialize a GWT Parser with a Test Case

[SetUp]
public void SetUp()
{
    const string multilineCase = @"Given Tom has 2 apples and 3 oranges
                                   And Jerry has 1 apple and 1 orange";
                                   
    // Initialize parser with the string scenario to analyze
    _gwtParser = TinyGWTParser.WithTestCase(multilineCase);
}

Parse Simple Data from a Single Matching Line

[Test]
public void Your_test_method()
{
    // Define your Regex pattern with capture groups
    var parseResult = _gwtParser
        .WithPattern(@"^Given (.*) has (\d+) apple(?:s|) and (\d+) orange(?:s|)$")
        .ParseSingleLine();

    // Access parsed string arrays via the result object
    string[] lineData = parseResult.ParsedData.Line; 
    // Result: ["Tom", "2", "3"]
}

Parse Multiple Lines matching a Pattern (with Automatic Context Swap)

[Test]
public void Your_test_method()
{
    // Automatically transforms 'And' lines into their preceding 'Given' or 'When/Then' keys
    var parseResult = _gwtParser
        .WithPattern(@"Given (.*) has (\d+) apple(?:s|) and (\d+) orange(?:s|)")
        .ParseMultiLines(From.TestCaseReplacedAndWithGivenWhenThen);

    // Iterates through every matching scenario group captured
    foreach(var match in parseResult.ParsedData)
    {
        string[] lineData = match.Line;
        // 1st Loop: ["Tom", "2", "3"]
        // 2nd Loop: ["Jerry", "1", "1"]
    }
}

Dynamically Convert Parsed Tokens into Specific Types (.To Method)

[Test]
public void Your_test_method()
{
    const string @case = "Given test data - Name: Tom, Age: 2, Favorite Fruit: apple, ID: 6579328A-B45A-48EB-BC1C-68018157F47A";
    var parser = TinyGWTParser.WithTestCase(@case);

    // Bind capture strings directly to specific keys and primitive/complex types
    var result = parser.WithPattern(@"Given test data - Name: (.*), Age: (\d+), Favorite Fruit: (apple|orange), ID: (.*)")
        .To("Name".As<string>(), "Age".As<int>(), "FavoriteFruit".As<Fruit>(), "Id".As<Guid>())
        .ParseSingleLine();

    // Access values cleanly as properties via a dynamically typed output object
    var name = result.ParsedData.Line.Name; // "Tom" (string)
    var age = result.ParsedData.Line.Age;   // 2 (int)
}

Construct Strongly-Typed Class Instances or Properties directly

[Test]
public void Your_test_method()
{
    // Instruct the parser to map the parameters automatically via Using.Constructor or Using.Properties
    var result = _gwtParser
        .WithPattern(@"Given (.*) has (\d+) apple(?:s|) and (\d+) orange(?:s|)")
        .To("name".As<string>(), "quantity".As<int>(), "favoriteFruit".As<Fruit>(), "date".As<DateTime>(), "time".As<TimeSpan>(), "dateTimeOffset".As<DateTimeOffset>(), "id".As<Guid>(), "hourOfDay".As<HourOfDay>())
        .ParseMultiLines<ObjectWithConstructor>(Using.Constructor);
        
    // Optionally inject static contextual variables not present in the text string
    var advancedResult = _gwtParser
        .WithPattern(@"Given Time: (.*)")
        .To("time".As<TimeSpan>())
        .WithAdditionalValuesOf("Tom".For("name"), Fruit.Apple.For("favoriteFruit"))
        .ParseSingleLine<ObjectWithConstructor>(Using.Constructor);
}

Parse Markdown GWT Tables into Strongly-Typed Collections

[Test]
public void Your_test_method()
{
    const string tableCase = @"Given following attendees
                               +-------+------+----------+
                               | name  | team | title    |
                               +-------+------+----------+
                               | Tom   | 6    | TechLead |
                               | Jerry | 2    | Manager  |
                               +-------+------+----------+";

    var parser = TinyGWTParser.WithTestCase(tableCase);

    // Directly bind GWT Markdown tables (supports horizontal and vertical flipped tables out of the box)
    var result = parser.WithPattern(@"^Given following attendees$")
                       .ParseSingleLine()
                       .WithTableOf<Attendee>();

    var firstAttendeeName = result.ParsedData.Table.First().Name; // "Tom"
    var firstAttendeeTeam = result.ParsedData.Table.First().Team; // 6
}

Additional Notes

  • Custom Mappers: You can declare custom mapper definitions by implementing IParser<T> to handle custom text patterns (like reading complex DateTime structures) effortlessly.
  • Safe Evaluation: If a scenario text block contains no matching lines, the dynamic parser gracefully initializes fields with their default(T) equivalents instead of crashing your suite with unhandled exceptions.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.29 197 10/19/2026
1.0.3 6,807 6/1/2020
0.0.49-beta 1,403 5/27/2019
0.0.40-beta 1,138 11/13/2018
0.0.36-beta 2,837 2/28/2017
0.0.31-beta 2,059 2/14/2017
0.0.22-beta 2,140 1/3/2017

Initial release.