Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
libexpr.hh
Go to the documentation of this file.
1#pragma once
3
4#include <gtest/gtest.h>
5#include <gmock/gmock.h>
6
8#include "nix/expr/value.hh"
9#include "nix/expr/nixexpr.hh"
10#include "nix/expr/nixexpr.hh"
11#include "nix/expr/eval.hh"
12#include "nix/expr/eval-gc.hh"
15
17
18namespace nix {
19class LibExprTest : public LibStoreTest
20{
21public:
22 static void SetUpTestSuite()
23 {
24 LibStoreTest::SetUpTestSuite();
25 initGC();
26 }
27
28protected:
29 LibExprTest(ref<Store> store, auto && makeEvalSettings)
30 : LibStoreTest()
31 , evalSettings(makeEvalSettings(readOnlyMode))
32 , state({}, store, fetchSettings, evalSettings, nullptr)
33 {
34 }
35
36 LibExprTest()
37 : LibExprTest(openStore("dummy://"), [](bool & readOnlyMode) {
38 EvalSettings settings{readOnlyMode};
39 settings.nixPath = {};
40 return settings;
41 })
42 {
43 }
44
45 Value eval(std::string input, bool forceValue = true)
46 {
47 Value v;
48 Expr * e = state.parseExprFromString(input, state.rootPath(CanonPath::root));
49 assert(e);
50 state.eval(e, v);
51 if (forceValue)
52 state.forceValue(v, noPos);
53 return v;
54 }
55
56 Value * maybeThunk(std::string input, bool forceValue = true)
57 {
58 Expr * e = state.parseExprFromString(input, state.rootPath(CanonPath::root));
59 assert(e);
60 return e->maybeThunk(state, state.baseEnv);
61 }
62
63 Symbol createSymbol(const char * value)
64 {
65 return state.symbols.create(value);
66 }
67
68 bool readOnlyMode = true;
69 fetchers::Settings fetchSettings{};
70 EvalSettings evalSettings{readOnlyMode};
71 EvalState state;
72};
73
74MATCHER(IsListType, "")
75{
76 return arg != nList;
77}
78
79MATCHER(IsList, "")
80{
81 return arg.type() == nList;
82}
83
84MATCHER(IsString, "")
85{
86 return arg.type() == nString;
87}
88
89MATCHER(IsNull, "")
90{
91 return arg.type() == nNull;
92}
93
94MATCHER(IsThunk, "")
95{
96 return arg.type() == nThunk;
97}
98
99MATCHER(IsAttrs, "")
100{
101 return arg.type() == nAttrs;
102}
103
104MATCHER_P(IsStringEq, s, fmt("The string is equal to \"%1%\"", s))
105{
106 if (arg.type() != nString) {
107 *result_listener << "Expected a string got " << arg.type();
108 return false;
109 }
110 return arg.string_view() == s;
111}
112
113MATCHER_P(IsIntEq, v, fmt("The string is equal to \"%1%\"", v))
114{
115 if (arg.type() != nInt) {
116 return false;
117 }
118 return arg.integer().value == v;
119}
120
121MATCHER_P(IsFloatEq, v, fmt("The float is equal to \"%1%\"", v))
122{
123 if (arg.type() != nFloat) {
124 return false;
125 }
126 return arg.fpoint() == v;
127}
128
129MATCHER(IsTrue, "")
130{
131 if (arg.type() != nBool) {
132 return false;
133 }
134 return arg.boolean() == true;
135}
136
137MATCHER(IsFalse, "")
138{
139 if (arg.type() != nBool) {
140 return false;
141 }
142 return arg.boolean() == false;
143}
144
145MATCHER_P(IsPathEq, p, fmt("Is a path equal to \"%1%\"", p))
146{
147 if (arg.type() != nPath) {
148 *result_listener << "Expected a path got " << arg.type();
149 return false;
150 } else {
151 auto path = arg.path();
152 if (path.path != CanonPath(p)) {
153 *result_listener << "Expected a path that equals \"" << p << "\" but got: " << path.path;
154 return false;
155 }
156 }
157 return true;
158}
159
160MATCHER_P(IsListOfSize, n, fmt("Is a list of size [%1%]", n))
161{
162 if (arg.type() != nList) {
163 *result_listener << "Expected list got " << arg.type();
164 return false;
165 } else if (arg.listSize() != (size_t) n) {
166 *result_listener << "Expected as list of size " << n << " got " << arg.listSize();
167 return false;
168 }
169 return true;
170}
171
172MATCHER_P(IsAttrsOfSize, n, fmt("Is a set of size [%1%]", n))
173{
174 if (arg.type() != nAttrs) {
175 *result_listener << "Expected set got " << arg.type();
176 return false;
177 } else if (arg.attrs()->size() != (size_t) n) {
178 *result_listener << "Expected a set with " << n << " attributes but got " << arg.attrs()->size();
179 return false;
180 }
181 return true;
182}
183
184} /* namespace nix */
Definition canon-path.hh:50
Definition eval.hh:420
Definition symbol-table.hh:24
Definition ref.hh:58
std::string fmt(const std::string &s)
Definition fmt.hh:67
ref< Store > openStore(StoreReference &&storeURI)
Definition store-registration.cc:24
Definition eval-settings.hh:50
Definition nixexpr.hh:100
virtual Value * maybeThunk(EvalState &state, Env &env)
Definition eval.cc:1072
Definition value.hh:1166
Definition fetch-settings.hh:27