-
Notifications
You must be signed in to change notification settings - Fork 246
Split contract.md into multiple sections, add part on external contracts #3386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| # `snforge` Overview | ||
|
|
||
| * [Running Tests](testing/running-tests.md) | ||
| * [Writing Tests](testing/testing.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is called the same as sub section
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[dev-dependencies] snforge_std = "0.44.0"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed here and in other place as well
| Sometimes the test code failing can be a desired behavior. | ||
| Instead of manually handling it, you can simply mark your test as `#[should_panic(...)]`. | ||
| [See here](./testing.md#expected-failures) for more details. | ||
| This chapter shows how to test smart contracts using Starknet Foundry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we shorten this entire paragraph? It sounds a little clunky
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've shortened it
|
|
||
| Note that the name after `mod` will be used as the contract name for testing purposes. | ||
|
|
||
| ## Writing Tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This heading is duplicated and doesn't look good for me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed the top level heading, I think it makes a bit more sense now
Co-authored-by: kkawula <[email protected]>
…ting-contracts-docs
| Sometimes we want to test contracts functions that can panic, like testing that function that verifies caller address | ||
| panics on invalid address. For that purpose Starknet also provides a `SafeDispatcher`, that returns a `Result` instead of | ||
| panicking. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Sometimes we want to test contracts functions that can panic, like testing that function that verifies caller address | |
| panics on invalid address. For that purpose Starknet also provides a `SafeDispatcher`, that returns a `Result` instead of | |
| panicking. | |
| Sometimes we want to test contract functions that may panic. For instance, an example of this would be a function which verifies caller address and panics when it encounters an invalid one. For that purpose Starknet provides `SafeDispatcher` that returns a `Result` instead of | |
| panicking. |
|
|
||
| ## `SafeDispatcher` | ||
|
|
||
| Using `SafeDispatcher` we can test that the function in fact panics with an expected message. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| Using `SafeDispatcher` we can test that the function in fact panics with an expected message. | |
| By using `SafeDispatcher`, we can test that the function in fact panics with an expected message. |
| Sometimes the test code failing can be a desired behavior. | ||
| Instead of manually handling it, you can simply mark your test as `#[should_panic(...)]`. | ||
| [See here](../contracts/handling-errors.md#expecting-test-failure) for more details. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| [See here](../contracts/handling-errors.md#expecting-test-failure) for more details. | |
| See [here](../contracts/handling-errors.md#expecting-test-failure) for more details. |
|
|
||
| ## Add a Dependency | ||
|
|
||
| First, add a dependency on the contract package, either in `Scarb.toml` directly or by using `scarb add packageName`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Cairo packages are named with snake_case 😅 :
| First, add a dependency on the contract package, either in `Scarb.toml` directly or by using `scarb add packageName`. | |
| First, add a dependency on the contract package, either in `Scarb.toml` directly or by using `scarb add package_name`. |
|
|
||
| ```toml | ||
| [[target.starknet-contract]] | ||
| build-external-contracts = ["externalPackage1::Contract1", "otherExternalPackage::path::to::Contract2"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above:
| build-external-contracts = ["externalPackage1::Contract1", "otherExternalPackage::path::to::Contract2"] | |
| build-external-contracts = ["external_package::Contract1", "other_external_package::path::to::Contract2"] |
| * [Writing Tests](testing/testing.md) | ||
| * [Test Attributes](testing/test-attributes.md) | ||
| * [Testing Smart Contracts](testing/contracts.md) | ||
| * [Writing Contracts Tests](testing/contracts/writing_tests.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| * [Writing Contracts Tests](testing/contracts/writing_tests.md) | |
| * [Writing Contract Tests](testing/contracts/writing_tests.md) |
| @@ -0,0 +1,50 @@ | |||
| # Writing Contracts Tests | |||
|
|
|||
| ## The Test Contract | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ## The Test Contract | |
| ## Test Contract |
| Collected 2 test(s) from testing_smart_contracts_handling_errors package | ||
| Running 2 test(s) from tests/ | ||
| [FAIL] testing_smart_contracts_handling_errors_integrationtest::panic::failing | ||
|
|
||
| Failure data: | ||
| (0x50414e4943 ('PANIC'), 0x444159544148 ('DAYTAH')) | ||
|
|
||
| [PASS] testing_smart_contracts_handling_errors_integrationtest::handle_panic::handling_string_errors (l1_gas: ~0, l1_data_gas: ~96, l2_gas: ~280000) | ||
| Running 0 test(s) from src/ | ||
| Tests: 1 passed, 1 failed, 0 skipped, 0 ignored, 0 filtered out | ||
|
|
||
| Failures: | ||
| testing_smart_contracts_handling_errors_integrationtest::panic::failing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is incorrect output for the example above
| Failure data: | ||
| (0x50414e4943 ('PANIC'), 0x444159544148 ('DAYTAH')) | ||
|
|
||
| [PASS] testing_smart_contracts_handling_errors_integrationtest::handle_panic::handling_string_errors (l1_gas: ~0, l1_data_gas: ~96, l2_gas: ~280000) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We show in the code snippet only the failing function test, so ideally the output shouldn't contain other tests
| ```rust | ||
| {{#include ../../../listings/testing_smart_contracts_handling_errors/tests/handle_panic.cairo}} | ||
| ``` | ||
| You also could skip the de-serialization of the `panic_data`, and not use `try_deserialize_bytearray_error`, but this way you can actually use assertions on the `ByteArray` that was used to panic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| You also could skip the de-serialization of the `panic_data`, and not use `try_deserialize_bytearray_error`, but this way you can actually use assertions on the `ByteArray` that was used to panic. | |
| You could also skip deserializing the `panic_data` and avoid using `try_deserialize_bytearray_error`, but in that case `panic_data` remains in its raw format as an array of felts. |
|
Hi! This pull request hasn't had any activity for a while, so I am |
|
This pull request has been automatically closed due to inactivity. |
Closes #
Introduced changes
contract.mdinto multiple sectionsChecklist
CHANGELOG.md