Skip to content
Permalink
Browse files

Add the function and its tests

  • Loading branch information...
adomokos committed Nov 5, 2018
1 parent 11ad845 commit e23149db832b3e1d83115e42063febd2f903ae35
Showing with 41 additions and 13 deletions.
  1. +41 −13 test/RomanNumeralsSpec.hs
@@ -1,6 +1,8 @@
module RomanNumeralsSpec where

import Test.Hspec
import Data.List (isPrefixOf, find)
import Data.Maybe (fromJust)

main :: IO ()
main = hspec spec
@@ -28,30 +30,56 @@ convertToRoman x =
head . filter (\(a,_) -> a <= x) $ conversions

convertFromRoman :: Roman -> Int
convertFromRoman "" = undefined
convertFromRoman "" = 0
convertFromRoman r =
number + convertFromRoman (drop (length roman) r)
where
(number, roman) = fromJust $ find (\(_,r') -> r' `isPrefixOf` r) conversions

spec :: Spec
spec =
spec = do
describe "Converting to Roman Numerals" $ do
it "converts 1 to I" $ do
it "converts 1 to I" $
convertToRoman 1 `shouldBe` "I"
it "converts 2 to II" $ do
it "converts 2 to II" $
convertToRoman 2 `shouldBe` "II"
it "converts 3 to III" $ do
it "converts 3 to III" $
convertToRoman 3 `shouldBe` "III"
it "converts 4 to IV" $ do
it "converts 4 to IV" $
convertToRoman 4 `shouldBe` "IV"
it "converts 5 to V" $ do
it "converts 5 to V" $
convertToRoman 5 `shouldBe` "V"
it "converts 6 to VI" $ do
it "converts 6 to VI" $
convertToRoman 6 `shouldBe` "VI"
it "converts 8 to VIII" $ do
it "converts 8 to VIII" $
convertToRoman 8 `shouldBe` "VIII"
it "converts 9 to IX" $ do
it "converts 9 to IX" $
convertToRoman 9 `shouldBe` "IX"
it "converts 10 to X" $ do
it "converts 10 to X" $
convertToRoman 10 `shouldBe` "X"
it "converts 11 to XI" $ do
it "converts 11 to XI" $
convertToRoman 11 `shouldBe` "XI"
it "converts 99 to L" $ do
it "converts 99 to L" $
convertToRoman 99 `shouldBe` "XCIX"

describe "Roman to Number Conversions" $ do
it "converts I to 1" $
convertFromRoman "I" `shouldBe` 1
it "converts II to 2" $
convertFromRoman "II" `shouldBe` 2
it "converts III to 3" $
convertFromRoman "III" `shouldBe` 3
it "converts IV to 4" $
convertFromRoman "IV" `shouldBe` 4
it "converts V to 5" $
convertFromRoman "V" `shouldBe` 5
it "converts VIII to 8" $
convertFromRoman "VIII" `shouldBe` 8
it "converts IX to 9" $
convertFromRoman "IX" `shouldBe` 9
it "converts X to 10" $
convertFromRoman "X" `shouldBe` 10
it "converts XI to 11" $
convertFromRoman "XI" `shouldBe` 11
it "converts XCIX to 99" $
convertFromRoman "XCIX" `shouldBe` 99

0 comments on commit e23149d

Please sign in to comment.
You can’t perform that action at this time.