diff options
author | Sibi Prabakaran <sibi@psibi.in> | 2020-06-14 15:40:18 +0530 |
---|---|---|
committer | jao <jao@gnu.org> | 2020-06-23 16:38:20 +0100 |
commit | 8aec308ae2030f8bff7bc58ec435b8a610951d0a (patch) | |
tree | 75224696b25a4926388afe57c5555713038b923d | |
parent | 505615b7fab38ba81fda92e0fba5b3d59cecc948 (diff) | |
download | xmobar-8aec308ae2030f8bff7bc58ec435b8a610951d0a.tar.gz xmobar-8aec308ae2030f8bff7bc58ec435b8a610951d0a.tar.bz2 |
Cleanup and add some tests
-rw-r--r-- | src/Xmobar/Plugins/Monitors/Cpu.hs | 12 | ||||
-rw-r--r-- | test/Xmobar/Plugins/Monitors/CpuSpec.hs | 41 | ||||
-rw-r--r-- | xmobar.cabal | 3 |
3 files changed, 47 insertions, 9 deletions
diff --git a/src/Xmobar/Plugins/Monitors/Cpu.hs b/src/Xmobar/Plugins/Monitors/Cpu.hs index f234dcf..02d2949 100644 --- a/src/Xmobar/Plugins/Monitors/Cpu.hs +++ b/src/Xmobar/Plugins/Monitors/Cpu.hs @@ -138,12 +138,6 @@ parseCpu cref = percent = map safeDiv dif return $ convertToCpuData percent -conditionalCompute :: [String] -> String -> IO String -> IO String -conditionalCompute allFields field action = if field `elem` allFields - then action - else pure [] - - data Field = Field { fieldName :: !String, fieldCompute :: !ShouldCompute @@ -187,8 +181,8 @@ computeFields (x:xs) inputFields = else (Field {fieldName = x, fieldCompute = Skip}) : (computeFields xs inputFields) formatCpu :: CpuArguments -> CpuData -> IO [String] -formatCpu args@CpuArguments{..} cpuData = do - strs <- mapM (formatField cpuParams cpuOpts cpuData) cpuFields +formatCpu CpuArguments{..} cpuInfo = do + strs <- mapM (formatField cpuParams cpuOpts cpuInfo) cpuFields pure $ filter (not . null) strs getInputFields :: CpuArguments -> [String] @@ -214,7 +208,7 @@ getArguments :: [String] -> IO CpuArguments getArguments cpuArgs = do initCpuData <- cpuData cpuDataRef <- newIORef initCpuData - cpuData <- parseCpu cpuDataRef + void $ parseCpu cpuDataRef cpuParams <- computePureConfig cpuArgs cpuConfig cpuInputTemplate <- runTemplateParser cpuParams cpuAllTemplate <- runExportParser (pExport cpuParams) diff --git a/test/Xmobar/Plugins/Monitors/CpuSpec.hs b/test/Xmobar/Plugins/Monitors/CpuSpec.hs new file mode 100644 index 0000000..449acd5 --- /dev/null +++ b/test/Xmobar/Plugins/Monitors/CpuSpec.hs @@ -0,0 +1,41 @@ +module Xmobar.Plugins.Monitors.CpuSpec + ( + spec, main + ) where + +import Test.Hspec +import Xmobar.Plugins.Monitors.Common +import Xmobar.Plugins.Monitors.Cpu +import Data.List + +main :: IO () +main = hspec spec + +spec :: Spec +spec = + describe "CPU Spec" $ do + it "works with total template" $ + do let args = ["-L","3","-H","50","--normal","green","--high","red", "-t", "Cpu: <total>%"] + cpuArgs <- getArguments args + cpuValue <- runCpu cpuArgs + cpuValue `shouldSatisfy` (\item -> "Cpu:" `isPrefixOf` item) + it "works with bar template" $ + do let args = ["-L","3","-H","50","--normal","green","--high","red", "-t", "Cpu: <total>% <bar>"] + cpuArgs <- getArguments args + cpuValue <- runCpu cpuArgs + cpuValue `shouldSatisfy` (\item -> "::" `isSuffixOf` item) + it "works with no icon pattern template" $ + do let args = ["-L","3","-H","50","--normal","green","--high","red", "-t", "Cpu: <total>% <bar>", "--", "--load-icon-pattern", "<icon=bright_%%.xpm/>"] + cpuArgs <- getArguments args + cpuValue <- runCpu cpuArgs + cpuValue `shouldSatisfy` (\item -> not $ "<icon=bright_" `isInfixOf` cpuValue) + it "works with icon pattern template" $ + do let args = ["-L","3","-H","50","--normal","green","--high","red", "-t", "Cpu: <total>% <bar> <ipat>", "--", "--load-icon-pattern", "<icon=bright_%%.xpm/>"] + cpuArgs <- getArguments args + cpuValue <- runCpu cpuArgs + cpuValue `shouldSatisfy` (\item -> "<icon=bright_" `isInfixOf` cpuValue) + it "works with other parameters in template" $ + do let args = ["-L","3","-H","50","--normal","green","--high","red", "-t", "Cpu: <user> <nice> <iowait>"] + cpuArgs <- getArguments args + cpuValue <- runCpu cpuArgs + cpuValue `shouldSatisfy` (\item -> "Cpu:" `isPrefixOf` cpuValue) diff --git a/xmobar.cabal b/xmobar.cabal index faedf51..7bfe85f 100644 --- a/xmobar.cabal +++ b/xmobar.cabal @@ -334,6 +334,8 @@ test-suite XmobarTest Xmobar.Plugins.Monitors.Common.Types Xmobar.Plugins.Monitors.Common.Output Xmobar.Plugins.Monitors.Common.Files + Xmobar.Plugins.Monitors.Cpu + Xmobar.Plugins.Monitors.Common.Run Xmobar.Run.Exec Xmobar.App.Timer Xmobar.System.Signal @@ -345,6 +347,7 @@ test-suite XmobarTest other-modules: Xmobar.Plugins.Monitors.Volume Xmobar.Plugins.Monitors.Alsa Xmobar.Plugins.Monitors.AlsaSpec + Xmobar.Plugins.Monitors.CpuSpec cpp-options: -DALSA |