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
|
#include "tests/libexpr.hh"
namespace nix {
using namespace testing;
// Mostly mirrored from print.cc in their constructions.
struct TypeValuePrintingTests : LibExprTest
{
void test(Value v, std::string_view expected)
{
ASSERT_EQ(showType(v), expected);
}
};
TEST_F(TypeValuePrintingTests, tInt)
{
Value vInt = {NewValueAs::integer, 10};
test(vInt, "an integer");
}
TEST_F(TypeValuePrintingTests, tBool)
{
Value vBool = {NewValueAs::boolean, true};
test(vBool, "a Boolean");
}
TEST_F(TypeValuePrintingTests, tString)
{
Value vString = {NewValueAs::string, "some-string"};
test(vString, "a string");
}
TEST_F(TypeValuePrintingTests, tStringWithCtx)
{
const char * context[] = {"some context", nullptr};
Value vString = {NewValueAs::string, "some-string", context};
test(vString, "a string with context");
}
TEST_F(TypeValuePrintingTests, tPath)
{
Value vPath = {NewValueAs::path, "/foo"};
test(vPath, "a path");
}
TEST_F(TypeValuePrintingTests, tNull)
{
Value vNull = {NewValueAs::null};
test(vNull, "null");
}
TEST_F(TypeValuePrintingTests, tAttrs)
{
Value vOne = {NewValueAs::integer, 1};
Value vTwo = {NewValueAs::integer, 2};
BindingsBuilder builder = evaluator.buildBindings(10);
builder.insert(evaluator.symbols.create("one"), vOne);
builder.insert(evaluator.symbols.create("two"), vTwo);
Value vAttrs = {NewValueAs::attrs, builder.finish()};
test(vAttrs, "a set");
}
TEST_F(TypeValuePrintingTests, tList)
{
Value vOne = {NewValueAs::integer, 1};
Value vTwo = {NewValueAs::integer, 2};
auto vList = evaluator.mem.newList(5);
vList->elems[0] = vOne;
vList->elems[1] = vTwo;
vList->size = 3;
test(Value(NewValueAs::list, vList), "a list");
}
TEST_F(TypeValuePrintingTests, vThunk)
{
EvalMemory mem;
Env env;
ExprInt e(noPos, 0);
Value vThunk{NewValueAs::thunk, mem, env, e};
test(vThunk, "a thunk");
}
TEST_F(TypeValuePrintingTests, vApp)
{
EvalMemory mem;
Value vFn{NewValueAs::null};
Value vApp{NewValueAs::app, mem, vFn, vFn};
test(vApp, "a function application");
}
TEST_F(TypeValuePrintingTests, vLambda)
{
EvalMemory mem;
Env env{.up = nullptr, .values = {}};
PosTable::Origin origin = evaluator.positions.addOrigin(std::monostate(), 1);
auto posIdx = evaluator.positions.add(origin, 0);
ExprLambda eLambda(posIdx, std::make_unique<AttrsPattern>(), std::make_unique<ExprInt>(noPos, 0));
eLambda.pattern->name = createSymbol("a");
Value vLambda{NewValueAs::lambda, mem, env, eLambda};
test(vLambda, "a function");
eLambda.setName(createSymbol("puppy"));
test(vLambda, "a function");
}
TEST_F(TypeValuePrintingTests, vPrimOp)
{
PrimOp primOp{{.name = "puppy"}};
Value vPrimOp = {NewValueAs::primop, primOp};
test(vPrimOp, "the built-in function 'puppy'");
}
TEST_F(TypeValuePrintingTests, vPrimOpApp)
{
EvalMemory mem;
PrimOp primOp{{.name = "puppy"}};
Value vPrimOp = {NewValueAs::primop, primOp};
Value vPrimOpApp{NewValueAs::app, mem, vPrimOp, vPrimOp};
test(vPrimOpApp, "the partially applied built-in function 'puppy'");
}
TEST_F(TypeValuePrintingTests, vExternal)
{
struct MyExternal : ExternalValueBase
{
public:
std::string showType() const override
{
return "an external value from MyExternal";
}
std::string typeOf() const override
{
return "";
}
virtual std::ostream & print(std::ostream & str) const override
{
str << "external value";
return str;
}
} myExternal;
Value vExternal = {NewValueAs::external, myExternal};
test(vExternal, "an external value from MyExternal");
}
TEST_F(TypeValuePrintingTests, vFloat)
{
Value vFloat = {NewValueAs::floating, 2.0};
test(vFloat, "a float");
}
TEST_F(TypeValuePrintingTests, vBlackhole)
{
Value vBlackhole{NewValueAs::blackhole};
test(vBlackhole, "a black hole");
}
}
|