From c263b18d53bcc78ede0cb045a9c1cf89ba2bd659 Mon Sep 17 00:00:00 2001 From: Maksim Kurnikov Date: Fri, 23 Oct 2020 17:25:36 +0300 Subject: [PATCH] add missing tests for current_time and script docstrings --- executor/tests/test_executor.rs | 31 ++++++++++ .../tests/test_compilation_check.rs | 61 +++++++++++-------- resources/assets/stdlib/time.move | 18 ++++++ 3 files changed, 86 insertions(+), 24 deletions(-) create mode 100644 resources/assets/stdlib/time.move diff --git a/executor/tests/test_executor.rs b/executor/tests/test_executor.rs index d1b0a69a..733bd49f 100644 --- a/executor/tests/test_executor.rs +++ b/executor/tests/test_executor.rs @@ -794,3 +794,34 @@ script { .unwrap(); assert_eq!(results.overall_gas_spent(), 10); } + +#[test] +fn test_set_current_time() { + let _pool = ConstPool::new(); + + let text = r" +/// current_time: 100 +script { + use 0x1::Time; + + fun main() { + assert(false, Time::now()); + } +} + "; + + let res = execute_script( + MoveFile::with_content(script_path(), text), + vec![stdlib_mod("time.move")], + "libra", + "0x1", + vec![], + ) + .unwrap() + .last() + .unwrap(); + assert_eq!( + res.error(), + "Execution aborted with code 100 in transaction script" + ); +} diff --git a/language_server/tests/test_compilation_check.rs b/language_server/tests/test_compilation_check.rs index 453ab401..04641332 100644 --- a/language_server/tests/test_compilation_check.rs +++ b/language_server/tests/test_compilation_check.rs @@ -80,6 +80,33 @@ fn diagnostics_with_deps( global_state.analysis().check_file(script_file) } +pub fn global_state_snapshot( + file: MoveFile, + config: Config, + additional_files: Vec, +) -> GlobalStateSnapshot { + let mut global_state = initialize_new_global_state(config); + let mut change = AnalysisChange::new(); + + for folder in &global_state.config().modules_folders { + for file in load_move_files(&[folder]).unwrap() { + let (fpath, text) = file.into(); + change.add_file(fpath, text); + } + } + + for file in additional_files { + let (fpath, text) = file.into(); + change.add_file(fpath, text); + } + + let (fpath, text) = file.into(); + change.update_file(fpath, text); + + global_state.apply_change(change); + global_state.snapshot() +} + #[cfg(test)] mod tests { use super::*; @@ -751,31 +778,17 @@ script { assert_eq!(errors.len(), 1); assert_eq!(errors[0].range, range((3, 1), (3, 5))); } -} - -pub fn global_state_snapshot( - file: MoveFile, - config: Config, - additional_files: Vec, -) -> GlobalStateSnapshot { - let mut global_state = initialize_new_global_state(config); - let mut change = AnalysisChange::new(); - for folder in &global_state.config().modules_folders { - for file in load_move_files(&[folder]).unwrap() { - let (fpath, text) = file.into(); - change.add_file(fpath, text); - } - } + #[test] + fn test_docstrings_are_allowed_on_scripts() { + let _pool = ConstPool::new(); - for file in additional_files { - let (fpath, text) = file.into(); - change.add_file(fpath, text); + let source = r" +/// signer: 0x1 +script { + fun main(_: &signer) {} +}"; + let errors = diagnostics(MoveFile::with_content(script_path(), source)); + assert!(errors.is_empty(), "{:#?}", errors); } - - let (fpath, text) = file.into(); - change.update_file(fpath, text); - - global_state.apply_change(change); - global_state.snapshot() } diff --git a/resources/assets/stdlib/time.move b/resources/assets/stdlib/time.move new file mode 100644 index 00000000..3349a9ff --- /dev/null +++ b/resources/assets/stdlib/time.move @@ -0,0 +1,18 @@ +address 0x1 { +module Time { + // A singleton resource holding the current Unix time in seconds + resource struct CurrentTimestamp { + seconds: u64, + } + + // Get the timestamp representing `now` in seconds. + public fun now(): u64 acquires CurrentTimestamp { + borrow_global(0x1).seconds + } + + // Helper function to determine if the blockchain is at genesis state. + public fun is_genesis(): bool { + !exists(0x1) + } +} +}