tests for comment parsing and parser benchmarks

This commit is contained in:
Benno Fünfstück 2014-08-27 23:54:31 +02:00
parent 3a08227c0c
commit 94b7e4b1d7
11 changed files with 22515 additions and 0 deletions

10
benchmarks/Main.hs Normal file
View File

@ -0,0 +1,10 @@
module Main where
import Criterion.Main
import qualified ParserBench
main :: IO ()
main = defaultMain
[ ParserBench.benchmarks
]

20
benchmarks/ParserBench.hs Normal file
View File

@ -0,0 +1,20 @@
module ParserBench (benchmarks) where
import Nix.Parser
import Control.Applicative
import Criterion
benchFile :: FilePath -> Benchmark
benchFile = bench <*> whnfIO . parseNixFile . ("data/" ++)
benchmarks :: Benchmark
benchmarks = bgroup "Parser"
[ benchFile "nixpkgs-all-packages.nix"
, benchFile "nixpkgs-all-packages-pretty.nix"
, benchFile "let-comments.nix"
, benchFile "let-comments-multiline.nix"
, benchFile "let.nix"
, benchFile "simple.nix"
, benchFile "simple-pretty.nix"
]

View File

@ -0,0 +1,13 @@
let
b.a = 3; /*
this is a multiline comment
/* we can also nest these comments
*/ /*
*/
*/
b.c = { e = {}; };
/* just some more comments
*/
b.c.e.f = 4;
/* this file is documented really well */
in b /* todo */

13
data/let-comments.nix Normal file
View File

@ -0,0 +1,13 @@
let
b.a = 3;
# this is a oneline comment
# we can also nest these comments
# #
#
#
b.c = { e = {}; };
# just some more comments
#
b.c.e.f = 4;
# this file is documented really well
in b # todo

5
data/let.nix Normal file
View File

@ -0,0 +1,5 @@
let
b.a = 3;
b.c = { e = {}; };
b.c.e.f = 4;
in b

File diff suppressed because it is too large Load Diff

11561
data/nixpkgs-all-packages.nix Normal file

File diff suppressed because it is too large Load Diff

28
data/simple-pretty.nix Normal file
View File

@ -0,0 +1,28 @@
{
a = 3;
b = 4;
c = {
inherit ({ a = 3; }) a;
e = 4;
f = 5;
};
d = if true
then { a = 1; b = 2; c = 3; }
else if false
then null
else {
inherit ({
a = 3;
b = 4;
cdefgads = 5;
}) cdefgads;
};
f = x: x;
list = [
1
2
(f 3)
((x: x) 3)
[ 4 5 ]
];
}

1
data/simple.nix Normal file
View File

@ -0,0 +1 @@
{ a = 3; b = 4; c = { inherit ({a = 3; }) a; e = 4; f = 5; }; d = if true then { a = 1; b = 2; c = 3; } else if false then null else { inherit ({a = 3; b = 4; cdefgads = 5; }) cdefgads; }; f = x: x; list = [ 1 2 (f 3) ((x: x) 3) [ 4 5 ] ]; }

View File

@ -87,6 +87,8 @@ Test-suite hnix-tests
Hs-source-dirs: tests
Default-language: Haskell2010
Main-is: Main.hs
Other-modules:
ParserTests
Build-depends:
base >= 4.3 && < 5
, containers
@ -96,6 +98,20 @@ Test-suite hnix-tests
, tasty-th
, tasty-hunit
Benchmark benchmarks
Type: exitcode-stdio-1.0
Hs-source-dirs: benchmarks
Default-language: Haskell2010
Main-is: Main.hs
Other-modules:
ParserBench
Build-depends:
base >= 4.3 && < 5
, containers
, text
, hnix
, criterion
source-repository head
type: git
location: git://github.com/jwiegley/hnix.git

View File

@ -294,6 +294,12 @@ case_operators = do
(mkOper2 NMinus (mkSym "a") (mkSym "b")) $
mkSym "c"
case_comments :: Assertion
case_comments = do
Success expected <- parseNixFile "data/let.nix"
assertParseFile "let-comments-multiline.nix" expected
assertParseFile "let-comments.nix" expected
tests :: TestTree
tests = $testGroupGenerator
@ -303,6 +309,13 @@ assertParseString str expected = case parseNixString str of
Success actual -> assertEqual ("When parsing " ++ str) expected actual
Failure err -> assertFailure $ "Unexpected error parsing `" ++ str ++ "':\n" ++ show err
assertParseFile :: FilePath -> NExpr -> Assertion
assertParseFile file expected = do
res <- parseNixFile $ "data/" ++ file
case res of
Success actual -> assertEqual ("Parsing data file " ++ file) expected actual
Failure err -> assertFailure $ "Unexpected error parsing data file `" ++ file ++ "':\n" ++ show err
assertParseFail :: String -> Assertion
assertParseFail str = case parseNixString str of
Failure _ -> return ()