Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions bin/update-tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<#
.SYNOPSIS
Generate the tests for exercises
.DESCRIPTION
Generate the tests for exercises that have a template.
The tests are generated from canonical data.
.PARAMETER Exercise
The slug of the exercise to generate the tests for (optional).
.PARAMETER CreateNew
Create a new test generator file before generating the tests (switch).
.PARAMETER SyncProbSpecs
Sync the prob-specs repo used (switch).
.EXAMPLE
The example below will generate the tests for exercises with a template
PS C:\> ./test.ps1
.EXAMPLE
The example below will generate the tests for the specified exercise
PS C:\> ./test.ps1 acronym
#>

[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Position = 0, Mandatory = $false)]
[string]$Exercise,

[Parameter()]
[switch]$New,

[Parameter()]
[switch]$SyncProbSpecs
)

$ErrorActionPreference = "Stop"
$PSNativeCommandUseErrorActionPreference = $true

function Run-Command($verb, $exercise = $null) {
if ($exercise) {
& dotnet run --project generators $verb --exercise $exercise
} else {
& dotnet run --project generators $verb
}
}

if ($SyncProbSpecs.IsPresent) {
Run-Command sync
}

if ($New.IsPresent) {
Run-Command new $Exercise
}

Run-Command update $Exercise
51 changes: 51 additions & 0 deletions exercises/practice/accumulate/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{{ func accumulator
case $0
when '(x) => x * x'
ret 'Function(x) x * x'
when '(x) => upcase(x)'
ret 'Function(x) x.ToUpper()'
when '(x) => reverse(x)'
ret 'Function(x) New String(x.Reverse().ToArray())'
when '(x) => accumulate(["1", "2", "3"], (y) => x + y)'
ret 'Function(x) String.Join(" ", New String() {"1", "2", "3"}.Accumulate(Function(y) x & y))'
else
ret $0
end
end }}

{{ func array_type
ret (object.typeof (array.first $0)) == "string" ? "String()" : "Integer()"
end }}

Public Class {{ testClass }}
{{- for test in tests }}
<Fact{{ if !for.first }}(Skip:="Remove this Skip property to run this test"){{ end }}>
Public Sub {{ test.testMethod }}()
Dim input As {{ test.input.list | array_type }} = {{ test.input.list | vb_literal }}
{{- if (object.typeof (array.first test.expected)) == "array" }}
Dim expected As {{ test.input.list | array_type }} = {
{{~ for row in test.expected ~}}
{{ row | array.join " " | vb_string_literal }}{{ if !for.last }},{{ end }}
{{~ end ~}}
}
{{- else }}
Dim expected As {{ test.expected | array_type }} = {{ test.expected | vb_literal }}
{{- end }}
Assert.Equal(expected, input.{{ test.testedMethod }}({{ test.input.accumulator | accumulator }}))
End Sub
{{ end ~}}

<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Accumulate_is_lazy()
Dim counter = 0
Dim accumulation = New Integer() {1, 2, 3}.Accumulate(
Function(x)
counter += 1
Return x
End Function)

Assert.Equal(0, counter)
accumulation.ToList()
Assert.Equal(3, counter)
End Sub
End Class
61 changes: 33 additions & 28 deletions exercises/practice/accumulate/AccumulateTests.vb
Original file line number Diff line number Diff line change
@@ -1,49 +1,54 @@
Public Class AccumulateTest
Public Class AccumulateTests
<Fact>
Public Sub EmptyAccumulationProducesEmptyAccumulation()
Assert.Equal(New Integer() {}.Accumulate(Function(x) x * x), New Integer() {})
Public Sub Accumulate_empty()
Dim input As Integer() = {}
Dim expected As Integer() = {}
Assert.Equal(expected, input.Accumulate(Function(x) x * x))
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub AccumulateSquares()
Assert.Equal({1, 2, 3}.Accumulate(Function(x) x * x), {1, 4, 9})
Public Sub Accumulate_squares()
Dim input As Integer() = {1, 2, 3}
Dim expected As Integer() = {1, 4, 9}
Assert.Equal(expected, input.Accumulate(Function(x) x * x))
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub AccumulateUpcases()
Assert.Equal(New List(Of String)() From {
"hello",
"world"
}.Accumulate(Function(x) x.ToUpper()), New List(Of String) From {
"HELLO",
"WORLD"
})
Public Sub Accumulate_upcases()
Dim input As String() = {"Hello", "world"}
Dim expected As String() = {"HELLO", "WORLD"}
Assert.Equal(expected, input.Accumulate(Function(x) x.ToUpper()))
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub AccumulateReversedStrings()
Assert.Equal("the quick brown fox etc".Split(" "c).Accumulate(AddressOf Reverse), "eht kciuq nworb xof cte".Split(" "c))
Public Sub Accumulate_reversed_strings()
Dim input As String() = {"the", "quick", "brown", "fox", "etc"}
Dim expected As String() = {"eht", "kciuq", "nworb", "xof", "cte"}
Assert.Equal(expected, input.Accumulate(Function(x) New String(x.Reverse().ToArray())))
End Sub

