aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/libutil/references.cc
blob: be2b59051e306fb2b50d285c0542426ac9e239a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "references.hh"
#include <gtest/gtest.h>

namespace nix {

using std::string;

struct RewriteParams {
    string originalString, finalString;
    StringMap rewrites;

    friend std::ostream& operator<<(std::ostream& os, const RewriteParams& bar) {
        StringSet strRewrites;
        for (auto & [from, to] : bar.rewrites)
            strRewrites.insert(from + "->" + to);
        return os <<
            "OriginalString: " << bar.originalString << std::endl <<
            "Rewrites: " << concatStringsSep(",", strRewrites) << std::endl <<
            "Expected result: " << bar.finalString;
    }
};

class RewriteTest : public ::testing::TestWithParam<RewriteParams> {
};

TEST_P(RewriteTest, IdentityRewriteIsIdentity) {
    RewriteParams param = GetParam();
    StringSink rewritten;
    auto rewriter = RewritingSink(param.rewrites, rewritten);
    rewriter(param.originalString);
    rewriter.flush();
    ASSERT_EQ(rewritten.s, param.finalString);
}

INSTANTIATE_TEST_CASE_P(
    references,
    RewriteTest,
    ::testing::Values(
        RewriteParams{ "foooo", "baroo", {{"foo", "bar"}, {"bar", "baz"}}},
        RewriteParams{ "foooo", "bazoo", {{"fou", "bar"}, {"foo", "baz"}}},
        RewriteParams{ "foooo", "foooo", {}}
    )
);

}