11 import Data.List (isPrefixOf)
12 import System.Directory (doesFileExist)
13 import Test.HUnit (Assertion, assertEqual)
14 import Test.Framework (Test, testGroup)
15 import Test.Framework.Providers.HUnit (testCase)
16 import Text.Regex.Posix ((=~))
17 import Text.XML.HXT.Core (
30 try_make_absolute_url,
33 import LWN.XHTML (XHTML, XML, to_xhtml, to_xml)
35 newtype Title = Title { getTitle :: String }
36 newtype Byline = Byline { getByline :: Maybe String }
37 newtype BodyHtml = BodyHtml { getBodyHtml :: String }
39 instance Show Title where
42 instance Show Byline where
43 show (Byline (Just bl)) = bl
44 show (Byline Nothing ) = ""
46 instance Show BodyHtml where
49 instance XHTML Title where
50 to_xhtml (Title t) = "<h2>" ++ t ++ "</h2>"
52 instance XHTML Byline where
53 to_xhtml (Byline (Just bl)) = "<p><em>" ++ bl ++ "</em></p>"
54 to_xhtml (Byline Nothing) = ""
56 instance XHTML BodyHtml where
57 to_xhtml = getBodyHtml
60 instance XML Title where
64 instance XML Byline where
65 to_xml (Byline (Just bl)) =
66 selem "p" [ selem "em" [ txt bl ] ]
67 to_xml (Byline Nothing) = none
69 instance XML BodyHtml where
70 to_xml (BodyHtml bh) =
71 (arr $ const bh) >>> hread
73 data Article = Article { title :: Title,
75 body_html :: BodyHtml }
77 instance XHTML Article where
78 to_xhtml (Article t bl b) =
85 instance XML Article where
86 to_xml (Article t bl b) =
87 selem "div" [to_xml t, to_xml bl, to_xml b]
89 -- | Convert the given article to either a URL or a filesystem
90 -- path. If the given article exists on the filesystem, we assume
91 -- it's a file. Otherwise, we check to see if it's a URL. Failing
92 -- that, we try to construct a URL from what we're given and do our
94 real_article_path :: String -> IO String
95 real_article_path path = do
96 is_file <- doesFileExist path
97 return $ if is_file then path else add_trailing_slash check_cases
99 abs_current = try_make_absolute_url ("/" ++ path)
100 abs_article = try_make_absolute_url ("Articles/" ++ path)
101 abs_full_article = try_make_absolute_url path
103 check_cases :: String
105 | is_lwn_url path = make_https path
106 | isPrefixOf "current" path = abs_current
107 | path =~ "^[0-9]+$" = abs_article
108 | path =~ "^/Articles/[0-9]+/?$" = abs_full_article
109 | otherwise = path -- Give up
113 test_current_article_path :: Assertion
114 test_current_article_path = do
115 let expected = "https://lwn.net/current/"
116 actual <- real_article_path "current"
117 assertEqual "Current article path constructed" expected actual
119 test_current_bigpage_article_path :: Assertion
120 test_current_bigpage_article_path = do
121 let expected = "https://lwn.net/current/bigpage"
122 actual <- real_article_path "current/bigpage"
123 assertEqual "Current bigpage article path constructed" expected actual
125 test_numbered_article_path :: Assertion
126 test_numbered_article_path = do
127 let expected = "https://lwn.net/Articles/69/"
128 actual <- real_article_path "69" -- I'm twelve
129 assertEqual "Numbered article path constructed" expected actual
132 test_full_article_path :: Assertion
133 test_full_article_path = do
134 let expected = "https://lwn.net/Articles/502979/"
135 actual <- real_article_path "https://lwn.net/Articles/502979/"
136 assertEqual "Full article path left alone" expected actual
138 test_non_https_article_path :: Assertion
139 test_non_https_article_path = do
140 let expected = "https://lwn.net/Articles/502979/"
141 actual <- real_article_path "http://lwn.net/Articles/502979/"
142 assertEqual "Non-https URL made https" expected actual
146 -- | Compares the output of (xshow . to_xml) and to_xhtml; they should
148 test_to_xml :: Assertion
150 actual_xml' <- runX . xshow $ to_xml input_article
151 let actual_xml = actual_xml' !! 0
153 let expected_xml = to_xhtml input_article
156 "The to_xml function works on a trivial example"
160 t = Title "Hello, world!"
161 bl = Byline $ Just "Breaking News"
162 b = BodyHtml "<p>Hello, world!</p>"
163 input_article = Article t bl b
167 article_tests :: Test
169 testGroup "Article Tests" [
170 testCase "Current article path constructed" test_current_article_path,
172 "Current bigpage article path constructed"
173 test_current_bigpage_article_path,
174 testCase "Numbered article path constructed" test_numbered_article_path,
175 testCase "Full article path left alone" test_full_article_path,
176 testCase "Non-https URL made https" test_non_https_article_path,
177 testCase "The to_xml function works on a trivial example" test_to_xml ]