Move syntax tests to a dedicated crate

* test_fmt moves out of fmt crate
* test_parse _mostly_ moves out of parse crate and into `test_snapshots.rs` (some simple tests remain)
* now there's only two fuzz targets, fuzz_expr and fuzz_module, that cover both parsing and formatting
* added a system to auto-add new snapshot entries for new test files
* took some commented-out tests in `test_parse` and converted them to snapshot tests
* moved test_fmt's verification of formatting consistency into test_snapshots
* fixed a huge derp on my part where the fmt fuzzer in #4758 was completely useless (broken by refactoring just prior to submitting the PR)
* fixed a formatting bug found by fuzzing (bound_variable.expr.roc) - that I missed earlier due to ^^^ that derp
* no longer have roc_test_utils as a dependency in fmt - which was causing problems for the wasm build
This commit is contained in:
Joshua Warner 2022-12-26 12:50:31 -08:00
parent f0b3c3eb08
commit bfeddc470a
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
701 changed files with 1929 additions and 1516 deletions

View file

@ -0,0 +1,21 @@
[package]
name = "test_syntax"
version = "0.0.1"
authors = ["The Roc Contributors"]
license = "UPL-1.0"
edition = "2021"
description = "Tests for the parse + fmt crates."
[dependencies]
roc_collections = { path = "../collections" }
roc_region = { path = "../region" }
roc_module = { path = "../module" }
roc_parse = { path = "../parse" }
roc_fmt = { path = "../fmt" }
roc_test_utils = { path = "../../test_utils" }
bumpalo.workspace = true
[dev-dependencies]
pretty_assertions.workspace = true
indoc.workspace = true
walkdir.workspace = true

View file

@ -0,0 +1,3 @@
target
corpus
artifacts

View file

