test(codegen): Add test for roundtripping a newtype
extern crate gluon;
#[macro_use]
extern crate gluon_codegen;
-extern crate serde;
-#[macro_use]
-extern crate serde_derive;
#[macro_use]
extern crate gluon_vm;
@@ -13,8 +10,7 @@ use gluon::vm::{self, ExternModule};
use gluon::{import, Compiler, Thread};
use init::new_vm;
-#[derive(Debug, PartialEq, Getable, Pushable, VmType, Serialize, Deserialize)]
-#[gluon(vm_type = "types.Struct")]
+#[derive(Debug, PartialEq, Getable, Pushable, VmType)]
struct Struct {
string: String,
number: u32,
@@ -41,14 +37,6 @@ fn new_struct(_: ()) -> Struct {
fn normal_struct() {
let vm = new_vm();
let mut compiler = Compiler::new();
-
- // must be generated by hand because of bug in make_source (see #542)
- let src = r#"
- type Struct = { string: String, number: Int, vec: Array Float }
- { Struct }
- "#;
-
- compiler.load_script(&vm, "types", &src).unwrap();
import::add_extern_module(&vm, "functions", load_struct_mod);
let script = r#"
@@ -70,3 +58,45 @@ fn normal_struct() {
}
);
}
+
+#[derive(Debug, PartialEq, VmType, Pushable, Getable)]
+struct Newtype(Struct);
+
+fn load_newtype_mod(vm: &Thread) -> vm::Result<ExternModule> {
+ let module = record! {
+ newtype_id => primitive!(1 newtype_id),
+ };
+
+ ExternModule::new(vm, module)
+}
+
+fn newtype_id(val: Newtype) -> Newtype {
+ val
+}
+
+#[test]
+fn newtype() {
+ let vm = new_vm();
+ let mut compiler = Compiler::new();
+ import::add_extern_module(&vm, "functions", load_newtype_mod);
+
+ // newtypes should map to the inner type
+ let script = r#"
+ let { newtype_id } = import! functions
+
+ newtype_id { string = "test", number = 42, vec = [1.0, 1.0, 2.0, 3.0, 5.0] }
+ "#;
+
+ let (s, _) = compiler
+ .run_expr::<Newtype>(&vm, "test", script)
+ .unwrap_or_else(|why| panic!("{}", why));
+
+ assert_eq!(
+ s,
+ Newtype(Struct {
+ string: "test".into(),
+ number: 42,
+ vec: vec![1.0, 1.0, 2.0, 3.0, 5.0],
+ })
+ );
+}
