hnix 0.3.3 → 0.3.4
raw patch · 3 files changed
+62/−7 lines, 3 filesdep ~data-fix
Dependency ranges changed: data-fix
Files
- Nix/Eval.hs +9/−6
- hnix.cabal +2/−1
- tests/EvalTests.hs +51/−0
Nix/Eval.hs view
@@ -29,6 +29,7 @@ | NVList [r] | NVSet (Map.Map Text r) | NVFunction (Params r) (NValue m -> m r)+ | NVLiteralPath FilePath deriving (Generic, Typeable, Functor) instance Show f => Show (NValueF m f) where@@ -38,6 +39,7 @@ go (NVList list) = showsCon1 "NVList" list go (NVSet attrs) = showsCon1 "NVSet" attrs go (NVFunction r _) = showsCon1 "NVFunction" r+ go (NVLiteralPath p) = showsCon1 "NVLiteralPath" p showsCon1 :: Show a => String -> a -> Int -> String -> String showsCon1 con a d = showParen (d > 10) $ showString (con ++ " ") . showsPrec 11 a@@ -46,11 +48,12 @@ valueText :: Functor m => NValue m -> Text valueText = cata phi where- phi (NVConstant a) = atomText a- phi (NVStr t) = t- phi (NVList _) = error "Cannot coerce a list to a string"- phi (NVSet _) = error "Cannot coerce a set to a string"- phi (NVFunction _ _) = error "Cannot coerce a function to a string"+ phi (NVConstant a) = atomText a+ phi (NVStr t) = t+ phi (NVList _) = error "Cannot coerce a list to a string"+ phi (NVSet _) = error "Cannot coerce a set to a string"+ phi (NVFunction _ _) = error "Cannot coerce a function to a string"+ phi (NVLiteralPath p) = Text.pack p buildArgument :: Params (NValue m) -> NValue m -> NValue m buildArgument paramSpec arg = either error (Fix . NVSet) $ case paramSpec of@@ -75,7 +78,7 @@ where err = error ("Undefined variable: " ++ show var) phi (NConstant x) = const $ return $ Fix $ NVConstant x phi (NStr str) = fmap (Fix . NVStr) . flip evalString str- phi (NLiteralPath _) = error "Path expressions are not yet supported"+ phi (NLiteralPath p) = const $ return $ Fix $ NVLiteralPath p phi (NEnvPath _) = error "Path expressions are not yet supported" phi (NUnary op arg) = \env -> arg env >>= \case
hnix.cabal view
@@ -1,5 +1,5 @@ Name: hnix-Version: 0.3.3+Version: 0.3.4 Synopsis: Haskell implementation of the Nix language Description: Haskell implementation of the Nix language.@@ -104,6 +104,7 @@ Main-is: Main.hs Other-modules: ParserTests+ EvalTests Build-depends: base >= 4.3 && < 5 , containers
+ tests/EvalTests.hs view
@@ -0,0 +1,51 @@+{-# LANGUAGE TemplateHaskell #-}+{-# LANGUAGE OverloadedStrings #-}+module EvalTests (tests) where++import Data.Fix++import Test.Tasty+import Test.Tasty.HUnit+import Test.Tasty.TH++import Nix.Eval+import Nix.Parser+import Nix.Expr++import Data.Monoid (Monoid(..))+import Prelude (String)++case_basic_sum :: Assertion+case_basic_sum = constantEqualStr "2" "1 + 1"++case_basic_function :: Assertion+case_basic_function = constantEqualStr "2" "(a: a) 2"++case_set_attr :: Assertion+case_set_attr = constantEqualStr "2" "{ a = 2; }.a"++case_function_set_arg :: Assertion+case_function_set_arg = constantEqualStr "2" "({ a }: 2) { a = 1; }"++case_function_set_two_arg :: Assertion+case_function_set_two_arg = constantEqualStr "2" "({ a, b ? 3 }: b - a) { a = 1; }"++-- case_function_set_two_arg_default_scope :: Assertion+-- case_function_set_two_arg_default_scope = constantEqualStr "2" "({ a, b ? a * 3 }: b - a) { a = 1; }"++tests :: TestTree+tests = $testGroupGenerator++-----------------------++constantEqual :: NExpr -> NExpr -> Assertion+constantEqual a b = do+ Fix (NVConstant a') <- evalExpr a (Fix (NVSet mempty))+ Fix (NVConstant b') <- evalExpr b (Fix (NVSet mempty))+ assertEqual "" a' b'++constantEqualStr :: String -> String -> Assertion+constantEqualStr a b =+ let Success a' = parseNixString a+ Success b' = parseNixString b+ in constantEqual a' b'