-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
Submodule cairo
updated
11 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
protostar/cheatable_starknet/callable_hint_locals/print_hint_local.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import Callable | ||
|
||
from protostar.cheatable_starknet.controllers.io import protostar_print | ||
from protostar.starknet.data_transformer import CairoData | ||
|
||
from .callable_hint_local import CallableHintLocal | ||
|
||
|
||
class PrintHintLocal(CallableHintLocal): | ||
@property | ||
def name(self) -> str: | ||
return "protostar_print" | ||
|
||
def _build( | ||
self, | ||
) -> Callable[[CairoData], None]: | ||
return self.protostar_print | ||
|
||
def protostar_print(self, data: CairoData) -> None: | ||
protostar_print(data=data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from protostar.starknet.data_transformer import CairoData | ||
from protostar.cairo.short_string import short_string_to_str | ||
|
||
|
||
def protostar_print(data: CairoData): | ||
for data_item in data: | ||
str_data = short_string_to_str(data_item) | ||
original_value_msg = f"original value: [{str(data_item)}]" | ||
converted_value_msg = f"converted to a string: [{str_data}]" | ||
print(original_value_msg, converted_value_msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from tests.integration._conftest import ProtostarProjectFixture | ||
from tests.integration.conftest import ( | ||
CreateProtostarProjectFixture, | ||
assert_cairo_test_cases, | ||
) | ||
from protostar.testing.test_results import PassedTestCaseResult | ||
|
||
|
||
@pytest.fixture(name="protostar_project", scope="function") | ||
def protostar_fixture(create_protostar_project: CreateProtostarProjectFixture): | ||
with create_protostar_project() as protostar_project: | ||
yield protostar_project | ||
|
||
|
||
async def test_print(protostar_project: ProtostarProjectFixture, datadir: Path): | ||
testing_summary = await protostar_project.protostar.test_cairo1( | ||
datadir / "print_test.cairo", | ||
) | ||
|
||
assert_cairo_test_cases( | ||
testing_summary, | ||
expected_passed_test_cases_names=["test_print_basic"], | ||
) | ||
|
||
expected_outputs = { | ||
"test_print_basic": [ | ||
"original value: [448378203247] converted to a string: [hello]", | ||
"original value: [1953658213] converted to a string: [true]", | ||
"original value: [439721161573] converted to a string: [false]", | ||
"original value: [1953658213] converted to a string: [true]", | ||
"original value: [1986358889] converted to a string: [veni]", | ||
"original value: [1986618473] converted to a string: [vidi]", | ||
"original value: [1986618217] converted to a string: [vici]", | ||
], | ||
} | ||
|
||
mapping_item = testing_summary.test_suites_mapping.get(datadir / "print_test.cairo") | ||
assert mapping_item is not None | ||
|
||
for item in mapping_item: | ||
assert isinstance(item, PassedTestCaseResult) | ||
expected_outputs_list = expected_outputs[item.test_case_name] | ||
for expected_output in expected_outputs_list: | ||
assert expected_output in item.captured_stdout["test"] |
22 changes: 22 additions & 0 deletions
22
tests/integration/cairo1/compiler_bindings/test_print/print_test.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use array::ArrayTrait; | ||
use protostar_print::PrintTrait; | ||
use result::ResultTrait; | ||
|
||
#[test] | ||
fn test_print_basic() { | ||
1.print(); | ||
|
||
'hello'.print(); | ||
|
||
let mut array = ArrayTrait::new(); | ||
array.append('veni'); | ||
array.append('vidi'); | ||
array.append('vici'); | ||
array.print(); | ||
|
||
(1 == 2).print(); | ||
|
||
true.print(); | ||
|
||
assert(1 == 1, 'xxx'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
sidebar_label: Debugging | ||
--- | ||
|
||
Currently, Cairo does not support a debugging mechanism per se, but we can print variables' values to the standard output. | ||
|
||
# Printing to stdout | ||
|
||
In order to print a variable's value to the standard output, we have to use `PrintTrait`: | ||
|
||
``` | ||
use array::ArrayTrait; | ||
use protostar_print::PrintTrait; | ||
use result::ResultTrait; | ||
#[test] | ||
fn test_print_basic() { | ||
1.print(); | ||
'hello'.print(); | ||
let mut array = ArrayTrait::new(); | ||
array.append('veni'); | ||
array.append('vidi'); | ||
array.append('vici'); | ||
array.print(); | ||
(1 == 2).print(); | ||
true.print(); | ||
assert(1 == 1, 'xxx'); | ||
} | ||
``` | ||
|
||
You can print numbers, booleans and [Cairo short strings](https://www.cairo-lang.org/docs/how_cairo_works/consts.html#short-string-literals) as well as arrays containing values of these types. |