|
| 1 | +use crate::integration::common::corelib::{corelib_path, predeployed_contracts}; |
| 2 | +use crate::integration::common::runner::Contract; |
| 3 | +use crate::{assert_passed, test_case}; |
| 4 | +use camino::Utf8PathBuf; |
| 5 | +use forge::run; |
| 6 | +use indoc::indoc; |
| 7 | +use std::path::Path; |
| 8 | + |
| 9 | +#[test] |
| 10 | +fn get_class_hash() { |
| 11 | + let test = test_case!( |
| 12 | + indoc!( |
| 13 | + r#" |
| 14 | + use array::ArrayTrait; |
| 15 | + use result::ResultTrait; |
| 16 | + use snforge_std::{ declare, PreparedContract, deploy, start_prank, get_class_hash }; |
| 17 | +
|
| 18 | + #[test] |
| 19 | + fn test_get_class_hash() { |
| 20 | + let class_hash = declare('GetClassHashCheckerUpg'); |
| 21 | + let prepared = PreparedContract { class_hash: class_hash, constructor_calldata: @ArrayTrait::new() }; |
| 22 | + let contract_address = deploy(prepared).unwrap(); |
| 23 | + assert(get_class_hash(contract_address) == class_hash, 'Incorrect class hash'); |
| 24 | + } |
| 25 | + "# |
| 26 | + ), |
| 27 | + Contract::from_code_path( |
| 28 | + "GetClassHashCheckerUpg".to_string(), |
| 29 | + Path::new("tests/data/contracts/get_class_hash_checker.cairo"), |
| 30 | + ) |
| 31 | + .unwrap() |
| 32 | + ); |
| 33 | + |
| 34 | + let result = run( |
| 35 | + &test.path().unwrap(), |
| 36 | + &String::from("src"), |
| 37 | + &test.path().unwrap().join("src/lib.cairo"), |
| 38 | + &Some(test.linked_libraries()), |
| 39 | + &Default::default(), |
| 40 | + &corelib_path(), |
| 41 | + &test.contracts(&corelib_path()).unwrap(), |
| 42 | + &Utf8PathBuf::from_path_buf(predeployed_contracts().to_path_buf()).unwrap(), |
| 43 | + ) |
| 44 | + .unwrap(); |
| 45 | + |
| 46 | + assert_passed!(result); |
| 47 | +} |
| 48 | + |
| 49 | +#[test] |
| 50 | +fn get_class_hash_replace_class() { |
| 51 | + let test = test_case!( |
| 52 | + indoc!( |
| 53 | + r#" |
| 54 | + use array::{ArrayTrait, SpanTrait}; |
| 55 | + use core::result::ResultTrait; |
| 56 | + use starknet::ClassHash; |
| 57 | + use snforge_std::{declare, deploy, get_class_hash, PreparedContract}; |
| 58 | +
|
| 59 | + #[starknet::interface] |
| 60 | + trait IUpgradeable<T> { |
| 61 | + fn upgrade(ref self: T, class_hash: ClassHash); |
| 62 | + } |
| 63 | +
|
| 64 | + #[starknet::interface] |
| 65 | + trait IHelloStarknet<TContractState> { |
| 66 | + fn increase_balance(ref self: TContractState, amount: felt252); |
| 67 | + fn get_balance(self: @TContractState) -> felt252; |
| 68 | + fn do_a_panic(self: @TContractState); |
| 69 | + fn do_a_panic_with(self: @TContractState, panic_data: Array<felt252>); |
| 70 | + } |
| 71 | +
|
| 72 | + #[test] |
| 73 | + fn test_get_class_hash_replace_class() { |
| 74 | + let class_hash = declare('GetClassHashCheckerUpg'); |
| 75 | +
|
| 76 | + let prepared = PreparedContract { |
| 77 | + class_hash: class_hash, |
| 78 | + constructor_calldata: @ArrayTrait::new() |
| 79 | + }; |
| 80 | +
|
| 81 | + let contract_address = deploy(prepared).unwrap(); |
| 82 | +
|
| 83 | + assert(get_class_hash(contract_address) == class_hash, 'Incorrect class hash'); |
| 84 | +
|
| 85 | + let hsn_class_hash = declare('HelloStarknet'); |
| 86 | + IUpgradeableDispatcher { contract_address }.upgrade(hsn_class_hash); |
| 87 | + assert(get_class_hash(contract_address) == hsn_class_hash, 'Incorrect upgrade class hash'); |
| 88 | +
|
| 89 | + let hello_dispatcher = IHelloStarknetDispatcher { contract_address }; |
| 90 | + hello_dispatcher.increase_balance(42); |
| 91 | + assert(hello_dispatcher.get_balance() == 42, 'Invalid balance'); |
| 92 | + } |
| 93 | + "# |
| 94 | + ), |
| 95 | + Contract::from_code_path( |
| 96 | + "GetClassHashCheckerUpg".to_string(), |
| 97 | + Path::new("tests/data/contracts/get_class_hash_checker.cairo"), |
| 98 | + ) |
| 99 | + .unwrap(), |
| 100 | + Contract::from_code_path( |
| 101 | + "HelloStarknet".to_string(), |
| 102 | + Path::new("tests/data/contracts/hello_starknet.cairo"), |
| 103 | + ) |
| 104 | + .unwrap() |
| 105 | + ); |
| 106 | + |
| 107 | + let result = run( |
| 108 | + &test.path().unwrap(), |
| 109 | + &String::from("src"), |
| 110 | + &test.path().unwrap().join("src/lib.cairo"), |
| 111 | + &Some(test.linked_libraries()), |
| 112 | + &Default::default(), |
| 113 | + &corelib_path(), |
| 114 | + &test.contracts(&corelib_path()).unwrap(), |
| 115 | + &Utf8PathBuf::from_path_buf(predeployed_contracts().to_path_buf()).unwrap(), |
| 116 | + ) |
| 117 | + .unwrap(); |
| 118 | + |
| 119 | + assert_passed!(result); |
| 120 | +} |
0 commit comments