Skip to content

Commit

Permalink
Add pretty unit tests (#31)
Browse files Browse the repository at this point in the history
Signed-off-by: Sascha Grunert <sgrunert@suse.com>
  • Loading branch information
saschagrunert committed Jun 28, 2019
1 parent 9584b3e commit f6a94ff
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 12 deletions.
2 changes: 1 addition & 1 deletion nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mkDerivation {
base hslogger lens optparse-applicative
];
testHaskellDepends = [
base lens megaparsec tasty tasty-hspec tasty-quickcheck
base lens megaparsec tasty tasty-hspec tasty-quickcheck time
];
prePatch = "hpack";
homepage = "https://github.com/saschagrunert/performabot#readme";
Expand Down
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ tests:
- tasty
- tasty-hspec
- tasty-quickcheck
- time
4 changes: 3 additions & 1 deletion performabot.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cabal-version: 1.12
--
-- see: https://github.com/sol/hpack
--
-- hash: 0c627d55fff6db0f3745e774010e624c0adbc7fa265e79a60506b182fbc4f8c9
-- hash: e66baad00a9ee7a23314f98b28839f9bbcb511da9795672b6bf1c4cecb9eb427

name: performabot
version: 0.1.0
Expand Down Expand Up @@ -86,6 +86,7 @@ test-suite performabot-test
other-modules:
ParserGoSpec
ParserSpec
PrettySpec
ResultSpec
Paths_performabot
hs-source-dirs:
Expand All @@ -100,4 +101,5 @@ test-suite performabot-test
, tasty
, tasty-hspec
, tasty-quickcheck
, time
default-language: Haskell2010
11 changes: 9 additions & 2 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import ParserGoSpec ( parserGoSpec )

import ParserSpec ( parserProps, parserSpec )

import PrettySpec ( prettySpec )

import ResultSpec ( resultSpec )

import Test.Tasty
Expand All @@ -25,11 +27,16 @@ main = do
-- Unit tests based on hspec
unitTests :: IO TestTree
unitTests = do
parserUnitTests <- testSpec "Parser.hs" parserSpec
parserGoUnitTests <- testSpec "ParserGo.hs" parserGoSpec
parserUnitTests <- testSpec "Parser.hs" parserSpec
prettyUnitTests <- testSpec "Pretty.hs" prettySpec
resultUnitTests <- testSpec "Result.hs" resultSpec
return $ testGroup "Unit"
[ parserGoUnitTests, parserUnitTests, resultUnitTests ]
[ parserGoUnitTests
, parserUnitTests
, prettyUnitTests
, resultUnitTests
]

propertyTests :: TestTree
propertyTests = testGroup "Property" [ parserProps ]
29 changes: 21 additions & 8 deletions test/ParserGoSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,33 @@ benchmark _ = emptyBenchmark
parserGoSpec :: Spec
parserGoSpec = parallel $ do
it "should succeed to parse" $ do
let s = parse Init " 10 samples:"
benchmark s ^. benchmarkSamples `shouldBe` 10
let res = benchmark . parse s $ " pullTime - Fastest Time: 0.944s, "
let s0 = parse Init " 10 samples:"
benchmark s0 ^. benchmarkSamples `shouldBe` 10
let s1 = parse s0 $ " pullTime - Fastest Time: 0.944s, "
++ "Average Time: 0.953s ± 0.008s, Slowest Time: 0.971s"
res ^. benchmarkAverage `shouldBe` 0.953
res ^. benchmarkDerivation `shouldBe` 0.008
res ^. benchmarkName `shouldBe` "pullTime"
res ^. benchmarkSamples `shouldBe` 10
res ^. benchmarkUnit `shouldBe` "s"
let s2 = parse s1 $ " other benchmark - Fastest Time: 0.944s, "
++ "Average Time: 0.123s ± 1.000s, Slowest Time: 0.971s"
let r1 = benchmark s1
let r2 = benchmark s2
r1 ^. benchmarkAverage `shouldBe` 0.953
r1 ^. benchmarkDerivation `shouldBe` 0.008
r1 ^. benchmarkName `shouldBe` "pullTime"
r1 ^. benchmarkSamples `shouldBe` 10
r1 ^. benchmarkUnit `shouldBe` "s"
r2 ^. benchmarkAverage `shouldBe` 0.123
r2 ^. benchmarkDerivation `shouldBe` 1.000
r2 ^. benchmarkName `shouldBe` "other benchmark"
r2 ^. benchmarkSamples `shouldBe` 10
r2 ^. benchmarkUnit `shouldBe` "s"

it "should succeed to parse a huge sample number" $
benchmark (parse Init " 192835128754 samples:")
^. benchmarkSamples `shouldBe` 192835128754

it "should succeed to parse with ansi colors" $
benchmark (parse Init " [1m20[0m samples:")
^. benchmarkSamples `shouldBe` 20

it "should fail to parse empty input" $ failure (parse Init "")
`shouldContain` "unexpected end of input"

Expand Down
36 changes: 36 additions & 0 deletions test/PrettySpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- | The pretty tests
--
-- @since 0.1.0
module PrettySpec ( prettySpec ) where

import Data.Time.Clock ( getCurrentTime )

import Model ( Benchmark(Benchmark), Test(Test) )

import Pretty ( header, prettyPrint )

import Test.Tasty.Hspec ( Spec, it, parallel, shouldContain )

-- Pretty.hs related unit tests
prettySpec :: Spec
prettySpec = parallel $ do
it "should succeed pretty print without previous result" $ do
let bs = [ Benchmark 0 0 "name" 1 "s" ]
res = prettyPrint bs Nothing
res `shouldContain` "Nothing to compare against"
res `shouldContain` header
res `shouldContain` "name"
res `shouldContain` "=\n```"

it "should succeed pretty print with previous result" $ do
x <- getCurrentTime
let t = Test x "" "" "" "" []
b1 = Benchmark 0 0 "name1" 1 "s"
b2 = Benchmark 1 2 "this is a very very long benchmark name" 2 "s"
res = prettyPrint [ b1, b2 ] $ Just (t, [ b1 ])
res `shouldContain` "Comparing to commit"
res `shouldContain` header
res `shouldContain` "name"
res `shouldContain` ""
res `shouldContain` "=\n```"

0 comments on commit f6a94ff

Please sign in to comment.