<commit_before>$here = Split-Path -Parent $MyInvocation.MyCommand.Path
# here : /branch/tests/Poshbox.Test
. "$here\..\TestCommon.ps1"

Describe "New-PSClass" {
    It 'cannot create two classes with the same name' {
        $className = [Guid]::NewGuid().ToString()
        $testClass = New-PSClass $className {} -PassThru
        { New-PSClass $className {} } | Should Throw
    }

    It 'can inherit using the name of a class instead of a variable of the class definition' {
        $className = [Guid]::NewGuid().ToString()
        $testClass = New-PSClass $className {} -PassThru

        $derivedClassName = [Guid]::NewGuid().ToString()
        $derivedClass = New-PSClass $derivedClassName -Inherit $className {} -PassThru
    }

    It 'can override OBJECT methods (like ToString())' {
        $className = [Guid]::NewGuid().ToString()
        $testClass = New-PSClass $className {
            method -override 'ToString' {
                return 'foo'
            }
        } -PassThru

        $sut = $testClass.New()
        $sut.ToString() | Should Be 'foo'
    }

    Context 'PassThru' {
        It 'Returns class object when given -PassThru switch' {
            $className = [Guid]::NewGuid().ToString()
            $testClass = New-PSClass $className {} -PassThru
            ($testClass -ne $null) | Should Be $true
            $testClass.__ClassName | Should Be $className
        }

        It 'has no return value without passthru, but can be accessed from Get-PSClass' {
            $className = [Guid]::NewGuid().ToString()
            $testClass = New-PSClass $className {}
            ($testClass -eq $null) | Should Be $true

            $testClass = Get-PSClass $className
            ($testClass -eq $null) | Should Be $false
            $testClass.__ClassName | Should Be $className
        }
    }

    Context "GivenStaticMethod" {
        It "runs provided script block" {
            $className = [Guid]::NewGuid().ToString()
            $testClass = New-PSClass $className {
                method "testMethod" -static {
                    return "expected"
                }
            } -PassThru
            $testClass.testMethod() | Should Be "expected"
        }
    }

    BeforeEach {
        $className = [Guid]::NewGuid().ToString()
        $testClass = New-PSClass $className {
            method "testMethodNoParams" { return "base" }
            note "foo" "base"
        } -PassThru
    }

    Context "GivenBaseClass" {
        It "can override method with empty params" {
            $className = [Guid]::NewGuid().ToString()
            $derivedClass = New-PSClass $className -inherit $testClass {
                method -override "testMethodNoParams" { return "expected" }
            } -PassThru

            $newDerived = $derivedClass.New()
            $newDerived.testMethodNoParams() | Should Be "expected"
        }

        It "can call non overridden base method" {
            $className = [Guid]::NewGuid().ToString()
            $derivedClass = New-PSClass $className -inherit $testClass {} -PassThru
            $newDerived = $derivedClass.New()
            $newDerived.testMethodNoParams() | Should Be "base"<commit_msg>adding tests<commit_after>$here = Split-Path -Parent $MyInvocation.MyCommand.Path
# here : /branch/tests/Poshbox.Test
. "$here\..\TestCommon.ps1"

Describe "New-PSClass" {
    It 'cannot create two classes with the same name' {
        $className = [Guid]::NewGuid().ToString()
        $testClass = New-PSClass $className {} -PassThru
        { New-PSClass $className {} } | Should Throw
    }

    It 'can inherit using the name of a class instead of a variable of the class definition' {
        $className = [Guid]::NewGuid().ToString()
        $testClass = New-PSClass $className {} -PassThru

        $derivedClassName = [Guid]::NewGuid().ToString()
        $derivedClass = New-PSClass $derivedClassName -Inherit $className {} -PassThru
    }

    It 'can override OBJECT methods (like ToString())' {
        $className = [Guid]::NewGuid().ToString()
        $testClass = New-PSClass $className {
            method -override 'ToString' {
                return 'foo'
            }
        } -PassThru

        $sut = $testClass.New()
        $sut.ToString() | Should Be 'foo'
    }

    Context 'PassThru' {
        It 'Returns class object when given -PassThru switch' {
            $className = [Guid]::NewGuid().ToString()
            $testClass = New-PSClass $className {} -PassThru
            ($testClass -ne $null) | Should Be $true
            $testClass.__ClassName | Should Be $className
        }

        It 'has no return value without passthru, but can be accessed from Get-PSClass' {
            $className = [Guid]::NewGuid().ToString()
            $testClass = New-PSClass $className {}
            ($testClass -eq $null) | Should Be $true

            $testClass = Get-PSClass $className
            ($testClass -eq $null) | Should Be $false
            $testClass.__ClassName | Should Be $className
        }
    }

    Context "GivenStaticMethod" {
        It "runs provided script block" {
            $className = [Guid]::NewGuid().ToString()
            $testClass = New-PSClass $className {
                method "testMethod" -static {
                    return "expected"
                }
            } -PassThru
            $testClass.testMethod() | Should Be "expected"
        }
    }

    BeforeEach {
        $className = [Guid]::NewGuid().ToString()
        $testClass = New-PSClass $className {
            method "testMethodNoParams" { return "base" }
            note "foo" "base"
        } -PassThru
    }

    Context "GivenBaseClass" {
        It 'can call function:base with multiple inheritance' {
            $className = [Guid]::NewGuid().ToString()
            $testClass = New-PSClass $className {
                note "_foo" "default"
                constructor {
                    $this._foo = $args[0]
                }
            } -PassThru

            $className2 = [Guid]::NewGuid().ToString()
            $testClass2 = New-PSClass $className2 -Inherit $className {
                constructor {
                    base $args[0]
                }
            } -PassThru

            $className3 = [Guid]::NewGuid().ToString()
            $testClass3 = New-PSClass $className3 -Inherit $className2 {
                constructor {
                    base $args[0]
                }
            } -PassThru

            $expectedValue = 'i am expected'
            $sut = $testClass3.New($expectedValue)
            $sut._foo | should be $expectedValue
        }

        It "can override method with empty params" {
            $className = [Guid]::NewGuid().ToString()
            $derivedClass = New-PSClass $className -inherit $testClass {
                method -override "testMethodNoParams" { return "expected" }
            } -PassThru

            $newDerived = $derivedClass.New()
            $newDerived.testMethodNoParams() | Should Be "expected"
        }

        It "can call non overridden base method" {
            $className = [Guid]::NewGuid().ToString()
            $derivedClass = New-PSClass $className -inherit $testClass {} -PassThru
            $newDerived = $derivedClass.New()
            $newDerived.testMethodNoParams() | Should Be "base"
{'fraction_non_alphanumeric': 0.11804891378603634, 'fraction_numerical': 0.0017761989342806395, 'mean_word_length': 1.903609678698929, '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': '3714114', 'n_tokens_mistral': 2039, 'n_tokens_neox': 1957, 'n_words': 515}