Address review comments; Add a test

This commit is contained in:
Zahary Karadjov 2020-07-11 22:07:14 +03:00 committed by zah
parent 93b04bc214
commit 74396747a3
3 changed files with 43 additions and 1 deletions

View File

@ -678,8 +678,18 @@ import json_serialization
export json_serialization
export writeValue, readValue
const
# http://facweb.cs.depaul.edu/sjost/it212/documents/ascii-pr.htm
PrintableAsciiChars = {'!'..'~'}
func `$`*(value: GraffitiBytes): string =
strip(string.fromBytes(distinctBase value), chars = Whitespace + {'\0'})
result = strip(string.fromBytes(distinctBase value),
leading = false,
chars = Whitespace + {'\0'})
# TODO: Perhaps handle UTF-8 at some point
if not allCharsInSet(result, PrintableAsciiChars):
result = "0x" & toHex(distinctBase value)
func init*(T: type GraffitiBytes, input: string): GraffitiBytes
{.raises: [ValueError, Defect].} =

View File

@ -17,6 +17,7 @@ import # Unit test
./test_beaconstate,
./test_bitseqs,
./test_block_pool,
./test_datatypes,
./test_helpers,
./test_keystore,
./test_mocking,

31
tests/test_datatypes.nim Normal file
View File

@ -0,0 +1,31 @@
# beacon_chain
# Copyright (c) 2018 Status Research & Development GmbH
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
{.used.}
import
unittest, typetraits,
../beacon_chain/spec/datatypes,
./testutil
suiteReport "Spec datatypes":
timedTest "Graffiti bytes":
var
g1 = GraffitiBytes.init "Hello"
g2 = default(GraffitiBytes)
g3 = g2
distinctBase(g3)[2] = byte(6)
check:
$g1 == "Hello"
$g2 == ""
$g3 == "0x0000060000000000000000000000000000000000000000000000000000000000"
g2 == GraffitiBytes.init("")
g3 == GraffitiBytes.init($g3)