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 all 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
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
}
41 changes: 35 additions & 6 deletions test/general-tests/constructor-test/check.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
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 noConstructorContract = "no_constructor.cairo";
const muteConstructorContract = "mute_constructor.cairo";
const commentedConstructorContract = "commented_constructor.cairo";
const emptyLineConstructorContract = "empty_line_constructor.cairo";

const contractNames = [
duplicateConstructorContract,
noConstructorContract,
muteConstructorContract,
commentedConstructorContract,
emptyLineConstructorContract
];

// Copy contracts to example repo to ensure files exists
for (const contractName of contractNames) {
const contractPath = path.join(sourcesPath, contractName);
copyFileSync(path.join(prefix, contractName), contractPath);
}

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

// Compile cairo1 contracts
hardhatStarknetCompile([sourcesPath, "--add-pythonic-hints"]);
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() {
}
}
10 changes: 10 additions & 0 deletions test/general-tests/constructor-test/no_constructor.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[contract]
mod Contract {

struct Storage {
}

#[external]
fn increase_balance() {
}
}