add test cases; improve error messages; remove unnecessary check

This commit is contained in:
lurchi 2018-08-22 20:35:40 +02:00
parent eaeed9a16d
commit 113cbc75da
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
1 changed files with 28 additions and 7 deletions

View File

@ -48,8 +48,7 @@ proc getUntil(input: string, delimiters: openArray[char], packet: var PsycPacket
packet.cursor = i
break
if not validate(input[i]):
raise newException(PsycSyntaxError,
"invalid character '" & input[i] & "' in " & $packet.state)
raise newException(PsycSyntaxError, "invalid character in " & $packet.state)
proc parseKeyword(input: string,
packet: var PsycPacket): tuple[complete: bool, value: string] =
@ -71,7 +70,7 @@ proc parseModifierValueLength(input: string,
if value.len() == 0:
raise newException(PsycSyntaxError, "missing length after " & $packet.state)
try:
result = (true, value.parseInt()) # may throw ValueError if invalid number
result = (true, value.parseInt())
except ValueError:
raise newException(PsycSyntaxError, "invalid length after " & $packet.state)
if result.value < 0:
@ -155,11 +154,10 @@ proc parseContentLength(input: string,
result.value = -1
else:
try:
result.value = value.parseInt() # parseInt may throw ValueError if invalid number
result.value = value.parseInt()
except ValueError:
raise newException(PsycSyntaxError, "invalid length after " & $packet.state)
if result.value < 0:
raise newException(PsycSyntaxError, "invalid length after " & $packet.state)
raise newException(PsycSyntaxError,
$packet.state & " is not a positive integer")
proc parseData(input: string,
packet: var PsycPacket): tuple[complete: bool, value: string] =
@ -457,4 +455,27 @@ suite "parser tests":
check(packet.state == ParseState.Complete)
check(packet.data == "Hello Alice!")
test "invalid character in keyword":
let input = ":\0\n"
try:
(needMore, unparsed) = parse(input, packet)
except PsycSyntaxError:
check(getCurrentExceptionMsg() == "invalid character in RoutingModifierName")
test "empty keyword":
let input = ":\n"
try:
(needMore, unparsed) = parse(input, packet)
except PsycSyntaxError:
check(getCurrentExceptionMsg() == "RoutingModifierName does not start with '_'")
test "invalid content length":
let input = "abc\n"
(needMore, unparsed) = parse(input, packet)
check(packet.state == ParseState.ContentLength)
try:
(needMore, unparsed) = parse(input, packet)
except PsycSyntaxError:
check(getCurrentExceptionMsg() == "ContentLength is not a positive integer")
echo "parser tests completed"