| 1 |
use crate::{spec::BinarySubtype, Binary, Bson, Document, JavaScriptCodeWithScope, Regex}; |
| 2 |
|
| 3 |
use proptest::prelude::*; |
| 4 |
|
| 5 |
fn arbitrary_binary_subtype() -> impl Strategy<Value = BinarySubtype> { |
| 6 |
prop_oneof![ |
| 7 |
Just(BinarySubtype::Generic), |
| 8 |
Just(BinarySubtype::Function), |
| 9 |
Just(BinarySubtype::BinaryOld), |
| 10 |
Just(BinarySubtype::UuidOld), |
| 11 |
Just(BinarySubtype::Uuid), |
| 12 |
Just(BinarySubtype::Md5), |
| 13 |
] |
| 14 |
} |
| 15 |
|
| 16 |
pub(crate) fn arbitrary_bson() -> impl Strategy<Value = Bson> { |
| 17 |
let leaf = prop_oneof![ |
| 18 |
Just(Bson::Null), |
| 19 |
any::<String>().prop_map(Bson::String), |
| 20 |
any::<bool>().prop_map(Bson::Boolean), |
| 21 |
any::<f64>().prop_map(Bson::Double), |
| 22 |
any::<i32>().prop_map(Bson::Int32), |
| 23 |
any::<i64>().prop_map(Bson::Int64), |
| 24 |
any::<(String, String)>().prop_map(|(pattern, options)| { |
| 25 |
Bson::RegularExpression(Regex::new(pattern, options)) |
| 26 |
}), |
| 27 |
any::<[u8; 12]>().prop_map(|bytes| Bson::ObjectId(crate::oid::ObjectId::from_bytes(bytes))), |
| 28 |
(arbitrary_binary_subtype(), any::<Vec<u8>>()).prop_map(|(subtype, bytes)| { |
| 29 |
let bytes = if let BinarySubtype::BinaryOld = subtype { |
| 30 |
// BinarySubtype::BinaryOld expects a four byte prefix, which the bson::Bson type |
| 31 |
// leaves up to the caller. |
| 32 |
|
| 33 |
let mut newbytes = Vec::with_capacity(bytes.len() + 4); |
| 34 |
newbytes.extend_from_slice(&(bytes.len() as i32).to_le_bytes()); |
| 35 |
newbytes.extend_from_slice(&bytes); |
| 36 |
newbytes |
| 37 |
} else { |
| 38 |
bytes |
| 39 |
}; |
| 40 |
Bson::Binary(Binary { subtype, bytes }) |
| 41 |
}), |
| 42 |
any::<String>().prop_map(Bson::JavaScriptCode), |
| 43 |
]; |
| 44 |
|
| 45 |
leaf.prop_recursive(4, 256, 10, |inner| { |
| 46 |
prop_oneof![ |
| 47 |
prop::collection::hash_map("[^\0]*", inner.clone(), 0..12) |
| 48 |
.prop_map(|map| Bson::Document(map.into_iter().collect())), |
| 49 |
prop::collection::vec(inner.clone(), 0..12).prop_map(Bson::Array), |
| 50 |
( |
| 51 |
prop::collection::hash_map("[^\0]*", inner, 0..12) |
| 52 |
.prop_map(|map| map.into_iter().collect::<Document>()), |
| 53 |
any::<String>() |
| 54 |
) |
| 55 |
.prop_map(|(scope, code)| Bson::JavaScriptCodeWithScope( |
| 56 |
JavaScriptCodeWithScope { code, scope } |
| 57 |
)), |
| 58 |
] |
| 59 |
}) |
| 60 |
} |