rhai/tests/comments.rs

100 lines
1.9 KiB
Rust
Raw Normal View History

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