Skip to content

Commit 22af165

Browse files
committed
chore(stdlib): Make zlib and gzip tests backend independent
1 parent 87cfb29 commit 22af165

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

src/stdlib/encode_gzip.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,35 +115,32 @@ impl FunctionExpression for EncodeGzipFn {
115115

116116
#[cfg(test)]
117117
mod test {
118-
use base64::Engine;
119-
120118
use crate::value;
121119

122120
use super::*;
123121

124-
fn decode_base64(text: &str) -> Vec<u8> {
125-
let engine = base64::engine::GeneralPurpose::new(
126-
&base64::alphabet::STANDARD,
127-
base64::engine::general_purpose::GeneralPurposeConfig::new(),
128-
);
129-
130-
engine.decode(text).expect("Cannot decode from Base64")
122+
fn encode(text: &str, level: flate2::Compression) -> Vec<u8> {
123+
let mut encoder = GzEncoder::new(text.as_bytes(), level);
124+
let mut output = vec![];
125+
encoder.read_to_end(&mut output).unwrap();
126+
output
131127
}
132128

133129
test_function![
134130
encode_gzip => EncodeGzip;
135131

136132
with_defaults {
137133
args: func_args![value: value!("you_have_successfully_decoded_me.congratulations.you_are_breathtaking.")],
138-
want: Ok(value!(decode_base64("H4sIAAAAAAAA/w3LgQ3AIAgEwI1ciVD8qqmVRKAJ29cBLjWo8weyEIHZHXMmVYhWVHpRRFfb7DHZhy4reQBv0LXB3p2fsVr5AXeBkepGAAAA").as_bytes())),
134+
want: Ok(value!(encode("you_have_successfully_decoded_me.congratulations.you_are_breathtaking.", flate2::Compression::default()).as_bytes())),
139135
tdef: TypeDef::bytes().infallible(),
140136
}
141137

142138
with_custom_compression_level {
143139
args: func_args![value: value!("you_have_successfully_decoded_me.congratulations.you_are_breathtaking."), compression_level: 9],
144-
want: Ok(value!(decode_base64("H4sIAAAAAAAC/w3LgQ3AIAgEwI1ciVD8qqmVRKAJ29cBLjWo8weyEIHZHXMmVYhWVHpRRFfb7DHZhy4reQBv0LXB3p2fsVr5AXeBkepGAAAA").as_bytes())),
140+
want: Ok(value!(encode("you_have_successfully_decoded_me.congratulations.you_are_breathtaking.", flate2::Compression::new(9)).as_bytes())),
145141
tdef: TypeDef::bytes().infallible(),
146142
}
143+
147144
invalid_constant_compression {
148145
args: func_args![value: value!("test"), compression_level: 11],
149146
want: Err("compression level must be <= 10"),

src/stdlib/encode_zlib.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,33 +116,29 @@ impl FunctionExpression for EncodeZlibFn {
116116

117117
#[cfg(test)]
118118
mod test {
119-
use base64::Engine;
120-
121119
use crate::value;
122120

123121
use super::*;
124122

125-
fn decode_base64(text: &str) -> Vec<u8> {
126-
let engine = base64::engine::GeneralPurpose::new(
127-
&base64::alphabet::STANDARD,
128-
base64::engine::general_purpose::GeneralPurposeConfig::new(),
129-
);
130-
131-
engine.decode(text).expect("Cannot decode from Base64")
123+
fn encode(text: &str, level: flate2::Compression) -> Vec<u8> {
124+
let mut encoder = ZlibEncoder::new(text.as_bytes(), level);
125+
let mut output = vec![];
126+
encoder.read_to_end(&mut output).unwrap();
127+
output
132128
}
133129

134130
test_function![
135131
encode_zlib => EncodeZlib;
136132

137133
with_defaults {
138134
args: func_args![value: value!("you_have_successfully_decoded_me.congratulations.you_are_breathtaking.")],
139-
want: Ok(value!(decode_base64("eJwNy4ENwCAIBMCNXIlQ/KqplUSgCdvXAS41qPMHshCB2R1zJlWIVlR6UURX2+wx2YcuK3kAb9C1wd6dn7Fa+QH9gRxr").as_bytes())),
135+
want: Ok(value!(encode("you_have_successfully_decoded_me.congratulations.you_are_breathtaking.", flate2::Compression::default()).as_bytes())),
140136
tdef: TypeDef::bytes().infallible(),
141137
}
142138

143139
with_custom_compression_level {
144140
args: func_args![value: value!("you_have_successfully_decoded_me.congratulations.you_are_breathtaking."), compression_level: 9],
145-
want: Ok(value!(decode_base64("eNoNy4ENwCAIBMCNXIlQ/KqplUSgCdvXAS41qPMHshCB2R1zJlWIVlR6UURX2+wx2YcuK3kAb9C1wd6dn7Fa+QH9gRxr").as_bytes())),
141+
want: Ok(value!(encode("you_have_successfully_decoded_me.congratulations.you_are_breathtaking.", flate2::Compression::new(9)).as_bytes())),
146142
tdef: TypeDef::bytes().infallible(),
147143
}
148144

0 commit comments

Comments
 (0)