X-Git-Url: http://gitweb.michael.orlitzky.com/?p=dead%2Fhalcyon.git;a=blobdiff_plain;f=src%2FHtml.hs;h=3abd82b037879becf76d8f22bab2c45de50ddd3f;hp=01004cf556b01f3dde21fd8b9585b82732c5b009;hb=1b72ed45ef890ed1329a32457b4d7f3a7fb37788;hpb=eed0d7b0f8ef28864c00925beef5c8853bcd44cc diff --git a/src/Html.hs b/src/Html.hs index 01004cf..3abd82b 100644 --- a/src/Html.hs +++ b/src/Html.hs @@ -1,14 +1,40 @@ module Html ( html_tests, - replace_entities - ) + replace_entities ) where -import Test.Framework (Test, testGroup) -import Test.Framework.Providers.HUnit (testCase) -import Test.HUnit (Assertion, assertEqual) -import Text.HTML.TagSoup.Entity (lookupEntity) +import Test.Tasty ( TestTree, testGroup ) +import Test.Tasty.HUnit ( (@?=), testCase ) +import Text.HTML.TagSoup.Entity ( lookupEntity ) + +-- | Replace (x)html entities in a 'String'. To do this, we search +-- through the string looking for ampersands which may indicate the +-- beginning of an entity. +-- +-- If we find one and there's a semicolon after it, we attempt to +-- look up the identifier that we found between the ampersand and +-- the semicolon. If an entity is found, we replace the ampersand, +-- semicolon, and everything in between with the entity. However if +-- no corresponding entity is found, we leave everything alone. +-- +-- Examples: +-- +-- >>> replace_entities "Hello, world!" +-- "Hello, world!" +-- +-- >>> replace_entities "Hello; world!" +-- "Hello; world!" +-- +-- >>> replace_entities "Hello, world & other worlds!" +-- "Hello, world & other worlds!" +-- +-- >>> replace_entities "Hello, world & other worlds; hello indeed!" +-- "Hello, world & other worlds; hello indeed!" +-- +-- >>> putStrLn $ replace_entities "Hello world—I guess" +-- Hello world—I guess +-- replace_entities :: String -> String replace_entities [] = [] replace_entities ('&':xs) = @@ -19,26 +45,23 @@ replace_entities ('&':xs) = replace_entities (x:xs) = x : replace_entities xs -html_tests :: Test +html_tests :: TestTree html_tests = - testGroup "HTML Tests" [ tc1 ] - where - tc1 = testCase - "All entities are replaced correctly." - test_replace_entities + testGroup "HTML Tests" [ test_replace_entities ] + -test_replace_entities :: Assertion +test_replace_entities :: TestTree test_replace_entities = - assertEqual description expected_text actual_text + testCase description $ actual @?= expected where - description = "All entities are replaced correctly." - actual_text = + description = "all entities are replaced correctly." + actual = replace_entities $ ""The moon is gay……" " ++ "said <insert the current president of the " ++ "United States of America>. “It’s " ++ "OK—–he’s not a real doctor.”" - expected_text = + expected = "\"The moon is gay……\" said . " ++ "“It’s OK—–he’s not a real doctor.”"