Files
gitks/tests/oid_test.rs
T

87 lines
2.1 KiB
Rust

//! Copyright (c) 2022-2026 GitDataAi All rights reserved.
use gitks::error::GitError;
use gitks::oid::{ObjectId, ZERO_OID, hex_to_bytes};
#[test]
fn test_hex_to_bytes_valid() {
let bytes = hex_to_bytes("abcdef0123456789").unwrap();
assert_eq!(bytes, vec![0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89]);
}
#[test]
fn test_hex_to_bytes_sha1() {
// 40-char SHA-1 hex
let hex = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
let bytes = hex_to_bytes(hex).unwrap();
assert_eq!(bytes.len(), 20);
}
#[test]
fn test_hex_to_bytes_odd_length() {
let result = hex_to_bytes("abc");
assert!(result.is_err());
match result.unwrap_err() {
GitError::InvalidOid(_) => {}
other => panic!("expected InvalidOid, got: {:?}", other),
}
}
#[test]
fn test_hex_to_bytes_invalid_hex() {
let result = hex_to_bytes("zzzz");
assert!(result.is_err());
}
#[test]
fn test_hex_to_bytes_non_ascii_does_not_panic() {
let result = hex_to_bytes("aéx");
assert!(result.is_err());
}
#[test]
fn test_hex_to_bytes_with_whitespace() {
let bytes = hex_to_bytes(" abcd ").unwrap();
assert_eq!(bytes, vec![0xab, 0xcd]);
}
#[test]
fn test_object_id_new_lowercase() {
let id = ObjectId::new("ABCDEF1234");
assert_eq!(id.as_str(), "abcdef1234");
}
#[test]
fn test_object_id_display() {
let id = ObjectId::new("abcdef");
assert_eq!(format!("{}", id), "abcdef");
}
#[test]
fn test_object_id_as_ref() {
let id = ObjectId::new("123456");
let s: &str = id.as_ref();
assert_eq!(s, "123456");
}
#[test]
fn test_object_id_try_into_gix() {
let hex = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
let id = ObjectId::new(hex);
let gix_id: gix::hash::ObjectId = (&id).try_into().unwrap();
assert_eq!(gix_id.to_string(), hex);
}
#[test]
fn test_object_id_try_into_invalid() {
let id = ObjectId::new("not_a_valid_hex_oid_at_all_way_too_long_for_any_hash");
let result: Result<gix::hash::ObjectId, _> = (&id).try_into();
assert!(result.is_err());
}
#[test]
fn test_zero_oid() {
assert_eq!(ZERO_OID.len(), 40);
assert!(ZERO_OID.chars().all(|c| c == '0'));
}