Skip to content

Commit

Permalink
Support log in tests (FuelLabs#3483)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha authored Dec 1, 2022
1 parent 7951992 commit c506f1d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
27 changes: 27 additions & 0 deletions sway-core/src/language/ty/ast_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
declaration_engine::{de_get_function, DeclMapping, ReplaceDecls},
error::*,
language::{parsed, ty::*},
transform::AttributeKind,
type_system::*,
types::DeterministicallyAborts,
};
Expand Down Expand Up @@ -182,6 +183,32 @@ impl TyAstNode {
}
}

/// Check to see if this node is a function declaration of a function annotated as test.
pub(crate) fn is_test_function(&self) -> CompileResult<bool> {
let mut warnings = vec![];
let mut errors = vec![];
match &self {
TyAstNode {
span,
content: TyAstNodeContent::Declaration(TyDeclaration::FunctionDeclaration(decl_id)),
..
} => {
let TyFunctionDeclaration { attributes, .. } = check!(
CompileResult::from(de_get_function(decl_id.clone(), span)),
return err(warnings, errors),
warnings,
errors
);
ok(
attributes.contains_key(&AttributeKind::Test),
warnings,
errors,
)
}
_ => ok(false, warnings, errors),
}
}

pub(crate) fn type_info(&self, type_engine: &TypeEngine) -> TypeInfo {
// return statement should be ()
match &self.content {
Expand Down
40 changes: 37 additions & 3 deletions sway-core/src/language/ty/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,13 @@ impl TyProgram {
warnings,
errors
);
if public {
let is_test = check!(
node.is_test_function(),
return err(warnings, errors),
warnings,
errors
);
if public || is_test {
ret.append(&mut check!(
node.collect_types_metadata(ctx),
return err(warnings, errors),
Expand All @@ -273,7 +279,13 @@ impl TyProgram {
warnings,
errors
);
if is_main {
let is_test = check!(
node.is_test_function(),
return err(warnings, errors),
warnings,
errors
);
if is_main || is_test {
data.append(&mut check!(
node.collect_types_metadata(ctx),
return err(warnings, errors),
Expand All @@ -293,7 +305,13 @@ impl TyProgram {
warnings,
errors
);
if is_main {
let is_test = check!(
node.is_test_function(),
return err(warnings, errors),
warnings,
errors
);
if is_main || is_test {
data.append(&mut check!(
node.collect_types_metadata(ctx),
return err(warnings, errors),
Expand All @@ -306,6 +324,22 @@ impl TyProgram {
}
TyProgramKind::Contract { abi_entries, .. } => {
let mut data = vec![];
for node in self.root.all_nodes.iter() {
let is_test = check!(
node.is_test_function(),
return err(warnings, errors),
warnings,
errors
);
if is_test {
data.append(&mut check!(
node.collect_types_metadata(ctx),
return err(warnings, errors),
warnings,
errors
));
}
}
for entry in abi_entries.iter() {
data.append(&mut check!(
TyAstNode::from(entry).collect_types_metadata(ctx),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
contract;

use std::logging::log;

abi MyContract {
fn test_function() -> bool;
}
Expand All @@ -19,5 +21,6 @@ fn test_foo() {
#[test]
fn test_bar() {
let meaning = 6 * 7;
log(meaning);
assert(meaning == 42);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
library lib_multi_test;

use std::logging::log;

fn pow2(x: u64) -> u64 {
log(x);
x * x
}

Expand All @@ -16,6 +19,7 @@ fn test_sub() {

#[test]
fn test_gt() {
log(100);
assert(101 > 100);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
script;

use std::logging::log;

fn main() {
revert(0);
}
Expand All @@ -11,5 +13,6 @@ fn test_foo() {

#[test]
fn test_bar() {
log("test");
assert(4 / 2 == 2);
}

0 comments on commit c506f1d

Please sign in to comment.