@ -0,0 +1,655 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "addr2line"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "ahash"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
dependencies = [
"getrandom",
"once_cell",
"version_check",
]
[[package]]
name = "arbitrary"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db55d72333851e17d572bec876e390cd3b11eb1ef53ae821dd9f3b653d2b4569"
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "backtrace"
version = "0.3.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cab84319d616cfb654d03394f38ab7e6f0919e181b1b57e1fd15e7fb4077d9a7"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "bitmaps"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "031043d04099746d8db04daf1fa424b2bc8bd69d92b25962dcde24da39ab64a2"
dependencies = [
"typenum",
]
[[package]]
name = "bitvec"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
dependencies = [
"funty",
"radium",
"tap",
"wyz",
]
[[package]]
name = "bumpalo"
version = "3.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba"
[[package]]
name = "cc"
version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "crossbeam-channel"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521"
dependencies = [
"cfg-if",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01a9af1f4c2ef74bb8aa1f7e19706bc72d03598c8a570bb5de72243c7a9d9d5a"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f"
dependencies = [
"cfg-if",
]
[[package]]
name = "ctor"
version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d2301688392eb071b0bf1a37be05c469d3cc4dbbd95df672fe28ab021e6a096"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "diff"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
[[package]]
name = "doc-comment"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10"
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "encode_unicode"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0"
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "funty"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
[[package]]
name = "getrandom"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "gimli"
version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d"
[[package]]
name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
dependencies = [
"ahash",
"bumpalo",
]
[[package]]
name = "heck"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "im"
version = "15.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0acd33ff0285af998aaf9b57342af478078f53492322fafc47450e09397e0e9"
dependencies = [
"bitmaps",
"rand_core",
"rand_xoshiro",
"sized-chunks",
"typenum",
"version_check",
]
[[package]]
name = "im-rc"
version = "15.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af1955a75fa080c677d3972822ec4bad316169ab1cfc6c257a942c2265dbe5fe"
dependencies = [
"bitmaps",
"rand_core",
"rand_xoshiro",
"sized-chunks",
"typenum",
"version_check",
]
[[package]]
name = "libc"
version = "0.2.131"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04c3b4822ccebfa39c02fc03d1534441b22ead323fa0f48bb7ddd8e6ba076a40"
[[package]]
name = "libfuzzer-sys"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee8c42ab62f43795ed77a965ed07994c5584cdc94fd0ebf14b22ac1524077acc"
dependencies = [
"arbitrary",
"cc",
]
[[package]]
name = "log"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if",
]
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "memoffset"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4"
dependencies = [
"autocfg",
]
[[package]]
name = "miniz_oxide"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f5c75688da582b8ffc1f1799e9db273f32133c49e048f614d22ec3256773ccc"
dependencies = [
"adler",
]
[[package]]
name = "num_cpus"
version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6058e64324c71e02bc2b150e4f3bc8286db6c83092132ffa3f6b1eab0f9def5"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "object"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21158b2c33aa6d4561f1c0a6ea283ca92bc54802a93b263e910746d679a7eb53"
dependencies = [
"memchr",
]
[[package]]
name = "once_cell"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1"
[[package]]
name = "output_vt100"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "628223faebab4e3e40667ee0b2336d34a5b960ff60ea743ddfdbcf7770bcfb66"
dependencies = [
"winapi",
]
[[package]]
name = "pretty_assertions"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a25e9bcb20aa780fd0bb16b72403a9064d6b3f22f026946029acb941a50af755"
dependencies = [
"ctor",
"diff",
"output_vt100",
"yansi",
]
[[package]]
name = "proc-macro2"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
"proc-macro2",
]
[[package]]
name = "radium"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
[[package]]
name = "rand_xoshiro"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f97cdb2a36ed4183de61b2f824cc45c9f1037f28afe0a322e9fff4c108b5aaa"
dependencies = [
"rand_core",
]
[[package]]
name = "rayon"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db3a213adf02b3bcfd2d3846bb41cb22857d131789e01df434fb7e7bc0759b7"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cac410af5d00ab6884528b4ab69d1e8e146e8d471201800fa1b4524126de6ad3"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]
[[package]]
name = "remove_dir_all"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "882f368737489ea543bc5c340e6f3d34a28c39980bd9a979e47322b26f60ac40"
dependencies = [
"libc",
"log",
"num_cpus",
"rayon",
"winapi",
]
[[package]]
name = "roc_collections"
version = "0.0.1"
dependencies = [
"bitvec",
"bumpalo",
"fnv",
"hashbrown",
"im",
"im-rc",
"wyhash",
]
[[package]]
name = "roc_error_macros"
version = "0.0.1"
[[package]]
name = "roc_fmt"
version = "0.0.1"
dependencies = [
"bumpalo",
"roc_collections",
"roc_module",
"roc_parse",
"roc_region",
"roc_test_utils",
]
[[package]]
name = "roc_ident"
version = "0.0.1"
[[package]]
name = "roc_module"
version = "0.0.1"
dependencies = [
"bumpalo",
"roc_collections",
"roc_error_macros",
"roc_ident",
"roc_region",
"snafu",
"static_assertions",
]
[[package]]
name = "roc_parse"
version = "0.0.1"
dependencies = [
"bumpalo",
"encode_unicode",
"roc_collections",
"roc_module",
"roc_region",
]
[[package]]
name = "roc_region"
version = "0.0.1"
dependencies = [
"static_assertions",
]
[[package]]
name = "roc_test_utils"
version = "0.0.1"
dependencies = [
"pretty_assertions",
"remove_dir_all",
]
[[package]]
name = "rustc-demangle"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "sized-chunks"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16d69225bde7a69b235da73377861095455d298f2b970996eec25ddbb42b3d1e"
dependencies = [
"bitmaps",
"typenum",
]
[[package]]
name = "snafu"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5177903bf45656592d9eb5c0e22f408fc023aae51dbe2088889b71633ba451f2"
dependencies = [
"backtrace",
"doc-comment",
"snafu-derive",
]
[[package]]
name = "snafu-derive"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "410b26ed97440d90ced3e2488c868d56a86e2064f5d7d6f417909b286afe25e5"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "syn"
version = "1.0.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tap"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "test_syntax"
version = "0.0.1"
dependencies = [
"bumpalo",
"roc_collections",
"roc_fmt",
"roc_module",
"roc_parse",
"roc_region",
"roc_test_utils",
]
[[package]]
name = "test_syntax-fuzz"
version = "0.0.0"
dependencies = [
"bumpalo",
"libfuzzer-sys",
"test_syntax",
]
[[package]]
name = "typenum"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
[[package]]
name = "unicode-ident"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf"
[[package]]
name = "version_check"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "wyhash"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf6e163c25e3fac820b4b453185ea2dea3b6a3e0a721d4d23d75bd33734c295"
dependencies = [
"rand_core",
]
[[package]]
name = "wyz"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30b31594f29d27036c383b53b59ed3476874d518f0efb151b27a4c275141390e"
dependencies = [
"tap",
]
[[package]]
name = "yansi"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"

