|
## Python language validator tests.
|
|
|
|
import std/[unittest, strutils, strformat]
|
|
import ../src/nimcheck
|
|
import test_common
|
|
|
|
proc checkValid(result: ValidationResult, context: string = "") =
|
|
if result.errors.len > 0:
|
|
var msg = &"Expected valid, got {result.errors.len} error(s)"
|
|
if context.len > 0: msg.add(&" [{context}]")
|
|
for e in result.errors: msg.add(&"\n [{e.code}] {e.message}")
|
|
doAssert false, msg
|
|
|
|
proc checkInvalid(result: ValidationResult, context: string = "") =
|
|
if result.errors.len == 0:
|
|
doAssert false, &"Expected errors, got none [{context}]"
|
|
|
|
suite "Python Validator":
|
|
test "valid Python code":
|
|
let result = validateSource(PythonGoodCode, flavor = lfPython)
|
|
checkValid(result)
|
|
|
|
test "invalid Python code returns errors":
|
|
let result = validateSource(PythonBadCode, flavor = lfPython)
|
|
checkInvalid(result)
|
|
|
|
test "valid .py file":
|
|
let result = validateFile("tests/fixtures/python/valid.py")
|
|
checkValid(result)
|
|
|
|
test "invalid .py file returns errors":
|
|
let result = validateFile("tests/fixtures/python/invalid.py")
|
|
check result.errors.len > 0
|
|
|
|
test "detectFlavor detects Python":
|
|
check $detectFlavor(PythonGoodCode) == "python"
|
|
|
|
test "inspectSource returns JSON":
|
|
let json = inspectSource(PythonGoodCode, flavor = lfPython)
|
|
check json.kind == JObject
|