Private Shared Function Reverse(value As String) As String
Dim chars = value.ToCharArray()
Array.Reverse(chars)
Return New String(chars)
End Function

<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub AccumulateWithinAccumulate()
Dim actual = New String() {"a", "b", "c"}.Accumulate(Function(c) String.Join(" ", New String() {"1", "2", "3"}.Accumulate(Function(d) c & d)))
Assert.Equal(actual, New String() {"a1 a2 a3", "b1 b2 b3", "c1 c2 c3"})
Public Sub Accumulate_recursively()
Dim input As String() = {"a", "b", "c"}
Dim expected As String() = {
"a1 a2 a3",
"b1 b2 b3",
"c1 c2 c3"
}
Assert.Equal(expected, input.Accumulate(Function(x) String.Join(" ", New String() {"1", "2", "3"}.Accumulate(Function(y) x & y))))
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub AccumulateIsLazy()
Public Sub Accumulate_is_lazy()
Dim counter = 0
Dim accumulation = New Integer() {1, 2, 3}.Accumulate(Function(x) x * System.Math.Max(System.Threading.Interlocked.Increment(counter), counter - 1))
Dim accumulation = New Integer() {1, 2, 3}.Accumulate(
Function(x)
counter += 1
Return x
End Function)

Assert.Equal(counter, 0)
Assert.Equal(0, counter)
accumulation.ToList()
Assert.Equal(counter, 3)
Assert.Equal(3, counter)
End Sub
End Class
26 changes: 26 additions & 0 deletions exercises/practice/grade-school/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Public Class {{ testClass }}
{{- for test in tests }}
<Fact{{ if !for.first }}(Skip:="Remove this Skip property to run this test"){{ end }}>
Public Sub {{ test.testMethod }}()
Dim sut = New GradeSchool()
{{- if test.property == "add" }}
{{ for i in 0..((array.size test.input.students) - 1) -}}
{{ student = test.input.students[i] -}}
{{ assertion = test.expected[i] ? "True" : "False" -}}
Assert.{{ assertion }}(sut.Add({{ student[0] | vb_string_literal }}, {{ student[1] }}))
{{ end -}}
{{- else }}
{{ for student in test.input.students -}}
sut.Add({{ student[0] | vb_string_literal }}, {{ student[1] }})
{{ end -}}
{{ if (array.size test.expected) == 0 -}}
Assert.Empty(sut.{{ test.testedMethod }}({{ test.input.desiredGrade }}))
{{ else -}}
Dim actual = sut.{{ test.testedMethod }}({{ test.input.desiredGrade }})
Dim expected = {{ test.expected | vb_literal }}
Assert.Equal(expected, actual)
{{ end -}}
{{ end -}}
End Sub
{{ end -}}
End Class
2 changes: 1 addition & 1 deletion exercises/practice/grade-school/GradeSchool.vb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Public Class GradeSchool
Throw New NotImplementedException("You need to implement this function.")
End Function

