Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Commit 4fcc69a

Browse files
Leon-azerty0xmemorygrinder
authored andcommitted
test: add tests
1 parent 27a5312 commit 4fcc69a

File tree

11 files changed

+195
-29
lines changed

11 files changed

+195
-29
lines changed

libs/ast-references/src/file_retriever.rs

Lines changed: 168 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -401,31 +401,188 @@ mod tests {
401401

402402
#[test]
403403
fn test_retrieve_contract_nodes_empty() {
404-
retrieve_file_reference_from_path("./tests/two.sol".to_string());
404+
let mut path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
405+
path.push("tests");
406+
path.push("contracts");
407+
path.push("file.sol");
408+
let path_str = path.to_str().unwrap().to_string();
409+
let source = fs::read_to_string(&path).unwrap();
410+
411+
let mut visitor = FileVisitor::new(path_str.clone());
412+
let file = ast_extractor::extract::extract_ast_from(source).unwrap();
413+
let contracts = file.items.iter().find(|item| match item {
414+
syn_solidity::Item::Contract(contract) => true,
415+
_ => false,
416+
});
417+
let contracts = match contracts {
418+
Some(syn_solidity::Item::Contract(var)) => var,
419+
_ => panic!("Expect contracts declaration"),
420+
};
421+
visitor.visit_item_contract(contracts);
422+
assert_eq!(visitor.file_reference.borrow().contracts.len(), 1);
405423
assert_eq!(1, 0)
406424
}
407425

408426
#[test]
409-
fn test_retrieve_event() {
410-
retrieve_file_reference_from_path("./tests/two.sol".to_string());
411-
assert_eq!(1, 0)
427+
fn test_retrieve_contract_event() {
428+
let mut path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
429+
path.push("tests");
430+
path.push("events");
431+
path.push("file.sol");
432+
let path_str = path.to_str().unwrap().to_string();
433+
let source = fs::read_to_string(&path).unwrap();
434+
435+
let mut visitor = FileVisitor::new(path_str.clone());
436+
let file = ast_extractor::extract::extract_ast_from(source).unwrap();
437+
let events = file.items.iter().find(|item| match item {
438+
syn_solidity::Item::Event(contract) => true,
439+
_ => false,
440+
});
441+
let events = match events {
442+
Some(syn_solidity::Item::Event(var)) => var,
443+
_ => panic!("Expect events declaration"),
444+
};
445+
visitor.visit_item_event(events);
446+
assert_eq!(visitor.file_reference.borrow().events.len(), 1);
412447
}
413448

414449
#[test]
415450
fn test_retrieve_functions() {
416-
retrieve_file_reference_from_path("./tests/two.sol".to_string());
417-
assert_eq!(1, 0)
451+
let mut path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
452+
path.push("tests");
453+
path.push("functions");
454+
path.push("contract.sol");
455+
let path_str = path.to_str().unwrap().to_string();
456+
let source = fs::read_to_string(&path).unwrap();
457+
458+
let mut visitor = FileVisitor::new(path_str.clone());
459+
let contract_ref = ContractReference::new(
460+
"Good".to_string(),
461+
Location::new(path_str, Bound { line: 1, column: 1 }, Bound::new(1, 10)),
462+
&visitor.file_reference,
463+
);
464+
visitor
465+
.file_reference
466+
.borrow_mut()
467+
.add_contract(contract_ref);
468+
visitor.current_contract = Some(visitor.file_reference.borrow().contracts[0].clone());
469+
let file = ast_extractor::extract::extract_ast_from(source).unwrap();
470+
let contract = file.items.iter().find(|item| match item {
471+
syn_solidity::Item::Contract(contract) => true,
472+
_ => false,
473+
});
474+
let contract = match contract {
475+
Some(syn_solidity::Item::Contract(contract)) => contract,
476+
_ => panic!("No contract found"),
477+
};
478+
let function = contract.body.iter().find(|item| match item {
479+
syn_solidity::Item::Function(_) => true,
480+
_ => false,
481+
});
482+
let function = match function {
483+
Some(syn_solidity::Item::Function(function)) => function,
484+
_ => panic!("No function found"),
485+
};
486+
visitor.visit_item_function(function);
487+
assert_eq!(
488+
visitor.file_reference.borrow().contracts[0]
489+
.borrow()
490+
.properties
491+
.len(),
492+
1
493+
);
418494
}
419495

420496
#[test]
421-
fn test_retrieve_structs() {
422-
retrieve_file_reference_from_path("./tests/two.sol".to_string());
423-
assert_eq!(1, 0)
497+
fn test_retrieve_contract_structs() {
498+
let mut path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
499+
path.push("tests");
500+
path.push("structs");
501+
path.push("contract.sol");
502+
let path_str = path.to_str().unwrap().to_string();
503+
let source = fs::read_to_string(&path).unwrap();
504+
505+
let mut visitor = FileVisitor::new(path_str.clone());
506+
let contract_ref = ContractReference::new(
507+
"Good".to_string(),
508+
Location::new(path_str, Bound { line: 1, column: 1 }, Bound::new(1, 10)),
509+
&visitor.file_reference,
510+
);
511+
visitor
512+
.file_reference
513+
.borrow_mut()
514+
.add_contract(contract_ref);
515+
visitor.current_contract = Some(visitor.file_reference.borrow().contracts[0].clone());
516+
let file = ast_extractor::extract::extract_ast_from(source).unwrap();
517+
let contract = file.items.iter().find(|item| match item {
518+
syn_solidity::Item::Contract(contract) => true,
519+
_ => false,
520+
});
521+
let contract = match contract {
522+
Some(syn_solidity::Item::Contract(contract)) => contract,
523+
_ => panic!("No contract found"),
524+
};
525+
let structs = contract.body.iter().find(|item| match item {
526+
syn_solidity::Item::Struct(_) => true,
527+
_ => false,
528+
});
529+
let structs = match structs {
530+
Some(syn_solidity::Item::Struct(structs)) => structs,
531+
_ => panic!("No structs found"),
532+
};
533+
visitor.visit_item_struct(structs);
534+
assert_eq!(
535+
visitor.file_reference.borrow().contracts[0]
536+
.borrow()
537+
.properties
538+
.len(),
539+
1
540+
);
541+
}
542+
543+
#[test]
544+
fn test_retrieve_file_structs() {
545+
let mut path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
546+
path.push("tests");
547+
path.push("structs");
548+
path.push("file.sol");
549+
let path_str = path.to_str().unwrap().to_string();
550+
let source = fs::read_to_string(&path).unwrap();
551+
552+
let mut visitor = FileVisitor::new(path_str.clone());
553+
let file = ast_extractor::extract::extract_ast_from(source).unwrap();
554+
let structs = file.items.iter().find(|item| match item {
555+
syn_solidity::Item::Struct(contract) => true,
556+
_ => false,
557+
});
558+
let structs = match structs {
559+
Some(syn_solidity::Item::Struct(var)) => var,
560+
_ => panic!("Expect structs declaration"),
561+
};
562+
visitor.visit_item_struct(structs);
563+
assert_eq!(visitor.file_reference.borrow().structs.len(), 1);
424564
}
425565

426566
#[test]
427567
fn test_retrieve_errors() {
428-
retrieve_file_reference_from_path("./tests/two.sol".to_string());
429-
assert_eq!(1, 0)
568+
let mut path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
569+
path.push("tests");
570+
path.push("errors");
571+
path.push("contract.sol");
572+
let path_str = path.to_str().unwrap().to_string();
573+
let source = fs::read_to_string(&path).unwrap();
574+
575+
let mut visitor = FileVisitor::new(path_str.clone());
576+
let file = ast_extractor::extract::extract_ast_from(source).unwrap();
577+
let errors = file.items.iter().find(|item| match item {
578+
syn_solidity::Item::Error(contract) => true,
579+
_ => false,
580+
});
581+
let errors = match errors {
582+
Some(syn_solidity::Item::Error(var)) => var,
583+
_ => panic!("Expect errors declaration"),
584+
};
585+
visitor.visit_item_error(errors);
586+
assert_eq!(visitor.file_reference.borrow().errors.len(), 1);
430587
}
431588
}

libs/ast-references/tests/contracts/contract.sol

Lines changed: 0 additions & 5 deletions
This file was deleted.

libs/ast-references/tests/contracts/file.sol

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
pragma solidity ^0.8.0;
22

33
contract Good {
4-
uint256 public x;
4+
enum State {
5+
Waiting,
6+
Ready,
7+
Active
8+
}
59
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
pragma solidity ^0.8.0;
22

3-
uint256 constant a = 1;
3+
enum State {
4+
Waiting,
5+
Ready,
6+
Active
7+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pragma solidity ^0.8.0;
22

33
contract Good {
4-
uint256 public x;
4+
error InsufficientBalance(uint256 available, uint256 required);
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pragma solidity ^0.8.0;
22

33
contract Good {
4-
uint256 public x;
4+
event Deposit(address indexed _from, bytes32 indexed _id, uint256 _value);
55
}

libs/ast-references/tests/events/file.sol

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pragma solidity ^0.8.0;
22

33
contract Good {
4-
uint256 public x;
4+
function f() public pure returns (uint) {
5+
return 1;
6+
}
57
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
pragma solidity ^0.8.0;
22

33
contract Good {
4-
uint256 public x;
4+
struct User {
5+
uint id;
6+
string name;
7+
uint age;
8+
uint balance;
9+
}
510
}

0 commit comments

Comments
 (0)