|
| 1 | +module Reporting.Error.ImportSpec (spec) where |
| 2 | + |
| 3 | +import Data.Set qualified as Set |
| 4 | +import Data.Text as T |
| 5 | +import Data.Text.Encoding as T |
| 6 | +import Data.Utf8 qualified as Utf8 |
| 7 | +import Prettyprinter qualified as P |
| 8 | +import Prettyprinter.Render.Text (renderStrict) |
| 9 | +import Reporting.Annotation qualified as A |
| 10 | +import Reporting.Error.Import qualified as Import |
| 11 | +import Reporting.Render.Code qualified as Code |
| 12 | +import Reporting.Report qualified as Report |
| 13 | +import Test.Hspec (Spec, describe, it) |
| 14 | + |
| 15 | +spec :: Spec |
| 16 | +spec = do |
| 17 | + describe "Import error reporting" $ do |
| 18 | + {- |
| 19 | + When "gren-lang/core" module is not listed in gren.json, the |
| 20 | + default imports that the compiler inserts into the code |
| 21 | + fail, but there is no source code region to report. |
| 22 | + We want to ensure the reporting code doesn't crash, and |
| 23 | + actually reports that modules like Basics (and others) are reported |
| 24 | + as not found. |
| 25 | + -} |
| 26 | + it "Give proper error if default package dependencies are not found" $ do |
| 27 | + let -- Empty source code |
| 28 | + source = Code.toSource (T.encodeUtf8 (T.pack "")) |
| 29 | + |
| 30 | + -- The "Module Not Found" Error |
| 31 | + err = |
| 32 | + Import.Error |
| 33 | + { -- IMPORTANT: The region here is zero, not pointing to any |
| 34 | + -- possible lines of source code. This happens when |
| 35 | + -- "import Basics" is added by default by the compiler, without |
| 36 | + -- it appearing in the actual source code. |
| 37 | + Import._region = A.Region (A.Position 0 0) (A.Position 0 0), |
| 38 | + Import._import = Utf8.fromChars "foo", |
| 39 | + Import._unimported = Set.singleton (Utf8.fromChars "Basics"), |
| 40 | + Import._problem = Import.NotFound |
| 41 | + } |
| 42 | + |
| 43 | + -- Create the report |
| 44 | + report = Import.toReport source err |
| 45 | + |
| 46 | + -- Convert the Prettyprinter Doc to a Data.Text |
| 47 | + messageText = renderStrict (P.layoutCompact $ Report._message report) |
| 48 | + in -- Does it have one of the missing modules reported in it? |
| 49 | + T.isInfixOf (T.pack "Basics") messageText |
0 commit comments