From 9bb3867d458b74e495139151656d975cd3d81861 Mon Sep 17 00:00:00 2001 From: rndnoise <34294193+rndnoise@users.noreply.github.com> Date: Sat, 17 Feb 2018 18:10:13 -0600 Subject: [PATCH] Fix :browse Prim, it has no ExternsFile Fixes #2672. --- src/Language/PureScript/Interactive.hs | 33 +++++++++++++------------- tests/TestPsci/CommandTest.hs | 4 ++++ tests/TestPsci/TestEnv.hs | 11 +++++---- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/Language/PureScript/Interactive.hs b/src/Language/PureScript/Interactive.hs index facde9e992..ede92d82a3 100644 --- a/src/Language/PureScript/Interactive.hs +++ b/src/Language/PureScript/Interactive.hs @@ -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 @@ -291,22 +292,22 @@ 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 diff --git a/tests/TestPsci/CommandTest.hs b/tests/TestPsci/CommandTest.hs index 2e3980da81..a84fdcaf16 100644 --- a/tests/TestPsci/CommandTest.hs +++ b/tests/TestPsci/CommandTest.hs @@ -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" diff --git a/tests/TestPsci/TestEnv.hs b/tests/TestPsci/TestEnv.hs index 13a655f1a2..a41c018871 100644 --- a/tests/TestPsci/TestEnv.hs +++ b/tests/TestPsci/TestEnv.hs @@ -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 @@ -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 @@ -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)