"Fossies" - the Fresh Open Source Software Archive

Member "PowerShell-7.2.6/test/powershell/engine/ETS/Adapter.Tests.ps1" (11 Aug 2022, 14717 Bytes) of package /linux/misc/PowerShell-7.2.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Microsoft PowerShell source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # Copyright (c) Microsoft Corporation.
    2 # Licensed under the MIT License.
    3 Describe "Adapter Tests" -tags "CI" {
    4     Context "Property Adapter Tests" {
    5         BeforeAll {
    6             $pso  = [System.Diagnostics.Process]::GetCurrentProcess()
    7             $processName = $pso.Name
    8 
    9             if(-not ('TestCodeMethodClass' -as "type"))
   10             {
   11                 class TestCodeMethodClass {
   12                     static [int] TestCodeMethod([PSObject] $target, [int] $i)
   13                     {
   14                         return 1;
   15                     }
   16                 }
   17             }
   18 
   19             $psmemberset = New-Object System.Management.Automation.PSMemberSet 'setname1'
   20             $psmemberset | Add-Member -MemberType NoteProperty -Name NoteName -Value 1
   21             $testmethod = [TestCodeMethodClass].GetMethod("TestCodeMethod")
   22             $psmemberset | Add-Member -MemberType CodeMethod -Name TestCodeMethod -Value $testmethod
   23 
   24             $document = New-Object System.Xml.XmlDocument
   25             $document.LoadXml("<book ISBN='12345'><title>Pride And Prejudice</title><price>19.95</price></book>")
   26             $doc = $document.DocumentElement
   27         }
   28 
   29         It "Can get a Dotnet parameterized property" {
   30             $col  = $pso.psobject.Properties.Match("*")
   31             $prop = $col.psobject.Members["Item"]
   32             $prop | Should -Not -BeNullOrEmpty
   33             $prop.IsGettable | Should -BeTrue
   34             $prop.IsSettable | Should -BeFalse
   35             $prop.TypeNameOfValue | Should -Be "System.Management.Automation.PSPropertyInfo"
   36             $prop.Invoke("ProcessName").Value | Should -Be $processName
   37         }
   38 
   39         It "Can get a property" {
   40             $pso.psobject.Properties["ProcessName"] | Should -Not -BeNullOrEmpty
   41         }
   42 
   43         It "Can access all properties" {
   44             $props = $pso.psobject.Properties.Match("*")
   45             $props | Should -Not -BeNullOrEmpty
   46             $props["ProcessName"].Value | Should -Be $processName
   47         }
   48 
   49         It "Can invoke a method" {
   50             $method = $pso.psobject.Methods["ToString"]
   51             $method.Invoke() | Should -Be ($pso.ToString())
   52         }
   53 
   54         It "Access a Method via MemberSet adapter" {
   55             $prop = $psmemberset.psobject.Members["TestCodeMethod"]
   56             $prop.Invoke(2) | Should -Be 1
   57         }
   58 
   59         It "Access misc properties via MemberSet adapter" {
   60             $prop  = $psmemberset.psobject.Properties["NoteName"]
   61             $prop | Should -Not -BeNullOrEmpty
   62             $prop.IsGettable | Should -BeTrue
   63             $prop.IsSettable | Should -BeTrue
   64             $prop.TypeNameOfValue | Should -Be "System.Int32"
   65         }
   66 
   67         It "Access all the properties via XmlAdapter" {
   68             $col  = $doc.psobject.Properties.Match("*")
   69             $col.Count | Should -Not -Be 0
   70             $prop = $col["price"]
   71             $prop | Should -Not -BeNullOrEmpty
   72         }
   73 
   74         It "Access all the properties via XmlAdapter" {
   75             $prop  = $doc.psobject.Properties["price"]
   76             $prop.Value | Should -Be "19.95"
   77             $prop.IsGettable | Should -Not -BeNullOrEmpty
   78             $prop.IsSettable | Should -Not -BeNullOrEmpty
   79             $prop.TypeNameOfValue | Should -Be "System.String"
   80         }
   81 
   82         It "Call to string on a XmlNode object" {
   83             $val  = $doc.ToString()
   84             $val | Should -Be "book"
   85         }
   86 
   87         It "Calls CodeMethod with void result" {
   88 
   89             class TestCodeMethodInvokationWithVoidReturn {
   90                 [int] $CallCounter
   91 
   92                 static [int] IntMethodCM([PSObject] $self) {
   93                     return $self.CallCounter
   94                 }
   95 
   96                 static [void] VoidMethodCM([PSObject] $self) {
   97                     $self.CallCounter++
   98                 }
   99 
  100                 static [Reflection.MethodInfo] GetMethodInfo([string] $name) {
  101                     return [TestCodeMethodInvokationWithVoidReturn].GetMethod($name)
  102                 }
  103             }
  104 
  105             Update-TypeData -Force -TypeName TestCodeMethodInvokationWithVoidReturn -MemberType CodeMethod -MemberName IntMethod -Value ([TestCodeMethodInvokationWithVoidReturn]::GetMethodInfo('IntMethodCM'))
  106             Update-TypeData -Force -TypeName TestCodeMethodInvokationWithVoidReturn -MemberType CodeMethod -MemberName VoidMethod -Value ([TestCodeMethodInvokationWithVoidReturn]::GetMethodInfo('VoidMethodCM'))
  107             try {
  108                 $o = [TestCodeMethodInvokationWithVoidReturn]::new()
  109                 $o.CallCounter | Should -Be 0
  110                 $o.VoidMethod()
  111                 $o.CallCounter | Should -Be 1
  112 
  113                 $o.IntMethod() | Should -Be 1
  114             }
  115             finally {
  116                 Remove-TypeData TestCodeMethodInvokationWithVoidReturn
  117             }
  118         }
  119 
  120         It "Count and length property works for singletons" {
  121             # Return magic Count and Length property if it absent.
  122             $x = 5
  123             $x.Count | Should -Be 1
  124             $x.Length | Should -Be 1
  125 
  126             $null.Count | Should -Be 0
  127             $null.Length | Should -Be 0
  128 
  129             (10).Count | Should -Be 1
  130             (10).Length | Should -Be 1
  131 
  132             ("a").Count | Should -Be 1
  133             # The Length property exists in String type, so here we check that we don't break strings.
  134             ("a").Length | Should -Be 1
  135             ("aa").Length | Should -Be 2
  136 
  137             ([psobject] @{ foo = 'bar' }).Count | Should -Be 1
  138             ([psobject] @{ foo = 'bar' }).Length | Should -Be 1
  139 
  140             ([pscustomobject] @{ foo = 'bar' }).Count | Should -Be 1
  141             ([pscustomobject] @{ foo = 'bar' }).Length | Should -Be 1
  142 
  143             # Return real Count and Length property if it present.
  144             ([pscustomobject] @{ foo = 'bar'; count = 5 }).Count | Should -Be 5
  145             ([pscustomobject] @{ foo = 'bar'; length = 5 }).Length | Should -Be 5
  146         }
  147     }
  148 
  149     Context "Null Magic Method Adapter Tests" {
  150         It "ForEach and Where works for Null" {
  151             $res = $null.ForEach({1})
  152             $res.Count | Should -Be 0
  153             $res.GetType().Name | Should -BeExactly "Collection``1"
  154 
  155             $null.Where({$true})
  156             $res.Count | Should -Be 0
  157             $res.GetType().Name | Should -BeExactly "Collection``1"
  158         }
  159     }
  160 
  161     Context "ForEach Magic Method Adapter Tests" {
  162         It "Common ForEach magic method tests" -Pending:$true {
  163         }
  164 
  165         It "ForEach magic method works for singletons" {
  166             $x = 5
  167             $x.ForEach({$_}) | Should -Be 5
  168             (5).ForEach({$_}) | Should -Be 5
  169             ("a").ForEach({$_}) | Should -BeExactly "a"
  170 
  171             ([pscustomobject]@{ foo = 'bar' }).ForEach({1}) | Should -Be 1
  172 
  173             $x = ([pscustomobject]@{ foo = 'bar' }).ForEach({$_})
  174             $x.Count | Should -Be 1
  175             $x[0].foo | Should -BeExactly "bar"
  176 
  177             $x = ([pscustomobject]@{ foo = 'bar' }).ForEach({$_ | Add-Member -NotePropertyName "foo2" -NotePropertyValue "bar2" -PassThru})
  178             $x.Count | Should -Be 1
  179             $x[0].foo | Should -BeExactly "bar"
  180             $x[0].foo2 | Should -BeExactly "bar2"
  181 
  182             # We call ForEach method defined in an object if it is present (not magic ForEach method).
  183             $x = [pscustomobject]@{ foo = 'bar' }
  184             $x | Add-Member -MemberType ScriptMethod -Name ForEach -Value {
  185                 param ( [int]$param1 )
  186                    $param1*2
  187                 } -PassThru -Force
  188             $x.ForEach(5) | Should -Be 10
  189         }
  190 
  191         # Pending: https://github.com/PowerShell/PowerShell/issues/6567
  192         It "ForEach magic method works for dynamic (DLR) things" -Pending:$true {
  193             Add-TestDynamicType
  194 
  195             $dynObj = [TestDynamic]::new()
  196             $results = @($dynObj, $dynObj).ForEach('FooProp')
  197             $results.Count | Should -Be 2
  198             $results[0] | Should -Be 123
  199             $results[1] | Should -Be 123
  200 
  201             # TODO: dynamic method calls
  202         }
  203     }
  204 
  205     Context "Where Magic Method Adapter Tests" {
  206         It "Common Where magic method tests" -Pending:$true {
  207         }
  208 
  209         It "Where magic method works for singletons" {
  210             $x = 5
  211             $x.Where({$true}) | Should -Be 5
  212             (5).Where({$true}) | Should -Be 5
  213             ("a").Where({$true}) | Should -Be "a"
  214 
  215             $x = ([pscustomobject] @{ foo = 'bar' }).Where({$true})
  216             $x.Count | Should -Be 1
  217             $x[0].foo | Should -BeExactly "bar"
  218 
  219             $x = ([pscustomobject] @{ foo = 'bar' }).Where({$true}, 0)
  220             $x.Count | Should -Be 1
  221             $x[0].foo | Should -BeExactly "bar"
  222 
  223             $x = ([pscustomobject] @{ foo = 'bar' }).Where({$true}, "Default")
  224             $x.Count | Should -Be 1
  225             $x[0].foo | Should -BeExactly "bar"
  226 
  227             $x = ([pscustomobject] @{ foo = 'bar' }).Where({$true}, "Default", 0)
  228             $x.Count | Should -Be 1
  229             $x[0].foo | Should -BeExactly "bar"
  230 
  231             $x = ([pscustomobject] @{ foo = 'bar' }).Where({$true}, "Default", "0")
  232             $x.Count | Should -Be 1
  233             $x[0].foo | Should -BeExactly "bar"
  234 
  235             # We call Where method defined in an object if it is present (not magic Where method).
  236             $x = [pscustomobject]@{ foo = 'bar' }
  237             $x | Add-Member -MemberType ScriptMethod -Name Where -Value {
  238                 param ( [int]$param1 )
  239                    $param1*2
  240                 } -PassThru -Force
  241             $x.Where(5) | Should -Be 10
  242         }
  243     }
  244 }
  245 
  246 Describe "Adapter XML Tests" -tags "CI" {
  247     BeforeAll {
  248         [xml]$x  = "<root><data/></root>"
  249         $testCases =
  250             @{ rval = @{testprop = 1}; value = 'a hash (psobject)' },
  251             @{ rval = $null;           value = 'a null (codemethod)' },
  252             @{ rval = 1;               value = 'a int (codemethod)' },
  253             @{ rval = "teststring";    value = 'a string (codemethod)' },
  254             @{ rval = @("teststring1", "teststring2");  value = 'a string array (codemethod)' },
  255             @{ rval = @(1,2); value = 'a int array (codemethod)' },
  256             @{ rval = [PSObject]::AsPSObject(1); value = 'a int (psobject wrapping)' },
  257             @{ rval = [PSObject]::AsPSObject("teststring"); value = 'a string (psobject wrapping)' },
  258             @{ rval = [PSObject]::AsPSObject([psobject]@("teststring1", "teststring2")); value = 'a string array (psobject wrapping)' },
  259             @{ rval = [PSObject]::AsPSObject(@(1,2)); value = 'int array (psobject wrapping)' }
  260     }
  261 
  262     Context "Can set XML node property to non-string object" {
  263         It "rval is <value>" -TestCases $testCases {
  264             # rval will be implicitly converted to 'string' type
  265             param($rval)
  266             {
  267                 { $x.root.data = $rval } | Should -Not -Throw
  268                 $x.root.data | Should -Be [System.Management.Automation.LanguagePrimitives]::ConvertTo($rval, [string])
  269             }
  270         }
  271     }
  272 }
  273 
  274 Describe "DataRow and DataRowView Adapter tests" -tags "CI" {
  275 
  276     BeforeAll {
  277         ## Define the DataTable schema
  278         $dataTable = [System.Data.DataTable]::new("inputs")
  279         $dataTable.Locale = [cultureinfo]::InvariantCulture
  280         $dataTable.Columns.Add("Id", [int]) > $null
  281         $dataTable.Columns.Add("FirstName", [string]) > $null
  282         $dataTable.Columns.Add("LastName", [string]) > $null
  283         $dataTable.Columns.Add("YearsInMS", [int]) > $null
  284 
  285         ## Add data entries
  286         $dataTable.Rows.Add(@(1, "joseph", "smith", 15)) > $null
  287         $dataTable.Rows.Add(@(2, "paul", "smith", 15)) > $null
  288         $dataTable.Rows.Add(@(3, "mary jo", "soe", 5)) > $null
  289         $dataTable.Rows.Add(@(4, "edmund`todd `n", "bush", 9)) > $null
  290     }
  291 
  292     Context "DataRow Adapter tests" {
  293 
  294         It "Should be able to access data columns" {
  295             $row = $dataTable.Rows[0]
  296             $row.Id | Should -Be 1
  297             $row.FirstName | Should -Be "joseph"
  298             $row.LastName | Should -Be "smith"
  299             $row.YearsInMS | Should -Be 15
  300         }
  301 
  302         It "DataTable should be enumerable in PowerShell" {
  303             ## Get the third entry in the data table
  304             $row = $dataTable | Select-Object -Skip 2 -First 1
  305             $row.Id | Should -Be 3
  306             $row.FirstName | Should -Be "mary jo"
  307             $row.LastName | Should -Be "soe"
  308             $row.YearsInMS | Should -Be 5
  309         }
  310     }
  311 
  312     Context "DataRowView Adapter tests" {
  313 
  314         It "Should be able to access data columns" {
  315             $rowView = $dataTable.DefaultView[1]
  316             $rowView.Id | Should -Be 2
  317             $rowView.FirstName | Should -Be "paul"
  318             $rowView.LastName | Should -Be "smith"
  319             $rowView.YearsInMS | Should -Be 15
  320         }
  321 
  322         It "DataView should be enumerable" {
  323             $rowView = $dataTable.DefaultView | Select-Object -Last 1
  324             $rowView.Id | Should -Be 4
  325             $rowView.FirstName | Should -Be "edmund`todd `n"
  326             $rowView.LastName | Should -Be "bush"
  327             $rowView.YearsInMS | Should -Be 9
  328         }
  329     }
  330 }
  331 
  332 Describe "Base method call on object mapped to PropertyOnlyAdapter should work" -tags "CI" {
  333     It "Base method call on object of a subclass of 'XmlDocument' -- Add-Type" {
  334         $code =@'
  335 namespace BaseMethodCallTest.OnXmlDocument {
  336     public class Foo : System.Xml.XmlDocument {
  337         public string MyName { get; set; }
  338         public override void LoadXml(string content) {
  339             MyName = content;
  340         }
  341     }
  342 }
  343 '@
  344         try {
  345             $null = [BaseMethodCallTest.OnXmlDocument.Foo]
  346         } catch {
  347             Add-Type -TypeDefinition $code
  348         }
  349 
  350         $foo = [BaseMethodCallTest.OnXmlDocument.Foo]::new()
  351         $foo.LoadXml('<test>bar</test>')
  352         $foo.MyName | Should -BeExactly '<test>bar</test>'
  353         $foo.ChildNodes.Count | Should -Be 0
  354 
  355         ([System.Xml.XmlDocument]$foo).LoadXml('<test>bar</test>')
  356         $foo.test | Should -BeExactly 'bar'
  357         $foo.ChildNodes.Count | Should -Be 1
  358     }
  359 
  360     It "Base method call on object of a subclass of 'XmlDocument' -- PowerShell Class" {
  361         class XmlDocChild : System.Xml.XmlDocument {
  362             [string] $MyName
  363             [void] LoadXml([string]$content) {
  364                 $this.MyName = $content
  365                 # Try to call the base type's .LoadXml() method.
  366                 ([System.Xml.XmlDocument] $this).LoadXml($content)
  367             }
  368         }
  369 
  370         $child = [XmlDocChild]::new()
  371         $child.LoadXml('<test>bar</test>')
  372         $child.MyName | Should -BeExactly '<test>bar</test>'
  373         $child.test | Should -BeExactly 'bar'
  374         $child.ChildNodes.Count | Should -Be 1
  375     }
  376 }