Skip to content

Commit 83470b7

Browse files
committed
Fix tests
1 parent 50a671f commit 83470b7

File tree

3 files changed

+42
-251
lines changed

3 files changed

+42
-251
lines changed

crates/ink/tests/compile_tests.rs

Lines changed: 0 additions & 251 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,6 @@ use std::{
2020
},
2121
};
2222

23-
#[test]
24-
fn ui_tests_blake2b_pass() {
25-
let t = trybuild::TestCases::new();
26-
t.pass("tests/ui/blake2b/pass/*.rs");
27-
}
28-
29-
#[test]
30-
fn ui_tests_blake2b_fail() {
31-
let t = trybuild::TestCases::new();
32-
t.compile_fail("tests/ui/blake2b/fail/*.rs");
33-
}
34-
3523
#[test]
3624
fn ui_tests_selector_id_pass() {
3725
let t = trybuild::TestCases::new();
@@ -55,242 +43,3 @@ fn ui_tests_selector_bytes_fail() {
5543
let t = trybuild::TestCases::new();
5644
t.compile_fail("tests/ui/selector_bytes/fail/*.rs");
5745
}
58-
59-
#[test]
60-
fn ui_tests_contract_pass() {
61-
let t = trybuild::TestCases::new();
62-
t.pass("tests/ui/contract/pass/*.rs");
63-
}
64-
65-
#[test]
66-
fn ui_tests_contract_constructor_pass() {
67-
let t = trybuild::TestCases::new();
68-
t.pass("tests/ui/contract/pass/constructor/*.rs");
69-
}
70-
71-
#[test]
72-
fn ui_tests_contract_message_pass() {
73-
let t = trybuild::TestCases::new();
74-
t.pass("tests/ui/contract/pass/message/*.rs");
75-
}
76-
77-
#[test]
78-
fn ui_tests_contract_example_pass() {
79-
let t = trybuild::TestCases::new();
80-
t.pass("tests/ui/contract/pass/example/*.rs");
81-
}
82-
83-
#[test]
84-
fn ui_tests_contract_event_pass() {
85-
let t = trybuild::TestCases::new();
86-
t.pass("tests/ui/contract/pass/event/*.rs");
87-
}
88-
89-
#[test]
90-
fn ui_tests_contract_fail() {
91-
let t = trybuild::TestCases::new();
92-
t.compile_fail("tests/ui/contract/fail/*.rs");
93-
}
94-
95-
#[test]
96-
fn ui_tests_contract_cfg_fail() {
97-
let t = trybuild::TestCases::new();
98-
t.compile_fail("tests/ui/contract/fail/cfg/*.rs");
99-
}
100-
101-
#[test]
102-
fn ui_tests_contract_constructor_fail() {
103-
let t = trybuild::TestCases::new();
104-
t.compile_fail("tests/ui/contract/fail/constructor/*.rs");
105-
}
106-
107-
#[test]
108-
fn ui_tests_contract_event_fail() {
109-
let t = trybuild::TestCases::new();
110-
t.compile_fail("tests/ui/contract/fail/event/*.rs");
111-
}
112-
113-
#[test]
114-
fn ui_tests_contract_impl_fail() {
115-
let t = trybuild::TestCases::new();
116-
t.compile_fail("tests/ui/contract/fail/impl/*.rs");
117-
}
118-
119-
#[test]
120-
fn ui_tests_contract_message_fail() {
121-
let t = trybuild::TestCases::new();
122-
t.compile_fail("tests/ui/contract/fail/message/*.rs");
123-
}
124-
125-
#[test]
126-
fn ui_tests_contract_trait_fail() {
127-
let t = trybuild::TestCases::new();
128-
t.compile_fail("tests/ui/contract/fail/trait/*.rs");
129-
}
130-
131-
#[test]
132-
fn ui_tests_event_pass() {
133-
let t = trybuild::TestCases::new();
134-
t.pass("tests/ui/event/pass/*.rs");
135-
}
136-
137-
#[test]
138-
fn ui_tests_event_fail() {
139-
let t = trybuild::TestCases::new();
140-
t.compile_fail("tests/ui/event/fail/*.rs");
141-
}
142-
143-
#[test]
144-
fn ui_tests_storage_item_pass() {
145-
let t = trybuild::TestCases::new();
146-
t.pass("tests/ui/storage_item/pass/*.rs");
147-
}
148-
149-
#[test]
150-
fn ui_tests_storage_item_fail() {
151-
let t = trybuild::TestCases::new();
152-
t.compile_fail("tests/ui/storage_item/fail/*.rs");
153-
}
154-
155-
#[test]
156-
fn ui_tests_trait_def_pass() {
157-
let t = trybuild::TestCases::new();
158-
t.pass("tests/ui/trait_def/pass/*.rs");
159-
}
160-
161-
#[test]
162-
fn ui_tests_trait_def_fail() {
163-
let t = trybuild::TestCases::new();
164-
t.compile_fail("tests/ui/trait_def/fail/*.rs");
165-
}
166-
167-
#[test]
168-
fn ui_tests_pay_with_call_pass() {
169-
let t = trybuild::TestCases::new();
170-
t.pass("tests/ui/pay_with_call/pass/multiple_args.rs");
171-
}
172-
173-
#[test]
174-
fn ui_tests_scale_derive_pass() {
175-
let t = trybuild::TestCases::new();
176-
t.pass("tests/ui/scale_derive/pass/*.rs");
177-
}
178-
179-
#[test]
180-
fn ui_tests_scale_derive_fail() {
181-
let t = trybuild::TestCases::new();
182-
t.compile_fail("tests/ui/scale_derive/fail/*.rs");
183-
}
184-
185-
#[test]
186-
// Builds `trybuild_wrapper` and runs all tests that require ABI `cfg` flags with it.
187-
// See `tests/trybuild_wrapper` crate docs for motivation.
188-
fn ui_tests_abi() {
189-
let tests = [
190-
// "sol" ABI.
191-
("sol", "tests/ui/abi/sol/pass/*.rs", "pass"),
192-
("sol", "tests/ui/abi/sol/fail/*.rs", "fail"),
193-
// "all" ABI.
194-
("all", "tests/ui/abi/all/pass/*.rs", "pass"),
195-
("all", "tests/ui/abi/all/fail/*.rs", "fail"),
196-
];
197-
198-
// Retrieves or generates path to `trybuild` wrapper.
199-
let wrapper = env::var("TRYBUILD_WRAPPER").unwrap_or_else(|_| {
200-
generate_trybuild_wrapper().unwrap_or_else(|err| panic!("Error: {err}"))
201-
});
202-
203-
// Runs tests and tracks failures.
204-
let mut failures = Vec::new();
205-
for (abi, path, expected) in tests {
206-
let res = trybuild_wrapper_test(&wrapper, abi, path, expected);
207-
if res.is_err() {
208-
failures.push(format!(" {path}"));
209-
}
210-
}
211-
212-
// Only pass if there are no failures.
213-
assert!(failures.is_empty(), "failures:\n{}", failures.join("\n"));
214-
}
215-
216-
/// Generates a `trybuild` wrapper executable and returns its path.
217-
fn generate_trybuild_wrapper() -> Result<String, String> {
218-
// Composes `cargo build` command.
219-
let mut cmd = Command::new("cargo");
220-
// Removes `CARGO_*` env vars (except `CARGO_TARGET_DIR`).
221-
cmd.env_clear();
222-
cmd.envs(
223-
env::vars()
224-
.filter(|(key, _)| !key.starts_with("CARGO_") || key == "CARGO_TARGET_DIR"),
225-
);
226-
cmd.current_dir("tests/trybuild_wrapper").args([
227-
"build",
228-
"--release",
229-
// JSON output is easier to parse.
230-
"--message-format=json",
231-
]);
232-
233-
// Compiles `trybuild` wrapper.
234-
let error_msg = "Failed to generate `trybuild` wrapper";
235-
let output = cmd.output().map_err(|err| format!("{error_msg}: {err}"))?;
236-
if !output.status.success() {
237-
return Err(if output.stderr.is_empty() {
238-
error_msg.to_string()
239-
} else {
240-
format!("{error_msg}: {}", String::from_utf8_lossy(&output.stderr))
241-
})
242-
}
243-
244-
// Parses JSON output for path to executable.
245-
// Ref: <https://doc.rust-lang.org/cargo/reference/external-tools.html#artifact-messages>
246-
// Ref: <https://doc.rust-lang.org/rustc/json.html>
247-
let stdout = String::from_utf8_lossy(&output.stdout);
248-
let exec_path = stdout.lines().find_map(|line| {
249-
if !line.contains("\"compiler-artifact\"") {
250-
return None;
251-
}
252-
let pat = "\"executable\":\"";
253-
let pat_start = line.find(pat)?;
254-
let path_start = pat_start + pat.len();
255-
let target_substr = &line[path_start..];
256-
let end_pos = target_substr.find("\"")?;
257-
Some(&target_substr[..end_pos])
258-
});
259-
exec_path
260-
.map(ToString::to_string)
261-
.ok_or_else(|| error_msg.to_string())
262-
}
263-
264-
// Runs ABI ui tests with specified `trybuild_wrapper`.
265-
// See `tests/trybuild_wrapper` crate docs for motivation.
266-
fn trybuild_wrapper_test(
267-
wrapper: &str,
268-
abi: &str,
269-
path: &str,
270-
expected: &str,
271-
) -> Result<(), ExitStatus> {
272-
let mut cmd = Command::new(wrapper);
273-
cmd.args(["trybuild", "--path", path, "--expected", expected]);
274-
275-
// Override `cargo` path while preserving initial path used by the test command.
276-
if let Ok(cargo) = env::var("CARGO") {
277-
cmd.env("TRYBUILD_WRAPPER_CARGO", cargo);
278-
}
279-
cmd.env("CARGO", wrapper);
280-
281-
// Set ABI `cfg` flags that will be passed to `cargo` by the `trybuild` wrapper.
282-
let abi_cfg = format!(
283-
"--cfg\x1fink_abi=\"{abi}\"\x1f--check-cfg\x1fcfg(ink_abi,values(\"ink\",\"sol\",\"all\"))"
284-
);
285-
cmd.env("TRYBUILD_WRAPPER_ENCODED_FLAGS", abi_cfg);
286-
287-
// Enable `std` and `unstable-hostfn` features (needed by events and metadata tests).
288-
cmd.env("TRYBUILD_WRAPPER_CARGO_ARGS", "--features std");
289-
290-
let exit_status = cmd.status().unwrap();
291-
if !exit_status.success() {
292-
return Err(exit_status)
293-
}
294-
295-
Ok(())
296-
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const _: [u8; 4] = ink::selector_bytes!(Abi::Ink, "test");
2+
3+
fn main() {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use ink_ir as ir;
2+
3+
macro_rules! assert_ink_abi {
4+
( $input:literal ) => {{
5+
const HASH: [u8; 4] = ink::selector_bytes!(Abi::Ink, $input);
6+
assert_eq!(
7+
HASH,
8+
ir::Selector::compute($input.as_bytes(), ir::SelectorAbi::Ink).to_bytes(),
9+
);
10+
}};
11+
}
12+
13+
macro_rules! assert_sol_abi {
14+
( $input:literal ) => {{
15+
const HASH: [u8; 4] = ink::selector_bytes!(Abi::Sol, $input);
16+
assert_eq!(
17+
HASH,
18+
ir::Selector::compute($input.as_bytes(), ir::SelectorAbi::Sol).to_bytes(),
19+
);
20+
}};
21+
}
22+
23+
fn main() {
24+
// Test ink! ABI
25+
assert_ink_abi!("hello");
26+
assert_ink_abi!("transfer");
27+
28+
// Test Solidity ABI
29+
assert_sol_abi!("hello");
30+
assert_sol_abi!("transfer");
31+
32+
// Verify they produce different results
33+
const INK_HELLO: [u8; 4] = ink::selector_bytes!(Abi::Ink, "hello");
34+
const SOL_HELLO: [u8; 4] = ink::selector_bytes!(Abi::Sol, "hello");
35+
assert_ne!(INK_HELLO, SOL_HELLO);
36+
37+
println!("Ink selector for 'hello': {:?}", INK_HELLO);
38+
println!("Sol selector for 'hello': {:?}", SOL_HELLO);
39+
}

0 commit comments

Comments
 (0)