aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/unit_test/SubmoduleSourceConstructionTest.hs
blob: d07a6edf50a310cf77ab1f05669629992e8b50a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module SubmoduleSourceConstructionTest
  ( test
  )
where

import           BuildModel                     ( RawSource(..)
                                                , Source(..)
                                                , processRawSource
                                                )
import           Hedge                          ( Result
                                                , Test
                                                , assertEquals
                                                , assertThat
                                                , fail'
                                                , givenInput
                                                , then'
                                                , whenTransformed
                                                )
import           System.FilePath                ( (</>) )

test :: IO (Test ())
test = return $ givenInput
  "a submodule"
  exampleSubmodule
  [ whenTransformed
      "processed to a source"
      processRawSource
      [ then' "it is a Submodule" checkIsSubmodule
      , then' "its source file name is the same as the original"
              checkSubmoduleSourceFileName
      , then'
        "its object file name is the 'flattened' path of the source file with '.o' appeneded"
        checkSubmoduleObjectFileName
      , then' "it knows what modules it uses directly" checkSubmoduleModulesUsed
      , then' "it knows its parent's name"             checkSubmoduleParentName
      , then' "it knows its name"                      checkSubmoduleName
      ]
  ]

exampleSubmodule :: RawSource
exampleSubmodule = RawSource submoduleSourceFileName' $ unlines
  [ "submodule (some_module:parent) child"
  , "  use module1"
  , "  USE MODULE2"
  , "  implicit none"
  , "end submodule"
  ]

submoduleSourceFileName' :: String
submoduleSourceFileName' = "some" </> "file" </> "somewhere.f90"

checkIsSubmodule :: Source -> Result
checkIsSubmodule Submodule{} = assertThat True
checkIsSubmodule _           = assertThat False

checkSubmoduleSourceFileName :: Source -> Result
checkSubmoduleSourceFileName s@(Submodule{}) =
  assertEquals submoduleSourceFileName' $ submoduleSourceFileName s
checkSubmoduleSourceFileName _ = fail' "wasn't a Submodule"

checkSubmoduleObjectFileName :: Source -> Result
checkSubmoduleObjectFileName s@(Submodule{}) =
  assertEquals ("." </> "some_file_somewhere.f90.o")
    $ (submoduleObjectFileName s) "."
checkSubmoduleObjectFileName _ = fail' "wasn't a Submodule"

checkSubmoduleModulesUsed :: Source -> Result
checkSubmoduleModulesUsed s@(Submodule{}) =
  assertEquals ["module1", "module2"] $ submoduleModulesUsed s
checkSubmoduleModulesUsed _ = fail' "wasn't a Submodule"

checkSubmoduleParentName :: Source -> Result
checkSubmoduleParentName s@(Submodule{}) =
  assertEquals "some_module@parent" (submoduleParentName s)
checkSubmoduleParentName _ = fail' "wasn't a Submodule"

checkSubmoduleName :: Source -> Result
checkSubmoduleName s@(Submodule{}) = assertEquals "child" $ submoduleName s
checkSubmoduleName _               = fail' "wasn't a Submodule"