tvl-depot/src/libexpr/eval-test.cc

82 lines
2.3 KiB
C++
Raw Normal View History

#include "nixexpr.hh"
#include "parser.hh"
#include "hash.hh"
#include "util.hh"
#include "nixexpr-ast.hh"
#include <cstdlib>
2010-03-28 20:27:07 +02:00
#include <cstring>
using namespace nix;
void doTest(string s)
{
EvalState state;
2010-03-30 11:22:33 +02:00
Expr e = parseExprFromString(state, s, absPath("."));
printMsg(lvlError, format(">>>>> %1%") % e);
2010-03-24 13:41:08 +01:00
Value v;
state.strictEval(e, v);
printMsg(lvlError, format("result: %1%") % v);
}
void run(Strings args)
{
2010-03-24 13:41:08 +01:00
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");
2010-03-24 12:06:05 +01:00
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; }");
2010-03-25 13:51:14 +01:00
//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
2010-03-25 16:38:37 +01:00
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");
2010-03-26 16:45:53 +01:00
doTest("true");
doTest("true == false");
doTest("__head [ 1 2 3 ]");
doTest("__add 1 2");
2010-03-29 11:43:55 +02:00
doTest("null");
2010-03-28 20:27:07 +02:00
doTest("\"foo\"");
doTest("let s = \"bar\"; in \"foo${s}\"");
2010-03-29 12:13:51 +02:00
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");
2010-03-30 11:22:33 +02:00
doTest("/etc/passwd");
doTest("import ./foo.nix");
2010-03-30 15:47:59 +02:00
doTest("map (x: __add 1 x) [ 1 2 3 ]");
doTest("map (__add 1) [ 1 2 3 ]");
}
void printHelp()
{
}
string programId = "eval-test";