Skip to content

Commit da1615b

Browse files
committed
Chapter 8 - Tests
1 parent f9fe717 commit da1615b

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

hs-blog.cabal

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,23 @@ executable hs-blog-gen
5757
, hs-blog
5858
ghc-options:
5959
-O
60+
61+
test-suite hs-blog-gen-test
62+
import: common-settings
63+
type: exitcode-stdio-1.0
64+
hs-source-dirs: test
65+
main-is: Spec.hs
66+
67+
other-modules:
68+
MarkupParsingSpec
69+
build-depends:
70+
base
71+
, hspec
72+
, hspec-discover
73+
, raw-strings-qq
74+
, hs-blog
75+
ghc-options:
76+
-O -threaded -rtsopts -with-rtsopts=-N
77+
build-tool-depends:
78+
hspec-discover:hspec-discover == 2.*
79+

test/MarkupParsingSpec.hs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
-- test/MarkupParsingSpec.hs
2+
3+
{-# language QuasiQuotes #-}
4+
5+
module MarkupParsingSpec where
6+
7+
import Test.Hspec
8+
import Text.RawString.QQ
9+
import HsBlog.Markup
10+
11+
spec :: Spec
12+
spec = do
13+
describe "Markup parsing tests" $ do
14+
simple
15+
multiline
16+
17+
simple :: Spec
18+
simple = do
19+
describe "simple" $ do
20+
it "Test empty" $
21+
shouldBe
22+
(parse "")
23+
[]
24+
25+
it "paragraph" $
26+
shouldBe
27+
(parse "hello world")
28+
[Paragraph "hello world"]
29+
30+
it "heading 1" $
31+
shouldBe
32+
(parse "* Heading 1")
33+
[Heading 1 "Heading 1"]
34+
35+
it "code" $
36+
shouldBe
37+
(parse "> main = putStrLn \"hello world!\"")
38+
[CodeBlock ["main = putStrLn \"hello world!\""]]
39+
40+
multiline :: Spec
41+
multiline = do
42+
describe "Multi-line tests" $ do
43+
it "example3" $
44+
shouldBe
45+
(parse example3)
46+
example3Result
47+
48+
it "example4" $
49+
shouldBe
50+
(parse example4)
51+
example4Result
52+
53+
example3 :: String
54+
example3 = [r|
55+
Remember that multiple lines with no separation
56+
are grouped together to a single paragraph
57+
but list items remain separate.
58+
59+
# Item 1 of a list
60+
# Item 2 of the same list
61+
|]
62+
63+
example3Result :: Document
64+
example3Result =
65+
[ Paragraph "Remember that multiple lines with no separation are grouped together to a single paragraph but list items remain separate."
66+
, OrderedList
67+
[ "Item 1 of a list"
68+
, "Item 2 of the same list"
69+
]
70+
]
71+
72+
example4 :: String
73+
example4 = [r|
74+
* Compiling programs with ghc
75+
76+
Running ghc invokes the Glasgow Haskell Compiler (GHC),
77+
and can be used to compile Haskell modules and programs into native
78+
executables and libraries.
79+
80+
Create a new Haskell source file named hello.hs, and write
81+
the following code in it:
82+
83+
> main = putStrLn "Hello, Haskell!"
84+
85+
Now, we can compile the program by invoking ghc with the file name:
86+
87+
> ➜ ghc hello.hs
88+
> [1 of 1] Compiling Main ( hello.hs, hello.o )
89+
> Linking hello ...
90+
91+
GHC created the following files:
92+
93+
- hello.hi - Haskell interface file
94+
- hello.o - Object file, the output of the compiler before linking
95+
- hello (or hello.exe on Microsoft Windows) - A native runnable executable.
96+
97+
GHC will produce an executable when the source file satisfies both conditions:
98+
99+
# Defines the main function in the source file
100+
# Defines the module name to be Main, or does not have a module declaration
101+
102+
Otherwise, it will only produce the .o and .hi files.
103+
|]
104+
105+
example4Result :: Document
106+
example4Result =
107+
[ Heading 1 "Compiling programs with ghc"
108+
, Paragraph "Running ghc invokes the Glasgow Haskell Compiler (GHC), and can be used to compile Haskell modules and programs into native executables and libraries."
109+
, Paragraph "Create a new Haskell source file named hello.hs, and write the following code in it:"
110+
, CodeBlock
111+
[ "main = putStrLn \"Hello, Haskell!\""
112+
]
113+
, Paragraph "Now, we can compile the program by invoking ghc with the file name:"
114+
, CodeBlock
115+
[ "➜ ghc hello.hs"
116+
, "[1 of 1] Compiling Main ( hello.hs, hello.o )"
117+
, "Linking hello ..."
118+
]
119+
, Paragraph "GHC created the following files:"
120+
, UnorderedList
121+
[ "hello.hi - Haskell interface file"
122+
, "hello.o - Object file, the output of the compiler before linking"
123+
, "hello (or hello.exe on Microsoft Windows) - A native runnable executable."
124+
]
125+
, Paragraph "GHC will produce an executable when the source file satisfies both conditions:"
126+
, OrderedList
127+
[ "Defines the main function in the source file"
128+
, "Defines the module name to be Main, or does not have a module declaration"
129+
]
130+
, Paragraph "Otherwise, it will only produce the .o and .hi files."
131+
]

test/Spec.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}

0 commit comments

Comments
 (0)