This commit is contained in:
Eelco Dolstra 2010-04-12 09:14:27 +00:00
parent f3dc7ab877
commit c3f228f296

View file

@ -10,9 +10,8 @@
using namespace nix; using namespace nix;
void doTest(string s) void doTest(EvalState & state, string s)
{ {
EvalState state;
Expr e = parseExprFromString(state, s, absPath(".")); Expr e = parseExprFromString(state, s, absPath("."));
printMsg(lvlError, format(">>>>> %1%") % e); printMsg(lvlError, format(">>>>> %1%") % e);
Value v; Value v;
@ -24,61 +23,65 @@ void doTest(string s)
void run(Strings args) void run(Strings args)
{ {
EvalState state;
printMsg(lvlError, format("size of value: %1% bytes") % sizeof(Value)); printMsg(lvlError, format("size of value: %1% bytes") % sizeof(Value));
doTest("123"); doTest(state, "123");
doTest("{ x = 1; y = 2; }"); doTest(state, "{ x = 1; y = 2; }");
doTest("{ x = 1; y = 2; }.y"); doTest(state, "{ x = 1; y = 2; }.y");
doTest("rec { x = 1; y = x; }.y"); doTest(state, "rec { x = 1; y = x; }.y");
doTest("(x: x) 1"); doTest(state, "(x: x) 1");
doTest("(x: y: y) 1 2"); doTest(state, "(x: y: y) 1 2");
doTest("x: x"); doTest(state, "x: x");
doTest("({x, y}: x) { x = 1; y = 2; }"); doTest(state, "({x, y}: x) { x = 1; y = 2; }");
doTest("({x, y}@args: args.x) { x = 1; y = 2; }"); doTest(state, "({x, y}@args: args.x) { x = 1; y = 2; }");
doTest("(args@{x, y}: args.x) { x = 1; y = 2; }"); doTest(state, "(args@{x, y}: args.x) { x = 1; y = 2; }");
doTest("({x ? 1}: x) { }"); doTest(state, "({x ? 1}: x) { }");
doTest("({x ? 1, y ? x}: y) { x = 2; }"); doTest(state, "({x ? 1, y ? x}: y) { x = 2; }");
doTest("({x, y, ...}: x) { x = 1; y = 2; z = 3; }"); doTest(state, "({x, y, ...}: x) { x = 1; y = 2; z = 3; }");
doTest("({x, y, ...}@args: args.z) { x = 1; y = 2; z = 3; }"); doTest(state, "({x, y, ...}@args: args.z) { x = 1; y = 2; z = 3; }");
//doTest("({x ? y, y ? x}: y) { }"); //doTest(state, "({x ? y, y ? x}: y) { }");
doTest("let x = 1; in x"); doTest(state, "let x = 1; in x");
doTest("with { x = 1; }; x"); doTest(state, "with { x = 1; }; x");
doTest("let x = 2; in with { x = 1; }; x"); // => 2 doTest(state, "let x = 2; in with { x = 1; }; x"); // => 2
doTest("with { x = 1; }; with { x = 2; }; x"); // => 1 doTest(state, "with { x = 1; }; with { x = 2; }; x"); // => 1
doTest("[ 1 2 3 ]"); doTest(state, "[ 1 2 3 ]");
doTest("[ 1 2 ] ++ [ 3 4 5 ]"); doTest(state, "[ 1 2 ] ++ [ 3 4 5 ]");
doTest("123 == 123"); doTest(state, "123 == 123");
doTest("123 == 456"); doTest(state, "123 == 456");
doTest("let id = x: x; in [1 2] == [(id 1) (id 2)]"); doTest(state, "let id = x: x; in [1 2] == [(id 1) (id 2)]");
doTest("let id = x: x; in [1 2] == [(id 1) (id 3)]"); doTest(state, "let id = x: x; in [1 2] == [(id 1) (id 3)]");
doTest("[1 2] == [3 (let x = x; in x)]"); doTest(state, "[1 2] == [3 (let x = x; in x)]");
doTest("{ x = 1; y.z = 2; } == { y = { z = 2; }; x = 1; }"); doTest(state, "{ x = 1; y.z = 2; } == { y = { z = 2; }; x = 1; }");
doTest("{ x = 1; y = 2; } == { x = 2; }"); doTest(state, "{ x = 1; y = 2; } == { x = 2; }");
doTest("{ x = [ 1 2 ]; } == { x = [ 1 ] ++ [ 2 ]; }"); doTest(state, "{ x = [ 1 2 ]; } == { x = [ 1 ] ++ [ 2 ]; }");
doTest("1 != 1"); doTest(state, "1 != 1");
doTest("true"); doTest(state, "true");
doTest("builtins.true"); doTest(state, "builtins.true");
doTest("true == false"); doTest(state, "true == false");
doTest("__head [ 1 2 3 ]"); doTest(state, "__head [ 1 2 3 ]");
doTest("__add 1 2"); doTest(state, "__add 1 2");
doTest("null"); doTest(state, "null");
doTest("null"); doTest(state, "null");
doTest("\"foo\""); doTest(state, "\"foo\"");
doTest("let s = \"bar\"; in \"foo${s}\""); doTest(state, "let s = \"bar\"; in \"foo${s}\"");
doTest("if true then 1 else 2"); doTest(state, "if true then 1 else 2");
doTest("if false then 1 else 2"); doTest(state, "if false then 1 else 2");
doTest("if false || true then 1 else 2"); doTest(state, "if false || true then 1 else 2");
doTest("let x = x; in if true || x then 1 else 2"); doTest(state, "let x = x; in if true || x then 1 else 2");
doTest("/etc/passwd"); doTest(state, "/etc/passwd");
//doTest("import ./foo.nix"); //doTest(state, "import ./foo.nix");
doTest("map (x: __add 1 x) [ 1 2 3 ]"); doTest(state, "map (x: __add 1 x) [ 1 2 3 ]");
doTest("map (builtins.add 1) [ 1 2 3 ]"); doTest(state, "map (builtins.add 1) [ 1 2 3 ]");
doTest("builtins.hasAttr \"x\" { x = 1; }"); doTest(state, "builtins.hasAttr \"x\" { x = 1; }");
doTest("let x = 1; as = rec { inherit x; y = as.x; }; in as.y"); doTest(state, "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(state, "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(state, "let as = rec { inherit (y) x; y = { x = 1; }; }; in as.x");
doTest("builtins.toXML 123"); doTest(state, "builtins.toXML 123");
doTest("builtins.toXML { a.b = \"x\" + \"y\"; c = [ 1 2 ] ++ [ 3 4 ]; }"); doTest(state, "builtins.toXML { a.b = \"x\" + \"y\"; c = [ 1 2 ] ++ [ 3 4 ]; }");
state.printStats();
} }