Skip to content

Commit f2bbcbd

Browse files
authored
test(parser): semantic & syntax tests from ethereum/solidity (#787)
* test(parser): semantic tests from ethereum/solidity Signed-off-by: Alexey Shekhirin <a.shekhirin@gmail.com>
1 parent f8a1e9c commit f2bbcbd

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

.github/workflows/test.yml

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ jobs:
3939
# Make sure "git describe --tags" works for solang --version
4040
# checkout@v2 requires git 2.18 or higher, which is not in our image
4141
uses: actions/checkout@v1
42+
with:
43+
submodules: recursive
4244
- name: Rust stable
4345
run: rustup default 1.59.0
4446
- name: Build
@@ -61,6 +63,8 @@ jobs:
6163
# Make sure "git describe --tags" works for solang --version
6264
# checkout@v2 requires git 2.18 or higher, which is not in our image
6365
uses: actions/checkout@v1
66+
with:
67+
submodules: recursive
6468
- name: Rust stable
6569
run: rustup default 1.59.0
6670
- name: Build
@@ -81,6 +85,7 @@ jobs:
8185
with:
8286
# Make sure "git describe --tags" works for solang --version
8387
fetch-depth: 0
88+
submodules: recursive
8489
- name: Download LLVM
8590
run: curl -sSL -o c:\llvm.zip https://github.com/hyperledger-labs/solang/releases/download/v0.1.11/llvm13.0-win.zip
8691
- name: Extract LLVM
@@ -115,6 +120,7 @@ jobs:
115120
with:
116121
# Make sure "git describe --tags" works for solang --version
117122
fetch-depth: 0
123+
submodules: recursive
118124
- name: Download LLVM
119125
run: curl -L --output llvm13.0-mac-arm.tar.xz https://github.com/hyperledger-labs/solang/releases/download/v0.1.11/llvm13.0-mac-arm.tar.xz
120126
- name: Extract LLVM
@@ -139,6 +145,7 @@ jobs:
139145
with:
140146
# Make sure "git describe --tags" works for solang --version
141147
fetch-depth: 0
148+
submodules: recursive
142149
- name: Download LLVM
143150
run: wget -q -O llvm13.0-mac-intel.tar.xz https://github.com/hyperledger-labs/solang/releases/download/v0.1.11/llvm13.0-mac-intel.tar.xz
144151
- name: Extract LLVM

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "solang-parser/testdata/solidity"]
2+
path = solang-parser/testdata/solidity
3+
url = https://github.com/ethereum/solidity

solang-parser/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ lalrpop-util = "0.19"
1818
phf = { version = "0.10", features = ["macros"] }
1919
unicode-xid = "0.2.0"
2020
itertools = "0.10"
21+
22+
[dev-dependencies]
23+
walkdir = "2.3.2"
24+
regex = "1.5.5"

solang-parser/src/test.rs

+114
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use crate::lexer::Lexer;
22
use crate::pt::*;
33
use crate::solidity;
4+
use std::sync::mpsc;
5+
use std::time::Duration;
6+
use std::{fs, path::Path, thread};
7+
use walkdir::WalkDir;
48

59
#[test]
610
fn parse_test() {
@@ -1003,3 +1007,113 @@ contract C {
10031007
let (actual_parse_tree, _) = crate::parse(src, 0).unwrap();
10041008
assert_eq!(actual_parse_tree.0.len(), 1);
10051009
}
1010+
1011+
#[test]
1012+
fn test_libsolidity() {
1013+
fn timeout_after<T, F>(d: Duration, f: F) -> Result<T, String>
1014+
where
1015+
T: Send + 'static,
1016+
F: FnOnce() -> T,
1017+
F: Send + 'static,
1018+
{
1019+
let (done_tx, done_rx) = mpsc::channel();
1020+
let handle = thread::spawn(move || {
1021+
let val = f();
1022+
done_tx.send(()).expect("Unable to send completion signal");
1023+
val
1024+
});
1025+
1026+
match done_rx.recv_timeout(d) {
1027+
Ok(_) => Ok(handle.join().expect("Thread panicked")),
1028+
Err(_) => Err(format!("Thread timeout-ed after {d:?}")),
1029+
}
1030+
}
1031+
1032+
let source_delimiter = regex::Regex::new(r"====.*====").unwrap();
1033+
let error_matcher = regex::Regex::new(r"// ----\r?\n// \w+( \d+)?:").unwrap();
1034+
1035+
let semantic_tests = WalkDir::new(
1036+
Path::new(env!("CARGO_MANIFEST_DIR"))
1037+
.join("testdata/solidity/test/libsolidity/semanticTests"),
1038+
)
1039+
.into_iter()
1040+
.collect::<Result<Vec<_>, _>>()
1041+
.unwrap()
1042+
.into_iter()
1043+
.map(|entry| (false, entry));
1044+
1045+
let syntax_tests = WalkDir::new(
1046+
Path::new(env!("CARGO_MANIFEST_DIR"))
1047+
.join("testdata/solidity/test/libsolidity/syntaxTests"),
1048+
)
1049+
.into_iter()
1050+
.collect::<Result<Vec<_>, _>>()
1051+
.unwrap()
1052+
.into_iter()
1053+
.map(|entry| (true, entry));
1054+
1055+
let errors = semantic_tests
1056+
.into_iter()
1057+
.chain(syntax_tests)
1058+
.map::<Result<_, String>, _>(|(syntax_test, entry)| {
1059+
if entry.file_name().to_string_lossy().ends_with(".sol") {
1060+
let source = match fs::read_to_string(entry.path()) {
1061+
Ok(source) => source,
1062+
Err(err) if matches!(err.kind(), std::io::ErrorKind::InvalidData) => {
1063+
return Ok(vec![])
1064+
}
1065+
Err(err) => return Err(err.to_string()),
1066+
};
1067+
1068+
let expect_error = syntax_test && error_matcher.is_match(&source);
1069+
1070+
Ok(source_delimiter
1071+
.split(&source)
1072+
.filter(|source_part| !source_part.is_empty())
1073+
.map(|part| {
1074+
(
1075+
entry.path().to_string_lossy().to_string(),
1076+
expect_error,
1077+
part.to_string(),
1078+
)
1079+
})
1080+
.collect::<Vec<_>>())
1081+
} else {
1082+
Ok(vec![])
1083+
}
1084+
})
1085+
.collect::<Result<Vec<_>, _>>()
1086+
.unwrap()
1087+
.into_iter()
1088+
.flatten()
1089+
.filter_map(|(path, expect_error, source_part)| {
1090+
let result = match timeout_after(Duration::from_secs(5), move || {
1091+
crate::parse(&source_part, 0)
1092+
}) {
1093+
Ok(result) => result,
1094+
Err(err) => return Some(format!("{:?}: \n\t{}", path, err)),
1095+
};
1096+
1097+
if let (Err(err), false) = (
1098+
result.map_err(|diags| {
1099+
format!(
1100+
"{:?}:\n\t{}",
1101+
path,
1102+
diags
1103+
.iter()
1104+
.map(|diag| format!("{diag:?}"))
1105+
.collect::<Vec<_>>()
1106+
.join("\n\t")
1107+
)
1108+
}),
1109+
expect_error,
1110+
) {
1111+
return Some(err);
1112+
}
1113+
1114+
None
1115+
})
1116+
.collect::<Vec<_>>();
1117+
1118+
assert!(errors.is_empty(), "{}", errors.join("\n"));
1119+
}

solang-parser/testdata/solidity

Submodule solidity added at d55b84f

0 commit comments

Comments
 (0)