bson 4.1.1-java → 4.2.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/Rakefile +18 -3
- data/lib/bson-ruby.jar +0 -0
- data/lib/bson.rb +6 -4
- data/lib/bson/array.rb +1 -1
- data/lib/bson/binary.rb +4 -2
- data/lib/bson/code.rb +1 -1
- data/lib/bson/code_with_scope.rb +1 -1
- data/lib/bson/config.rb +1 -1
- data/lib/bson/date.rb +1 -1
- data/lib/bson/date_time.rb +1 -1
- data/lib/bson/decimal128.rb +318 -0
- data/lib/bson/decimal128/builder.rb +448 -0
- data/lib/bson/document.rb +2 -2
- data/lib/bson/environment.rb +13 -1
- data/lib/bson/false_class.rb +1 -1
- data/lib/bson/float.rb +1 -1
- data/lib/bson/hash.rb +1 -1
- data/lib/bson/int32.rb +46 -0
- data/lib/bson/int64.rb +46 -0
- data/lib/bson/integer.rb +1 -1
- data/lib/bson/max_key.rb +1 -1
- data/lib/bson/min_key.rb +1 -1
- data/lib/bson/object_id.rb +3 -2
- data/lib/bson/open_struct.rb +57 -0
- data/lib/bson/regexp.rb +87 -19
- data/lib/bson/registry.rb +1 -1
- data/lib/bson/specialized.rb +1 -1
- data/lib/bson/string.rb +1 -1
- data/lib/bson/symbol.rb +1 -1
- data/lib/bson/time.rb +1 -1
- data/lib/bson/timestamp.rb +2 -2
- data/lib/bson/true_class.rb +1 -1
- data/lib/bson/version.rb +2 -2
- data/spec/bson/array_spec.rb +1 -1
- data/spec/bson/binary_spec.rb +2 -1
- data/spec/bson/corpus_spec.rb +68 -0
- data/spec/bson/decimal128_spec.rb +1583 -0
- data/spec/bson/document_spec.rb +1 -1
- data/spec/bson/driver_bson_spec.rb +77 -0
- data/spec/bson/int32_spec.rb +58 -0
- data/spec/bson/int64_spec.rb +58 -0
- data/spec/bson/open_struct_spec.rb +144 -0
- data/spec/bson/raw_spec.rb +540 -0
- data/spec/bson/regexp_spec.rb +7 -7
- data/spec/bson/timestamp_spec.rb +1 -1
- data/spec/spec_helper.rb +5 -0
- data/spec/support/common_driver.rb +347 -0
- data/spec/support/corpus-tests/array.json +43 -0
- data/spec/support/corpus-tests/boolean.json +27 -0
- data/spec/support/corpus-tests/code.json +67 -0
- data/spec/support/corpus-tests/code_w_scope.json +78 -0
- data/spec/support/corpus-tests/document.json +36 -0
- data/spec/support/corpus-tests/double.json +69 -0
- data/spec/support/corpus-tests/failures/binary.json +69 -0
- data/spec/support/corpus-tests/failures/datetime.json +31 -0
- data/spec/support/corpus-tests/failures/dbpointer.json +42 -0
- data/spec/support/corpus-tests/failures/int64.json +38 -0
- data/spec/support/corpus-tests/failures/symbol.json +62 -0
- data/spec/support/corpus-tests/failures/undefined.json +13 -0
- data/spec/support/corpus-tests/int32.json +38 -0
- data/spec/support/corpus-tests/maxkey.json +12 -0
- data/spec/support/corpus-tests/minkey.json +12 -0
- data/spec/support/corpus-tests/null.json +12 -0
- data/spec/support/corpus-tests/oid.json +28 -0
- data/spec/support/corpus-tests/regex.json +37 -0
- data/spec/support/corpus-tests/string.json +67 -0
- data/spec/support/corpus-tests/timestamp.json +18 -0
- data/spec/support/corpus-tests/top.json +62 -0
- data/spec/support/corpus.rb +265 -0
- data/spec/support/driver-spec-tests/decimal128/decimal128-1.json +363 -0
- data/spec/support/driver-spec-tests/decimal128/decimal128-2.json +793 -0
- data/spec/support/driver-spec-tests/decimal128/decimal128-3.json +1771 -0
- data/spec/support/driver-spec-tests/decimal128/decimal128-4.json +165 -0
- data/spec/support/driver-spec-tests/decimal128/decimal128-5.json +402 -0
- data/spec/support/driver-spec-tests/decimal128/decimal128-6.json +131 -0
- data/spec/support/driver-spec-tests/decimal128/decimal128-7.json +327 -0
- data/spec/support/shared_examples.rb +1 -1
- metadata +77 -4
- metadata.gz.sig +0 -0
@@ -0,0 +1,62 @@
|
|
1
|
+
{
|
2
|
+
"description": "Symbol",
|
3
|
+
"bson_type": "0x0E",
|
4
|
+
"deprecated": true,
|
5
|
+
"test_key": "a",
|
6
|
+
"valid": [
|
7
|
+
{
|
8
|
+
"description": "Empty string",
|
9
|
+
"bson": "0D0000000E6100010000000000"
|
10
|
+
},
|
11
|
+
{
|
12
|
+
"description": "Single character",
|
13
|
+
"bson": "0E0000000E610002000000620000"
|
14
|
+
},
|
15
|
+
{
|
16
|
+
"description": "Multi-character",
|
17
|
+
"bson": "190000000E61000D0000006162616261626162616261620000"
|
18
|
+
},
|
19
|
+
{
|
20
|
+
"description": "two-byte UTF-8 (\u00e9)",
|
21
|
+
"bson": "190000000E61000D000000C3A9C3A9C3A9C3A9C3A9C3A90000"
|
22
|
+
},
|
23
|
+
{
|
24
|
+
"description": "three-byte UTF-8 (\u2606)",
|
25
|
+
"bson": "190000000E61000D000000E29886E29886E29886E298860000"
|
26
|
+
},
|
27
|
+
{
|
28
|
+
"description": "Embedded nulls",
|
29
|
+
"bson": "190000000E61000D0000006162006261620062616261620000"
|
30
|
+
}
|
31
|
+
],
|
32
|
+
"decodeErrors": [
|
33
|
+
{
|
34
|
+
"description": "bad symbol length: 0 (but no 0x00 either)",
|
35
|
+
"bson": "0C0000000261000000000000"
|
36
|
+
},
|
37
|
+
{
|
38
|
+
"description": "bad symbol length: -1",
|
39
|
+
"bson": "0C000000026100FFFFFFFF00"
|
40
|
+
},
|
41
|
+
{
|
42
|
+
"description": "bad symbol length: eats terminator",
|
43
|
+
"bson": "10000000026100050000006200620000"
|
44
|
+
},
|
45
|
+
{
|
46
|
+
"description": "bad symbol length: longer than rest of document",
|
47
|
+
"bson": "120000000200FFFFFF00666F6F6261720000"
|
48
|
+
},
|
49
|
+
{
|
50
|
+
"description": "symbol is not null-terminated",
|
51
|
+
"bson": "1000000002610004000000616263FF00"
|
52
|
+
},
|
53
|
+
{
|
54
|
+
"description": "empty symbol, but extra null",
|
55
|
+
"bson": "0E00000002610001000000000000"
|
56
|
+
},
|
57
|
+
{
|
58
|
+
"description": "invalid UTF-8",
|
59
|
+
"bson": "0E00000002610002000000E90000"
|
60
|
+
}
|
61
|
+
]
|
62
|
+
}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
{
|
2
|
+
"description": "Undefined type (deprecated)",
|
3
|
+
"bson_type": "0x06",
|
4
|
+
"deprecated": true,
|
5
|
+
"test_key": "a",
|
6
|
+
"valid": [
|
7
|
+
{
|
8
|
+
"description": "Undefined",
|
9
|
+
"bson": "0800000006610000",
|
10
|
+
"extjson": "{\"a\" : {\"$undefined\" : true}}"
|
11
|
+
}
|
12
|
+
]
|
13
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
{
|
2
|
+
"description": "Int32 type",
|
3
|
+
"bson_type": "0x10",
|
4
|
+
"test_key": "i",
|
5
|
+
"valid": [
|
6
|
+
{
|
7
|
+
"description": "MinValue",
|
8
|
+
"bson": "0C0000001069000000008000",
|
9
|
+
"extjson": "{\"i\" : -2147483648}"
|
10
|
+
},
|
11
|
+
{
|
12
|
+
"description": "MaxValue",
|
13
|
+
"bson": "0C000000106900FFFFFF7F00",
|
14
|
+
"extjson": "{\"i\" : 2147483647}"
|
15
|
+
},
|
16
|
+
{
|
17
|
+
"description": "-1",
|
18
|
+
"bson": "0C000000106900FFFFFFFF00",
|
19
|
+
"extjson": "{\"i\" : -1}"
|
20
|
+
},
|
21
|
+
{
|
22
|
+
"description": "0",
|
23
|
+
"bson": "0C0000001069000000000000",
|
24
|
+
"extjson": "{\"i\" : 0}"
|
25
|
+
},
|
26
|
+
{
|
27
|
+
"description": "1",
|
28
|
+
"bson": "0C0000001069000100000000",
|
29
|
+
"extjson": "{\"i\" : 1}"
|
30
|
+
}
|
31
|
+
],
|
32
|
+
"decodeErrors": [
|
33
|
+
{
|
34
|
+
"description": "Bad int32 field length",
|
35
|
+
"bson": "090000001061000500"
|
36
|
+
}
|
37
|
+
]
|
38
|
+
}
|
@@ -0,0 +1,28 @@
|
|
1
|
+
{
|
2
|
+
"description": "ObjectId",
|
3
|
+
"bson_type": "0x07",
|
4
|
+
"test_key": "a",
|
5
|
+
"valid": [
|
6
|
+
{
|
7
|
+
"description": "All zeroes",
|
8
|
+
"bson": "1400000007610000000000000000000000000000",
|
9
|
+
"extjson": "{\"a\" : {\"$oid\" : \"000000000000000000000000\"}}"
|
10
|
+
},
|
11
|
+
{
|
12
|
+
"description": "All ones",
|
13
|
+
"bson": "14000000076100FFFFFFFFFFFFFFFFFFFFFFFF00",
|
14
|
+
"extjson": "{\"a\" : {\"$oid\" : \"ffffffffffffffffffffffff\"}}"
|
15
|
+
},
|
16
|
+
{
|
17
|
+
"description": "Random",
|
18
|
+
"bson": "1400000007610056E1FC72E0C917E9C471416100",
|
19
|
+
"extjson": "{\"a\" : {\"$oid\" : \"56e1fc72e0c917e9c4714161\"}}"
|
20
|
+
}
|
21
|
+
],
|
22
|
+
"decodeErrors": [
|
23
|
+
{
|
24
|
+
"description": "OID truncated",
|
25
|
+
"bson": "1200000007610056E1FC72E0C917E9C471"
|
26
|
+
}
|
27
|
+
]
|
28
|
+
}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
{
|
2
|
+
"description": "Regular Expression type",
|
3
|
+
"bson_type": "0x0B",
|
4
|
+
"test_key": "a",
|
5
|
+
"valid": [
|
6
|
+
{
|
7
|
+
"description": "empty regex with no options",
|
8
|
+
"bson": "0A0000000B6100000000",
|
9
|
+
"extjson": "{\"a\" : {\"$regex\" : \"\", \"$options\" : \"\"}}"
|
10
|
+
},
|
11
|
+
{
|
12
|
+
"description": "regex without options",
|
13
|
+
"bson": "0D0000000B6100616263000000",
|
14
|
+
"extjson": "{\"a\" : {\"$regex\" : \"abc\", \"$options\" : \"\"}}"
|
15
|
+
},
|
16
|
+
{
|
17
|
+
"description": "regex with options",
|
18
|
+
"bson": "0F0000000B610061626300696D0000",
|
19
|
+
"extjson": "{\"a\" : {\"$regex\" : \"abc\", \"$options\" : \"im\"}}"
|
20
|
+
},
|
21
|
+
{
|
22
|
+
"description": "regex with slash",
|
23
|
+
"bson": "110000000B610061622F636400696D0000",
|
24
|
+
"extjson": "{\"a\" : {\"$regex\" : \"ab/cd\", \"$options\" : \"im\"}}"
|
25
|
+
}
|
26
|
+
],
|
27
|
+
"decodeErrors": [
|
28
|
+
{
|
29
|
+
"description": "embedded null in pattern",
|
30
|
+
"bson": "0F0000000B610061006300696D0000"
|
31
|
+
},
|
32
|
+
{
|
33
|
+
"description": "embedded null in flags",
|
34
|
+
"bson": "100000000B61006162630069006D0000"
|
35
|
+
}
|
36
|
+
]
|
37
|
+
}
|
@@ -0,0 +1,67 @@
|
|
1
|
+
{
|
2
|
+
"description": "String",
|
3
|
+
"bson_type": "0x02",
|
4
|
+
"test_key": "a",
|
5
|
+
"valid": [
|
6
|
+
{
|
7
|
+
"description": "Empty string",
|
8
|
+
"bson": "0D000000026100010000000000",
|
9
|
+
"extjson": "{\"a\" : \"\"}"
|
10
|
+
},
|
11
|
+
{
|
12
|
+
"description": "Single character",
|
13
|
+
"bson": "0E00000002610002000000620000",
|
14
|
+
"extjson": "{\"a\" : \"b\"}"
|
15
|
+
},
|
16
|
+
{
|
17
|
+
"description": "Multi-character",
|
18
|
+
"bson": "190000000261000D0000006162616261626162616261620000",
|
19
|
+
"extjson": "{\"a\" : \"abababababab\"}"
|
20
|
+
},
|
21
|
+
{
|
22
|
+
"description": "two-byte UTF-8 (\u00e9)",
|
23
|
+
"bson": "190000000261000D000000C3A9C3A9C3A9C3A9C3A9C3A90000",
|
24
|
+
"extjson": "{\"a\" : \"\\u00e9\\u00e9\\u00e9\\u00e9\\u00e9\\u00e9\"}"
|
25
|
+
},
|
26
|
+
{
|
27
|
+
"description": "three-byte UTF-8 (\u2606)",
|
28
|
+
"bson": "190000000261000D000000E29886E29886E29886E298860000",
|
29
|
+
"extjson": "{\"a\" : \"\\u2606\\u2606\\u2606\\u2606\"}"
|
30
|
+
},
|
31
|
+
{
|
32
|
+
"description": "Embedded nulls",
|
33
|
+
"bson": "190000000261000D0000006162006261620062616261620000",
|
34
|
+
"extjson": "{\"a\" : \"ab\\u0000bab\\u0000babab\"}"
|
35
|
+
}
|
36
|
+
],
|
37
|
+
"decodeErrors": [
|
38
|
+
{
|
39
|
+
"description": "bad string length: 0 (but no 0x00 either)",
|
40
|
+
"bson": "0C0000000261000000000000"
|
41
|
+
},
|
42
|
+
{
|
43
|
+
"description": "bad string length: -1",
|
44
|
+
"bson": "0C000000026100FFFFFFFF00"
|
45
|
+
},
|
46
|
+
{
|
47
|
+
"description": "bad string length: eats terminator",
|
48
|
+
"bson": "10000000026100050000006200620000"
|
49
|
+
},
|
50
|
+
{
|
51
|
+
"description": "bad string length: longer than rest of document",
|
52
|
+
"bson": "120000000200FFFFFF00666F6F6261720000"
|
53
|
+
},
|
54
|
+
{
|
55
|
+
"description": "string is not null-terminated",
|
56
|
+
"bson": "1000000002610004000000616263FF00"
|
57
|
+
},
|
58
|
+
{
|
59
|
+
"description": "empty string, but extra null",
|
60
|
+
"bson": "0E00000002610001000000000000"
|
61
|
+
},
|
62
|
+
{
|
63
|
+
"description": "invalid UTF-8",
|
64
|
+
"bson": "0E00000002610002000000E90000"
|
65
|
+
}
|
66
|
+
]
|
67
|
+
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
{
|
2
|
+
"description": "Timestamp type",
|
3
|
+
"bson_type": "0x11",
|
4
|
+
"test_key": "a",
|
5
|
+
"valid": [
|
6
|
+
{
|
7
|
+
"description": "Timestamp: (123456789, 42)",
|
8
|
+
"bson": "100000001161002A00000015CD5B0700",
|
9
|
+
"extjson": "{\"a\" : {\"$timestamp\" : {\"t\" : 123456789, \"i\" : 42}}}"
|
10
|
+
}
|
11
|
+
],
|
12
|
+
"decodeErrors": [
|
13
|
+
{
|
14
|
+
"description": "Truncated timestamp field",
|
15
|
+
"bson": "0f0000001161002A00000015CD5B00"
|
16
|
+
}
|
17
|
+
]
|
18
|
+
}
|
@@ -0,0 +1,62 @@
|
|
1
|
+
{
|
2
|
+
"description": "Top-level document validity",
|
3
|
+
"bson_type": "0x00",
|
4
|
+
"decodeErrors": [
|
5
|
+
{
|
6
|
+
"description": "An object size that's too small to even include the object size, but is a well-formed, empty object",
|
7
|
+
"bson": "0100000000"
|
8
|
+
},
|
9
|
+
{
|
10
|
+
"description": "An object size that's only enough for the object size, but is a well-formed, empty object",
|
11
|
+
"bson": "0400000000"
|
12
|
+
},
|
13
|
+
{
|
14
|
+
"description": "One object, with length shorter than size (missing EOO)",
|
15
|
+
"bson": "05000000"
|
16
|
+
},
|
17
|
+
{
|
18
|
+
"description": "One object, sized correctly, with a spot for an EOO, but the EOO is 0x01",
|
19
|
+
"bson": "0500000001"
|
20
|
+
},
|
21
|
+
{
|
22
|
+
"description": "One object, sized correctly, with a spot for an EOO, but the EOO is 0xff",
|
23
|
+
"bson": "05000000FF"
|
24
|
+
},
|
25
|
+
{
|
26
|
+
"description": "One object, sized correctly, with a spot for an EOO, but the EOO is 0x70",
|
27
|
+
"bson": "0500000070"
|
28
|
+
},
|
29
|
+
{
|
30
|
+
"description": "Byte count is zero (with non-zero input length)",
|
31
|
+
"bson": "00000000000000000000"
|
32
|
+
},
|
33
|
+
{
|
34
|
+
"description": "Stated length exceeds byte count, with truncated document",
|
35
|
+
"bson": "1200000002666F6F0004000000626172"
|
36
|
+
},
|
37
|
+
{
|
38
|
+
"description": "Stated length less than byte count, with garbage after envelope",
|
39
|
+
"bson": "1200000002666F6F00040000006261720000DEADBEEF"
|
40
|
+
},
|
41
|
+
{
|
42
|
+
"description": "Stated length exceeds byte count, with valid envelope",
|
43
|
+
"bson": "1300000002666F6F00040000006261720000"
|
44
|
+
},
|
45
|
+
{
|
46
|
+
"description": "Stated length less than byte count, with valid envelope",
|
47
|
+
"bson": "1100000002666F6F00040000006261720000"
|
48
|
+
},
|
49
|
+
{
|
50
|
+
"description": "Invalid BSON type low range",
|
51
|
+
"bson": "07000000000000"
|
52
|
+
},
|
53
|
+
{
|
54
|
+
"description": "Invalid BSON type high range",
|
55
|
+
"bson": "07000000800000"
|
56
|
+
},
|
57
|
+
{
|
58
|
+
"description": "Document truncated mid-key",
|
59
|
+
"bson": "1200000002666F"
|
60
|
+
}
|
61
|
+
]
|
62
|
+
}
|
@@ -0,0 +1,265 @@
|
|
1
|
+
# Copyright (C) 2016 MongoDB, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require 'json'
|
16
|
+
|
17
|
+
module BSON
|
18
|
+
module Corpus
|
19
|
+
|
20
|
+
# Represents a test from the driver BSON Corpus.
|
21
|
+
#
|
22
|
+
# @since 4.2.0
|
23
|
+
class Spec
|
24
|
+
|
25
|
+
# The spec description.
|
26
|
+
#
|
27
|
+
# @return [ String ] The spec description.
|
28
|
+
#
|
29
|
+
# @since 4.2.0
|
30
|
+
attr_reader :description
|
31
|
+
|
32
|
+
# The document key of the object to test.
|
33
|
+
#
|
34
|
+
# @return [ String ] The document key.
|
35
|
+
#
|
36
|
+
# @since 4.2.0
|
37
|
+
attr_reader :test_key
|
38
|
+
|
39
|
+
# Instantiate the new spec.
|
40
|
+
#
|
41
|
+
# @example Create the spec.
|
42
|
+
# Spec.new(file)
|
43
|
+
#
|
44
|
+
# @param [ String ] file The name of the json file.
|
45
|
+
#
|
46
|
+
# @since 4.2.0
|
47
|
+
def initialize(file)
|
48
|
+
@spec = ::JSON.parse(File.read(file))
|
49
|
+
@valid = @spec['valid'] || []
|
50
|
+
@invalid = @spec['decodeErrors'] || []
|
51
|
+
@description = @spec['description']
|
52
|
+
@test_key = @spec['test_key']
|
53
|
+
end
|
54
|
+
|
55
|
+
# Get a list of tests that are expected to pass.
|
56
|
+
#
|
57
|
+
# @example Get the list of valid tests.
|
58
|
+
# spec.valid_tests
|
59
|
+
#
|
60
|
+
# @return [ Array<BSON::Corpus::Test> ] The list of valid Tests.
|
61
|
+
#
|
62
|
+
# @since 4.2.0
|
63
|
+
def valid_tests
|
64
|
+
@valid_tests ||=
|
65
|
+
@valid.collect do |test|
|
66
|
+
BSON::Corpus::Test.new(self, test)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Get a list of tests that raise exceptions.
|
71
|
+
#
|
72
|
+
# @example Get the list of invalid tests.
|
73
|
+
# spec.invalid_tests
|
74
|
+
#
|
75
|
+
# @return [ Array<BSON::Corpus::Test> ] The list of invalid Tests.
|
76
|
+
#
|
77
|
+
# @since 4.2.0
|
78
|
+
def invalid_tests
|
79
|
+
@invalid_tests ||=
|
80
|
+
@invalid.collect do |test|
|
81
|
+
BSON::Corpus::Test.new(self, test)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# The class of the bson object to test.
|
86
|
+
#
|
87
|
+
# @example Get the class of the object to test.
|
88
|
+
# spec.klass
|
89
|
+
#
|
90
|
+
# @return [ Class ] The object class.
|
91
|
+
#
|
92
|
+
# @since 4.2.0
|
93
|
+
def klass
|
94
|
+
@klass ||= BSON.const_get(description)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Represents a single BSON Corpus test.
|
99
|
+
#
|
100
|
+
# @since 4.2.0
|
101
|
+
class Test
|
102
|
+
|
103
|
+
# The test description.
|
104
|
+
#
|
105
|
+
# @return [ String ] The test description.
|
106
|
+
#
|
107
|
+
# @since 4.2.0
|
108
|
+
attr_reader :description
|
109
|
+
|
110
|
+
# Name of a field in a valid test case extjson document that should be
|
111
|
+
# checked against the case's string field.
|
112
|
+
#
|
113
|
+
# @return [ String ] The json representation of the object.
|
114
|
+
#
|
115
|
+
# @since 4.2.0
|
116
|
+
attr_reader :test_key
|
117
|
+
|
118
|
+
# Instantiate the new Test.
|
119
|
+
#
|
120
|
+
# @example Create the test.
|
121
|
+
# Test.new(test)
|
122
|
+
#
|
123
|
+
# @param [ Corpus::Spec ] spec The test specification.
|
124
|
+
# @param [ Hash ] test The test specification.
|
125
|
+
#
|
126
|
+
# @since 4.2.0
|
127
|
+
def initialize(spec, test)
|
128
|
+
@spec = spec
|
129
|
+
@description = test['description']
|
130
|
+
@canonical_bson = test['canonical_bson']
|
131
|
+
@extjson = ::JSON.parse(test['extjson']) if test['extjson']
|
132
|
+
@bson = test['bson']
|
133
|
+
@test_key = spec.test_key
|
134
|
+
end
|
135
|
+
|
136
|
+
# The correct representation of the subject as bson.
|
137
|
+
#
|
138
|
+
# @example Get the correct representation of the subject as bson.
|
139
|
+
# test.correct_bson
|
140
|
+
#
|
141
|
+
# @return [ String ] The correct bson bytes.
|
142
|
+
#
|
143
|
+
# @since 4.2.0
|
144
|
+
def correct_bson
|
145
|
+
@correct_bson ||= decode_hex(@canonical_bson || @bson)
|
146
|
+
end
|
147
|
+
|
148
|
+
# Given the hex representation of bson, decode it into a Document,
|
149
|
+
# then reencoded it to bson.
|
150
|
+
#
|
151
|
+
# @example Decoded the bson hex representation, then reencode.
|
152
|
+
# test.reencoded_bson
|
153
|
+
#
|
154
|
+
# @return [ String ] The reencoded bson bytes.
|
155
|
+
#
|
156
|
+
# @since 4.2.0
|
157
|
+
def reencoded_bson
|
158
|
+
bson_bytes = decode_hex(@bson)
|
159
|
+
buffer = BSON::ByteBuffer.new(bson_bytes)
|
160
|
+
BSON::Document.from_bson(buffer).to_bson.to_s
|
161
|
+
end
|
162
|
+
|
163
|
+
# Given the hex representation of the canonical bson, decode it into a Document,
|
164
|
+
# then reencoded it to bson.
|
165
|
+
#
|
166
|
+
# @example Decoded the canonical bson hex representation, then reencode.
|
167
|
+
# test.reencoded_canonical_bson
|
168
|
+
#
|
169
|
+
# @return [ String ] The reencoded canonical bson bytes.
|
170
|
+
#
|
171
|
+
# @since 4.2.0
|
172
|
+
def reencoded_canonical_bson
|
173
|
+
bson_bytes = decode_hex(@canonical_bson)
|
174
|
+
buffer = BSON::ByteBuffer.new(bson_bytes)
|
175
|
+
BSON::Document.from_bson(buffer).to_bson.to_s
|
176
|
+
end
|
177
|
+
|
178
|
+
# Whether the canonical bson should be tested.
|
179
|
+
#
|
180
|
+
# @example Determine if the canonical bson should be tested.
|
181
|
+
# test.test_canonical_bson?
|
182
|
+
#
|
183
|
+
# @return [ true, false ] Whether the canonical bson should be tested.
|
184
|
+
#
|
185
|
+
# @since 4.2.0
|
186
|
+
def test_canonical_bson?
|
187
|
+
@canonical_bson && (@bson != @canonical_bson)
|
188
|
+
end
|
189
|
+
|
190
|
+
# The correct representation of the subject as extended json.
|
191
|
+
#
|
192
|
+
# @example Get the correct representation of the subject as extended json.
|
193
|
+
# test.correct_extjson
|
194
|
+
#
|
195
|
+
# @return [ String ] The correct extended json representation.
|
196
|
+
#
|
197
|
+
# @since 4.2.0
|
198
|
+
def correct_extjson
|
199
|
+
@canonical_extjson || @extjson
|
200
|
+
end
|
201
|
+
|
202
|
+
# Whether the extended json should be tested.
|
203
|
+
#
|
204
|
+
# @example Determine if the extended json should be tested.
|
205
|
+
# test.test_extjson?
|
206
|
+
#
|
207
|
+
# @return [ true, false ] Whether the extended json should be tested.
|
208
|
+
#
|
209
|
+
# @since 4.2.0
|
210
|
+
def test_extjson?
|
211
|
+
!!@extjson
|
212
|
+
end
|
213
|
+
|
214
|
+
# Get the extended json representation of the decoded doc from the provided
|
215
|
+
# bson hex representation.
|
216
|
+
#
|
217
|
+
# @example Get the extended json representation of the decoded doc.
|
218
|
+
# test.extjson_from_encoded_bson
|
219
|
+
#
|
220
|
+
# @return [ Hash ] The extended json representation.
|
221
|
+
#
|
222
|
+
# @since 4.2.0
|
223
|
+
def extjson_from_bson
|
224
|
+
subject = decode_hex(@bson)
|
225
|
+
buffer = BSON::ByteBuffer.new(subject)
|
226
|
+
::JSON.parse(BSON::Document.from_bson(buffer).to_json)
|
227
|
+
end
|
228
|
+
|
229
|
+
# Get the extended json representation of the decoded doc from the provided
|
230
|
+
# canonical bson hex representation.
|
231
|
+
#
|
232
|
+
# @example Get the extended json representation of the canonical decoded doc.
|
233
|
+
# test.extjson_from_canonical_bson
|
234
|
+
#
|
235
|
+
# @return [ Hash ] The extended json representation.
|
236
|
+
#
|
237
|
+
# @since 4.2.0
|
238
|
+
def extjson_from_canonical_bson
|
239
|
+
subject = decode_hex(@canonical_bson)
|
240
|
+
buffer = BSON::ByteBuffer.new(subject)
|
241
|
+
::JSON.parse(BSON::Document.from_bson(buffer).to_json)
|
242
|
+
end
|
243
|
+
|
244
|
+
# Get the extended json representation of the decoded doc from the provided
|
245
|
+
# extended json representation. (Verifies roundtrip)
|
246
|
+
#
|
247
|
+
# @example Get the extended json representation of the canonical decoded doc.
|
248
|
+
# test.extjson_from_encoded_extjson
|
249
|
+
#
|
250
|
+
# @return [ Hash ] The extended json representation.
|
251
|
+
#
|
252
|
+
# @since 4.2.0
|
253
|
+
def extjson_from_encoded_extjson
|
254
|
+
doc = BSON::Document.new(@extjson)
|
255
|
+
::JSON.parse(doc.to_json)
|
256
|
+
end
|
257
|
+
|
258
|
+
private
|
259
|
+
|
260
|
+
def decode_hex(obj)
|
261
|
+
[ obj ].pack('H*')
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|