Skip to content

Commit

Permalink
Merge pull request #125 from dfinance/add-missing-tests
Browse files Browse the repository at this point in the history
add missing tests for current_time and script docstrings
  • Loading branch information
mkurnikov authored Oct 23, 2020
2 parents 1e2992b + c263b18 commit 3440543
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 24 deletions.
31 changes: 31 additions & 0 deletions executor/tests/test_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
}
61 changes: 37 additions & 24 deletions language_server/tests/test_compilation_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MoveFile>,
) -> 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::*;
Expand Down Expand Up @@ -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<MoveFile>,
) -> 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()
}
18 changes: 18 additions & 0 deletions resources/assets/stdlib/time.move
Original file line number Diff line number Diff line change
@@ -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<CurrentTimestamp>(0x1).seconds
}

// Helper function to determine if the blockchain is at genesis state.
public fun is_genesis(): bool {
!exists<Self::CurrentTimestamp>(0x1)
}
}
}

0 comments on commit 3440543

Please sign in to comment.