about summary refs log tree commit diff
path: root/src/libexpr/eval-test.cc
blob: eac95094fd1f23e832556a4ec71e0812f6b07c68 (plain) (blame)
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
#include "nixexpr.hh"
#include "parser.hh"
#include "hash.hh"
#include "util.hh"
#include "nixexpr-ast.hh"

#include <cstdlib>
#include <cstring>

using namespace nix;


void doTest(string s)
{
    EvalState state;
    Expr e = parseExprFromString(state, s, absPath("."));
    printMsg(lvlError, format(">>>>> %1%") % e);
    Value v;
    state.eval(e, v);
    state.strictForceValue(v);
    printMsg(lvlError, format("result: %1%") % v);
}


void run(Strings args)
{
    printMsg(lvlError, format("size of value: %1% bytes") % sizeof(Value));
    
    doTest("123");
    doTest("{ x = 1; y = 2; }");
    doTest("{ x = 1; y = 2; }.y");
    doTest("rec { x = 1; y = x; }.y");
    doTest("(x: x) 1");
    doTest("(x: y: y) 1 2");
    doTest("x: x");
    doTest("({x, y}: x) { x = 1; y = 2; }");
    doTest("({x, y}@args: args.x) { x = 1; y = 2; }");
    doTest("(args@{x, y}: args.x) { x = 1; y = 2; }");
    doTest("({x ? 1}: x) { }");
    doTest("({x ? 1, y ? x}: y) { x = 2; }");
    doTest("({x, y, ...}: x) { x = 1; y = 2; z = 3; }");
    doTest("({x, y, ...}@args: args.z) { x = 1; y = 2; z = 3; }");
    //doTest("({x ? y, y ? x}: y) { }");
    doTest("let x = 1; in x");
    doTest("with { x = 1; }; x");
    doTest("let x = 2; in with { x = 1; }; x"); // => 2
    doTest("with { x = 1; }; with { x = 2; }; x"); // => 1
    doTest("[ 1 2 3 ]");
    doTest("[ 1 2 ] ++ [ 3 4 5 ]");
    doTest("123 == 123");
    doTest("123 == 456");
    doTest("let id = x: x; in [1 2] == [(id 1) (id 2)]");
    doTest("let id = x: x; in [1 2] == [(id 1) (id 3)]");
    doTest("[1 2] == [3 (let x = x; in x)]");
    doTest("{ x = 1; y.z = 2; } == { y = { z = 2; }; x = 1; }");
    doTest("{ x = 1; y = 2; } == { x = 2; }");
    doTest("{ x = [ 1 2 ]; } == { x = [ 1 ] ++ [ 2 ]; }");
    doTest("1 != 1");
    doTest("true");
    doTest("builtins.true");
    doTest("true == false");
    doTest("__head [ 1 2 3 ]");
    doTest("__add 1 2");
    doTest("null");
    doTest("null");
    doTest("\"foo\"");
    doTest("let s = \"bar\"; in \"foo${s}\"");
    doTest("if true then 1 else 2");
    doTest("if false then 1 else 2");
    doTest("if false || true then 1 else 2");
    doTest("let x = x; in if true || x then 1 else 2");
    doTest("/etc/passwd");
    //doTest("import ./foo.nix");
    doTest("map (x: __add 1 x) [ 1 2 3 ]");
    doTest("map (builtins.add 1) [ 1 2 3 ]");
    doTest("builtins.hasAttr \"x\" { x = 1; }");
    doTest("let x = 1; as = rec { inherit x; y = as.x; }; in as.y");
    doTest("let as = { x = 1; }; bs = rec { inherit (as) x; y = x; }; in bs.y");
    doTest("let as = rec { inherit (y) x; y = { x = 1; }; }; in as.x");
    doTest("builtins.toXML 123");
    doTest("builtins.toXML { a.b = \"x\" + \"y\"; c = [ 1 2 ] ++ [ 3 4 ]; }");
}


void printHelp()
{
}


string programId = "eval-test";