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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
31 changes: 30 additions & 1 deletion test/general-tests/constructor-test/check.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
import { hardhatStarknetCompileDeprecated, hardhatStarknetTest } from "../../utils/cli-functions";
import path from "path";
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
import {
hardhatStarknetCompile,
hardhatStarknetCompileDeprecated,
hardhatStarknetTest
} from "../../utils/cli-functions";
import { copyFileSync } from "fs";
import { assertContains, rmrfSync } from "../../utils/utils";

const prefix = path.join(__dirname);
const contract1 = "duplicate_constructor.cairo";
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
const contract1Path = path.join("cairo1-contracts", contract1);
FabijanC marked this conversation as resolved.
Show resolved Hide resolved

const contract2 = "no_constructor.cairo";
const contract2Path = path.join("cairo1-contracts", contract2);

const contract3 = "mute_constructor.cairo";
const contract3Path = path.join("cairo1-contracts", contract3);

copyFileSync(path.join(prefix, contract1), contract1Path);
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
copyFileSync(path.join(prefix, contract2), contract2Path);
copyFileSync(path.join(prefix, contract3), contract3Path);

const exptectedErrorMsg = "Error: Expected at most one constructor.";
Nathan-SL marked this conversation as resolved.
Show resolved Hide resolved
const execution = hardhatStarknetCompile([contract1Path], true);
assertContains(execution.stderr, exptectedErrorMsg);
rmrfSync(contract1Path);

// Compile cairo1 contracts
hardhatStarknetCompile(["cairo1-contracts/", "--add-pythonic-hints"]);
FabijanC marked this conversation as resolved.
Show resolved Hide resolved

hardhatStarknetCompileDeprecated(
"contracts/contract.cairo contracts/simple_storage.cairo contracts/empty_constructor.cairo".split(
Expand Down
26 changes: 26 additions & 0 deletions test/general-tests/constructor-test/duplicate_constructor.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[contract]
mod Contract {
struct Storage {
balance: felt252,
}

#[constructor]
fn constructor(initial_balance: felt252) {
balance::write(initial_balance);
}

#[constructor]
fn constructor2(initial_balance: felt252) {
balance::write(initial_balance);
}

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

#[view]
fn get_balance() -> felt252 {
balance::read()
}
}
21 changes: 21 additions & 0 deletions test/general-tests/constructor-test/mute_constructor.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#[contract]
mod Contract {
struct Storage {
balance: felt252,
}

// #[constructor]
fn constructor(initial_balance: felt252) {
balance::write(initial_balance);
}

#[external]
fn increase_balance(amount1: felt252, amount2: felt252) {
balance::write(balance::read() + amount1 + amount2);
}

#[view]
fn get_balance() -> felt252 {
balance::read()
}
}
16 changes: 16 additions & 0 deletions test/general-tests/constructor-test/no_constructor.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[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);
}

#[view]
fn get_balance() -> felt252 {
balance::read()
}
}