View file

@ -0,0 +1,30 @@
[package]
name = "test_syntax-fuzz"
version = "0.0.0"
authors = ["Automatically generated"]
publish = false
edition = "2021"
[package.metadata]
cargo-fuzz = true
[dependencies]
libfuzzer-sys = "0.3"
bumpalo = { version = "3.6.1", features = ["collections"] }
test_syntax = { path = "../../test_syntax" }
# Prevent this from interfering with workspaces
[workspace]
members = ["."]
[[bin]]
name = "fuzz_expr"
path = "fuzz_targets/fuzz_expr.rs"
test = false
doc = false
[[bin]]
name = "fuzz_module"
path = "fuzz_targets/fuzz_module.rs"
test = false
doc = false

View file

@ -0,0 +1,13 @@
# fuzz
To setup fuzzing you will need to install cargo-fuzz and run with rust nightly:
```sh
$ cargo install cargo-fuzz
$ cargo +nightly fuzz run -j<cores> <target> -- -dict=../parse/fuzz/dict.txt
```
The different targets can be found by running `cargo fuzz list`.
When a bug is found, it will be reported with commands to run it again and look for a minimized version.
If you are going to file a bug, please minimize the input before filing the bug.

View file

@ -0,0 +1,40 @@
"if"
"then"
"else"
"when"
"as"
"is"
"expect"
"dbg"
"has"
"app"
"platform"
"package"
"provides"
"requires"
"exposes"
"imports"
"effects"
"interface"
"|>"
"=="
"!="
"&&"
"||"
"+"
"*"
"-"
"//"
"/"
"<="
"<"
">="
">"
"^"
"%"
":="
":"
"@"
"->"

View file

@ -0,0 +1,14 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
use bumpalo::Bump;
use test_syntax::test_helpers::Input;
fuzz_target!(|data: &[u8]| {
if let Ok(input) = std::str::from_utf8(data) {
let input = Input::Expr(input);
let arena = Bump::new();
if input.parse_in(&arena).is_ok() {
input.check_invariants(|_| (), true);
}
}
});

View file

@ -0,0 +1,14 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
use bumpalo::Bump;
use test_syntax::test_helpers::Input;
fuzz_target!(|data: &[u8]| {
if let Ok(input) = std::str::from_utf8(data) {
let input = Input::Full(input);
let arena = Bump::new();
if input.parse_in(&arena).is_ok() {
input.check_invariants(|_| (), true);
}
}
});

View file

@ -0,0 +1 @@
pub mod test_helpers;

View file

