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