Nix 2.26.3
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
7#include "fetch-settings.hh"
8#include "value.hh"
9#include "nixexpr.hh"
10#include "nixexpr.hh"
11#include "eval.hh"
12#include "eval-gc.hh"
13#include "eval-inline.hh"
14#include "eval-settings.hh"
15
16#include "tests/libstore.hh"
17
18namespace nix {
19 class LibExprTest : public LibStoreTest {
20 public:
21 static void SetUpTestSuite() {
22 LibStoreTest::SetUpTestSuite();
23 initGC();
24 }
25
26 protected:
27 LibExprTest()
28 : LibStoreTest()
29 , state({}, store, fetchSettings, evalSettings, nullptr)
30 {
31 evalSettings.nixPath = {};
32 }
33 Value eval(std::string input, bool forceValue = true) {
34 Value v;
35 Expr * e = state.parseExprFromString(input, state.rootPath(CanonPath::root));
36 assert(e);
37 state.eval(e, v);
38 if (forceValue)
39 state.forceValue(v, noPos);
40 return v;
41 }
42
43 Value * maybeThunk(std::string input, bool forceValue = true) {
44 Expr * e = state.parseExprFromString(input, state.rootPath(CanonPath::root));
45 assert(e);
46 return e->maybeThunk(state, state.baseEnv);
47 }
48
49 Symbol createSymbol(const char * value) {
50 return state.symbols.create(value);
51 }
52
53 bool readOnlyMode = true;
54 fetchers::Settings fetchSettings{};
55 EvalSettings evalSettings{readOnlyMode};
56 EvalState state;
57 };
58
59 MATCHER(IsListType, "") {
60 return arg != nList;
61 }
62
63 MATCHER(IsList, "") {
64 return arg.type() == nList;
65 }
66
67 MATCHER(IsString, "") {
68 return arg.type() == nString;
69 }
70
71 MATCHER(IsNull, "") {
72 return arg.type() == nNull;
73 }
74
75 MATCHER(IsThunk, "") {
76 return arg.type() == nThunk;
77 }
78
79 MATCHER(IsAttrs, "") {
80 return arg.type() == nAttrs;
81 }
82
83 MATCHER_P(IsStringEq, s, fmt("The string is equal to \"%1%\"", s)) {
84 if (arg.type() != nString) {
85 return false;
86 }
87 return std::string_view(arg.c_str()) == s;
88 }
89
90 MATCHER_P(IsIntEq, v, fmt("The string is equal to \"%1%\"", v)) {
91 if (arg.type() != nInt) {
92 return false;
93 }
94 return arg.integer().value == v;
95 }
96
97 MATCHER_P(IsFloatEq, v, fmt("The float is equal to \"%1%\"", v)) {
98 if (arg.type() != nFloat) {
99 return false;
100 }
101 return arg.fpoint() == v;
102 }
103
104 MATCHER(IsTrue, "") {
105 if (arg.type() != nBool) {
106 return false;
107 }
108 return arg.boolean() == true;
109 }
110
111 MATCHER(IsFalse, "") {
112 if (arg.type() != nBool) {
113 return false;
114 }
115 return arg.boolean() == false;
116 }
117
118 MATCHER_P(IsPathEq, p, fmt("Is a path equal to \"%1%\"", p)) {
119 if (arg.type() != nPath) {
120 *result_listener << "Expected a path got " << arg.type();
121 return false;
122 } else {
123 auto path = arg.path();
124 if (path.path != CanonPath(p)) {
125 *result_listener << "Expected a path that equals \"" << p << "\" but got: " << path.path;
126 return false;
127 }
128 }
129 return true;
130 }
131
132
133 MATCHER_P(IsListOfSize, n, fmt("Is a list of size [%1%]", n)) {
134 if (arg.type() != nList) {
135 *result_listener << "Expected list got " << arg.type();
136 return false;
137 } else if (arg.listSize() != (size_t)n) {
138 *result_listener << "Expected as list of size " << n << " got " << arg.listSize();
139 return false;
140 }
141 return true;
142 }
143
144 MATCHER_P(IsAttrsOfSize, n, fmt("Is a set of size [%1%]", n)) {
145 if (arg.type() != nAttrs) {
146 *result_listener << "Expected set got " << arg.type();
147 return false;
148 } else if (arg.attrs()->size() != (size_t) n) {
149 *result_listener << "Expected a set with " << n << " attributes but got " << arg.attrs()->size();
150 return false;
151 }
152 return true;
153 }
154
155
156} /* namespace nix */
Definition canon-path.hh:41
Definition eval.hh:182
Definition symbol-table.hh:58
std::string fmt(const std::string &s)
Definition fmt.hh:67
return s
Definition lexer.l:459
std::shared_ptr< T > p
Definition lexer.l:1269
const T & value
Definition lexer.l:492
void forceValue(Value &v, const PosIdx pos)
Definition eval-settings.hh:13
Definition nixexpr.hh:81
Definition value.hh:167
Definition fetch-settings.hh:15