hnix/tests/EvalTests.hs

101 lines
2.6 KiB
Haskell
Raw Normal View History

2015-12-21 06:55:53 +01:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -Wno-missing-signatures -Wno-orphans #-}
2015-12-21 06:55:53 +01:00
module EvalTests (tests) where
2018-03-30 11:00:36 +02:00
import Data.String.Interpolate
import Nix
2018-03-30 11:00:36 +02:00
import Nix.Expr
import Nix.Parser
import Nix.Value
2018-03-30 11:00:36 +02:00
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.TH
import TestCommon
2015-12-21 06:55:53 +01:00
case_basic_sum =
constantEqualStr "2" "1 + 1"
case_basic_function =
constantEqualStr "2" "(a: a) 2"
2015-12-21 06:55:53 +01:00
case_set_attr =
constantEqualStr "2" "{ a = 2; }.a"
2015-12-21 06:55:53 +01:00
case_function_set_arg =
constantEqualStr "2" "({ a }: 2) { a = 1; }"
2015-12-21 06:55:53 +01:00
case_function_set_two_arg =
constantEqualStr "2" "({ a, b ? 3 }: b - a) { a = 1; }"
2015-12-21 06:55:53 +01:00
case_function_set_two_arg_default_scope =
constantEqualStr "2" "({ x ? 1, y ? x * 3 }: y - x) {}"
2015-12-21 06:55:53 +01:00
case_function_default_env =
constantEqualStr "2" "let default = 2; in ({ a ? default }: a) {}"
2018-01-28 16:54:59 +01:00
case_function_definition_uses_environment =
constantEqualStr "3" "let f = (let a=1; in x: x+a); in f 2"
2018-01-28 16:54:59 +01:00
case_function_atpattern =
constantEqualStr "2" "(({a}@attrs:attrs) {a=2;}).a"
2018-01-28 16:54:59 +01:00
case_function_ellipsis =
constantEqualStr "2" "(({a, ...}@attrs:attrs) {a=0; b=2;}).b"
2015-12-21 06:55:53 +01:00
case_function_default_value_not_in_atpattern =
constantEqualStr "false" "({a ? 2}@attrs: attrs ? a) {}"
2018-04-03 23:43:40 +02:00
case_function_arg_shadowing =
constantEqualStr "6" "(y: y: x: x: x + y) 1 2 3 4"
case_function_recursive_args =
constantEqualStr "2" "({ x ? 1, y ? x * 3}: y - x) {}"
case_function_recursive_sets =
constantEqualStr "[ [ 6 4 100 ] 4 ]" [i|
let x = rec {
y = 2;
z = { w = 4; };
v = rec {
u = 6;
t = [ u z.w s ];
};
}; s = 100; in [ x.v.t x.z.w ]
|]
case_nested_with =
constantEqualStr "2" "with { x = 1; }; with { x = 2; }; x"
case_match_failure_null = assertEvalMatchesNix "builtins.match \"ab\" \"abc\""
-----------------------
2018-03-28 03:42:31 +02:00
2015-12-21 06:55:53 +01:00
tests :: TestTree
tests = $testGroupGenerator
instance (Show r, Eq r) => Eq (NValueF m r) where
NVConstant x == NVConstant y = x == y
NVList x == NVList y = and (zipWith (==) x y)
x == y = error $ "Need to add comparison for values: "
++ show x ++ " == " ++ show y
2015-12-21 06:55:53 +01:00
constantEqual :: NExprLoc -> NExprLoc -> Assertion
2015-12-21 06:55:53 +01:00
constantEqual a b = do
-- putStrLn =<< lint (stripAnnotation a)
2018-04-04 08:00:15 +02:00
a' <- evalLoc Nothing a
-- putStrLn =<< lint (stripAnnotation b)
2018-04-04 08:00:15 +02:00
b' <- evalLoc Nothing b
2015-12-21 06:55:53 +01:00
assertEqual "" a' b'
constantEqualStr :: String -> String -> Assertion
constantEqualStr a b =
let Success a' = parseNixStringLoc a
Success b' = parseNixStringLoc b
2015-12-21 06:55:53 +01:00
in constantEqual a' b'