@ -0,0 +1,239 @@
use bumpalo::Bump;
use roc_fmt::{annotation::Formattable, module::fmt_module};
use roc_parse::{
ast::{Defs, Expr, Module},
module::module_defs,
parser::{Parser, SyntaxError},
state::State,
test_helpers::{parse_defs_with, parse_expr_with, parse_header_with},
};
use roc_test_utils::assert_multiline_str_eq;
use roc_fmt::spaces::RemoveSpaces;
use roc_fmt::Buf;
/// Source code to parse. Usually in the form of a test case.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Input<'a> {
/// A header (e.g. `interface "foo" ...`)
Header(&'a str),
/// A sequence of module definitions (e.g. `f = \x -> x + 1`)
ModuleDefs(&'a str),
/// A single expression
Expr(&'a str),
/// Both the header and the module defs
Full(&'a str),
}
// Owned version of `Input`
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InputOwned {
Header(String),
ModuleDefs(String),
Expr(String),
Full(String),
}
impl InputOwned {
fn as_ref(&self) -> Input {
match self {
InputOwned::Header(s) => Input::Header(s),
InputOwned::ModuleDefs(s) => Input::ModuleDefs(s),
InputOwned::Expr(s) => Input::Expr(s),
InputOwned::Full(s) => Input::Full(s),
}
}
}
/// Output AST of a successful parse
#[derive(Debug, Clone)]
pub enum Output<'a> {
Header(Module<'a>),
ModuleDefs(Defs<'a>),
Expr(Expr<'a>),
Full {
header: Module<'a>,
module_defs: Defs<'a>,
},
}
impl<'a> Output<'a> {
fn format(&self) -> InputOwned {
let arena = Bump::new();
let mut buf = Buf::new_in(&arena);
match self {
Output::Header(header) => {
fmt_module(&mut buf, header);
buf.fmt_end_of_file();
InputOwned::Header(buf.as_str().to_string())
}
Output::ModuleDefs(defs) => {
defs.format(&mut buf, 0);
buf.fmt_end_of_file();
InputOwned::ModuleDefs(buf.as_str().to_string())
}
Output::Expr(expr) => {
expr.format(&mut buf, 0);
InputOwned::Expr(buf.as_str().to_string())
}
Output::Full {
header,
module_defs,
} => {
fmt_module(&mut buf, header);
module_defs.format(&mut buf, 0);
buf.fmt_end_of_file();
InputOwned::Full(buf.as_str().to_string())
}
}
}
pub fn debug_format_inner(&self) -> String {
match self {
Output::Header(header) => format!("{:#?}\n", header),
Output::ModuleDefs(defs) => format!("{:#?}\n", defs),
Output::Expr(expr) => format!("{:#?}\n", expr),
Output::Full { .. } => format!("{:#?}\n", self),
}
}
}
impl<'a> RemoveSpaces<'a> for Output<'a> {
fn remove_spaces(&self, arena: &'a Bump) -> Self {
match self {
Output::Header(header) => Output::Header(header.remove_spaces(arena)),
Output::ModuleDefs(defs) => Output::ModuleDefs(defs.remove_spaces(arena)),
Output::Expr(expr) => Output::Expr(expr.remove_spaces(arena)),
Output::Full {
header,
module_defs,
} => Output::Full {
header: header.remove_spaces(arena),
module_defs: module_defs.remove_spaces(arena),
},
}
}
}
impl<'a> Input<'a> {
pub fn as_str(&self) -> &'a str {
match self {
Input::Header(s) => s,
Input::ModuleDefs(s) => s,
Input::Expr(s) => s,
Input::Full(s) => s,
}
}
pub fn parse_in(&self, arena: &'a Bump) -> Result<Output<'a>, SyntaxError<'a>> {
match self {
Input::Header(input) => {
let header = parse_header_with(arena, input)?;
Ok(Output::Header(header))
}
Input::ModuleDefs(input) => {
let module_defs = parse_defs_with(arena, input)?;
Ok(Output::ModuleDefs(module_defs))
}
Input::Expr(input) => {
let expr = parse_expr_with(arena, input)?;
Ok(Output::Expr(expr))
}
Input::Full(input) => {
let state = State::new(input.as_bytes());
let min_indent = 0;
let (_, header, state) = roc_parse::module::header()
.parse(arena, state.clone(), min_indent)
.map_err(|(_, fail)| SyntaxError::Header(fail))?;
let (_, module_defs, _state) = module_defs()
.parse(arena, state, min_indent)
.map_err(|(_, fail)| fail)?;
Ok(Output::Full {
header,
module_defs,
})
}
}
}
/// Parse and re-format the given input, and pass the output to `check_formatting`
/// for verification. The expectation is that `check_formatting` assert the result matches
/// expectations (or, overwrite the expectation based on a command-line flag)
/// Optionally, based on the value of `check_idempotency`, also verify that the formatting
/// is idempotent - that if we reformat the output, we get the same result.
pub fn check_invariants(
&self,
handle_formatted_output: impl Fn(Input),
check_idempotency: bool,
) {
let arena = Bump::new();
let actual = self.parse_in(&arena).unwrap_or_else(|err| {
panic!("Unexpected parse failure when parsing this for formatting:\n\n{}\n\nParse error was:\n\n{:?}\n\n", self.as_str(), err);
});
let output = actual.format();
handle_formatted_output(output.as_ref());
let reparsed_ast = output.as_ref().parse_in(&arena).unwrap_or_else(|err| {
panic!(
"After formatting, the source code no longer parsed!\n\n\
Parse error was: {:?}\n\n\
The original code was:\n\n{}\n\n\
The code that failed to parse:\n\n{}\n\n\
The original ast was:\n\n{:#?}\n\n",
err,
self.as_str(),
output.as_ref().as_str(),
actual
);
});
let ast_normalized = actual.remove_spaces(&arena);
let reparsed_ast_normalized = reparsed_ast.remove_spaces(&arena);
// HACK!
// We compare the debug format strings of the ASTs, because I'm finding in practice that _somewhere_ deep inside the ast,
// the PartialEq implementation is returning `false` even when the Debug-formatted impl is exactly the same.
// I don't have the patience to debug this right now, so let's leave it for another day...
// TODO: fix PartialEq impl on ast types
if format!("{:?}", ast_normalized) != format!("{:?}", reparsed_ast_normalized) {
panic!(
"Formatting bug; formatting didn't reparse to the same AST (after removing spaces)\n\n\
* * * Source code before formatting:\n{}\n\n\
* * * Source code after formatting:\n{}\n\n\
* * * AST before formatting:\n{:#?}\n\n\
* * * AST after formatting:\n{:#?}\n\n",
self.as_str(),
output.as_ref().as_str(),
ast_normalized,
reparsed_ast_normalized
);
}
// Now verify that the resultant formatting is _idempotent_ - i.e. that it doesn't change again if re-formatted
if check_idempotency {
let reformatted = reparsed_ast.format();
if output != reformatted {
eprintln!("Formatting bug; formatting is not stable.\nOriginal code:\n{}\n\nFormatted code:\n{}\n\n", self.as_str(), output.as_ref().as_str());
eprintln!("Reformatting the formatted code changed it again, as follows:\n\n");
assert_multiline_str_eq!(output.as_ref().as_str(), reformatted.as_ref().as_str());
}
}
}
}

View file

@ -0,0 +1 @@
Expr(Ability(DemandColon(@15), @7), @0)

View file

@ -0,0 +1,4 @@
MEq has
eq b c : a, a -> U64 | a has MEq
1

View file

@ -0,0 +1 @@
Expr(Ability(DemandAlignment(4, @49), @40), @0)

View file

@ -0,0 +1,5 @@
MEq has
eq : a, a -> U64 | a has MEq
neq : a, a -> U64 | a has MEq
1

View file

@ -0,0 +1 @@
Expr(Ability(DemandAlignment(-1, @8), @7), @0)

View file

@ -0,0 +1,4 @@
MEq has
eq : a, a -> U64 | a has MEq
1

View file

@ -0,0 +1 @@
Expr(Ability(DemandName(@12), @7), @0)

View file

@ -0,0 +1 @@
Expr(InParens(End(@3), @0), @0)

View file

@ -0,0 +1 @@
Expr(Space(HasTab, @17), @0)

View file

@ -0,0 +1,2 @@
# comment with a
4

View file

@ -0,0 +1 @@
Expr(DefMissingFinalExpr2(Start(@11), @11), @0)

View file

@ -0,0 +1 @@
Expr(BadOperator("++", @14), @0)

View file

@ -0,0 +1,2 @@
main =
[] ++ []

View file

@ -0,0 +1 @@
Expr(ElmStyleFunction(@2-5, @6), @0)

View file

@ -0,0 +1 @@
Expr(When(Pattern(Start(@28), @28), @0), @0)

View file

@ -0,0 +1,6 @@
when Just 4 is
Just 4 | ->
4
_ ->
2

View file

@ -0,0 +1 @@
Expr(Type(TInlineAlias(ArgumentNotLowercase(@50), @51), @4), @0)

View file

@ -0,0 +1 @@
f : List elem -> [Nil, Cons elem a] as LinkedList U

View file

@ -0,0 +1 @@
Expr(Type(TInlineAlias(NotAnAlias(@39), @40), @4), @0)

View file

@ -0,0 +1 @@
f : List elem -> [Nil, Cons elem a] as a

View file

@ -0,0 +1 @@
Expr(Type(TInlineAlias(Qualified(@39), @58), @4), @0)

View file

@ -0,0 +1 @@
f : List elem -> [Nil, Cons elem a] as Module.LinkedList a

View file

@ -0,0 +1 @@
Expr(Pattern(NotAPattern(@3), @3), @0)

View file

@ -0,0 +1 @@
Expr(When(IfGuard(Start(@28), @27), @0), @0)

View file

@ -0,0 +1,6 @@
when Just 4 is
Just if ->
4
_ ->
2

View file

@ -0,0 +1 @@
Expr(If(Else(@16), @0), @0)

View file

@ -0,0 +1 @@
if 5 == 5 then 2

View file

@ -0,0 +1 @@
Expr(If(IndentThenToken(@17), @8), @0)

View file

@ -0,0 +1,5 @@
x =
if 5 == 5
then 2 else 3
x

View file

@ -0,0 +1 @@
Header(Imports(ListEnd(@87), @65))

View file

@ -0,0 +1,4 @@
app "test-missing-comma"
packages { pf: "platform/main.roc" }
imports [pf.Task Base64]
provides [main, @Foo] to pf

View file

@ -0,0 +1 @@
Expr(BadOperator(":", @21), @0)

View file

@ -0,0 +1,4 @@
main =
(\x -> x) : I64
3

View file

@ -0,0 +1 @@
Expr(BadOperator("**", @13), @0)

View file

@ -0,0 +1,2 @@
main =
5 ** 3

View file

@ -0,0 +1 @@
Expr(Closure(Arg(@3), @0), @0)

View file

@ -0,0 +1 @@
Expr(Closure(Arg(@1), @0), @0)

View file

@ -0,0 +1 @@
Expr(Closure(Arg(@1), @0), @0)

View file

@ -0,0 +1 @@
Expr(Closure(IndentBody(@5), @0), @0)

View file

@ -0,0 +1 @@
Expr(List(End(@7), @0), @0)

View file

@ -0,0 +1 @@
[1, 2, , 3]

View file

@ -0,0 +1 @@
Expr(When(Pattern(List(End(@22), @15), @15), @0), @0)

View file

@ -0,0 +1,2 @@
when [] is
[1, 2, -> ""

View file

@ -0,0 +1 @@
Expr(When(Pattern(List(End(@22), @15), @15), @0), @0)

View file

@ -0,0 +1,3 @@
when [] is
[1, 2,
3] -> ""

View file

@ -0,0 +1 @@
Expr(When(Pattern(List(Rest(@16), @15), @15), @0), @0)

View file

@ -0,0 +1,2 @@
when [] is
[...] -> ""

View file

@ -0,0 +1 @@
Expr(List(End(@6), @0), @0)

View file

@ -0,0 +1 @@
Expr(Str(EndlessMulti(@3), @0), @0)

View file

@ -0,0 +1 @@
"""there is no end

View file

@ -0,0 +1 @@
Expr(When(Arrow(@24), @0), @0)

View file

@ -0,0 +1,6 @@
when Just 4 is
Just when ->
4
_ ->
2

View file

@ -0,0 +1 @@
Expr(Closure(Pattern(PInParens(End(@4), @1), @1), @0), @0)

View file

@ -0,0 +1 @@
Expr(Closure(Pattern(PInParens(End(@5), @1), @1), @0), @0)

View file

@ -0,0 +1 @@
Expr(Closure(Pattern(PInParens(End(@2), @1), @1), @0), @0)

View file

@ -0,0 +1 @@
Expr(Closure(Pattern(PInParens(End(@4), @1), @1), @0), @0)

View file

@ -0,0 +1 @@
Expr(Type(TRecord(End(@13), @4), @4), @0)

View file

@ -0,0 +1 @@
f : { a: Int,

View file

@ -0,0 +1 @@
Expr(Type(TRecord(Field(@6), @4), @4), @0)

View file

@ -0,0 +1 @@
Expr(Type(TRecord(End(@11), @4), @4), @0)

View file

@ -0,0 +1 @@
f : { foo bar }

View file

@ -0,0 +1 @@
Expr(Type(TRecord(End(@5), @4), @4), @0)

View file

@ -0,0 +1 @@
Expr(Type(TRecord(IndentEnd(@5), @4), @4), @0)

View file

@ -0,0 +1,2 @@
f : {
foo : I64,

View file

@ -0,0 +1 @@
Expr(Type(TRecord(Space(HasTab, @10), @4), @4), @0)

View file

@ -0,0 +1 @@
f : { foo }

View file

@ -0,0 +1 @@
Expr(Str(EndlessSingle(@1), @0), @0)

View file

@ -0,0 +1 @@
"there is no end

View file

@ -0,0 +1 @@
Header(Space(HasMisplacedCarriageReturn, @1))

View file

@ -0,0 +1 @@
Expr(Type(TTagUnion(End(@9), @4), @4), @0)

View file

@ -0,0 +1 @@
f : [Yes,

Some files were not shown because too many files have changed in this diff Show more