mod common;
use apollo_errors::{CodeCase, Error as ErrorTrait, FieldCase, FormatConfig};
use common::{ErrorWithFields, ErrorWithRenamedField, SimpleError};
use insta::assert_snapshot;
#[test]
fn test_simple_error_text() {
let error = SimpleError::Simple;
let text = error.to_text(FormatConfig::default());
assert_snapshot!(text, @"[errors::simple] Something went wrong");
}
#[test]
fn test_another_error_text() {
let error = SimpleError::Another;
let text = error.to_text(FormatConfig::default());
assert_snapshot!(text, @"[errors::another] Another error occurred");
}
#[test]
fn test_error_with_fields_text() {
let error = ErrorWithFields::InvalidPort {
port: 8080,
config_file: "/etc/config.toml".to_string(),
};
let text = error.to_text(FormatConfig::default());
assert_snapshot!(text, @"[config::invalid_port] Invalid port");
}
#[test]
fn test_text_with_interpolated_message() {
use common::ConfigStructError;
let error = ConfigStructError {
port: 8080,
config_path: "/etc/config.toml".to_string(),
};
let text = error.to_text(FormatConfig::default());
assert_snapshot!(text, @"[structs::config_error] Configuration error: invalid port 8080");
}
#[test]
fn test_kebab_field_case_screaming_snake_code_case_text() {
let error = ErrorWithFields::InvalidPort {
port: 8080,
config_file: "/etc/config.toml".to_string(),
};
let config = FormatConfig {
field_case: FieldCase::KebabCase,
code_case: CodeCase::ScreamingSnakeCase,
};
let text = error.to_text(config);
assert_snapshot!(text, @"[CONFIG_INVALID_PORT] Invalid port");
}
#[test]
fn test_pascal_field_case_default_code_case_text() {
let error = ErrorWithFields::InvalidPort {
port: 8080,
config_file: "/etc/config.toml".to_string(),
};
let config = FormatConfig {
field_case: FieldCase::PascalCase,
..FormatConfig::default()
};
let text = error.to_text(config);
assert_snapshot!(text, @"[config::invalid_port] Invalid port");
}
#[test]
fn test_default_field_case_screaming_snake_code_case_text() {
let error = ErrorWithFields::InvalidPort {
port: 8080,
config_file: "/etc/config.toml".to_string(),
};
let config = FormatConfig {
code_case: CodeCase::ScreamingSnakeCase,
..FormatConfig::default()
};
let text = error.to_text(config);
assert_snapshot!(text, @"[CONFIG_INVALID_PORT] Invalid port");
}
#[test]
fn test_renamed_field_pascal_case_screaming_snake_code_text() {
let error = ErrorWithRenamedField::WithRename {
my_field: "value".to_string(),
};
let config = FormatConfig {
field_case: FieldCase::PascalCase,
code_case: CodeCase::ScreamingSnakeCase,
};
let text = error.to_text(config);
assert_snapshot!(text, @"[TEST_RENAMED_FIELD] Field was renamed");
}