Skip to content

Commit

Permalink
feat(qmetaobject): add basic "is" wrappers for QJSValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Zolotukhin authored and ogoffart committed Jan 6, 2022
1 parent e912556 commit 7f8c6d6
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions qmetaobject/src/qtdeclarative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,24 @@ cpp_class!(
);

impl QJSValue {
pub fn is_bool(&self) -> bool {
cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" {
return self->isBool();
})
}

pub fn is_number(&self) -> bool {
cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" {
return self->isNumber();
})
}

pub fn is_string(&self) -> bool {
cpp!(unsafe [self as "const QJSValue *"] -> bool as "bool" {
return self->isString();
})
}

pub fn to_string(&self) -> QString {
cpp!(unsafe [self as "const QJSValue *"] -> QString as "QString" {
return self->toString();
Expand Down Expand Up @@ -1026,6 +1044,33 @@ mod qjsvalue_tests {
assert_eq!(foo.to_variant().to_qbytearray(), "45".into());
}

#[test]
fn test_is_bool() {
let bool_value = QJSValue::from(true);
let num_value = QJSValue::from(42);

assert!(bool_value.is_bool());
assert!(!num_value.is_bool());
}

#[test]
fn test_is_number() {
let string_value = QJSValue::from(QString::from("Konqui"));
let num_value = QJSValue::from(42);

assert!(num_value.is_number());
assert!(!string_value.is_number());
}

#[test]
fn test_is_string() {
let string_value = QJSValue::from(QString::from("Konqui"));
let num_value = QJSValue::from(42);

assert!(string_value.is_string());
assert!(!num_value.is_string());
}

#[test]
fn test_qvariantlist_from_iter() {
let v = vec![1u32, 2u32, 3u32];
Expand Down

0 comments on commit 7f8c6d6

Please sign in to comment.