Add check for empty string

This commit is contained in:
coffeepots 2018-07-09 09:43:23 +01:00
parent 4e300f1539
commit f943c584c7

View File

@ -17,7 +17,7 @@ proc encodeQuantity*(value: SomeUnsignedInt): string =
template hasHexHeader*(value: string | HexDataStr | HexQuantityStr): bool = template hasHexHeader*(value: string | HexDataStr | HexQuantityStr): bool =
template strVal: untyped = value.string template strVal: untyped = value.string
if strVal[0] == '0' and strVal[1] in {'x', 'X'} and strVal.len > 2: true if strVal != "" and strVal[0] == '0' and strVal[1] in {'x', 'X'} and strVal.len > 2: true
else: false else: false
template isHexChar*(c: char): bool = template isHexChar*(c: char): bool =
@ -94,6 +94,12 @@ proc fromJson*(n: JsonNode, argName: string, result: var HexQuantityStr) =
when isMainModule: when isMainModule:
import unittest import unittest
suite "Hex quantity": suite "Hex quantity":
test "Empty string":
expect ValueError:
let
source = ""
x = hexQuantityStr source
check %x == %source
test "Even length": test "Even length":
let let
source = "0x123" source = "0x123"