From 040f10e19dffaf6b0e8960177c3ae43d8e192615 Mon Sep 17 00:00:00 2001 From: Jack Wright <56345+ayax79@users.noreply.github.com> Date: Sat, 13 Apr 2024 11:03:44 -0700 Subject: [PATCH] Added a PluginTest method that will call custom_value_to_base_value (#12502) # Description Added a method for getting the base value for a PluginCustomValue. cc: @devyn --------- Co-authored-by: Jack Wright --- crates/nu-plugin-test-support/src/plugin_test.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/nu-plugin-test-support/src/plugin_test.rs b/crates/nu-plugin-test-support/src/plugin_test.rs index 18872af44d..3f9326a33b 100644 --- a/crates/nu-plugin-test-support/src/plugin_test.rs +++ b/crates/nu-plugin-test-support/src/plugin_test.rs @@ -8,7 +8,8 @@ use nu_plugin::{Plugin, PluginCommand, PluginCustomValue, PluginSource}; use nu_protocol::{ debugger::WithoutDebug, engine::{EngineState, Stack, StateWorkingSet}, - report_error_new, Example, LabeledError, PipelineData, ShellError, Span, Value, + report_error_new, CustomValue, Example, IntoSpanned as _, LabeledError, PipelineData, + ShellError, Span, Value, }; use crate::{diff::diff_by_line, fake_register::fake_register}; @@ -345,4 +346,17 @@ impl PluginTest { _ => Ok(a == b), } } + + /// This implements custom value comparison with `plugin.custom_value_to_base_value()` to behave + /// as similarly as possible to comparison in the engine. + pub fn custom_value_to_base_value( + &self, + val: &dyn CustomValue, + span: Span, + ) -> Result { + let mut serialized = PluginCustomValue::serialize_from_custom_value(val, span)?; + serialized.set_source(Some(self.source.clone())); + let persistent = self.source.persistent(None)?.get_plugin(None)?; + persistent.custom_value_to_base_value(serialized.into_spanned(span)) + } }