10 years later... updates for modern Nim.
FossilOrigin-Name: 7361ae25595c1916e9701a25033b684e3cd089d8abb71ead3803a59e36189f63
This commit is contained in:
parent
ae7b77df4c
commit
9aa2912286
23 changed files with 640 additions and 371 deletions
15
tests/tnetobjects/t_can_add_array_elements.nim
Normal file
15
tests/tnetobjects/t_can_add_array_elements.nim
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# vim: set et sta sw=4 ts=4 :
|
||||
|
||||
import tnetstring
|
||||
|
||||
let tnet_array = newTNetstringArray()
|
||||
for i in 1 .. 10:
|
||||
let tnet_obj = newTNetstringInt( i )
|
||||
tnet_array.add( tnet_obj )
|
||||
tnet_array[ 6 ] = newTNetstringString( "yep" )
|
||||
|
||||
assert tnet_array.len == 10
|
||||
assert tnet_array[ 4 ].num == 5
|
||||
assert tnet_array[ 6 ].str == "yep"
|
||||
|
||||
|
||||
13
tests/tnetobjects/t_can_be_hashed.nim
Normal file
13
tests/tnetobjects/t_can_be_hashed.nim
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# vim: set et sta sw=4 ts=4 :
|
||||
|
||||
import
|
||||
std/hashes
|
||||
import tnetstring
|
||||
|
||||
# Hashes to the underlying object type.
|
||||
|
||||
let tnet_int = parse_tnetstring( "1:1#" )
|
||||
|
||||
assert tnet_int.hash == 1.hash
|
||||
assert parse_tnetstring( "4:true!" ).hash == hash( true.int )
|
||||
|
||||
43
tests/tnetobjects/t_can_be_serialized.nim
Normal file
43
tests/tnetobjects/t_can_be_serialized.nim
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
# vim: set et sta sw=4 ts=4 :
|
||||
|
||||
import tnetstring
|
||||
|
||||
|
||||
let tstr = "308:9:givenName,6:Mahlon,16:departmentNumber,22:Information Technology," &
|
||||
"5:title,19:Senior Technologist,13:accountConfig,48:7:vmemail,4:true!7:allpage," &
|
||||
"5:false!7:galhide,0:~}13:homeDirectory,14:/home/m/mahlon,3:uid,6:mahlon,9:yubi" &
|
||||
"KeyId,12:vvidhghkhehj,5:gecos,12:Mahlon Smith,2:sn,5:Smith,14:employeeNumber,5:12921#}"
|
||||
let tnet_null = newTNetstringNull()
|
||||
var tnet_obj = parse_tnetstring( tstr )
|
||||
|
||||
# full round trip
|
||||
assert tstr == tnet_obj.dump_tnetstring
|
||||
|
||||
# objects and their defaults
|
||||
|
||||
tnet_obj = newTNetstringString( "Hello." )
|
||||
|
||||
assert tnet_obj.getStr == "Hello."
|
||||
assert tnet_null.getStr("nope") == "nope"
|
||||
assert tnet_null.getStr == ""
|
||||
|
||||
tnet_obj = newTNetstringInt( 42 )
|
||||
assert tnet_obj.getInt == 42
|
||||
assert tnet_null.getInt == 0
|
||||
assert tnet_null.getInt(1) == 1
|
||||
|
||||
tnet_obj = newTNetstringFloat( 1.0 )
|
||||
assert tnet_obj.getFloat == 1.0
|
||||
assert tnet_null.getFloat == 0
|
||||
assert tnet_null.getFloat(0.1) == 0.1
|
||||
|
||||
tnet_obj = newTNetstringObject()
|
||||
tnet_obj[ "yay" ] = newTNetstringInt( 1 )
|
||||
assert tnet_obj.getFields[0].val == newTNetstringInt(1)
|
||||
assert tnet_null.getFields.len == 0
|
||||
|
||||
tnet_obj = newTNetstringArray()
|
||||
tnet_obj.add( newTNetstringInt(1) )
|
||||
assert tnet_obj.getElems[0] == newTNetstringInt(1)
|
||||
assert tnet_null.getElems.len == 0
|
||||
|
||||
17
tests/tnetobjects/t_can_paired_elements.nim
Normal file
17
tests/tnetobjects/t_can_paired_elements.nim
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# vim: set et sta sw=4 ts=4 :
|
||||
|
||||
import tnetstring
|
||||
|
||||
let tnet_obj = newTNetstringObject()
|
||||
tnet_obj.add( "yo", newTNetstringInt(1) )
|
||||
tnet_obj.add( "yep", newTNetstringInt(2) )
|
||||
|
||||
assert tnet_obj["yo"].num == 1
|
||||
assert tnet_obj["yep"].num == 2
|
||||
assert tnet_obj.len == 2
|
||||
|
||||
tnet_obj[ "more" ] = newTNetstringInt(1)
|
||||
tnet_obj[ "yo" ] = newTNetstringInt(1) # dup check
|
||||
|
||||
assert tnet_obj.len == 3
|
||||
|
||||
13
tests/tnetobjects/t_deep_copies.nim
Normal file
13
tests/tnetobjects/t_deep_copies.nim
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# vim: set et sta sw=4 ts=4 :
|
||||
|
||||
import tnetstring
|
||||
|
||||
let original = parse_tnetstring( "35:2:hi,8:1:a,1:b,]5:there,8:1:c,1:d,]}" )
|
||||
let copied = original.copy
|
||||
|
||||
# Same values
|
||||
assert copied == original
|
||||
|
||||
# Different instances
|
||||
assert cast[pointer](original) != cast[pointer](copied)
|
||||
|
||||
7
tests/tnetobjects/t_index_slicing.nim
Normal file
7
tests/tnetobjects/t_index_slicing.nim
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# vim: set et sta sw=4 ts=4 :
|
||||
|
||||
import tnetstring
|
||||
|
||||
let tnet_obj = parse_tnetstring( "20:1:1#1:2#1:3#1:4#1:5#]" )
|
||||
assert tnet_obj[ 2 ].num == 3
|
||||
|
||||
18
tests/tnetobjects/t_iterators.nim
Normal file
18
tests/tnetobjects/t_iterators.nim
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# vim: set et sta sw=4 ts=4 :
|
||||
|
||||
import tnetstring
|
||||
|
||||
var
|
||||
keys: seq[ string ]
|
||||
vals: seq[ string ]
|
||||
|
||||
for key, val in parse_tnetstring( "35:2:hi,8:1:a,1:b,]5:there,8:1:c,1:d,]}" ):
|
||||
keys.add( key )
|
||||
|
||||
for item in val:
|
||||
vals.add( item.str )
|
||||
|
||||
assert keys == @["hi","there"]
|
||||
assert vals == @["a","b","c","d"]
|
||||
|
||||
|
||||
10
tests/tnetobjects/t_key_accessors.nim
Normal file
10
tests/tnetobjects/t_key_accessors.nim
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# vim: set et sta sw=4 ts=4 :
|
||||
|
||||
import tnetstring
|
||||
|
||||
let tnet_obj = parse_tnetstring( "11:2:hi,3:yep,}" )
|
||||
|
||||
assert $tnet_obj["hi"] == "yep"
|
||||
assert tnet_obj.has_key( "hi" ) == true
|
||||
assert tnet_obj.has_key( "nope-not-here" ) == false
|
||||
|
||||
10
tests/tnetobjects/t_key_deletion.nim
Normal file
10
tests/tnetobjects/t_key_deletion.nim
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# vim: set et sta sw=4 ts=4 :
|
||||
|
||||
import tnetstring
|
||||
|
||||
let tnet_obj = parse_tnetstring( "35:2:hi,8:1:a,1:b,]5:there,8:1:c,1:d,]}" )
|
||||
|
||||
assert tnet_obj.fields.len == 2
|
||||
tnet_obj.delete( "hi" )
|
||||
assert tnet_obj.fields.len == 1
|
||||
|
||||
11
tests/tnetobjects/t_length_checks.nim
Normal file
11
tests/tnetobjects/t_length_checks.nim
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# vim: set et sta sw=4 ts=4 :
|
||||
|
||||
import tnetstring
|
||||
|
||||
let tnet_obj = parse_tnetstring( "35:2:hi,8:1:a,1:b,]5:there,8:1:c,1:d,]}" )
|
||||
|
||||
assert parse_tnetstring( "0:~" ).len == 0
|
||||
assert tnet_obj.len == 2
|
||||
assert parse_tnetstring( "8:1:1#1:2#]" ).len == 2
|
||||
assert parse_tnetstring( "5:hallo," ).len == 5
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue