Skip to content

Commit

Permalink
tests(sync/tracks): compare objects, not strings (#320)
Browse files Browse the repository at this point in the history
This test previously performed a string comparison, because the
`TrackExercise` object had a non-exported field (which we didn't want to
export just for testing purposes).

However, in 2b70195 (#307) we refactored to remove that field.
Therefore in this commit we simplify the test by just comparing the
objects directly.
  • Loading branch information
ee7 authored May 11, 2021
1 parent b3ee377 commit 214172f
Showing 1 changed file with 21 additions and 35 deletions.
56 changes: 21 additions & 35 deletions tests/test_tracks.nim
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
# This file implements tests for `src/tracks.nim`
import std/[os, osproc, parseutils, sets, strformat, unittest]
import std/[os, osproc, sets, strformat, unittest]
import "."/[cli, sync/tracks]

func oneLine(s: string): string =
## Returns the string `s`, but:
## - replaces each newline character with a single space.
## - strips all per-line leading whitespace.
## - strips trailing whitespace.
result = newStringOfCap(s.len)
var i = 0
var line: string
while i < s.len:
i += s.skipWhitespace(i)
i += s.parseUntil(line, '\n', i)
result.add line
result.add " "
# Remove final two space characters.
result.setLen(result.len - 2)

proc main =
suite "findTrackExercises":
const trackDir = ".test_tracks_nim_track_repo"
Expand All @@ -42,31 +26,33 @@ proc main =
check:
trackExercises.len == 68

# Here, just test against the string representation of each object (as we
# don't want to export more types from `tracks.nim` just for testing).
# This is one way of testing the public interface rather than implementation
# details.
test "returns the expected object for `hello-world`":
const expectedHelloWorld = """
(slug: "hello-world",
tests: (included: {"af9ffe10-dc13-42d8-a742-e7bdafac449d"},
excluded: {}))
""".oneLine()
const expectedHelloWorld =
TrackExercise(
slug: "hello-world",
tests: TrackExerciseTests(
included: ["af9ffe10-dc13-42d8-a742-e7bdafac449d"].toHashSet(),
excluded: initHashSet[string](0)
)
)

check:
trackExercises[0].`$` == expectedHelloWorld
trackExercises[0] == expectedHelloWorld

test "returns the expected object for `two-fer`":
const expectedTwoFer = """
(slug: "two-fer",
tests: (included: {"1cf3e15a-a3d7-4a87-aeb3-ba1b43bc8dce",
"3549048d-1a6e-4653-9a79-b0bda163e8d5",
"b4c6dbb8-b4fb-42c2-bafd-10785abe7709"},
excluded: {}))
""".oneLine()
const expectedTwoFer =
TrackExercise(
slug: "two-fer",
tests: TrackExerciseTests(
included: ["1cf3e15a-a3d7-4a87-aeb3-ba1b43bc8dce",
"3549048d-1a6e-4653-9a79-b0bda163e8d5",
"b4c6dbb8-b4fb-42c2-bafd-10785abe7709"].toHashSet(),
excluded: initHashSet[string](0)
)
)

check:
trackExercises[1].`$` == expectedTwoFer
trackExercises[1] == expectedTwoFer

# Try to remove the track directory, but allow the tests to pass if there
# was an error removing it.
Expand Down

0 comments on commit 214172f

Please sign in to comment.