Skip to content

Commit e11907b

Browse files
committed
Add tests for well-formedness
1 parent 35c1b65 commit e11907b

File tree

9 files changed

+143
-18
lines changed

9 files changed

+143
-18
lines changed

Cargo.lock

Lines changed: 4 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/indexing/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
fxhash = "0.2.1"
88
hashbrown = "0.15.2"
99
indexmap = "2.7.0"
10+
insta = "1.42.0"
1011
itertools = "0.14.0"
1112
la-arena = "0.3.1"
1213
petgraph = "0.7.0"

crates/indexing/src/error.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ pub enum IndexingError {
1313
DuplicateSignature { signature: DeclarationId, duplicate: DeclarationId },
1414

1515
InvalidRole { role: DeclarationId },
16-
NonConsecutive { before: DeclarationId, after: DeclarationId },
16+
InvalidOrder { early: DeclarationId, late: DeclarationId },
17+
NonConsecutive { first: DeclarationId, second: DeclarationId },
1718
}
1819

1920
/// The kind of a duplicate item.

crates/indexing/src/wellformed.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,36 @@ fn check_type_item(errors: &mut Vec<IndexingError>, item: &TypeItem) {
3636

3737
fn check_value_group_id(errors: &mut Vec<IndexingError>, group: &ValueGroupId) {
3838
let ValueGroupId { signature, equations } = group;
39-
let signature = signature.iter().copied();
39+
if let Some(signature) = signature {
40+
if let [equation, ..] = &equations[..] {
41+
if signature > equation {
42+
errors.push(IndexingError::InvalidOrder { early: *equation, late: *signature });
43+
} else if !equation.consecutive_of(signature) {
44+
errors.push(IndexingError::NonConsecutive { first: *signature, second: *equation });
45+
}
46+
}
47+
}
4048
let equations = equations.iter().copied();
41-
for (zero, one) in signature.chain(equations).tuple_windows() {
42-
if !one.consecutive_of(zero) {
43-
errors.push(IndexingError::NonConsecutive { before: zero, after: one });
49+
for (first, second) in equations.tuple_windows() {
50+
if !second.consecutive_of(first) {
51+
errors.push(IndexingError::NonConsecutive { first, second });
4452
}
4553
}
4654
}
4755

4856
fn check_type_group_id(errors: &mut Vec<IndexingError>, group: &TypeGroupId) {
4957
if let TypeGroupId { signature: Some(signature), declaration: Some(declaration), .. } = group {
50-
if !declaration.consecutive_of(signature) {
51-
errors.push(IndexingError::NonConsecutive { before: *signature, after: *declaration });
58+
if signature > declaration {
59+
errors.push(IndexingError::InvalidOrder { early: *declaration, late: *signature });
60+
} else if !declaration.consecutive_of(signature) {
61+
errors.push(IndexingError::NonConsecutive { first: *signature, second: *declaration });
5262
}
5363
}
5464
if let TypeGroupId { declaration: Some(declaration), role: Some(role), .. } = group {
55-
if !role.consecutive_of(declaration) {
56-
errors.push(IndexingError::NonConsecutive { before: *declaration, after: *role });
65+
if declaration > role {
66+
errors.push(IndexingError::InvalidOrder { early: *role, late: *declaration });
67+
} else if !role.consecutive_of(declaration) {
68+
errors.push(IndexingError::NonConsecutive { first: *declaration, second: *role });
5769
}
5870
}
5971
}

crates/indexing/tests/indexing.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,57 @@
1+
use indexing::{IndexingErrors, IndexingResult};
2+
use rowan::ast::AstNode;
3+
use syntax::cst;
14

5+
fn index<'s>(lines: impl AsRef<[&'s str]>) -> (cst::Module, IndexingResult, IndexingErrors) {
6+
let source = format!("module Main where\n{}", lines.as_ref().join("\n"));
7+
8+
let lexed = lexing::lex(&source);
9+
let tokens = lexing::layout(&lexed);
10+
11+
let (module, _) = parsing::parse(&lexed, &tokens);
12+
let module = cst::Module::cast(module).unwrap();
13+
14+
let (index, errors) = indexing::index(&module);
15+
(module, index, errors)
16+
}
17+
18+
#[test]
19+
fn value_non_consecutive() {
20+
let (_, _, errors) = index([
21+
"isJust :: forall a. Maybe a -> Bool",
22+
"isJust (Just _) = true",
23+
"life = 42",
24+
"isJust Nothing = false",
25+
]);
26+
insta::assert_debug_snapshot!(errors);
27+
}
28+
29+
#[test]
30+
fn value_late_signature() {
31+
let (_, _, errors) = index([
32+
"isJust (Just _) = true",
33+
"isJust Nothing = false",
34+
"isJust :: forall a. Maybe a -> Bool",
35+
]);
36+
insta::assert_debug_snapshot!(errors);
37+
}
38+
39+
#[test]
40+
fn type_non_consecutive() {
41+
let (_, _, errors) = index([
42+
"data Maybe :: Type -> Type",
43+
"life :: Int",
44+
"life = 42",
45+
"data Maybe a = Just a | Nothing",
46+
]);
47+
insta::assert_debug_snapshot!(errors);
48+
}
49+
50+
#[test]
51+
fn type_late_signature() {
52+
let (_, _, errors) = index([
53+
"data Maybe a = Just a | Nothing",
54+
"data Maybe :: Type -> Type",
55+
]);
56+
insta::assert_debug_snapshot!(errors);
57+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: crates/indexing/tests/indexing.rs
3+
expression: errors
4+
snapshot_kind: text
5+
---
6+
[
7+
InvalidOrder {
8+
early: Id(
9+
0,
10+
),
11+
late: Id(
12+
1,
13+
),
14+
},
15+
]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: crates/indexing/tests/indexing.rs
3+
expression: errors
4+
snapshot_kind: text
5+
---
6+
[
7+
NonConsecutive {
8+
first: Id(
9+
0,
10+
),
11+
second: Id(
12+
3,
13+
),
14+
},
15+
]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: crates/indexing/tests/indexing.rs
3+
expression: errors
4+
snapshot_kind: text
5+
---
6+
[
7+
InvalidOrder {
8+
early: Id(
9+
0,
10+
),
11+
late: Id(
12+
2,
13+
),
14+
},
15+
]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: crates/indexing/tests/indexing.rs
3+
expression: errors
4+
snapshot_kind: text
5+
---
6+
[
7+
NonConsecutive {
8+
first: Id(
9+
1,
10+
),
11+
second: Id(
12+
3,
13+
),
14+
},
15+
]

0 commit comments

Comments
 (0)