rhai/tests/comments.rs

100 lines
1.9 KiB
Rust
Raw Normal View History

2020-11-14 23:43:36 +08:00
use rhai::{Engine, EvalAltResult, INT};
2017-11-03 09:58:51 -07:00
#[test]
2020-11-14 23:43:36 +08:00
fn test_comments() -> Result<(), Box<EvalAltResult>> {
let engine = Engine::new();
2017-11-03 09:58:51 -07:00
2020-11-14 23:43:36 +08:00
assert_eq!(
engine.eval::<INT>("let x = 42; x // I am a single line comment, yay!")?,
42
);
2017-11-03 09:58:51 -07:00
2020-11-14 23:43:36 +08:00
assert_eq!(
engine.eval::<INT>(
2021-06-05 15:26:43 +08:00
"
let /* I am a
multi-line
comment, yay!
*/ x = 42; x
2021-06-05 15:26:43 +08:00
"
2020-11-14 23:43:36 +08:00
)?,
42
);
2023-02-13 08:59:58 +08:00
engine.run("/* Hello world */")?;
2020-12-18 16:07:19 +08:00
Ok(())
}
#[cfg(not(feature = "no_function"))]
2021-04-09 23:13:33 +08:00
#[cfg(feature = "metadata")]
2020-12-18 16:07:19 +08:00
#[test]
fn test_comments_doc() -> Result<(), Box<EvalAltResult>> {
2021-05-03 13:07:51 +08:00
let engine = Engine::new();
2020-12-18 16:07:19 +08:00
let ast = engine.compile(
2021-04-20 12:01:35 +08:00
"
2020-12-18 16:07:19 +08:00
/// Hello world
fn foo() {}
",
)?;
assert_eq!(
ast.iter_functions().next().unwrap().comments[0],
"/// Hello world"
);
assert!(engine
.compile(
2021-04-20 12:01:35 +08:00
"
2020-12-18 16:07:19 +08:00
/// Hello world
let x = 42;
"
)
.is_err());
engine.compile(
2021-04-20 12:01:35 +08:00
"
///////////////
let x = 42;
/***************/
let x = 42;
",
)?;
2020-12-18 16:07:19 +08:00
let ast = engine.compile(
2021-04-20 12:01:35 +08:00
"
2020-12-18 16:07:19 +08:00
/** Hello world
** how are you?
**/
fn foo() {}
",
)?;
2022-12-03 10:50:58 +08:00
#[cfg(not(feature = "no_position"))]
2020-12-18 16:07:19 +08:00
assert_eq!(
ast.iter_functions().next().unwrap().comments[0],
2022-12-02 17:09:48 +08:00
"/** Hello world\n** how are you?\n**/"
2020-12-18 16:07:19 +08:00
);
2022-12-03 10:50:58 +08:00
#[cfg(feature = "no_position")]
assert_eq!(
ast.iter_functions().next().unwrap().comments[0],
"/** Hello world\n ** how are you?\n **/",
);
2020-12-18 16:07:19 +08:00
assert!(engine
.compile(
2021-04-20 12:01:35 +08:00
"
2020-12-18 16:07:19 +08:00
/** Hello world */
let x = 42;
"
)
.is_err());
2020-11-14 23:43:36 +08:00
Ok(())
2017-11-03 09:58:51 -07:00
}