Fix encode/decode todo's (#13683)

Mistakes have been made. I forgot about a bunch of `todo`s in the helper
functions. So, this PR replaces them with proper errors. It also adds
tests for parse-time evaluation, because one `todo` I missed was in a
`run_const` function.
This commit is contained in:
Andrej Kolčin
2024-08-24 17:02:02 +03:00
committed by GitHub
parent 525eac1afd
commit 3f332bef35
7 changed files with 76 additions and 65 deletions

View File

@ -2,13 +2,15 @@ use data_encoding::HEXUPPER;
use rand::prelude::*;
use rand_chacha::ChaCha8Rng;
use nu_test_support::nu;
mod base32;
mod base32hex;
mod base64;
mod hex;
/// Generate a few random binaries.
pub fn random_bytes() -> Vec<String> {
fn random_bytes() -> Vec<String> {
const NUM: usize = 32;
let mut rng = ChaCha8Rng::seed_from_u64(4);
@ -17,8 +19,26 @@ pub fn random_bytes() -> Vec<String> {
let length = rng.gen_range(0..512);
let mut bytes = vec![0u8; length];
rng.fill_bytes(&mut bytes);
let hex_bytes = HEXUPPER.encode(&bytes);
format!("0x[{}]", hex_bytes)
HEXUPPER.encode(&bytes)
})
.collect()
}
pub fn test_canonical(cmd: &str) {
for value in random_bytes() {
let outcome = nu!("0x[{}] | encode {1} | decode {1} | to nuon", value, cmd);
let nuon_value = format!("0x[{value}]");
assert_eq!(outcome.out, nuon_value);
}
}
pub fn test_const(cmd: &str) {
for value in random_bytes() {
let outcome = nu!(
r#"const out = (0x[{}] | encode {1} | decode {1} | encode hex); $out"#,
value,
cmd
);
assert_eq!(outcome.out, value);
}
}