<commit_before>module Test.GitHUD.Config.Parse (
  configParserTests
  ) where

import Test.Tasty
import Test.Tasty.HUnit

import Text.Parsec (parse)
import Text.Parsec.String (Parser)

import GitHUD.Config.Parse
import GitHUD.Config.Types
import GitHUD.Terminal.Types

configParserTests :: TestTree
configParserTests = testGroup "Config Parser Test"
  [ testItemParser
    , testCommentParser
    , testConfigItemFolder
    , testColorConfigToColor
    , testIntensityConfigToIntensity
  ]

testItemParser :: TestTree
testItemParser = testGroup "#itemParser"
  [ testCase "properly formed config item" $
      utilConfigItemParser itemParser "some_test_key=some Complex ⚡ value"
      @?= Item "some_test_key" "some Complex ⚡ value"

    , testCase "dash characters are not allowed in keys" $
        utilConfigItemParser itemParser "some-key=dash"
        @?= ErrorLine

    , testCase "num characters are not allowed in keys" $
        utilConfigItemParser itemParser "some123=dash"
        @?= ErrorLine

    , testCase "empty keys are not allowed" $
        utilConfigItemParser itemParser "=dash"
        @?= ErrorLine

    , testCase "Comment should not work" $
        utilConfigItemParser itemParser "#some comment"
        @?= ErrorLine
  ]

testCommentParser :: TestTree
testCommentParser = testGroup "#commentParser"
  [ testCase "proper comment" $
      utilConfigItemParser commentParser "#some comment\n"
      @?= Comment

    , testCase "not a comment if start with a space" $
        utilConfigItemParser commentParser " #some non comment\n"
        @?= ErrorLine
  ]

testConfigItemFolder :: TestTree
testConfigItemFolder = testGroup "#configItemFolder"
  [   testCase "Comment should have no impact on the config" $
        configItemsFolder defaultConfig (Comment)
        @?= defaultConfig

    , testCase "ErrorLines should have no impact on the config" $
        configItemsFolder defaultConfig (ErrorLine)
        @?= defaultConfig

    , testCase "Key: git_repo_indicator" $
        configItemsFolder defaultConfig (Item "git_repo_indicator" "foo")
        @?= defaultConfig { confRepoIndicator = "foo" }

    , testCase "Key: no_upstream_text" $
        configItemsFolder defaultConfig (Item "no_upstream_text" "foo")
        @?= defaultConfig { confNoUpstreamString = "foo" }

    , testCase "Key: no_upstream_indicator" $
        configItemsFolder defaultConfig (Item "no_upstream_indicator" "foo")
        @?= defaultConfig { confNoUpstreamIndicator = "foo" }

    , testCase "Key: no_upstream_indicator_color" $
        configItemsFolder defaultConfig (Item "no_upstream_indicator_color" "Black")
        @?= defaultConfig { confNoUpstreamIndicatorColor = Black }

    , testCase "Key: no_upstream_indicator_color - invalid color" $
        configItemsFolder defaultConfig (Item "no_upstream_indicator_color" "FOO")
        @?= defaultConfig { confNoUpstreamIndicatorColor = White }

    , testCase "Key: no_upstream_indicator_intensity" $
        configItemsFolder defaultConfig (Item "no_upstream_indicator_intensity" "Dull")
        @?= defaultConfig { confNoUpstreamIndicatorIntensity = Dull }

    , testCase "Key: no_upstream_indicator_intensity - invalid intensity" $
        configItemsFolder defaultConfig (Item "no_upstream_indicator_intensity" "FOO")
        @?= defaultConfig { confNoUpstreamIndicatorIntensity = Dull }
  ]

utilConfigItemParser :: Parser ConfigItem -> String -> ConfigItem
utilConfigItemParser parser str =
  either
    (const ErrorLine)
    id
    (parse parser "" str)

testIntensityConfigToIntensity :: TestTree
testIntensityConfigToIntensity = testGroup "#intensityConfigToIntensity"
  [   testCase "valid intensity - return it" $
        intensityConfigToIntensity "Vivid" @?= Vivid

    , testCase "invalid intensity - default to Dull" $
        intensityConfigToIntensity "Foo" @?= Dull
  ]

testColorConfigToColor :: TestTree
testColorConfigToColor = testGroup "#colorConfigToColor"
  [   testCase "valid color - return it" $
        colorConfigToColor "Cyan" @?= Cyan

    , testCase "invalid color - default to White" $
        colorConfigToColor "Foo" @?= White
  ]<commit_msg>[Test] First iteration of a DSL for the folder function<commit_after>module Test.GitHUD.Config.Parse (
  configParserTests
  ) where

import Test.Tasty
import Test.Tasty.HUnit

import Text.Parsec (parse)
import Text.Parsec.String (Parser)

import GitHUD.Config.Parse
import GitHUD.Config.Types
import GitHUD.Terminal.Types

