1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// use std::sync::Mutex;
// use crate::mlua::Variadic;
// use bevy::prelude::*;
// use bevy_mod_scripting;
// use nano9::{api::N9Args, *};
// #[derive(Resource, Default)]
// pub struct Failed(Option<String>);
// pub struct TestAPI;
// impl APIProvider for TestAPI {
// type APITarget = Mutex<Lua>;
// type ScriptContext = Mutex<Lua>;
// type DocTarget = LuaDocFragment;
// fn attach_api(&mut self, ctx: &mut Self::APITarget) -> Result<(), ScriptError> {
// let ctx = ctx.get_mut().unwrap();
// ctx.globals()
// .set(
// "fail",
// ctx.create_function(|ctx, msg: Option<String>| {
// let world = ctx.get_world()?;
// let mut world = world.write();
// world.insert_resource(Failed(msg));
// Ok(())
// })
// .map_err(ScriptError::new_other)?,
// )
// .map_err(ScriptError::new_other)?;
// Ok(())
// }
// }
// fn new_app() -> App {
// let mut app = App::new();
// app.add_plugins(MinimalPlugins)
// .add_plugins(bevy::state::app::StatesPlugin)
// .add_plugins(bevy::asset::AssetPlugin::default())
// .add_plugins(bevy::render::prelude::ImagePlugin::default())
// .add_plugins(Nano9Plugin::default())
// .add_api_provider::<LuaScriptHost<N9Args>>(Box::new(TestAPI));
// app
// }
// fn run_lua_test(script: impl Into<String>) {
// let mut app = new_app();
// let script = script.into();
// app.add_systems(Update, move |world: &mut World| {
// let entity = world.spawn(()).id();
// // run script
// world.resource_scope(|world, mut host: Mut<LuaScriptHost<N9Args>>| {
// if let Err(e) = host.run_one_shot(
// script.as_bytes(),
// "script.lua",
// entity,
// world,
// LuaEvent {
// hook_name: "once".to_owned(),
// args: Variadic::new(),
// recipients: Recipients::All,
// },
// ) {
// panic!("{}", e);
// }
// // .expect("Something went wrong in the script!");
// });
// });
// app.update();
// if let Some(events) = app.world().get_resource::<Events<ScriptErrorEvent>>() {
// let mut reader = events.get_reader();
// for r in reader.read(events) {
// assert!(false, "{}", r.error);
// }
// }
// if let Some(failed) = app.world().get_resource::<Failed>() {
// if let Some(msg) = &failed.0 {
// assert!(false, "{}", msg);
// } else {
// assert!(false);
// }
// }
// }
// #[cfg(test)]
// mod test {
// use super::*;
// #[test]
// #[ignore]
// fn change_camera_position() {
// run_lua_test(
// r#"
// function once()
// camera.x = 1
// if camera.x ~= 1 then
// fail("camera x not set.");
// end
// end
// "#,
// );
// }
// #[test]
// #[ignore]
// fn default_camera_position() {
// run_lua_test(
// r#"
// function once()
// --notthere.y
// if camera.x ~= 64 then
// fail("camera set to "..camera.x);
// --fail("camera set to ");
// end
// end
// "#,
// );
// }
// #[test]
// #[ignore]
// fn render_text() {
// run_lua_test(
// r#"
// function once()
// text:print("Hello, World!")
// end
// "#,
// );
// }
// // #[test]
// // fn test_fail() {
// // run_lua_test(
// // r#"
// // function once()
// // fail("what")
// // end
// // "#);
// // }
// }