oct 0.26.0

Octonary transcodings.
Documentation
// Copyright 2024-2025 Gabriel Bjørnager Jensen.
//
// This Source Code Form is subject to the terms of
// the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, you
// can obtain one at:
// <https://mozilla.org/MPL/2.0/>.

//! Tests for [`io::Error`].
//!
//! [`io::Error`]: crate::io::Error

#![cfg(test)]

use oct::io::{Error, ErrorKind};

#[cfg(feature = "alloc")]
use core::fmt::{self, Display, Formatter};

#[cfg(feature = "alloc")]
use alloc::ffi::CString;

#[cfg(feature = "alloc")]
use alloc::string::ToString;

#[cfg(feature = "alloc")]
#[test]
fn test_custom_error() {
	#[derive(Debug)]
	struct SourceError;

	impl Display for SourceError {
		fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
			write!(f, "!")
		}
	}

	impl core::error::Error for SourceError {}

	let e = Error::other(SourceError);

	assert_eq!(e.kind(), ErrorKind::Other);

	let inner = e.into_inner().unwrap();
	inner.downcast::<SourceError>().unwrap();
}

#[cfg(feature = "alloc")]
#[test]
fn test_error_message() {
	let e = CString::new([0]).unwrap_err();

	let e = Error::from(e);

	assert_eq!(
		e.to_string(),
		"invalid data: nul octet found in c-like string",
	);
}

#[test]
fn test_simple_error() {
	let e = Error::from(ErrorKind::Unsupported);

	assert_eq!(e.kind(), ErrorKind::Unsupported);
}