move rust-sciter in

This commit is contained in:
rustdesk
2021-05-23 10:55:19 +08:00
parent bdfb59dd1a
commit 237e889604
76 changed files with 16950 additions and 83 deletions

View File

@@ -0,0 +1,201 @@
#![allow(unused_variables)]
extern crate sciter;
extern crate sciter_serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_bytes;
extern crate serde;
use sciter_serde::{from_value, to_value};
// serialize, deserialize and compare with the original value.
// taken from [serde_bincode](https://github.com/TyOverby/bincode/blob/master/tests/test.rs)
fn the_same<V>(actual: V, expr: &'static str)
where V: serde::Serialize + serde::de::DeserializeOwned + PartialEq + std::fmt::Debug + 'static
{
let sv = to_value(&actual).expect(&format!("to_value({})", expr));
let dv = from_value(&sv).expect(&format!("from_value({})", expr));
let decoded = dv;
assert_eq!(actual, decoded, "the_same({:?})", expr);
}
macro_rules! the_same {
($e:expr) => {
the_same($e, stringify!($e))
}
}
#[test]
fn basic_types() {
the_same!(true);
the_same!(false);
the_same!(7i8);
the_same!(7i16);
the_same!(7i32);
// the_same!(7i64); there are no 64-bit integers in Sciter, only floats.
the_same!(7u8);
the_same!(7u16);
the_same!(7u32);
// the_same!(7u64); ditto
the_same!(7f32);
the_same!(7f64);
the_same!(-7i32);
// the_same!(-7isize);
the_same!(Box::new(7));
}
#[test]
fn strings() {
the_same!("7".to_string());
}
#[test]
fn tuples() {
the_same!( (1,) );
the_same!( (1,2) );
the_same!( (1,2,3) );
the_same!( (1, "7".to_string(), ()) );
}
#[test]
fn structs() {
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
struct Test {
x: bool,
y: i32,
z: String,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
struct Nested {
inner: Test,
payload: Option<String>,
}
let t = Test { x: true, y: 7, z: "42".to_string() };
the_same!(t.clone());
let n = Nested { inner: t.clone(), payload: Some("Some".to_string()) };
the_same!(n.clone());
}
#[test]
fn newtypes() {
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
struct Test(u32);
the_same!(Test(7));
}
#[test]
fn newtuples() {
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
struct Test(u32, bool);
the_same!(Test(7, false));
}
#[test]
fn options() {
the_same!(None::<bool>);
the_same!(Some(true));
the_same!(Some(false));
}
#[test]
fn enums() {
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
enum Test {
Zero,
One(u32),
Two(u32, u32),
Three { x: u32, y: u32, z: u32 },
Five,
}
the_same!(Test::Zero);
// the_same!(Test::One(7));
// the_same!(Test::Two(7, 7));
// the_same!(Test::Three { x: 1, y: 2, z: 3});
the_same!(Test::Five);
}
#[test]
fn arrays() {
let v = [1,2,3];
the_same!(v);
let v = vec![1,2,3];
the_same!(v);
}
#[test]
#[should_panic]
fn unsupported_u64() {
the_same!(7u64);
}
#[test]
#[should_panic]
fn unsupported_i64() {
the_same!(-7i64);
}
#[test]
#[should_panic]
fn unsupported_usize() {
the_same!(7usize);
}
#[test]
fn newtype_variant() {
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
enum Test {
Zero,
One(u32),
Two(u32, u32),
Three { x: u32, y: u32, z: u32 },
}
the_same!(Test::One(7));
}
#[test]
fn tuple_variant() {
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
enum Test {
Zero,
One(u32),
Two(u32, u32),
Three { x: u32, y: u32, z: u32 },
}
the_same!(Test::Two(7, 7));
}
#[test]
fn struct_variant() {
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
enum Test {
Zero,
One(u32),
Two(u32, u32),
Three { x: u32, y: u32, z: u32 },
}
the_same!(Test::Three { x: 1, y: 2, z: 3 });
}

View File

@@ -0,0 +1,94 @@
#![allow(unused_variables)]
extern crate sciter;
extern crate sciter_serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_bytes;
extern crate serde;
use sciter::{Value};
use sciter_serde::{from_value, to_value};
#[test]
fn basic_types() {
// bool
let v: bool = from_value(&Value::from(true)).unwrap();
assert_eq!(v, true);
// integer types
let v: i32 = from_value(&Value::from(0)).unwrap();
assert_eq!(v, 0);
let v: i32 = from_value(&Value::from(7i32)).unwrap();
assert_eq!(v, 7i32);
// float
let v: f32 = from_value(&Value::from(7.0)).unwrap();
assert_eq!(v, 7.0);
let v: f64 = from_value(&Value::from(7.0)).unwrap();
assert_eq!(v, 7.0);
// Option
let v = Value::null();
let v: Option<i32> = from_value(&v).unwrap();
assert_eq!(v, None);
let v = Value::from(7);
let v: Option<i32> = from_value(&v).unwrap();
assert_eq!(v, Some(7));
}
#[test]
fn strings() {
let v: char = from_value(&Value::from("7")).unwrap();
assert_eq!(v, '7');
let v: String = from_value(&Value::from("7")).unwrap();
assert_eq!(v, "7");
let v: serde_bytes::ByteBuf = from_value(&Value::from(b"hello".as_ref())).unwrap();
let v: &[u8] = &v;
assert_eq!(v, b"hello".as_ref());
}
#[test]
fn arrays() {
let it = [1,2,3].iter();
let v: Value = it.cloned().collect();
let v: Vec<i32> = from_value(&v).unwrap();
assert_eq!(v, &[1,2,3]);
}
#[test]
fn structs() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Test {
int: u32,
seq: Vec<String>,
}
println!("");
let a = Test { int: 7, seq: vec!["a".to_owned(), "b".to_owned()]};
let v: Value = to_value(&a).unwrap();
println!("serialized Test:\n {:?}", v);
println!("keys:");
v.keys().inspect(|i| println!(" {:?}", i)).count();
println!("values:");
v.values().inspect(|i| println!(" {:?}", i)).count();
println!("items:");
v.items().iter().inspect(|i| println!(" {:?}", i)).count();
let e: Test = from_value(&v).unwrap();
println!("deserialized Test:\n {:?}", e);
assert_eq!(a, e);
}

View File

@@ -0,0 +1,113 @@
#![allow(unused_variables)]
extern crate sciter;
extern crate sciter_serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_bytes;
extern crate serde;
use sciter::{Value};
use sciter_serde::{to_value};
#[test]
fn basic_types() {
// bool
let v = to_value(&true).unwrap();
assert!(v.is_bool());
assert_eq!(v, Value::from(true));
// integer types
let v = to_value(&0).unwrap();
assert!(v.is_int());
assert_eq!(v.to_int(), Some(0));
let v = to_value(&7u8).unwrap();
assert_eq!(v, Value::from(7));
let v = to_value(&7u16).unwrap();
assert_eq!(v, Value::from(7));
let v = to_value(&7u32).unwrap();
assert_eq!(v, Value::from(7));
let v = to_value(&7i8).unwrap();
assert_eq!(v, Value::from(7));
let v = to_value(&7i16).unwrap();
assert_eq!(v, Value::from(7));
let v = to_value(&7i32).unwrap();
assert_eq!(v, Value::from(7));
let v = to_value(&7.0).unwrap();
assert!(v.is_float());
// 64-bit
// let v = to_value(&7u64).unwrap();
// assert!(v.is_float());
// assert_eq!(v, Value::from(7.0));
// Option
// let v = to_value(&Some(7)).unwrap();
// assert!(v.is_int());
// let v = to_value(&None).unwrap();
// assert!(v.is_null());
}
#[test]
fn strings() {
// strings
let v = to_value(&'h').unwrap();
assert!(v.is_string());
assert_eq!(v, Value::from("h"));
let v = to_value("hello").unwrap();
assert!(v.is_string());
assert_eq!(v, Value::from("hello"));
// doesn't work because Rust doesn't have specialization yet (https://github.com/rust-lang/rust#31844)
// let v = to_value(b"hello").unwrap();
// println!("b'hello': {:?}", v);
// assert!(v.is_bytes());
// assert_eq!(v.as_bytes(), Some(b"hello".as_ref()));
use serde_bytes::Bytes;
let v = to_value(&Bytes::new(b"hello")).unwrap();
assert!(v.is_bytes());
assert_eq!(v.as_bytes(), Some(b"hello".as_ref()));
}
#[test]
fn arrays() {
let a = [1,2,3];
let v = to_value(&a).unwrap();
assert!(v.is_array());
assert_eq!(v.len(), a.len());
let a = vec![1,2,3];
let v = to_value(&a).unwrap();
assert!(v.is_array());
assert_eq!(v.len(), a.len());
}
#[test]
fn structs() {
#[derive(Serialize)]
struct Test {
int: u32,
seq: Vec<&'static str>,
}
let a = Test { int: 7, seq: vec!["a", "b"]};
let v = to_value(&a).unwrap();
assert!(v.is_map());
assert_eq!(v.len(), 2);
assert_eq!(v.get_item("int"), Value::from(7) );
assert_eq!(v.get_item("seq").len(), 2);
}