feat: EvalState.new_value_str
(cherry picked from commit 94830e4c23ddc742eb7a70cb3e0c4cf17a1a0231)
This commit is contained in:
parent
52778c37e9
commit
cd5fa278eb
1 changed files with 62 additions and 0 deletions
|
|
@ -124,6 +124,19 @@ impl EvalState {
|
|||
Ok(i)
|
||||
}
|
||||
|
||||
/// Create a new value containing the passed string.
|
||||
/// Returns a string value without any string context.
|
||||
pub fn new_value_str(&self, s: &str) -> Result<Value> {
|
||||
let s = CString::new(s).with_context(|| "new_value_str: contains null byte")?;
|
||||
let v = unsafe {
|
||||
let value = self.new_value_uninitialized();
|
||||
raw::init_string(self.context.ptr(), value.raw_ptr(), s.as_ptr());
|
||||
value
|
||||
};
|
||||
self.context.check_err()?;
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
/// Not exposed, because the caller must always explicitly handle the context or not accept one at all.
|
||||
fn get_string(&self, value: &Value) -> Result<String> {
|
||||
let mut r = result_string_init!();
|
||||
|
|
@ -433,6 +446,55 @@ mod tests {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_state_new_string() {
|
||||
gc_registering_current_thread(|| {
|
||||
let store = Store::open("auto").unwrap();
|
||||
let es = EvalState::new(store).unwrap();
|
||||
let v = es.new_value_str("hello").unwrap();
|
||||
es.force(&v).unwrap();
|
||||
let t = es.value_type(&v).unwrap();
|
||||
assert!(t == ValueType::String);
|
||||
let s = es.require_string(&v).unwrap();
|
||||
assert!(s == "hello");
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_state_new_string_empty() {
|
||||
gc_registering_current_thread(|| {
|
||||
let store = Store::open("auto").unwrap();
|
||||
let es = EvalState::new(store).unwrap();
|
||||
let v = es.new_value_str("").unwrap();
|
||||
es.force(&v).unwrap();
|
||||
let t = es.value_type(&v).unwrap();
|
||||
assert!(t == ValueType::String);
|
||||
let s = es.require_string(&v).unwrap();
|
||||
assert!(s == "");
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_state_new_string_invalid() {
|
||||
gc_registering_current_thread(|| {
|
||||
let store = Store::open("auto").unwrap();
|
||||
let es = EvalState::new(store).unwrap();
|
||||
let r = es.new_value_str("hell\0no");
|
||||
match r {
|
||||
Ok(_) => panic!("expected an error"),
|
||||
Err(e) => {
|
||||
if !e.to_string().contains("contains null byte") {
|
||||
eprintln!("{}", e);
|
||||
assert!(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eval_state_value_attrset() {
|
||||
gc_registering_current_thread(|| {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue