Nix
2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Toggle main menu visibility
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
7
#include "
nix/fetchers/fetch-settings.hh
"
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
"
13
#include "
nix/expr/eval-inline.hh
"
14
#include "
nix/expr/eval-settings.hh
"
15
16
#include "
nix/store/tests/libstore.hh
"
17
18
namespace
nix {
19
class
LibExprTest :
public
LibStoreTest
20
{
21
public
:
22
static
void
SetUpTestSuite()
23
{
24
LibStoreTest::SetUpTestSuite();
25
initGC();
26
}
27
28
protected
:
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
74
MATCHER(IsListType,
""
)
75
{
76
return
arg != nList;
77
}
78
79
MATCHER(IsList,
""
)
80
{
81
return
arg.type() == nList;
82
}
83
84
MATCHER(IsString,
""
)
85
{
86
return
arg.type() == nString;
87
}
88
89
MATCHER(IsNull,
""
)
90
{
91
return
arg.type() == nNull;
92
}
93
94
MATCHER(IsThunk,
""
)
95
{
96
return
arg.type() == nThunk;
97
}
98
99
MATCHER(IsAttrs,
""
)
100
{
101
return
arg.type() == nAttrs;
102
}
103
104
MATCHER_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
113
MATCHER_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
121
MATCHER_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
129
MATCHER(IsTrue,
""
)
130
{
131
if
(arg.type() != nBool) {
132
return
false
;
133
}
134
return
arg.boolean() ==
true
;
135
}
136
137
MATCHER(IsFalse,
""
)
138
{
139
if
(arg.type() != nBool) {
140
return
false
;
141
}
142
return
arg.boolean() ==
false
;
143
}
144
145
MATCHER_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
160
MATCHER_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
172
MATCHER_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 */
nix::CanonPath
Definition
canon-path.hh:50
nix::EvalState
Definition
eval.hh:420
nix::Symbol
Definition
symbol-table.hh:24
nix::ref
Definition
ref.hh:58
eval-gc.hh
eval-inline.hh
eval-settings.hh
eval.hh
fetch-settings.hh
nix::fmt
std::string fmt(const std::string &s)
Definition
fmt.hh:67
libstore.hh
nixexpr.hh
nix::openStore
ref< Store > openStore(StoreReference &&storeURI)
Definition
store-registration.cc:24
nix::EvalSettings
Definition
eval-settings.hh:50
nix::Expr
Definition
nixexpr.hh:100
nix::Expr::maybeThunk
virtual Value * maybeThunk(EvalState &state, Env &env)
Definition
eval.cc:1072
nix::Value
Definition
value.hh:1166
nix::fetchers::Settings
Definition
fetch-settings.hh:27
value.hh
libexpr-test-support
include
nix
expr
tests
libexpr.hh
Generated by
1.18.0