Public Function Grade(ByVal grade As Integer) As IEnumerable(Of String)
Public Function Grade(ByVal pGrade As Integer) As IEnumerable(Of String)
Throw New NotImplementedException("You need to implement this function.")
End Function
End Class
60 changes: 36 additions & 24 deletions exercises/practice/grade-school/GradeSchoolTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ Public Class GradeSchoolTests
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Student_is_added_to_the_roster()
Dim sut = New GradeSchool()
Dim expected = {"Aimee"}
sut.Add("Aimee", 2)
Assert.Equal(expected, sut.Roster())
Dim actual = sut.Roster()
Dim expected = {"Aimee"}
Assert.Equal(expected, actual)
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Expand All @@ -30,11 +31,12 @@ Public Class GradeSchoolTests
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Multiple_students_in_the_same_grade_are_added_to_the_roster()
Dim sut = New GradeSchool()
Dim expected = {"Blair", "James", "Paul"}
sut.Add("Blair", 2)
sut.Add("James", 2)
sut.Add("Paul", 2)
Assert.Equal(expected, sut.Roster())
Dim actual = sut.Roster()
Dim expected = {"Blair", "James", "Paul"}
Assert.Equal(expected, actual)
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Expand All @@ -49,12 +51,13 @@ Public Class GradeSchoolTests
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Student_not_added_to_same_grade_in_the_roster_more_than_once()
Dim sut = New GradeSchool()
Dim expected = {"Blair", "James", "Paul"}
sut.Add("Blair", 2)
sut.Add("James", 2)
sut.Add("James", 2)
sut.Add("Paul", 2)
Assert.Equal(expected, sut.Roster())
Dim actual = sut.Roster()
Dim expected = {"Blair", "James", "Paul"}
Assert.Equal(expected, actual)
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Expand All @@ -67,10 +70,11 @@ Public Class GradeSchoolTests
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Students_in_multiple_grades_are_added_to_the_roster()
Dim sut = New GradeSchool()
Dim expected = {"Chelsea", "Logan"}
sut.Add("Chelsea", 3)
sut.Add("Logan", 7)
Assert.Equal(expected, sut.Roster())
Dim actual = sut.Roster()
Dim expected = {"Chelsea", "Logan"}
Assert.Equal(expected, actual)
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Expand All @@ -85,46 +89,50 @@ Public Class GradeSchoolTests
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Student_not_added_to_multiple_grades_in_the_roster()
Dim sut = New GradeSchool()
Dim expected = {"Blair", "James", "Paul"}
sut.Add("Blair", 2)
sut.Add("James", 2)
sut.Add("James", 3)
sut.Add("Paul", 3)
Assert.Equal(expected, sut.Roster())
Dim actual = sut.Roster()
Dim expected = {"Blair", "James", "Paul"}
Assert.Equal(expected, actual)
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Students_are_sorted_by_grades_in_the_roster()
Dim sut = New GradeSchool()
Dim expected = {"Anna", "Peter", "Jim"}
sut.Add("Jim", 3)
sut.Add("Peter", 2)
sut.Add("Anna", 1)
Assert.Equal(expected, sut.Roster())
Dim actual = sut.Roster()
Dim expected = {"Anna", "Peter", "Jim"}
Assert.Equal(expected, actual)
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Students_are_sorted_by_name_in_the_roster()
Dim sut = New GradeSchool()
Dim expected = {"Alex", "Peter", "Zoe"}
sut.Add("Peter", 2)
sut.Add("Zoe", 2)
sut.Add("Alex", 2)
Assert.Equal(expected, sut.Roster())
Dim actual = sut.Roster()
Dim expected = {"Alex", "Peter", "Zoe"}
Assert.Equal(expected, actual)
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Students_are_sorted_by_grades_and_then_by_name_in_the_roster()
Dim sut = New GradeSchool()
Dim expected = {"Anna", "Barb", "Charlie", "Alex", "Peter", "Zoe", "Jim"}
sut.Add("Peter", 2)
sut.Add("Anna", 1)
sut.Add("Barb", 1)
sut.Add("Zoe", 2)
sut.Add("Alex", 2)
sut.Add("Jim", 3)
sut.Add("Charlie", 1)
Assert.Equal(expected, sut.Roster())
Dim actual = sut.Roster()
Dim expected = {"Anna", "Barb", "Charlie", "Alex", "Peter", "Zoe", "Jim"}
Assert.Equal(expected, actual)
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Expand All @@ -146,43 +154,47 @@ Public Class GradeSchoolTests
<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Student_not_added_to_same_grade_more_than_once()
Dim sut = New GradeSchool()
Dim expected = {"Blair", "James", "Paul"}
sut.Add("Blair", 2)
sut.Add("James", 2)
sut.Add("James", 2)
sut.Add("Paul", 2)
Assert.Equal(expected, sut.Grade(2))
Dim actual = sut.Grade(2)
Dim expected = {"Blair", "James", "Paul"}
Assert.Equal(expected, actual)
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Student_not_added_to_multiple_grades()
Dim sut = New GradeSchool()
Dim expected = {"Blair", "James"}
sut.Add("Blair", 2)
sut.Add("James", 2)
sut.Add("James", 3)
sut.Add("Paul", 3)
Assert.Equal(expected, sut.Grade(2))
Dim actual = sut.Grade(2)
Dim expected = {"Blair", "James"}
Assert.Equal(expected, actual)
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Student_not_added_to_other_grade_for_multiple_grades()
Dim sut = New GradeSchool()
Dim expected = {"Paul"}
sut.Add("Blair", 2)
sut.Add("James", 2)
sut.Add("James", 3)
sut.Add("Paul", 3)
Assert.Equal(expected, sut.Grade(3))
Dim actual = sut.Grade(3)
Dim expected = {"Paul"}
Assert.Equal(expected, actual)
End Sub

<Fact(Skip:="Remove this Skip property to run this test")>
Public Sub Students_are_sorted_by_name_in_a_grade()
Dim sut = New GradeSchool()
Dim expected = {"Bradley", "Franklin"}
sut.Add("Franklin", 5)
sut.Add("Bradley", 5)
sut.Add("Jeff", 1)
Assert.Equal(expected, sut.Grade(5))
Dim actual = sut.Grade(5)
Dim expected = {"Bradley", "Franklin"}
Assert.Equal(expected, actual)
End Sub
End Class
Loading