Skip to content

Commit 64aacef

Browse files
authored
Add powershell example (#462)
1 parent 29a8850 commit 64aacef

File tree

6 files changed

+255
-0
lines changed

6 files changed

+255
-0
lines changed

example/powershell/READMDE.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Install
2+
3+
## Required Powershell Modules
4+
5+
In order to run 'Pester'-Tests please install the Pester module from PSGallery: [https://www.powershellgallery.com/packages/Pester](https://www.powershellgallery.com/packages/Pester)
6+
7+
```powershell
8+
Install-Module -Name Pester
9+
```
10+
11+
## Install the necessary plugins in Visual Studio Code (vscode)
12+
13+
Open the **Extension** menu, then search and install the following extensions:
14+
15+
- `ms-vscode.powershell`
16+
- `pspester.pester-test`
17+
- `ryanluker.vscode-coverage-gutters`
18+
19+
# Test
20+
21+
Execute the _tests\RunTests.ps1_ file to execute the present Pester tests (i.e. _Calendar.Tests.ps1_) which will create the _src\coverage.xml_ file.
22+
Open _src\Appointment.psm1_ or _src\Calendar.psm1_ to check the code coverage.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<#
2+
.SYNOPSIS
3+
Returns a new Appointment object with the given title
4+
5+
.PARAMETER Title
6+
Title of the Appointment
7+
#>
8+
function New-Appointment
9+
{
10+
[CmdletBinding()]
11+
param(
12+
[String] $Title
13+
)
14+
return [Appointment]::new($Title)
15+
}
16+
17+
class Appointment
18+
{
19+
[String] $Title
20+
[DateTime] $StartTime
21+
[DateTime] $EndTime
22+
23+
Appointment([String] $Title)
24+
{
25+
$this.Title = $Title
26+
$this.StartTime = [DateTime]::Now
27+
$this.EndTime = $this.StartTime.AddHours(1)
28+
}
29+
30+
Appointment([String] $Title, [DateTime] $StartTime)
31+
{
32+
$this.Title = $Title
33+
$this.StartTime = $StartTime
34+
$this.EndTime = $this.StartTime.AddHours(1)
35+
}
36+
37+
Appointment([String] $Title, [DateTime] $StartTime, [DateTime] $EndTime)
38+
{
39+
$this.Title = $Title
40+
$this.StartTime = $StartTime
41+
$this.EndTime = $this.StartTime.AddHours(1)
42+
}
43+
44+
Appointment([String] $Title, [DateTime] $StartTime, [Int32] $Duration)
45+
{
46+
$this.Title = $Title
47+
$this.StartTime = $StartTime
48+
$this.EndTime = $this.StartTime.AddHours($Duration)
49+
}
50+
51+
[Boolean] IsPastAppointment()
52+
{
53+
return $this.EndTime -lt [DateTime]::Now
54+
}
55+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using namespace System.Collections
2+
using namespace System.Collections.Generic
3+
using module '.\Appointment.psm1'
4+
5+
<#
6+
.SYNOPSIS
7+
Returns a new Calendar object with the given name
8+
9+
.PARAMETER Name
10+
Name / ID of the Calendar
11+
12+
.NOTES
13+
throws a ArgumentNullException if the given Name is null or an empty string
14+
#>
15+
function Get-Calendar
16+
{
17+
[CmdletBinding()]
18+
param(
19+
[Parameter(Mandatory = $true)][String] $Name
20+
)
21+
22+
if ([String]::IsNullOrWhiteSpace($Name))
23+
{
24+
throw [System.ArgumentNullException]::new('Name can not be null or empty string')
25+
}
26+
return [Calendar]::new($Name, $env:username)
27+
}
28+
29+
class Calendar
30+
{
31+
[String] $Name
32+
[String] $Owner
33+
[List[Appointment]] $Appointments
34+
35+
Calendar([String] $Name, [String] $Owner)
36+
{
37+
$this.Name = $Name
38+
$this.Owner = $Owner
39+
$this.Appointments = New-Object List[Appointment]
40+
}
41+
42+
[void] AddAppointment([Appointment] $Appointment)
43+
{
44+
$this.Appointments.Add($Appointment)
45+
}
46+
47+
[void] AddAppointment([String] $Title, [DateTime] $StartTime, [DateTime] $EndTime)
48+
{
49+
$appointment = [Appointment]::new($Title, $StartTime, $EndTime)
50+
$this.Appointments.Add($appointment)
51+
}
52+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.1//EN" "report.dtd"[]>
3+
<report name="Pester (10/30/2024 19:40:04)">
4+
<sessioninfo id="this" start="1730317204286" dump="1730317204714" />
5+
<package name=".">
6+
<class name="Appointment" sourcefilename="Appointment.psm1">
7+
<method name="New-Appointment" desc="()" line="14">
8+
<counter type="INSTRUCTION" missed="0" covered="1" />
9+
<counter type="LINE" missed="0" covered="1" />
10+
<counter type="METHOD" missed="0" covered="1" />
11+
</method>
12+
<method name="Appointment" desc="()" line="25">
13+
<counter type="INSTRUCTION" missed="9" covered="3" />
14+
<counter type="LINE" missed="9" covered="3" />
15+
<counter type="METHOD" missed="0" covered="1" />
16+
</method>
17+
<method name="IsPastAppointment" desc="()" line="53">
18+
<counter type="INSTRUCTION" missed="0" covered="1" />
19+
<counter type="LINE" missed="0" covered="1" />
20+
<counter type="METHOD" missed="0" covered="1" />
21+
</method>
22+
<counter type="INSTRUCTION" missed="9" covered="5" />
23+
<counter type="LINE" missed="9" covered="5" />
24+
<counter type="METHOD" missed="0" covered="3" />
25+
<counter type="CLASS" missed="0" covered="1" />
26+
</class>
27+
<class name="Calendar" sourcefilename="Calendar.psm1">
28+
<method name="Get-Calendar" desc="()" line="22">
29+
<counter type="INSTRUCTION" missed="1" covered="2" />
30+
<counter type="LINE" missed="1" covered="2" />
31+
<counter type="METHOD" missed="0" covered="1" />
32+
</method>
33+
<method name="Calendar" desc="()" line="37">
34+
<counter type="INSTRUCTION" missed="0" covered="3" />
35+
<counter type="LINE" missed="0" covered="3" />
36+
<counter type="METHOD" missed="0" covered="1" />
37+
</method>
38+
<method name="AddAppointment" desc="()" line="44">
39+
<counter type="INSTRUCTION" missed="3" covered="0" />
40+
<counter type="LINE" missed="3" covered="0" />
41+
<counter type="METHOD" missed="1" covered="0" />
42+
</method>
43+
<counter type="INSTRUCTION" missed="4" covered="5" />
44+
<counter type="LINE" missed="4" covered="5" />
45+
<counter type="METHOD" missed="1" covered="2" />
46+
<counter type="CLASS" missed="0" covered="1" />
47+
</class>
48+
<sourcefile name="Appointment.psm1">
49+
<line nr="14" mi="0" ci="1" mb="0" cb="0" />
50+
<line nr="25" mi="0" ci="1" mb="0" cb="0" />
51+
<line nr="26" mi="0" ci="1" mb="0" cb="0" />
52+
<line nr="27" mi="0" ci="1" mb="0" cb="0" />
53+
<line nr="32" mi="1" ci="0" mb="0" cb="0" />
54+
<line nr="33" mi="1" ci="0" mb="0" cb="0" />
55+
<line nr="34" mi="1" ci="0" mb="0" cb="0" />
56+
<line nr="39" mi="1" ci="0" mb="0" cb="0" />
57+
<line nr="40" mi="1" ci="0" mb="0" cb="0" />
58+
<line nr="41" mi="1" ci="0" mb="0" cb="0" />
59+
<line nr="46" mi="1" ci="0" mb="0" cb="0" />
60+
<line nr="47" mi="1" ci="0" mb="0" cb="0" />
61+
<line nr="48" mi="1" ci="0" mb="0" cb="0" />
62+
<line nr="53" mi="0" ci="1" mb="0" cb="0" />
63+
<counter type="INSTRUCTION" missed="9" covered="5" />
64+
<counter type="LINE" missed="9" covered="5" />
65+
<counter type="METHOD" missed="0" covered="3" />
66+
<counter type="CLASS" missed="0" covered="1" />
67+
</sourcefile>
68+
<sourcefile name="Calendar.psm1">
69+
<line nr="22" mi="0" ci="1" mb="0" cb="0" />
70+
<line nr="24" mi="1" ci="0" mb="0" cb="0" />
71+
<line nr="26" mi="0" ci="1" mb="0" cb="0" />
72+
<line nr="37" mi="0" ci="1" mb="0" cb="0" />
73+
<line nr="38" mi="0" ci="1" mb="0" cb="0" />
74+
<line nr="39" mi="0" ci="1" mb="0" cb="0" />
75+
<line nr="44" mi="1" ci="0" mb="0" cb="0" />
76+
<line nr="49" mi="1" ci="0" mb="0" cb="0" />
77+
<line nr="50" mi="1" ci="0" mb="0" cb="0" />
78+
<counter type="INSTRUCTION" missed="4" covered="5" />
79+
<counter type="LINE" missed="4" covered="5" />
80+
<counter type="METHOD" missed="1" covered="2" />
81+
<counter type="CLASS" missed="0" covered="1" />
82+
</sourcefile>
83+
<counter type="INSTRUCTION" missed="13" covered="10" />
84+
<counter type="LINE" missed="13" covered="10" />
85+
<counter type="METHOD" missed="1" covered="5" />
86+
<counter type="CLASS" missed="0" covered="2" />
87+
</package>
88+
<counter type="INSTRUCTION" missed="13" covered="10" />
89+
<counter type="LINE" missed="13" covered="10" />
90+
<counter type="METHOD" missed="1" covered="5" />
91+
<counter type="CLASS" missed="0" covered="2" />
92+
</report>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Describe 'Get-Calendar Cmdlet' {
2+
BeforeAll {
3+
Import-Module "$PSScriptRoot\..\src\Calendar.psm1"
4+
}
5+
6+
It 'returns a calender object' {
7+
$calendar = Get-Calendar -Name 'Private'
8+
$calendar.GetType() | Should -BeExactly 'Calendar'
9+
}
10+
}
11+
12+
Describe 'Get-Appointment Cmdlet' {
13+
BeforeAll {
14+
Import-Module "$PSScriptRoot\..\src\Appointment.psm1"
15+
}
16+
17+
It 'creates an appointment' {
18+
$appointment = New-Appointment -Title 'Daily Meeting'
19+
$appointment.Title | Should -BeExactly 'Daily Meeting'
20+
}
21+
22+
It 'checks if appointment is in the past' {
23+
$appointment = New-Appointment -Title 'Daily Meeting'
24+
$appointment.IsPastAppointment() | Should -Be $false
25+
}
26+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
$configuration = New-PesterConfiguration
2+
$configuration.Run.Path = "$PSScriptRoot"
3+
$configuration.CodeCoverage.Enabled = $true
4+
$configuration.CodeCoverage.Path = "$PSScriptRoot\..\src"
5+
$configuration.CodeCoverage.OutputFormat = 'CoverageGutters'
6+
$configuration.CodeCoverage.OutputPath = "$PSScriptRoot\..\src\coverage.xml"
7+
8+
Invoke-Pester -Configuration $configuration

0 commit comments

Comments
 (0)