From e634d0a737d02d5475e833d3c18a3a2fb645fb92 Mon Sep 17 00:00:00 2001 From: AidanV Date: Wed, 3 Jul 2024 14:31:24 -0700 Subject: [PATCH] Test fails #1 but seems to work in ui --- app/Main.hs | 5 ++++- lib/Calc.hs | 20 ++++++++++---------- tests/BackendTests.hs | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 679b73d..523dec2 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -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) diff --git a/lib/Calc.hs b/lib/Calc.hs index b1a3837..96f70cc 100644 --- a/lib/Calc.hs +++ b/lib/Calc.hs @@ -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 diff --git a/tests/BackendTests.hs b/tests/BackendTests.hs index d71a0fb..c62be65 100644 --- a/tests/BackendTests.hs +++ b/tests/BackendTests.hs @@ -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")