Skip to content
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

Add tests for constructor checker #377

Merged
merged 8 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/test-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ trap 'for killable in $(jobs -p); do kill -9 $killable; done' EXIT

# setup example repo
rm -rf starknet-hardhat-example
EXAMPLE_REPO_BRANCH="plugin"
EXAMPLE_REPO_BRANCH="constructor-checker"
if [[ "$CIRCLE_BRANCH" == "master" ]] && [[ "$EXAMPLE_REPO_BRANCH" != "plugin" ]]; then
echo "Invalid example repo branch: $EXAMPLE_REPO_BRANCH"
exit 1
Expand Down
8 changes: 8 additions & 0 deletions test/general-tests/cairo0-constructor-test/check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { hardhatStarknetCompileDeprecated, hardhatStarknetTest } from "../../utils/cli-functions";

hardhatStarknetCompileDeprecated(
"contracts/contract.cairo contracts/simple_storage.cairo contracts/empty_constructor.cairo".split(
" "
)
);
hardhatStarknetTest("--no-compile test/cairo0-constructor.test.ts".split(" "));
12 changes: 12 additions & 0 deletions test/general-tests/cairo0-constructor-test/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import "@shardlabs/starknet-hardhat-plugin";

module.exports = {
starknet: {
network: "devnet"
},
networks: {
devnet: {
url: "http://127.0.0.1:5050"
}
}
};
4 changes: 4 additions & 0 deletions test/general-tests/cairo0-constructor-test/network.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "../../network.schema",
"devnet": true
}
42 changes: 36 additions & 6 deletions test/general-tests/constructor-test/check.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
import { hardhatStarknetCompileDeprecated, hardhatStarknetTest } from "../../utils/cli-functions";
import path from "path";
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
import { hardhatStarknetCompile, hardhatStarknetTest } from "../../utils/cli-functions";
import { copyFileSync } from "fs";
import { assertContains, rmrfSync } from "../../utils/utils";

hardhatStarknetCompileDeprecated(
"contracts/contract.cairo contracts/simple_storage.cairo contracts/empty_constructor.cairo".split(
" "
)
);
const prefix = path.join(__dirname);
const sourcesPath = "cairo1-contracts";

const duplicateConstructorContract = "duplicate_constructor.cairo";
const contract1Path = path.join(sourcesPath, duplicateConstructorContract);
FabijanC marked this conversation as resolved.
Show resolved Hide resolved

const noConstructorContract = "no_constructor.cairo";
const contract2Path = path.join(sourcesPath, noConstructorContract);

const muteConstructorContract = "mute_constructor.cairo";
const contract3Path = path.join(sourcesPath, muteConstructorContract);

const commentedConstructorContract = "commented_constructor.cairo";
const contract4Path = path.join(sourcesPath, commentedConstructorContract);

const emptyLineConstructorContract = "empty_line_constructor.cairo";
const contract5Path = path.join(sourcesPath, emptyLineConstructorContract);

// Copy contracts to example repo to ensure files exists
copyFileSync(path.join(prefix, duplicateConstructorContract), contract1Path);
copyFileSync(path.join(prefix, noConstructorContract), contract2Path);
copyFileSync(path.join(prefix, muteConstructorContract), contract3Path);
copyFileSync(path.join(prefix, commentedConstructorContract), contract4Path);
copyFileSync(path.join(prefix, emptyLineConstructorContract), contract5Path);

const expectedErrorMsg = "Error: Expected at most one constructor.";
const execution = hardhatStarknetCompile([contract1Path], true);
assertContains(execution.stderr, expectedErrorMsg);
rmrfSync(contract1Path);

// Compile cairo1 contracts
hardhatStarknetCompile(["cairo1-contracts/", "--add-pythonic-hints"]);
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
hardhatStarknetTest("--no-compile test/constructor.test.ts".split(" "));
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[contract]
mod Contract {

#[constructor]
// comment
fn constructor() {
}
}
11 changes: 11 additions & 0 deletions test/general-tests/constructor-test/duplicate_constructor.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[contract]
mod Contract {

#[constructor]
fn constructor() {
}

#[constructor]
fn constructor2() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#[contract]
mod Contract {

#[constructor]


fn constructor() {
}
}
7 changes: 7 additions & 0 deletions test/general-tests/constructor-test/mute_constructor.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[contract]
mod Contract {

// #[constructor]
fn constructor() {
}
}
12 changes: 12 additions & 0 deletions test/general-tests/constructor-test/no_constructor.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[contract]
mod Contract {

struct Storage {
balance: felt252,
}

#[external]
fn increase_balance(amount1: felt252, amount2: felt252) {
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
balance::write(balance::read() + amount1 + amount2);
}
}