aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/libstore/outputs-spec.cc
blob: 72a7da97481eca603b82fff07360ae4d399708f8 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "tests/outputs-spec.hh"

#include <nlohmann/json.hpp>
#include <gtest/gtest.h>
#include <rapidcheck/gtest.h>

namespace nix {

TEST(OutputsSpec, no_empty_names) {
    ASSERT_DEATH(OutputsSpec::Names { std::set<std::string> { } }, "");
}

#define TEST_DONT_PARSE(NAME, STR)           \
    TEST(OutputsSpec, bad_ ## NAME) {        \
        std::optional OutputsSpecOpt =       \
            OutputsSpec::parseOpt(STR);      \
        ASSERT_FALSE(OutputsSpecOpt);        \
    }

TEST_DONT_PARSE(empty, "")
TEST_DONT_PARSE(garbage, "&*()")
TEST_DONT_PARSE(double_star, "**")
TEST_DONT_PARSE(star_first, "*,foo")
TEST_DONT_PARSE(star_second, "foo,*")

#undef TEST_DONT_PARSE

TEST(OutputsSpec, all) {
    std::string_view str = "*";
    OutputsSpec expected = OutputsSpec::All { };
    ASSERT_EQ(OutputsSpec::parse(str), expected);
    ASSERT_EQ(expected.to_string(), str);
}

TEST(OutputsSpec, names_out) {
    std::string_view str = "out";
    OutputsSpec expected = OutputsSpec::Names { "out" };
    ASSERT_EQ(OutputsSpec::parse(str), expected);
    ASSERT_EQ(expected.to_string(), str);
}

TEST(OutputsSpec, names_underscore) {
    std::string_view str = "a_b";
    OutputsSpec expected = OutputsSpec::Names { "a_b" };
    ASSERT_EQ(OutputsSpec::parse(str), expected);
    ASSERT_EQ(expected.to_string(), str);
}

TEST(OutputsSpec, names_numberic) {
    std::string_view str = "01";
    OutputsSpec expected = OutputsSpec::Names { "01" };
    ASSERT_EQ(OutputsSpec::parse(str), expected);
    ASSERT_EQ(expected.to_string(), str);
}

TEST(OutputsSpec, names_out_bin) {
    OutputsSpec expected = OutputsSpec::Names { "out", "bin" };
    ASSERT_EQ(OutputsSpec::parse("out,bin"), expected);
    // N.B. This normalization is OK.
    ASSERT_EQ(expected.to_string(), "bin,out");
}

#define TEST_SUBSET(X, THIS, THAT) \
    X((OutputsSpec { THIS }).isSubsetOf(THAT));

TEST(OutputsSpec, subsets_all_all) {
    TEST_SUBSET(ASSERT_TRUE, OutputsSpec::All { }, OutputsSpec::All { });
}

TEST(OutputsSpec, subsets_names_all) {
    TEST_SUBSET(ASSERT_TRUE, OutputsSpec::Names { "a" }, OutputsSpec::All { });
}

TEST(OutputsSpec, subsets_names_names_eq) {
    TEST_SUBSET(ASSERT_TRUE, OutputsSpec::Names { "a" }, OutputsSpec::Names { "a" });
}

TEST(OutputsSpec, subsets_names_names_noneq) {
    TEST_SUBSET(ASSERT_TRUE, OutputsSpec::Names { "a" }, (OutputsSpec::Names { "a", "b" }));
}

TEST(OutputsSpec, not_subsets_all_names) {
    TEST_SUBSET(ASSERT_FALSE, OutputsSpec::All { }, OutputsSpec::Names { "a" });
}

TEST(OutputsSpec, not_subsets_names_names) {
    TEST_SUBSET(ASSERT_FALSE, (OutputsSpec::Names { "a", "b" }), (OutputsSpec::Names { "a" }));
}

#undef TEST_SUBSET

#define TEST_UNION(RES, THIS, THAT) \
    ASSERT_EQ(OutputsSpec { RES }, (OutputsSpec { THIS }).union_(THAT));

TEST(OutputsSpec, union_all_all) {
    TEST_UNION(OutputsSpec::All { }, OutputsSpec::All { }, OutputsSpec::All { });
}

TEST(OutputsSpec, union_all_names) {
    TEST_UNION(OutputsSpec::All { }, OutputsSpec::All { }, OutputsSpec::Names { "a" });
}

TEST(OutputsSpec, union_names_all) {
    TEST_UNION(OutputsSpec::All { }, OutputsSpec::Names { "a" }, OutputsSpec::All { });
}

TEST(OutputsSpec, union_names_names) {
    TEST_UNION((OutputsSpec::Names { "a", "b" }), OutputsSpec::Names { "a" }, OutputsSpec::Names { "b" });
}

#undef TEST_UNION

#define TEST_DONT_PARSE(NAME, STR)                   \
    TEST(ExtendedOutputsSpec, bad_ ## NAME) {        \
        std::optional extendedOutputsSpecOpt =       \
            ExtendedOutputsSpec::parseOpt(STR);      \
        ASSERT_FALSE(extendedOutputsSpecOpt);        \
    }

TEST_DONT_PARSE(carot_empty, "^")
TEST_DONT_PARSE(prefix_carot_empty, "foo^")
TEST_DONT_PARSE(garbage, "^&*()")
TEST_DONT_PARSE(double_star, "^**")
TEST_DONT_PARSE(star_first, "^*,foo")
TEST_DONT_PARSE(star_second, "^foo,*")

#undef TEST_DONT_PARSE

TEST(ExtendedOutputsSpec, defeault) {
    std::string_view str = "foo";
    auto [prefix, extendedOutputsSpec] = ExtendedOutputsSpec::parse(str);
    ASSERT_EQ(prefix, "foo");
    ExtendedOutputsSpec expected = ExtendedOutputsSpec::Default { };
    ASSERT_EQ(extendedOutputsSpec, expected);
    ASSERT_EQ(std::string { prefix } + expected.to_string(), str);
}

TEST(ExtendedOutputsSpec, all) {
    std::string_view str = "foo^*";
    auto [prefix, extendedOutputsSpec] = ExtendedOutputsSpec::parse(str);
    ASSERT_EQ(prefix, "foo");
    ExtendedOutputsSpec expected = OutputsSpec::All { };
    ASSERT_EQ(extendedOutputsSpec, expected);
    ASSERT_EQ(std::string { prefix } + expected.to_string(), str);
}

TEST(ExtendedOutputsSpec, out) {
    std::string_view str = "foo^out";
    auto [prefix, extendedOutputsSpec] = ExtendedOutputsSpec::parse(str);
    ASSERT_EQ(prefix, "foo");
    ExtendedOutputsSpec expected = OutputsSpec::Names { "out" };
    ASSERT_EQ(extendedOutputsSpec, expected);
    ASSERT_EQ(std::string { prefix } + expected.to_string(), str);
}

TEST(ExtendedOutputsSpec, out_bin) {
    auto [prefix, extendedOutputsSpec] = ExtendedOutputsSpec::parse("foo^out,bin");
    ASSERT_EQ(prefix, "foo");
    ExtendedOutputsSpec expected = OutputsSpec::Names { "out", "bin" };
    ASSERT_EQ(extendedOutputsSpec, expected);
    ASSERT_EQ(std::string { prefix } + expected.to_string(), "foo^bin,out");
}

TEST(ExtendedOutputsSpec, many_carrot) {
    auto [prefix, extendedOutputsSpec] = ExtendedOutputsSpec::parse("foo^bar^out,bin");
    ASSERT_EQ(prefix, "foo^bar");
    ExtendedOutputsSpec expected = OutputsSpec::Names { "out", "bin" };
    ASSERT_EQ(extendedOutputsSpec, expected);
    ASSERT_EQ(std::string { prefix } + expected.to_string(), "foo^bar^bin,out");
}


#define TEST_JSON(TYPE, NAME, STR, VAL)                      \
                                                             \
    TEST(TYPE, NAME ## _to_json) {                           \
        using nlohmann::literals::operator "" _json;         \
        ASSERT_EQ(                                           \
            STR ## _json,                                    \
            /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
            ((nlohmann::json) TYPE { VAL }));                \
    }                                                        \
                                                             \
    TEST(TYPE, NAME ## _from_json) {                         \
        using nlohmann::literals::operator "" _json;         \
        ASSERT_EQ(                                           \
            /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
            TYPE { VAL },                                    \
            (STR ## _json).get<TYPE>());                     \
    }

TEST_JSON(OutputsSpec, all, R"(["*"])", OutputsSpec::All { })
TEST_JSON(OutputsSpec, name, R"(["a"])", OutputsSpec::Names { "a" })
TEST_JSON(OutputsSpec, names, R"(["a","b"])", (OutputsSpec::Names { "a", "b" }))

TEST_JSON(ExtendedOutputsSpec, def, R"(null)", ExtendedOutputsSpec::Default { })
TEST_JSON(ExtendedOutputsSpec, all, R"(["*"])", ExtendedOutputsSpec::Explicit { OutputsSpec::All { } })
TEST_JSON(ExtendedOutputsSpec, name, R"(["a"])", ExtendedOutputsSpec::Explicit { OutputsSpec::Names { "a" } })
TEST_JSON(ExtendedOutputsSpec, names, R"(["a","b"])", (ExtendedOutputsSpec::Explicit { OutputsSpec::Names { "a", "b" } }))

#undef TEST_JSON

#ifndef COVERAGE

RC_GTEST_PROP(
    OutputsSpec,
    prop_round_rip,
    (const OutputsSpec & o))
{
    RC_ASSERT(o == OutputsSpec::parse(o.to_string()));
}

#endif

}