1{-# LANGUAGE OverloadedStrings #-}23module Library where45import Test.Tasty6import Test.Tasty.HUnit78import Data.Text ()9import SchemeDoc10import qualified SchemeDoc.Format.Library as L11import SchemeDoc.Types12import Util1314getLibs :: String -> [L.Library]15getLibs input = map snd libraries16 where17 -- Scheme S-expressions for the parsed input.18 (Right expr) = parse input1920 -- Documented library definitions in the input21 (Right libraries) = findDocLibs expr2223getLib :: String -> L.Library24getLib = head . getLibs2526------------------------------------------------------------------------2728libraryParser :: TestTree29libraryParser =30 testGroup31 "Tests for the Library parser"32 [ testCase "Library with exports and without declarations" $ do33 let lib = getLib ";;> my comment\n(define-library (foo) (export string-length))"3435 assertEqual "Library name" "foo" $ L.name lib36 assertEqual "Exports string-length" True $ L.exports lib "string-length"37 assertEqual "Doesn't export foo" False $ L.exports lib "foo"38 , testCase "Library with renamed identifier" $ do39 let lib = getLib ";;> my comment\n(define-library (foo) (export (rename string-length strlen)))"4041 assertEqual "Exports renamed internal" True $ L.exports lib "string-length"42 assertEqual "Doesn't export renamed external" False $ L.exports lib "strlen"43 assertEqual "Renamed identifier" (Just "strlen") $ L.externalId lib "string-length"44 assertEqual "Unknown identifier" Nothing $ L.externalId lib "foo"45 , testCase "Multiple lib definitions in a single file" $ do46 let libs = getLibs ";;> foo lib\n(define-library (foo))\n;;> bar library\n(define-library (bar))"47 assertEqual "Amount of libraries" 2 $ length libs4849 assertEqual "First lib name" "foo" $ L.name (libs !! 0)50 assertEqual "Second lib name" "bar" $ L.name (libs !! 1)51 , testCase "Multipart lib name" $ do52 let lib = getLib ";;> my comment\n(define-library (foo 42 bar 23 baz))"53 assertEqual "" "foo 42 bar 23 baz" $ L.name lib54 , testCase "Library with lib declaration" $ do55 let lib = getLib ";;> scheme example lib\n(define-library (scheme example) (begin (define x 23) (define y 42)))"56 expanded <- L.expand lib5758 assertEqual59 ""60 [ List61 [ Id "begin"62 , List [Id "define", Id "x", Number 23]63 , List [Id "define", Id "y", Number 42]64 ]65 ]66 expanded67 , testCase "Library with multiple declarations" $ do68 let lib = getLib ";;> my lib\n(define-library (scheme example) (begin (define x 23)) (begin (define y 42)))"69 expanded <- L.expand lib7071 assertEqual72 ""73 [ List [Id "begin", List [Id "define", Id "y", Number 42]]74 , List [Id "begin", List [Id "define", Id "x", Number 23]]75 ]76 expanded77 , testCase "Library with include" $ do78 let lib = getLib ";;> doc comment\n(define-library (some example) (include \"test/testdata/simple-include.scm\"))"79 expanded <- L.expand lib8081 assertEqual "" [List [Id "begin", List [Id "define", Id "x", Str "foo"]]] expanded82 ]