git.iko.soy/typeable/country-codes
on master help sign in
doc and tests
commit670b1ae14a24204e69cf77426b3d2ad3f0aa2cee authorjpmoresmau <jp@moresmau.fr> dateSat 26 Apr 2014, 17:16 (12 years ago) parent7415654
3 files changed+33−3
exe/Main.hs+10−1 src/Data/CountryCodes.hs+3−1 test/country-codes-tests.hs+20−1
exe/Main.hs+10−1
@@ -1,4 +1,5 @@
11{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
2+-- | generate the ISO31661.hs file from the wikipedia html file
23module Main where
34
45import Text.HTML.TagSoup
@@ -9,15 +10,21 @@ import Data.Maybe (mapMaybe)
910import Data.Monoid ((<>))
1011import Data.List (foldl')
1112
13+
14+-- | entry point
1215main :: IO()
1316main = do
17+ -- read from a local version
1418 txt <- T.readFile "data/ISO 3166-1 alpha-2 - Wikipedia, the free encyclopedia.html"
19+ -- extract data
1520 let assocs = extract txt
21+ -- generate Haskell code
1622 let haskell=generate assocs
23+ -- write file
1724 T.writeFile "src/Data/CountryCodes/ISO31661.hs" haskell
1825 return ()
1926
20−
27+-- | extract tuples (code,name) from wikipedia file
2128extract :: T.Text -> [(T.Text,T.Text)]
2229extract txt =
2330 let tts=parseTags txt
@@ -40,6 +47,7 @@ extract txt =
4047 in Just (cid,name)
4148 ) rows
4249
50+-- | generate haskell code
4351generate :: [(T.Text,T.Text)] -> T.Text
4452generate assocs =
4553 let
@@ -115,5 +123,6 @@ generate assocs =
115123 toName :: T.Text -> (T.Text,T.Text) -> T.Text
116124 toName acc (i,n) = acc <> nl <> "toName " <> i <> " = \"" <> n <> "\""
117125
126+-- | new line
118127nl :: T.Text
119128nl = "\n"
src/Data/CountryCodes.hs+3−1
@@ -1,7 +1,9 @@
11module Data.CountryCodes (
22 CountryCode(..)
33 , allNames
4+ , fromMText
45 , fromText
6+ , fromMName
57 , fromName
68 , toText
79 , toName
@@ -12,6 +14,6 @@ import Data.Text (Text)
1214
1315import Data.CountryCodes.ISO31661
1416
15−
17+-- | list all codes with names
1618allNames :: [(CountryCode,Text)]
1719allNames = map (id &&& toName) $ enumFrom minBound
test/country-codes-tests.hs+20−1
@@ -2,10 +2,29 @@
22-- | entry module for tests
33module Main where
44
5+import Control.Monad (forM_)
6+import Data.Aeson
57import Test.Framework
8+import Test.HUnit (Assertion)
69
710
11+import Data.CountryCodes
12+
813-- | test entry point
914main :: IO()
10−main = undefined -- htfMain htf_importedTests
15+main = htfMain [htf_thisModulesTests]
16+
17+-- | CountryCode -> Text -> CountryCode
18+test_Text :: Assertion
19+test_Text = forM_ [minBound .. maxBound] $ \c ->
20+ assertEqual c (fromText $ toText c)
21+
22+-- | CountryCode -> name -> CountryCode
23+test_Name :: Assertion
24+test_Name = forM_ [minBound .. maxBound] $ \c ->
25+ assertEqual c (fromName $ toName c)
1126
27+-- | CountryCode -> JSON -> CountryCode
28+test_JSON :: Assertion
29+test_JSON = forM_ ([minBound .. maxBound]::[CountryCode]) $ \c ->
30+ assertEqual (Data.Aeson.Success c) (fromJSON $ toJSON c)