<commit_before>﻿Import-Module PSScriptAnalyzer
$ruleName = "PSUseWhitespace"
$ruleConfiguration = @{
    Enable = $true
    CheckOpenBrace = $false
    CheckOpenParen = $false
    CheckOperator = $false
}

$settings = @{
    IncludeRules = @($ruleName)
    Rules = @{
        PSUseWhitespace = $ruleConfiguration
    }
}

Describe "UseWhitespace" {
    Context "When an open brace follows a keyword" {
        BeforeAll {

            $ruleConfiguration.CheckOpenBrace = $true
            $ruleConfiguration.CheckOpenParen = $false
            $ruleConfiguration.CheckOperator = $false
        }

        It "Should find a violation if an open brace does not follow whitespace" {
            $def =  @'
if ($true){}
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 1
        }

        It "Should not find violation if an open brace follows a whitespace" {
            $def =  @'
if($true) {}
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 0

        }

    }

    Context "When a parenthesis follows a keyword" {
        BeforeAll {
            $ruleConfiguration.CheckOpenBrace = $false
            $ruleConfiguration.CheckOpenParen = $true
            $ruleConfiguration.CheckOperator = $false
        }

        It "Should find violation in an if statement" {
            $def =  @'
if($true) {}
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 1
        }

        It "Should not find a violation in a function definition" {
            $def =  @'
function foo($param1) {

}
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 0
        }

        It "Should not find a violation in a param block" {
            $def =  @'
function foo() {
    param( )
}
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 0
        }

        It "Should not find a violation in a nested open paren" {
            $def =  @'
function foo($param) {
    ((Get-Process))
}
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 0
        }

        It "Should not find a violation on a method call" {
            $def =  @'
$x.foo("bar")
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 0
        }
    }

    Context "When there is whitespace around assignment and binary operators" {
        BeforeAll {
            $ruleConfiguration.CheckOpenParen = $false
            $ruleConfiguration.CheckOpenBrace = $false
            $ruleConfiguration.CheckOperator = $true
        }

        It "Should find a violation if no whitespace around an assignment operator" {
            $def =  @'
$x=1
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 1
        }

        It "Should find a violation if no whitespace before an assignment operator" {
            $def =  @'
$x= 1
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 1
        }

        It "Should find a violation if no whitespace after an assignment operator" {
            $def =  @'
$x =1
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 1
        }

        It "Should find a violation if there is a whitespaces not of size 1 around an assignment operator" {
            $def =  @'
$x  =  1
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 1
        }

        It "Should not find violation if there are whitespaces of size 1 around an assignment operator" {
            $def =  @'
$x = 1
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 0
        }
    }

    Context "When a comma is not followed by a space" {
        BeforeAll {
            $ruleConfiguration.CheckOpenBrace = $false
            $ruleConfiguration.CheckOpenParen = $false
            $ruleConfiguration.CheckOperator = $false
            $ruleConfiguration.CheckSeparator = $true
        }

        It "Should find a violation" {
            $def = @'
$x = @(1,2)
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 1
        }

        It "Should not find a violation if a space follows a comma" {
            $def = @'
$x = @(1, 2)
'@<commit_msg>Update tests for usewhitespace rule<commit_after>﻿Import-Module PSScriptAnalyzer
$ruleName = "PSUseWhitespace"
$ruleConfiguration = @{
    Enable = $true
    CheckOpenBrace = $false
    CheckOpenParen = $false
    CheckOperator = $false
    CheckSeparator = $false
}

$settings = @{
    IncludeRules = @($ruleName)
    Rules = @{
        PSUseWhitespace = $ruleConfiguration
    }
}

Describe "UseWhitespace" {
    Context "When an open brace follows a keyword" {
        BeforeAll {

            $ruleConfiguration.CheckOpenBrace = $true
            $ruleConfiguration.CheckOpenParen = $false
            $ruleConfiguration.CheckOperator = $false
            $ruleConfiguration.CheckSeparator = $false
        }

        It "Should find a violation if an open brace does not follow whitespace" {
            $def =  @'
if ($true){}
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 1
        }

        It "Should not find violation if an open brace follows a whitespace" {
            $def =  @'
if($true) {}
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 0

        }

    }

    Context "When a parenthesis follows a keyword" {
        BeforeAll {
            $ruleConfiguration.CheckOpenBrace = $false
            $ruleConfiguration.CheckOpenParen = $true
            $ruleConfiguration.CheckOperator = $false
            $ruleConfiguration.CheckSeparator = $false
        }

        It "Should find violation in an if statement" {
            $def =  @'
if($true) {}
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 1
        }

        It "Should not find a violation in a function definition" {
            $def =  @'
function foo($param1) {

}
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 0
        }

        It "Should not find a violation in a param block" {
            $def =  @'
function foo() {
    param( )
}
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 0
        }

        It "Should not find a violation in a nested open paren" {
            $def =  @'
function foo($param) {
    ((Get-Process))
}
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 0
        }

        It "Should not find a violation on a method call" {
            $def =  @'
$x.foo("bar")
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 0
        }
    }

    Context "When there is whitespace around assignment and binary operators" {
        BeforeAll {
            $ruleConfiguration.CheckOpenParen = $false
            $ruleConfiguration.CheckOpenBrace = $false
            $ruleConfiguration.CheckOperator = $true
            $ruleConfiguration.CheckSeparator = $false
        }

        It "Should find a violation if no whitespace around an assignment operator" {
            $def =  @'
$x=1
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 1
        }

        It "Should find a violation if no whitespace before an assignment operator" {
            $def =  @'
$x= 1
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 1
        }

        It "Should find a violation if no whitespace after an assignment operator" {
            $def =  @'
$x =1
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 1
        }

        It "Should find a violation if there is a whitespaces not of size 1 around an assignment operator" {
            $def =  @'
$x  =  1
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 1
        }

        It "Should not find violation if there are whitespaces of size 1 around an assignment operator" {
            $def =  @'
$x = @"
"abc"
"@
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 0
        }

        It "Should not find violation if there are whitespaces of size 1 around an assignment operator for here string" {
            $def =  @'
$x = 1
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 0
        }
    }

    Context "When a comma is not followed by a space" {
        BeforeAll {
            $ruleConfiguration.CheckOpenBrace = $false
            $ruleConfiguration.CheckOpenParen = $false
            $ruleConfiguration.CheckOperator = $false
            $ruleConfiguration.CheckSeparator = $true
        }

        It "Should find a violation" {
            $def = @'
$x = @(1,2)
'@
            $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
            $violations.Count | Should Be 1
        }

        It "Should not find a violation if a space follows a comma" {
            $def = @'
$x = @(1, 2)
'@
{'fraction_non_alphanumeric': 0.08960915157292659, 'fraction_numerical': 0.004957102001906578, 'mean_word_length': 2.0094664371772804, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7640794', 'n_tokens_mistral': 2941, 'n_tokens_neox': 2804, 'n_words': 880}