Skip to content

Commit

Permalink
chore: make all tests annotated with #[cfg(test)] (denoland#9347)
Browse files Browse the repository at this point in the history
  • Loading branch information
magurotuna committed Feb 1, 2021
1 parent 23281be commit 84f8b87
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 110 deletions.
41 changes: 23 additions & 18 deletions cli/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);

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"
)
#[cfg(test)]
mod tests {
use super::*;

#[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"
);
}
}
13 changes: 9 additions & 4 deletions cli/program_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 8 additions & 3 deletions core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 42 additions & 37 deletions core/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
]
);
}
}
29 changes: 17 additions & 12 deletions runtime/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
77 changes: 41 additions & 36 deletions runtime/ops/dispatch_minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 84f8b87

Please sign in to comment.