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 <jack.wright@disqo.com>
This commit is contained in:
Jack Wright 2024-04-13 11:03:44 -07:00 committed by GitHub
parent 10a9a17b8c
commit 040f10e19d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<Value, ShellError> {
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))
}
}