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 packet.cursor = i
break break
if not validate(input[i]): if not validate(input[i]):
raise newException(PsycSyntaxError, raise newException(PsycSyntaxError, "invalid character in " & $packet.state)
"invalid character '" & input[i] & "' in " & $packet.state)
proc parseKeyword(input: string, proc parseKeyword(input: string,
packet: var PsycPacket): tuple[complete: bool, value: string] = packet: var PsycPacket): tuple[complete: bool, value: string] =
@ -71,7 +70,7 @@ proc parseModifierValueLength(input: string,
if value.len() == 0: if value.len() == 0:
raise newException(PsycSyntaxError, "missing length after " & $packet.state) raise newException(PsycSyntaxError, "missing length after " & $packet.state)
try: try:
result = (true, value.parseInt()) # may throw ValueError if invalid number result = (true, value.parseInt())
except ValueError: except ValueError:
raise newException(PsycSyntaxError, "invalid length after " & $packet.state) raise newException(PsycSyntaxError, "invalid length after " & $packet.state)
if result.value < 0: if result.value < 0:
@ -155,11 +154,10 @@ proc parseContentLength(input: string,
result.value = -1 result.value = -1
else: else:
try: try:
result.value = value.parseInt() # parseInt may throw ValueError if invalid number result.value = value.parseInt()
except ValueError: except ValueError:
raise newException(PsycSyntaxError, "invalid length after " & $packet.state) raise newException(PsycSyntaxError,
if result.value < 0: $packet.state & " is not a positive integer")
raise newException(PsycSyntaxError, "invalid length after " & $packet.state)
proc parseData(input: string, proc parseData(input: string,
packet: var PsycPacket): tuple[complete: bool, value: string] = packet: var PsycPacket): tuple[complete: bool, value: string] =
@ -457,4 +455,27 @@ suite "parser tests":
check(packet.state == ParseState.Complete) check(packet.state == ParseState.Complete)
check(packet.data == "Hello Alice!") 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" echo "parser tests completed"