Skip to content

Commit

Permalink
Test fails #1 but seems to work in ui
Browse files Browse the repository at this point in the history
  • Loading branch information
AidanV committed Jul 3, 2024
1 parent 2e8a341 commit e634d0a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
5 changes: 4 additions & 1 deletion app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ drawUI st = maybe [ui] (:[]) (st ^. maybeError)
15
case st ^. previousAnswers of
[] -> str " "
prevAns -> Brick.Widgets.Core.vBox $ reverse $ zipWith (\i e -> str ([i] ++ ": " ++ e)) ['a' ..] $ map (reverse . merge (replicate 16 ' ') . reverse) prevAns
prevAns -> Brick.Widgets.Core.vBox $
reverse $
zipWith (\i e -> str ("#" ++ [i] ++ " : " ++ e)) ['a' ..] $
map (reverse . merge (replicate 15 ' ') . reverse) prevAns
)
)
<=> Brick.Widgets.Border.border (str "= " <+> hLimit 48 e1)
Expand Down
20 changes: 10 additions & 10 deletions lib/Calc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import Text.Read

calculateWithVar :: [String] -> String -> Maybe Double
calculateWithVar prevAns eq =
let variables = zip ['a' ..] prevAns
replacedString = foldl (\acc (c, prevAns) -> replaceCharWithString c prevAns acc) eq variables
in calculate replacedString
let variables = zip (map (\x -> '#' :[x]) ['a' ..]) prevAns
replacedString = foldl (\acc (i, prevAns) -> replace i prevAns acc) eq variables
in calculate replacedString
where
replace from to [] = []
replace from to s@(x:xs) =
if take (length from) s == from
then to ++ replace from to (drop (length from) s)
else x : replace from to xs


replaceCharWithString :: Char -> String -> String -> String
replaceCharWithString c replacement [] =
[]
replaceCharWithString c replacement (firstChar : rest) =
if firstChar == c
then replacement ++ replaceCharWithString c replacement rest
else firstChar : replaceCharWithString c replacement rest

-- TODO: Figure out why Num does not work
calculate :: String -> Maybe Double -- (Num a, Read a, Show a) => String -> a
Expand Down
2 changes: 1 addition & 1 deletion tests/BackendTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ testSqrt :: Test
testSqrt = TestCase (assertEqual "Calculating: 16 sqrt" (Just 4.0) $ calculate "16 sqrt")

testAdditionWithVar :: Test
testAdditionWithVar = TestCase (assertEqual "Calculating: 1 a + where a = 10" (Just 11.0) $ calculateWithVar ["10.0"] "1 a +")
testAdditionWithVar = TestCase (assertEqual "Calculating: 1 #1 + where #1 = 10" (Just 11.0) $ calculateWithVar ["10.0"] "1 #1 +")

testPointOne :: Test
testPointOne = TestCase (assertEqual "Calculate: .1" (Just 0.1) $ calculate ".1")
Expand Down

0 comments on commit e634d0a

Please sign in to comment.