Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/Language/PureScript/Interactive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import Prelude.Compat
import Protolude (ordNub)

import Data.List (sort, find, foldl')
import Data.Maybe (mapMaybe)
import Data.Maybe (fromMaybe, mapMaybe)
import qualified Data.Map as M
import Data.Monoid ((<>))
import qualified Data.Set as S
import Data.Text (Text)
import qualified Data.Text as T

Expand Down Expand Up @@ -291,22 +292,21 @@ handleBrowse
handleBrowse print' moduleName = do
st <- get
env <- asks psciEnvironment
if isModInEnv moduleName st
then print' $ printModuleSignatures moduleName env
else case lookupUnQualifiedModName moduleName st of
Just unQualifiedName ->
if isModInEnv unQualifiedName st
then print' $ printModuleSignatures unQualifiedName env
else failNotInEnv moduleName
Nothing ->
failNotInEnv moduleName
case findMod moduleName (psciLoadedExterns st) (psciImportedModules st) of
Just qualName -> print' $ printModuleSignatures qualName env
Nothing -> failNotInEnv moduleName
where
isModInEnv modName =
any ((== modName) . P.getModuleName . fst) . psciLoadedExterns
failNotInEnv modName =
print' $ T.unpack $ "Module '" <> N.runModuleName modName <> "' is not valid."
lookupUnQualifiedModName quaModName st =
(\(modName,_,_) -> modName) <$> find ( \(_, _, mayQuaName) -> mayQuaName == Just quaModName) (psciImportedModules st)
findMod needle externs imports =
let qualMod = fromMaybe needle (lookupUnQualifiedModName needle imports)
primMod = P.ModuleName [P.ProperName "Prim"]
modules = S.fromList (primMod : (P.getModuleName . fst <$> externs))
in if qualMod `S.member` modules
then Just qualMod
else Nothing

failNotInEnv modName = print' $ T.unpack $ "Module '" <> N.runModuleName modName <> "' is not valid."
lookupUnQualifiedModName needle imports =
(\(modName,_,_) -> modName) <$> find (\(_,_,mayQuaName) -> mayQuaName == Just needle) imports

-- | Return output as would be returned by tab completion, for tools integration etc.
handleComplete
Expand Down
4 changes: 4 additions & 0 deletions tests/TestPsci/CommandTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ commandTests = context "commandTests" $ do
":complete ma" `prints` unlines ["map", "mapFlipped"]
run "import Control.Monad as M"
":complete M.a" `prints` unlines ["M.ap", "M.apply"]

specPSCi ":browse" $ do
":browse Mirp" `printed` flip shouldContain "is not valid"
":browse Prim" `printed` flip shouldContain "class Partial"
11 changes: 6 additions & 5 deletions tests/TestPsci/TestEnv.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module TestPsci.TestEnv where
import Prelude ()
import Prelude.Compat

import Control.Monad (void)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.RWS.Strict (evalRWST, RWST)
import qualified Language.PureScript as P
Expand All @@ -12,7 +13,7 @@ import System.Exit
import System.FilePath ((</>))
import qualified System.FilePath.Glob as Glob
import System.Process (readProcessWithExitCode)
import Test.Hspec (shouldBe)
import Test.Hspec (shouldBe, Expectation)

-- | A monad transformer for handle PSCi actions in tests
type TestPSCi a = RWST PSCiConfig () PSCiState IO a
Expand Down Expand Up @@ -90,7 +91,7 @@ evaluatesTo command expected = runAndEval command evalJsAndCompare ignorePrinted

-- | An assertion to check command PSCi printed output against a given string
prints :: String -> String -> TestPSCi ()
prints command expected = runAndEval command evalJsAndIgnore evalPrinted
where
evalJsAndIgnore = jsEval *> return ()
evalPrinted s = s `equalsTo` expected
prints command expected = printed command (`shouldBe` expected)

printed :: String -> (String -> Expectation) -> TestPSCi ()
printed command f = runAndEval command (void jsEval) (liftIO . f)