Skip to content

Commit

Permalink
nimsuggest test template def and fixture refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
saem committed Dec 20, 2020
1 parent 1cc1d4a commit adb069d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 19 deletions.
5 changes: 5 additions & 0 deletions nimsuggest/tests/massertish.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Template for testing defs

template doAssertish*(cond: untyped, msg: string = "") =
## template to allow def lookup testing
if not cond: quit()
3 changes: 3 additions & 0 deletions nimsuggest/tests/minclude_import.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Creates an awkward set of dependencies between this, import, and include.
# This pattern appears in the compiler, compiler/(sem|ast|semexprs).nim.

import massertish
import minclude_types

proc say*(g: Greet): string =
doAssertish(true, "always works")
g.greeting & ", " & g.subject & "!"

include minclude_include

proc say*(): string =
doAssertish(1 + 1 == 2, "math works")
say(create())
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import massertish

func rereplace*(s, sub: string; by: string = ""): string {.used.} =
## competes for priority in suggestion, here first, but never used in test

doAssertish(true, "always works")
result = by

func replace*(s, sub: string; by: string = ""): string =
## this is a test version of strutils.replace, it simply returns `by`

doAssertish("".len == 0, "empty string is empty")
result = by

func rerereplace*(s, sub: string; by: string = ""): string {.used.} =
## isn't used and appears last, lowest priority

doAssertish(false, "never works")
result = by
8 changes: 4 additions & 4 deletions nimsuggest/tests/tdot4.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# suggestions are prioritized

from system import string, echo
import mdot4_strutils
import mstrutils

proc main(inp: string): string =
# use replace here and see if it occurs in the result, it should gain
Expand All @@ -14,7 +14,7 @@ discard """
$nimsuggest --tester $file
>sug $1
sug;;skProc;;tdot4.main;;proc (inp: string): string;;$file;;6;;5;;"";;100;;None
sug;;skFunc;;mdot4_strutils.replace;;proc (s: string, sub: string, by: string): string{.noSideEffect, gcsafe, locks: 0.};;*mdot4_strutils.nim;;5;;5;;"this is a test version of strutils.replace, it simply returns `by`";;100;;None
sug;;skFunc;;mdot4_strutils.rereplace;;proc (s: string, sub: string, by: string): string{.noSideEffect, gcsafe, locks: 0.};;*mdot4_strutils.nim;;1;;5;;"competes for priority in suggestion, here first, but never used in test";;100;;None
sug;;skFunc;;mdot4_strutils.rerereplace;;proc (s: string, sub: string, by: string): string{.noSideEffect, gcsafe, locks: 0.};;*mdot4_strutils.nim;;9;;5;;"isn\'t used and appears last, lowest priority";;100;;None
sug;;skFunc;;mstrutils.replace;;proc (s: string, sub: string, by: string): string{.noSideEffect, gcsafe, locks: 0.};;*mstrutils.nim;;9;;5;;"this is a test version of strutils.replace, it simply returns `by`";;100;;None
sug;;skFunc;;mstrutils.rereplace;;proc (s: string, sub: string, by: string): string{.noSideEffect, gcsafe, locks: 0.};;*mstrutils.nim;;3;;5;;"competes for priority in suggestion, here first, but never used in test";;100;;None
sug;;skFunc;;mstrutils.rerereplace;;proc (s: string, sub: string, by: string): string{.noSideEffect, gcsafe, locks: 0.};;*mstrutils.nim;;15;;5;;"isn\'t used and appears last, lowest priority";;100;;None
"""
24 changes: 9 additions & 15 deletions nimsuggest/tests/tstrutils.nim
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import mstrutils

discard """
$nimsuggest --tester lib/pure/strutils.nim
>def lib/pure/strutils.nim:2022:8
def;;skTemplate;;assertions.doAssert;;template (cond: untyped, msg: string);;*lib/system/assertions.nim;;58;;9;;"Similar to ``assert`` but is always turned on*";;100
$nimsuggest --tester $file
>def $path/mstrutils.nim:6:4
def;;skTemplate;;massertish.doAssertish;;template (cond: untyped, msg: string);;*/massertish.nim;;3;;9;;"template to allow def lookup testing";;100
>def $path/mstrutils.nim:12:3
def;;skTemplate;;massertish.doAssertish;;template (cond: untyped, msg: string);;*/massertish.nim;;3;;9;;"template to allow def lookup testing";;100
>def $path/mstrutils.nim:18:12
def;;skTemplate;;massertish.doAssertish;;template (cond: untyped, msg: string);;*/massertish.nim;;3;;9;;"template to allow def lookup testing";;100
"""

# Line 2022 in strutils.nim is doAssert and this is unlikely to change
# soon since there are a whole lot of doAsserts there.

#[
Notes:
* metamorphic testing or propery based testing might suit nimsuggest better
* easier option is to isolate the dependency with test modules
Open Questions:
* Why not use unit tests or code fixtures for these tests?
]#

0 comments on commit adb069d

Please sign in to comment.