Skip to content

Latest commit

 

History

History
116 lines (92 loc) · 4.36 KB

ScrubNonDeterministicOutput.md

File metadata and controls

116 lines (92 loc) · 4.36 KB

How to Scrub Non-Deterministic Output

Contents

Introduction

If you are having trouble getting tests running reproducibly, you might need to use a "scrubber" to convert the non-deterministic text to something stable.

Interface

Fundamentally, a scrubber is function that takes a string and returns a string. You can create ones by passing in a function or a lambda. We also have some pre-made ones for your convenience.

Lambda example

Approvals::verify(
    "1 2 3 4 5 6",
    Options().withScrubber(
        [](const std::string& t) {return StringUtils::replaceAll(t, "3", "Fizz");}
    ));

snippet source | anchor

This would produce:

1 2 Fizz 4 5 6

snippet source | anchor

(In the real world, scrubbers are generally used to remove text that is expected to differ between test runs... Here, we use a trivial example for ease of explanation.)

Pre-made Scrubbers

GUID

You can scrub GUIDs by using a pointer to the function Scrubbers::scrubGuid, for example the following code:

std::string jsonFromRestCall = R"(
    {
        child: {
            id: b34b4da8-090e-49d8-bd35-7e79f633a2ea
            parent1: 2fd78d4a-ad49-447d-96a8-deda585a9aa5
            parent2: 05f77de3-3790-4d45-b045-def96c9cd371
        }
        person: {
            name: mom
            id: 2fd78d4a-ad49-447d-96a8-deda585a9aa5
        }
        person: {
            name: dad
            id: 05f77de3-3790-4d45-b045-def96c9cd371
        }
    }
    )";
Approvals::verify(jsonFromRestCall, Options().withScrubber(Scrubbers::scrubGuid));

snippet source | anchor

will produce:

        {
            child: {
                id: guid_1
                parent1: guid_2
                parent2: guid_3
            }
            person: {
                name: mom
                id: guid_2
            }
            person: {
                name: dad
                id: guid_3
            }
        }
        

snippet source | anchor

Notice that when GUIDs are repeated within the same file, they are replaced with the same text.


Back to User Guide