diff --git a/docs/developing/testing.md b/docs/developing/testing.md index 8345cb4c7..3de4e5690 100644 --- a/docs/developing/testing.md +++ b/docs/developing/testing.md @@ -38,10 +38,10 @@ The following SmartPy test code snippet is for a Tezos smart contract that acts ```bash if "templates" not in __name__: - @sp.add_test(name="Calculator") + @sp.add_test() def test(): c1 = main.Calculator() - scenario = sp.test_scenario(main) + scenario = sp.test_scenario("Calculator") scenario.h1("Calculator") scenario += c1 c1.multiply(x=2, y=5) diff --git a/docs/smart-contracts/events.md b/docs/smart-contracts/events.md index a1f01f2f4..dded545be 100644 --- a/docs/smart-contracts/events.md +++ b/docs/smart-contracts/events.md @@ -72,10 +72,10 @@ def main(): if "templates" not in __name__: - @sp.add_test(name="Events") + @sp.add_test() def test(): c1 = main.Events(12) - scenario = sp.test_scenario(main) + scenario = sp.test_scenario("Events", main) scenario.h1("Add") scenario += c1 c1.add(2).run( diff --git a/docs/smart-contracts/languages/smartpy.mdx b/docs/smart-contracts/languages/smartpy.mdx index e55e8cf2d..36388e62f 100644 --- a/docs/smart-contracts/languages/smartpy.mdx +++ b/docs/smart-contracts/languages/smartpy.mdx @@ -55,17 +55,6 @@ sp.verify(self.data.x > 2) Here, `sp.verify()` checks that the field `x` is larger than `2` and raises an error if it is not. This is performed at run time, i.e., in the blockchain, once translated into Michelson. -Since Python does not allow its control statements to be overloaded, certain language constructs are desugared by a pre-processor: `sp.if`, `sp.else`, `sp.for`, `sp.while` are SmartPy commands. - -For example, we will use: - -```python -sp.if self.data.x > 2: - self.data.x += 1 -``` - -If we would have used the `if` native to Python it would not be interpreted and compiled in Michelson. - ## About the raffle contract A raffle is a game of chance that distributes a winning prize. @@ -108,10 +97,10 @@ class Raffle(sp.Contract): def open_raffle(self): pass - @sp.add_test(name = "Raffle") + @sp.add_test() def test(): r = Raffle() - scenario = sp.test_scenario() + scenario = sp.test_scenario("Raffle") scenario.h1("Raffle") scenario += r ``` @@ -199,7 +188,7 @@ class Raffle(sp.Contract): alice = sp.test_account("Alice") admin = sp.test_account("Administrator") r = Raffle(admin.address) - scenario = sp.test_scenario() + scenario = sp.test_scenario("Raffle") scenario.h1("Raffle") scenario += r @@ -346,12 +335,12 @@ On _SmartPy_, a test is a method of the contract class, preceded by `@sp.add_tes Inside this method, you need to instantiate your contract class and your scenarios, to which you will add the contract instance and all the related calls that you want to test. For instance: ```python -@sp.add_test(name="Raffle") +@sp.add_test() def test(): alice = sp.test_account("Alice") admin = sp.test_account("Administrator") r = Raffle(admin.address) - scenario = sp.test_scenario() + scenario = sp.test_scenario("Raffle") scenario.h1("Raffle") scenario += r ``` @@ -560,14 +549,14 @@ class Raffle(sp.Contract): self.data.sold_tickets[ticket_id] = current_player - @sp.add_test(name="Raffle") + @sp.add_test() def test(): alice = sp.test_account("Alice") jack = sp.test_account("Jack") admin = sp.test_account("Administrator") r = Raffle(admin.address) - scenario = sp.test_scenario() + scenario = sp.test_scenario("Raffle") scenario.h1("Raffle") scenario += r @@ -774,13 +763,13 @@ class Raffle(sp.Contract): self.data.sold_tickets = sp.map() self.data.raffle_is_open = False - @sp.add_test(name="Raffle") + @sp.add_test() def test(): alice = sp.test_account("Alice") jack = sp.test_account("Jack") admin = sp.test_account("Administrator") r = Raffle(admin.address) - scenario = sp.test_scenario() + scenario = sp.test_scenario("Raffle") scenario.h1("Raffle") scenario += r diff --git a/docs/tutorials/smart-contract/smartpy.mdx b/docs/tutorials/smart-contract/smartpy.mdx index d3b3a8320..4454d4e94 100644 --- a/docs/tutorials/smart-contract/smartpy.mdx +++ b/docs/tutorials/smart-contract/smartpy.mdx @@ -2,7 +2,7 @@ title: Deploy a smart contract with SmartPy authors: 'John Joubert, Sasha Aldrick, Tim McMackin' last_update: - date: 9 November 2023 + date: 15 February 2024 --- This tutorial covers writing and deploying a simple smart contract with the SmartPy programming language. @@ -91,7 +91,7 @@ Now you have an account and funds that you can use to work with Tezos. The contract that you will create has these basic parts: -- A function that initializes the contract and sets the starting value for it storage. +- A function that initializes the contract and sets the starting value for its storage. - Internal functions called entrypoints that run code when clients call the contract. @@ -115,6 +115,9 @@ You can work with SmartPy code in any IDE, but this online IDE keeps you from ha def main(): class StoreGreeting(sp.Contract): def __init__(self, greeting): # Note the indentation + # Initialize the storage with a string passed at deployment time + # Cast the greeting parameter to a string + sp.cast(greeting, sp.string) self.data.greeting = greeting @sp.entrypoint # Note the indentation @@ -139,22 +142,28 @@ You can work with SmartPy code in any IDE, but this online IDE keeps you from ha 1. Add this code, which creates automated tests: ```python - @sp.add_test(name = "StoreGreeting") + # Automated tests that run on simulation + @sp.add_test() def test(): - scenario = sp.test_scenario(main) + # Initialize the test scenario + scenario = sp.test_scenario("StoreGreeting", main) scenario.h1("StoreGreeting") + # Initialize the contract and pass the starting value contract = main.StoreGreeting("Hello") scenario += contract + # Verify that the value in storage was set correctly scenario.verify(contract.data.greeting == "Hello") + # Test the entrypoints and check the new storage value contract.replace(text = "Hi") contract.append(text = ", there!") scenario.verify(contract.data.greeting == "Hi, there!") ``` - These tests run automatically on compilation to verify that the replace and append entrypoints work. + When you run the SmartPy file, SmartPy runs a simulation in which it tests and compiles the contract. + In this case, the tests verify that the replace and append endpoints work. For more information about SmartPy and tests, see the [SmartPy documentation](https://smartpy.io/). The SmartPy online IDE looks like this: @@ -170,6 +179,9 @@ import smartpy as sp def main(): class StoreGreeting(sp.Contract): def __init__(self, greeting): # Note the indentation + # Initialize the storage with a string passed at deployment time + # Cast the greeting parameter to a string + sp.cast(greeting, sp.string) self.data.greeting = greeting @sp.entrypoint # Note the indentation @@ -180,16 +192,21 @@ def main(): def append(self, params): self.data.greeting += params.text -@sp.add_test(name = "StoreGreeting") +# Automated tests that run on simulation +@sp.add_test() def test(): - scenario = sp.test_scenario(main) + # Initialize the test scenario + scenario = sp.test_scenario("Test scenario", main) scenario.h1("StoreGreeting") + # Initialize the contract and pass the starting value contract = main.StoreGreeting("Hello") scenario += contract + # Verify that the value in storage was set correctly scenario.verify(contract.data.greeting == "Hello") + # Test the entrypoints and check the new storage value contract.replace(text = "Hi") contract.append(text = ", there!") scenario.verify(contract.data.greeting == "Hi, there!")