<commit_before>{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
module ReportSpec (main, spec) where

import           Test.Hspec.ShouldBe
import           Data.String.Builder
import           Test.HUnit

import           Data.Monoid
import           System.IO
import           System.IO.Silently (hCapture)
import           Control.Monad.Trans.State
import           Report
import           Interpreter (withInterpreter)
import           Location (noLocation)

main :: IO ()
main = hspecX spec

capture :: Report a -> IO String
capture = fmap fst . hCapture [stderr] . (`execStateT` ReportState 0 mempty)

shouldGive :: IO String -> Builder -> Assertion
action `shouldGive` expected = action `shouldReturn` build expected

spec :: Specs
spec = do

  describe "report" $ do
    it "writes to stderr" $ do
      capture $ do
        report "foobar"
      `shouldReturn` "foobar\n"

    it "overwrites any intermediate output" $ do
      capture $ do
        report_ "foo"
        report  "bar"
      `shouldReturn` "foo\rbar\n"

    it "blank out intermediate output if necessary" $ do
      capture $ do
        report_ "foobar"
        report  "baz"
      `shouldReturn` "foobar\rbaz   \n"

  describe "report_" $ do
    it "writes intermediate output to stderr" $ do
      capture $ do
        report_ "foobar"
      `shouldReturn` "foobar"

    it "overwrites any intermediate output" $ do
      capture $ do
        report_ "foo"
        report_ "bar"
      `shouldReturn` "foo\rbar"

    it "blank out intermediate output if necessary" $ do
      capture $ do
        report_ "foobar"
        report_  "baz"
      `shouldReturn` "foobar\rbaz   "

  describe "reportFailure" $ do
    it "works for one-line test output" $ do
      capture $ do
        reportFailure ["foo"] ["bar"]
      `shouldGive` do
        "expected: foo"
        " but got: bar"

    it "works for multi-line test output" $ do
      capture $ do
        reportFailure ["foo", "bar"] ["foo", "baz"]
      `shouldGive` do
        "expected: foo"
        "          bar"
        " but got: foo"
        "          baz"

    it "quotes output if any output line ends with trailing whitespace" $ do
      capture $ do
        reportFailure ["foo", "bar   "] ["foo", "baz"]
      `shouldGive` do
        "expected: \"foo\""
        "          \"bar   \""
        " but got: \"foo\""
        "          \"baz\""

    it "uses show to format output lines if any output line contains \"unsafe\" characters" $ do
      capture $ do
        reportFailure ["foo\160bar"] ["foo bar"]
      `shouldGive` do
        "expected: \"foo\\160bar\""
        " but got: \"foo bar\""

  describe "runProperty" $ do
    it "runs a Bool property" $ withInterpreter [] $ \repl -> do
      runProperty repl (noLocation "True") `shouldReturn` Success

    it "runs a Bool property with an explicit type signature" $ withInterpreter [] $ \repl -> do
      runProperty repl (noLocation "True :: Bool") `shouldReturn` Success<commit_msg>Add more tests<commit_after>{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
module ReportSpec (main, spec) where

import           Test.Hspec.ShouldBe
import           Data.String.Builder
import           Test.HUnit

import           Data.Monoid
import           System.IO
import           System.IO.Silently (hCapture)
import           Control.Monad.Trans.State
import           Report
import           Interpreter (withInterpreter)
import           Location (noLocation)

main :: IO ()
main = hspecX spec

capture :: Report a -> IO String
capture = fmap fst . hCapture [stderr] . (`execStateT` ReportState 0 mempty)

shouldGive :: IO String -> Builder -> Assertion
action `shouldGive` expected = action `shouldReturn` build expected

spec :: Specs
spec = do

  describe "report" $ do
    it "writes to stderr" $ do
      capture $ do
        report "foobar"
      `shouldReturn` "foobar\n"

    it "overwrites any intermediate output" $ do
      capture $ do
        report_ "foo"
        report  "bar"
      `shouldReturn` "foo\rbar\n"

    it "blank out intermediate output if necessary" $ do
      capture $ do
        report_ "foobar"
        report  "baz"
      `shouldReturn` "foobar\rbaz   \n"

  describe "report_" $ do
    it "writes intermediate output to stderr" $ do
      capture $ do
        report_ "foobar"
      `shouldReturn` "foobar"

    it "overwrites any intermediate output" $ do
      capture $ do
        report_ "foo"
        report_ "bar"
      `shouldReturn` "foo\rbar"

    it "blank out intermediate output if necessary" $ do
      capture $ do
        report_ "foobar"
        report_  "baz"
      `shouldReturn` "foobar\rbaz   "

  describe "reportFailure" $ do
    it "works for one-line test output" $ do
      capture $ do
        reportFailure ["foo"] ["bar"]
      `shouldGive` do
        "expected: foo"
        " but got: bar"

    it "works for multi-line test output" $ do
      capture $ do
        reportFailure ["foo", "bar"] ["foo", "baz"]
      `shouldGive` do
        "expected: foo"
        "          bar"
        " but got: foo"
        "          baz"

    it "quotes output if any output line ends with trailing whitespace" $ do
      capture $ do
        reportFailure ["foo", "bar   "] ["foo", "baz"]
      `shouldGive` do
        "expected: \"foo\""
        "          \"bar   \""
        " but got: \"foo\""
        "          \"baz\""

    it "uses show to format output lines if any output line contains \"unsafe\" characters" $ do
      capture $ do
        reportFailure ["foo\160bar"] ["foo bar"]
      `shouldGive` do
        "expected: \"foo\\160bar\""
        " but got: \"foo bar\""

  describe "runProperty" $ do
    it "reports a failing property" $ withInterpreter [] $ \repl -> do
      let expression = noLocation "False"
      runProperty repl expression `shouldReturn` PropertyFailure expression "Falsifiable (after 1 test):  "

    it "runs a Bool property" $ withInterpreter [] $ \repl -> do
      runProperty repl (noLocation "True") `shouldReturn` Success

    it "runs a Bool property with an explicit type signature" $ withInterpreter [] $ \repl -> do
      runProperty repl (noLocation "True :: Bool") `shouldReturn` Success

    it "runs an implicitly quantified property" $ withInterpreter [] $ \repl -> do
      runProperty repl (noLocation "(reverse . reverse) xs == (xs :: [Int])") `shouldReturn` Success

    it "runs an explicitly quantified property" $ withInterpreter [] $ \repl -> do
      runProperty repl (noLocation "\\xs -> (reverse . reverse) xs == (xs :: [Int])") `shouldReturn` Success

    it "allows to mix implicit and explicit quantification" $ withInterpreter [] $ \repl -> do
      runProperty repl (noLocation "\\x -> x + y == y + x") `shouldReturn` Success
{'fraction_non_alphanumeric': 0.10987074030552292, 'fraction_numerical': 0.0022032902467685076, 'mean_word_length': 2.1952135147817926, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7417592', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1964, 'n_words': 679}