Skip to content

Commit

Permalink
Add logging macros tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lrubasze committed Dec 19, 2024
1 parent bb4447c commit 07f86bb
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
20 changes: 20 additions & 0 deletions radix-engine-tests/assets/blueprints/logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,25 @@ mod logger {
pub fn assert_length_5(message: String) {
assert_eq!(message.len(), 5);
}

fn mutate_in_place(input: &mut u8) -> u8 {
*input += 1;
*input
}

// This function tests the logging macros.
// If the respective log level is enabled, the macro arguments will be
// ignored (even if an argument is an expression that mutates data or has side effects).
pub fn mutate_input_if_log_level_enabled(level: Level, number: String) -> u8 {
let mut number = number.parse::<u8>().unwrap();
match level {
Level::Error => error!("Mutated input = {}", Self::mutate_in_place(&mut number)),
Level::Warn => warn!("Mutated input = {}", Self::mutate_in_place(&mut number)),
Level::Info => info!("Mutated input = {}", Self::mutate_in_place(&mut number)),
Level::Debug => debug!("Mutated input = {}", Self::mutate_in_place(&mut number)),
Level::Trace => trace!("Mutated input = {}", Self::mutate_in_place(&mut number)),
}
number
}
}
}
97 changes: 97 additions & 0 deletions radix-engine-tests/tests/vm/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ fn call<S: AsRef<str>>(function_name: &str, message: S) -> TransactionReceipt {
receipt
}

fn call_log_macro<S: AsRef<str>>(
ledger: &mut LedgerSimulator<NoExtension, InMemorySubstateDatabase>,
package_address: PackageAddress,
level: Level,
message: S,
) -> TransactionReceipt {
let manifest = ManifestBuilder::new()
.lock_fee_from_faucet()
.call_function(
package_address,
"Logger",
"mutate_input_if_log_level_enabled",
manifest_args!(level, message.as_ref().to_owned()),
)
.build();
let receipt = ledger.execute_manifest(manifest, vec![]);

receipt
}

#[test]
fn test_emit_log() {
// Arrange
Expand Down Expand Up @@ -110,3 +130,80 @@ fn test_assert_length_5() {
})
}
}

#[test]
fn test_log_macros_enabled() {
// Arrange
let mut ledger = LedgerSimulatorBuilder::new().build();
// PackageLoader compiles with all logs enabled (CompileProfile::FastWithTraceLogs)
let package_address = ledger.publish_package_simple(PackageLoader::get("logger"));

let input = "2";
let output_log = "Mutated input = 3";

for level in [
Level::Error,
Level::Warn,
Level::Info,
Level::Debug,
Level::Trace,
] {
// Act
let receipt = call_log_macro(&mut ledger, package_address, level, input);

// Assert
{
receipt.expect_commit_success();

let logs = receipt.expect_commit(true).application_logs.clone();
let output = receipt.expect_commit(true).output::<u8>(1);

assert_eq!(output, 3);

let expected_logs = vec![(level, output_log.to_owned())];
assert_eq!(logs, expected_logs)
}
}
}

#[test]
fn test_log_macros_disabled() {
use std::path::PathBuf;

// Arrange
let mut ledger = LedgerSimulatorBuilder::new().build();
let manifest_dir = PathBuf::from_str(env!("CARGO_MANIFEST_DIR")).unwrap();
let package_dir = manifest_dir
.join("assets")
.join("blueprints")
.join("logger");
// Disable all logging macros
let package = ledger.compile_with_option(package_dir, CompileProfile::FastWithNoLogs);

let package_address = ledger.publish_package_simple(package);
let input = "2";

for level in [
Level::Error,
Level::Warn,
Level::Info,
Level::Debug,
Level::Trace,
] {
// Act
let receipt = call_log_macro(&mut ledger, package_address, level, input);

// Assert
{
receipt.expect_commit_success();

let logs = receipt.expect_commit(true).application_logs.clone();
let output = receipt.expect_commit(true).output::<u8>(1);

assert_eq!(output, 2);

let expected_logs: Vec<(Level, String)> = vec![];
assert_eq!(logs, expected_logs)
}
}
}

0 comments on commit 07f86bb

Please sign in to comment.