configParserTests :: TestTree
configParserTests = testGroup "Config Parser Test"
  [ testItemParser
    , testCommentParser
    , testConfigItemFolder
    , testColorConfigToColor
    , testIntensityConfigToIntensity
  ]

testItemParser :: TestTree
testItemParser = testGroup "#itemParser"
  [ testCase "properly formed config item" $
      utilConfigItemParser itemParser "some_test_key=some Complex ⚡ value"
      @?= Item "some_test_key" "some Complex ⚡ value"

    , testCase "dash characters are not allowed in keys" $
        utilConfigItemParser itemParser "some-key=dash"
        @?= ErrorLine

    , testCase "num characters are not allowed in keys" $
        utilConfigItemParser itemParser "some123=dash"
        @?= ErrorLine

    , testCase "empty keys are not allowed" $
        utilConfigItemParser itemParser "=dash"
        @?= ErrorLine

    , testCase "Comment should not work" $
        utilConfigItemParser itemParser "#some comment"
        @?= ErrorLine
  ]

testCommentParser :: TestTree
testCommentParser = testGroup "#commentParser"
  [ testCase "proper comment" $
      utilConfigItemParser commentParser "#some comment\n"
      @?= Comment

    , testCase "not a comment if start with a space" $
        utilConfigItemParser commentParser " #some non comment\n"
        @?= ErrorLine
  ]

testConfigItemFolder :: TestTree
testConfigItemFolder = testGroup "#configItemFolder"
  [   testCase "Comment should have no impact on the config" $
        configItemsFolder defaultConfig (Comment)
        @?= defaultConfig

    , testCase "ErrorLines should have no impact on the config" $
        configItemsFolder defaultConfig (ErrorLine)
        @?= defaultConfig

    , testCase "Key: git_repo_indicator" $
        configItemsFolder defaultConfig (Item "git_repo_indicator" "foo")
        @?= defaultConfig { confRepoIndicator = "foo" }

    , testCase "Key: no_upstream_text" $
        configItemsFolder defaultConfig (Item "no_upstream_text" "foo")
        @?= defaultConfig { confNoUpstreamString = "foo" }

    , testCase "Key: no_upstream_indicator" $
        configItemsFolder defaultConfig (Item "no_upstream_indicator" "foo")
        @?= defaultConfig { confNoUpstreamIndicator = "foo" }

    , testCase "Key: no_upstream_indicator_color" $
        configItemsFolder defaultConfig (Item "no_upstream_indicator_color" "Black")
        @?= defaultConfig { confNoUpstreamIndicatorColor = Black }

    , testCase "Key: no_upstream_indicator_color - invalid color" $
        configItemsFolder defaultConfig (Item "no_upstream_indicator_color" "FOO")
        @?= defaultConfig { confNoUpstreamIndicatorColor = White }

    , testCase "Key: no_upstream_indicator_intensity" $
        configItemsFolder defaultConfig (Item "no_upstream_indicator_intensity" "Dull")
        @?= defaultConfig { confNoUpstreamIndicatorIntensity = Dull }

    , testCase "Key: no_upstream_indicator_intensity - invalid intensity" $
        configItemsFolder defaultConfig (Item "no_upstream_indicator_intensity" "FOO")
        @?= defaultConfig { confNoUpstreamIndicatorIntensity = Dull }

    , testCase "Key: remote_commits_indicator" $
        (((expectKey "remote_commits_indicator")
          (withValue "FOO"))
          `toChangeField` confRemoteCommitsIndicator)
          `toValue` "FOO"
  ]

expectKey :: String -> (String -> Config)
expectKey key = (configItemsFolder defaultConfig) . (Item key)

withValue :: a -> a
withValue = id

toChangeField :: Config -> (Config -> String) -> String
toChangeField config field = field config

toValue :: String -> String -> Assertion
toValue actual expected = actual @?= expected

infix 1 `toChangeField`

utilConfigItemParser :: Parser ConfigItem -> String -> ConfigItem
utilConfigItemParser parser str =
  either
    (const ErrorLine)
    id
    (parse parser "" str)

testIntensityConfigToIntensity :: TestTree
testIntensityConfigToIntensity = testGroup "#intensityConfigToIntensity"
  [   testCase "valid intensity - return it" $
        intensityConfigToIntensity "Vivid" @?= Vivid

    , testCase "invalid intensity - default to Dull" $
        intensityConfigToIntensity "Foo" @?= Dull
  ]

testColorConfigToColor :: TestTree
testColorConfigToColor = testGroup "#colorConfigToColor"
  [   testCase "valid color - return it" $
        colorConfigToColor "Cyan" @?= Cyan

    , testCase "invalid color - default to White" $
        colorConfigToColor "Foo" @?= White
  ]
{'fraction_non_alphanumeric': 0.08548640754886543, 'fraction_numerical': 0.0007863401482812851, 'mean_word_length': 3.7533368926855313, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '7567526', 'n_tokens_mistral': 2600, 'n_tokens_neox': 2559, 'n_words': 651}