/
310-document.t
79 lines (63 loc) · 2.35 KB
/
310-document.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use v6;
use Test;
use BSON::Document;
#-------------------------------------------------------------------------------
subtest {
my BSON::Document $d .= new: ('a' ... 'd') Z=> 121..124;
is $d<a>, 121, "\$<a> = $d<a>";
is $d.keys, <a b c d>, 'keys a, b, c, d';
# keys 'a'..'d' are cstring => 0xnn 0x00
# 121..124 are numbers in size int32 => bson code 0x10
#
my Buf $etst = Buf.new(
0x21, 0x00 xx 3,
0x10, 0x61, 0x00, 0x79, 0x00, 0x00, 0x00, # 10 'a' 121
0x10, 0x62, 0x00, 0x7a, 0x00, 0x00, 0x00, # 10 'b' 122
0x10, 0x63, 0x00, 0x7b, 0x00, 0x00, 0x00, # 10 'c' 123
0x10, 0x64, 0x00, 0x7c, 0x00, 0x00, 0x00, # 10 'd' 124
0x00
);
my Buf $edoc = $d.encode;
is-deeply $edoc, $etst, 'Encoded document is correct';
is $d<b>:delete, 122, '$d<b> deleted';
is $d.keys, <a c d>, 'keys a, c, d';
$etst = Buf.new(
0x1a, 0x00 xx 3,
0x10, 0x61, 0x00, 0x79, 0x00, 0x00, 0x00, # 10 'a' 121
0x10, 0x63, 0x00, 0x7b, 0x00, 0x00, 0x00, # 10 'c' 123
0x10, 0x64, 0x00, 0x7c, 0x00, 0x00, 0x00, # 10 'd' 124
0x00
);
$edoc = $d.encode;
is-deeply $edoc, $etst, 'Encoded document still correct after deletion';
$d<b> = 2663;
is $d<b>, 2663, '$d<b> added at end';
is $d.keys, <a c d b>, 'keys a, c, d, b';
$etst = Buf.new(
0x21, 0x00 xx 3,
0x10, 0x61, 0x00, 0x79, 0x00, 0x00, 0x00, # 10 'a' 121
0x10, 0x63, 0x00, 0x7b, 0x00, 0x00, 0x00, # 10 'c' 123
0x10, 0x64, 0x00, 0x7c, 0x00, 0x00, 0x00, # 10 'd' 124
0x10, 0x62, 0x00, 0x67, 0x0a, 0x00, 0x00, # 10 'b' 2663
0x00
);
$edoc = $d.encode;
is-deeply $edoc, $etst, 'Encoded document still correct after addition';
}, "Document encoding associative";
#-------------------------------------------------------------------------------
subtest {
my BSON::Document $d .= new;
my Buf $new-data = Buf.new(
0x2d, 0x00 xx 3,
0x10, 0x6b, 0x65, 0x79, 0x30, 0x00, 0x7a, 0x00 xx 3, # 10 'key0' 122
0x10, 0x6b, 0x65, 0x79, 0x31, 0x00, 0x7b, 0x00 xx 3, # 10 'key1' 123
0x10, 0x6b, 0x65, 0x79, 0x32, 0x00, 0x7c, 0x00 xx 3, # 10 'key2' 124
0x10, 0x6b, 0x65, 0x79, 0x33, 0x00, 0x7d, 0x00 xx 3, # 10 'key3' 125
0x00
);
$d.decode($new-data);
is $d<key0>, 122, "\$d<key0> => $d<key0>";
}, "Document decoding";
#-------------------------------------------------------------------------------
# Cleanup
done-testing;