|
## Config file (JSON, YAML, TOML) 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
|
|
|
|
suite "Config Validators":
|
|
test "JSON valid":
|
|
checkValid(validateSource(JsonGoodCode, flavor = lfJSON))
|
|
|
|
test "JSON invalid returns errors":
|
|
check validateSource(JsonBadCode, flavor = lfJSON).errors.len > 0
|
|
|
|
test "JSON valid file":
|
|
checkValid(validateFile("tests/fixtures/json/valid.json"))
|
|
|
|
test "JSON invalid file returns errors":
|
|
check validateFile("tests/fixtures/json/invalid.json").errors.len > 0
|
|
|
|
test "JSON detect":
|
|
check $detectFlavor(JsonGoodCode) == "json"
|
|
|
|
test "YAML valid":
|
|
checkValid(validateSource(YamlGoodCode, flavor = lfYAML))
|
|
|
|
test "YAML invalid returns errors":
|
|
check validateSource(YamlBadCode, flavor = lfYAML).errors.len > 0
|
|
|
|
test "YAML valid file":
|
|
checkValid(validateFile("tests/fixtures/yaml/valid.yaml"))
|
|
|
|
test "YAML invalid file returns errors":
|
|
check validateFile("tests/fixtures/yaml/invalid.yaml").errors.len > 0
|
|
|
|
test "YAML detect":
|
|
check $detectFlavor(YamlGoodCode) == "yaml"
|
|
|
|
test "TOML valid":
|
|
checkValid(validateSource(TomlGoodCode, flavor = lfTOML))
|
|
|
|
test "TOML invalid returns errors":
|
|
check validateSource(TomlBadCode, flavor = lfTOML).errors.len > 0
|
|
|
|
test "TOML valid file":
|
|
checkValid(validateFile("tests/fixtures/toml/valid.toml"))
|
|
|
|
test "TOML invalid file returns errors":
|
|
check validateFile("tests/fixtures/toml/invalid.toml").errors.len > 0
|
|
|
|
test "TOML detect":
|
|
check $detectFlavor(TomlGoodCode) == "toml"
|