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

Commit

Permalink
refactor(solidity./core/tests-positions-server): refactored response …
Browse files Browse the repository at this point in the history
…structure to add contract and tests names
  • Loading branch information
0xmemorygrinder committed Dec 11, 2023
1 parent 76d6288 commit 2d3845e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ pub struct GetTestsPositionsParams {

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TestContract {
pub contract_range: Range,
pub tests_ranges: Vec<Range>,
pub name: String,
pub range: Range,
pub tests: Vec<Test>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Test {
pub name: String,
pub range: Range,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GetTestsPositionsResponse {
pub ranges: Vec<TestContract>,
pub contracts: Vec<TestContract>,
}

pub struct GetTestsPositionsRequest {}
Expand All @@ -23,4 +30,4 @@ impl Request for GetTestsPositionsRequest {
type Params = GetTestsPositionsParams;
type Result = GetTestsPositionsResponse;
const METHOD: &'static str = "osmium/getTestsPositions";
}
}
26 changes: 16 additions & 10 deletions toolchains/solidity/core/crates/tests-positions-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use get_tests_positions::{GetTestsPositionsParams, GetTestsPositionsResponse, TestContract};
use get_tests_positions::{GetTestsPositionsParams, GetTestsPositionsResponse, TestContract, Test};
use osmium_libs_solidity_ast_extractor::retriever::retrieve_functions_nodes;
use osmium_libs_solidity_ast_extractor::File;
use osmium_libs_solidity_ast_extractor::{
Expand Down Expand Up @@ -58,27 +58,33 @@ impl Backend {
}

pub fn extract_tests_positions(&self, ast: File) -> Result<GetTestsPositionsResponse> {
let mut ranges = vec![];
let mut res = vec![];
let contracts = retrieve_contract_nodes(&ast);
for contract in contracts {
let mut tests_ranges = vec![];
let mut tests: Vec<Test> = vec![];
let mut functions = retrieve_functions_nodes(&contract);
let tests = functions.iter_mut().filter(|f| {
let contract_tests = functions.iter_mut().filter(|f| {
f.name.is_some() && f.name.as_ref().unwrap().as_string().starts_with("test")
});
for test in tests {
for test in contract_tests {
let name = match &test.name {
Some(name) => name,
None => continue,
};
tests_ranges.push(range_from_spanned(name));
tests.push(
Test {
name: name.as_string(),
range: range_from_spanned(name),
}
);
}
ranges.push(TestContract {
contract_range: range_from_spanned(&contract.name),
tests_ranges,
res.push(TestContract {
name: contract.name.as_string(),
range: range_from_spanned(&contract.name),
tests,
});
}
Ok(GetTestsPositionsResponse { ranges })
Ok(GetTestsPositionsResponse { contracts: res })
}
}

Expand Down

0 comments on commit 2d3845e

Please sign in to comment.