Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / third_party / betocore / test_helper / src / lib.rs
lib.rs79 lines · 2.8 KiBRaw
| 1 | // Copyright 2022 Google LLC |
| 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 | //! Helper crate for common functions used in testing |
| 16 | |
| 17 | #![allow(clippy::unwrap_used, clippy::expect_used)] |
| 18 | |
| 19 | use itertools::Itertools; |
| 20 | use std::fs; |
| 21 | use std::io::Read; |
| 22 | |
| 23 | /// Returns data file path for specific build system. Input is the path to the file relative to the |
| 24 | /// workspace root dir |
| 25 | pub fn get_data_file(file: &str) -> std::path::PathBuf { |
| 26 | let mut full_path = std::path::PathBuf::from(env!("WORKSPACE_DIR")); |
| 27 | full_path.push(file); |
| 28 | full_path |
| 29 | } |
| 30 | |
| 31 | /// Opens a file at the specified path (relative to the workspace root) |
| 32 | /// and yields its contents as a string |
| 33 | pub fn load_data_file_contents_as_string(file: &str) -> String { |
| 34 | let full_path = get_data_file(file); |
| 35 | let mut file = fs::File::open(full_path).expect("Should be able to open data file"); |
| 36 | let mut data = String::new(); |
| 37 | let _ = file.read_to_string(&mut data).expect("should be able to read data file"); |
| 38 | data |
| 39 | } |
| 40 | |
| 41 | /// Opens a json file at the specified path and parses it into a value |
| 42 | pub fn parse_json_data_file(file: &str) -> serde_json::Value { |
| 43 | let data = load_data_file_contents_as_string(file); |
| 44 | serde_json::de::from_str(data.as_str()).expect("should be able to parse json date file") |
| 45 | } |
| 46 | |
| 47 | /// extract a string from a jsonvalue |
| 48 | pub fn extract_key_str<'a>(value: &'a serde_json::Value, key: &str) -> &'a str { |
| 49 | value.get(key).unwrap().as_str().unwrap() |
| 50 | } |
| 51 | |
| 52 | /// Decode a hex-encoded vec at `key` |
| 53 | pub fn extract_key_vec(value: &serde_json::Value, key: &str) -> Vec<u8> { |
| 54 | hex::decode(value.get(key).unwrap().as_str().unwrap()).unwrap() |
| 55 | } |
| 56 | |
| 57 | /// Decode a hex-encoded array at `key` |
| 58 | pub fn extract_key_array<const N: usize>(value: &serde_json::Value, key: &str) -> [u8; N] { |
| 59 | extract_key_vec(value, key).try_into().unwrap() |
| 60 | } |
| 61 | |
| 62 | /// Convert a hex string to a Vec of the hex bytes |
| 63 | pub fn string_to_hex(str: &str) -> Vec<u8> { |
| 64 | hex::decode(str).unwrap() |
| 65 | } |
| 66 | |
| 67 | /// Format data as comma separated hex bytes for the convenience of test data in FFI tests. |
| 68 | /// |
| 69 | /// # Examples |
| 70 | /// |
| 71 | /// ``` |
| 72 | /// use test_helper::hex_bytes; |
| 73 | /// |
| 74 | /// assert_eq!("0x12, 0x34", hex_bytes(&[0x12, 0x34])); |
| 75 | /// ``` |
| 76 | #[cfg(feature = "std")] |
| 77 | pub fn hex_bytes(data: impl AsRef<[u8]>) -> String { |
| 78 | hex::encode_upper(data).chars().tuples().map(|(a, b)| format!("0x{a}{b}")).join(", ") |
| 79 | } |