mirror of
https://github.com/denoland/deno.git
synced 2025-01-11 08:33:43 -05:00
chore: make all tests annotated with #[cfg(test)]
(#9347)
This commit is contained in:
parent
23281be33a
commit
84f8b87f1b
6 changed files with 139 additions and 109 deletions
39
cli/diff.rs
39
cli/diff.rs
|
@ -146,22 +146,27 @@ pub fn diff(orig_text: &str, edit_text: &str) -> String {
|
|||
diff
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_diff() {
|
||||
let simple_console_log_unfmt = "console.log('Hello World')";
|
||||
let simple_console_log_fmt = "console.log(\"Hello World\");";
|
||||
assert_eq!(
|
||||
colors::strip_ansi_codes(&diff(
|
||||
simple_console_log_unfmt,
|
||||
simple_console_log_fmt
|
||||
)),
|
||||
"1 | -console.log('Hello World')\n1 | +console.log(\"Hello World\");\n"
|
||||
);
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
let line_number_unfmt = "\n\n\n\nconsole.log(\n'Hello World'\n)";
|
||||
let line_number_fmt = "console.log(\n\"Hello World\"\n);";
|
||||
assert_eq!(
|
||||
colors::strip_ansi_codes(&diff(line_number_unfmt, line_number_fmt)),
|
||||
"1 | -\n2 | -\n3 | -\n4 | -\n5 | -console.log(\n1 | +console.log(\n6 | -'Hello World'\n2 | +\"Hello World\"\n7 | -)\n3 | +);\n"
|
||||
)
|
||||
#[test]
|
||||
fn test_diff() {
|
||||
let simple_console_log_unfmt = "console.log('Hello World')";
|
||||
let simple_console_log_fmt = "console.log(\"Hello World\");";
|
||||
assert_eq!(
|
||||
colors::strip_ansi_codes(&diff(
|
||||
simple_console_log_unfmt,
|
||||
simple_console_log_fmt
|
||||
)),
|
||||
"1 | -console.log('Hello World')\n1 | +console.log(\"Hello World\");\n"
|
||||
);
|
||||
|
||||
let line_number_unfmt = "\n\n\n\nconsole.log(\n'Hello World'\n)";
|
||||
let line_number_fmt = "console.log(\n\"Hello World\"\n);";
|
||||
assert_eq!(
|
||||
colors::strip_ansi_codes(&diff(line_number_unfmt, line_number_fmt)),
|
||||
"1 | -\n2 | -\n3 | -\n4 | -\n5 | -console.log(\n1 | +console.log(\n6 | -'Hello World'\n2 | +\"Hello World\"\n7 | -)\n3 | +);\n"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -351,8 +351,13 @@ fn source_map_from_code(code: String) -> Option<Vec<u8>> {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thread_safe() {
|
||||
fn f<S: Send + Sync>(_: S) {}
|
||||
f(ProgramState::mock(vec![], None));
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn thread_safe() {
|
||||
fn f<S: Send + Sync>(_: S) {}
|
||||
f(ProgramState::mock(vec![], None));
|
||||
}
|
||||
}
|
||||
|
|
11
core/lib.rs
11
core/lib.rs
|
@ -78,7 +78,12 @@ pub fn v8_version() -> &'static str {
|
|||
v8::V8::get_version()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_v8_version() {
|
||||
assert!(v8_version().len() > 3);
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_v8_version() {
|
||||
assert!(v8_version().len() > 3);
|
||||
}
|
||||
}
|
||||
|
|
79
core/ops.rs
79
core/ops.rs
|
@ -114,43 +114,6 @@ impl Default for OpTable {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn op_table() {
|
||||
let state = Rc::new(RefCell::new(OpState::new()));
|
||||
|
||||
let foo_id;
|
||||
let bar_id;
|
||||
{
|
||||
let op_table = &mut state.borrow_mut().op_table;
|
||||
foo_id = op_table.register_op("foo", |_, _| Op::Sync(b"oof!"[..].into()));
|
||||
assert_eq!(foo_id, 1);
|
||||
bar_id = op_table.register_op("bar", |_, _| Op::Sync(b"rab!"[..].into()));
|
||||
assert_eq!(bar_id, 2);
|
||||
}
|
||||
|
||||
let foo_res = OpTable::route_op(foo_id, state.clone(), Default::default());
|
||||
assert!(matches!(foo_res, Op::Sync(buf) if &*buf == b"oof!"));
|
||||
let bar_res = OpTable::route_op(bar_id, state.clone(), Default::default());
|
||||
assert!(matches!(bar_res, Op::Sync(buf) if &*buf == b"rab!"));
|
||||
|
||||
let catalog_res = OpTable::route_op(0, state, Default::default());
|
||||
let mut catalog_entries = match catalog_res {
|
||||
Op::Sync(buf) => serde_json::from_slice::<HashMap<String, OpId>>(&buf)
|
||||
.map(|map| map.into_iter().collect::<Vec<_>>())
|
||||
.unwrap(),
|
||||
_ => panic!("unexpected `Op` variant"),
|
||||
};
|
||||
catalog_entries.sort_by(|(_, id1), (_, id2)| id1.partial_cmp(id2).unwrap());
|
||||
assert_eq!(
|
||||
catalog_entries,
|
||||
vec![
|
||||
("ops".to_owned(), 0),
|
||||
("foo".to_owned(), 1),
|
||||
("bar".to_owned(), 2)
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
/// Creates an op that passes data synchronously using JSON.
|
||||
///
|
||||
/// The provided function `op_fn` has the following parameters:
|
||||
|
@ -304,3 +267,45 @@ pub fn op_close(
|
|||
|
||||
Ok(json!({}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn op_table() {
|
||||
let state = Rc::new(RefCell::new(OpState::new()));
|
||||
|
||||
let foo_id;
|
||||
let bar_id;
|
||||
{
|
||||
let op_table = &mut state.borrow_mut().op_table;
|
||||
foo_id = op_table.register_op("foo", |_, _| Op::Sync(b"oof!"[..].into()));
|
||||
assert_eq!(foo_id, 1);
|
||||
bar_id = op_table.register_op("bar", |_, _| Op::Sync(b"rab!"[..].into()));
|
||||
assert_eq!(bar_id, 2);
|
||||
}
|
||||
|
||||
let foo_res = OpTable::route_op(foo_id, state.clone(), Default::default());
|
||||
assert!(matches!(foo_res, Op::Sync(buf) if &*buf == b"oof!"));
|
||||
let bar_res = OpTable::route_op(bar_id, state.clone(), Default::default());
|
||||
assert!(matches!(bar_res, Op::Sync(buf) if &*buf == b"rab!"));
|
||||
|
||||
let catalog_res = OpTable::route_op(0, state, Default::default());
|
||||
let mut catalog_entries = match catalog_res {
|
||||
Op::Sync(buf) => serde_json::from_slice::<HashMap<String, OpId>>(&buf)
|
||||
.map(|map| map.into_iter().collect::<Vec<_>>())
|
||||
.unwrap(),
|
||||
_ => panic!("unexpected `Op` variant"),
|
||||
};
|
||||
catalog_entries.sort_by(|(_, id1), (_, id2)| id1.partial_cmp(id2).unwrap());
|
||||
assert_eq!(
|
||||
catalog_entries,
|
||||
vec![
|
||||
("ops".to_owned(), 0),
|
||||
("foo".to_owned(), 1),
|
||||
("bar".to_owned(), 2)
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,21 +11,26 @@ pub fn deno_isolate_init() -> Snapshot {
|
|||
Snapshot::Static(data)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cli_snapshot() {
|
||||
let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
|
||||
startup_snapshot: Some(deno_isolate_init()),
|
||||
..Default::default()
|
||||
});
|
||||
js_runtime
|
||||
.execute(
|
||||
"<anon>",
|
||||
r#"
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn cli_snapshot() {
|
||||
let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
|
||||
startup_snapshot: Some(deno_isolate_init()),
|
||||
..Default::default()
|
||||
});
|
||||
js_runtime
|
||||
.execute(
|
||||
"<anon>",
|
||||
r#"
|
||||
if (!(bootstrap.mainRuntime && bootstrap.workerRuntime)) {
|
||||
throw Error("bad");
|
||||
}
|
||||
console.log("we have console.log!!!");
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,23 +75,6 @@ impl Into<Box<[u8]>> for ErrorRecord {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_record() {
|
||||
let expected = vec![
|
||||
1, 0, 0, 0, 255, 255, 255, 255, 11, 0, 0, 0, 66, 97, 100, 82, 101, 115,
|
||||
111, 117, 114, 99, 101, 69, 114, 114, 111, 114,
|
||||
];
|
||||
let err_record = ErrorRecord {
|
||||
promise_id: 1,
|
||||
arg: -1,
|
||||
error_len: 11,
|
||||
error_class: b"BadResource",
|
||||
error_message: b"Error".to_vec(),
|
||||
};
|
||||
let buf: Box<[u8]> = err_record.into();
|
||||
assert_eq!(buf, expected.into_boxed_slice());
|
||||
}
|
||||
|
||||
pub fn parse_min_record(bytes: &[u8]) -> Option<Record> {
|
||||
if bytes.len() % std::mem::size_of::<i32>() != 0 {
|
||||
return None;
|
||||
|
@ -113,25 +96,6 @@ pub fn parse_min_record(bytes: &[u8]) -> Option<Record> {
|
|||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_min_record() {
|
||||
let buf = vec![1, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0];
|
||||
assert_eq!(
|
||||
parse_min_record(&buf),
|
||||
Some(Record {
|
||||
promise_id: 1,
|
||||
arg: 3,
|
||||
result: 4
|
||||
})
|
||||
);
|
||||
|
||||
let buf = vec![];
|
||||
assert_eq!(parse_min_record(&buf), None);
|
||||
|
||||
let buf = vec![5];
|
||||
assert_eq!(parse_min_record(&buf), None);
|
||||
}
|
||||
|
||||
pub fn minimal_op<F>(op_fn: F) -> Box<OpFn>
|
||||
where
|
||||
F: Fn(Rc<RefCell<OpState>>, bool, i32, BufVec) -> MinimalOp + 'static,
|
||||
|
@ -203,3 +167,44 @@ where
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_error_record() {
|
||||
let expected = vec![
|
||||
1, 0, 0, 0, 255, 255, 255, 255, 11, 0, 0, 0, 66, 97, 100, 82, 101, 115,
|
||||
111, 117, 114, 99, 101, 69, 114, 114, 111, 114,
|
||||
];
|
||||
let err_record = ErrorRecord {
|
||||
promise_id: 1,
|
||||
arg: -1,
|
||||
error_len: 11,
|
||||
error_class: b"BadResource",
|
||||
error_message: b"Error".to_vec(),
|
||||
};
|
||||
let buf: Box<[u8]> = err_record.into();
|
||||
assert_eq!(buf, expected.into_boxed_slice());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_min_record() {
|
||||
let buf = vec![1, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0];
|
||||
assert_eq!(
|
||||
parse_min_record(&buf),
|
||||
Some(Record {
|
||||
promise_id: 1,
|
||||
arg: 3,
|
||||
result: 4
|
||||
})
|
||||
);
|
||||
|
||||
let buf = vec![];
|
||||
assert_eq!(parse_min_record(&buf), None);
|
||||
|
||||
let buf = vec![5];
|
||||
assert_eq!(parse_min_record(&buf), None);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue