]>
Commit | Line | Data |
---|---|---|
064997fb FG |
1 | use crate::{LexedStr, PrefixEntryPoint, Step}; |
2 | ||
3 | #[test] | |
4 | fn vis() { | |
5 | check(PrefixEntryPoint::Vis, "pub(crate) fn foo() {}", "pub(crate)"); | |
6 | check(PrefixEntryPoint::Vis, "fn foo() {}", ""); | |
7 | check(PrefixEntryPoint::Vis, "pub(fn foo() {}", "pub"); | |
8 | check(PrefixEntryPoint::Vis, "pub(crate fn foo() {}", "pub(crate"); | |
9 | check(PrefixEntryPoint::Vis, "crate fn foo() {}", "crate"); | |
10 | } | |
11 | ||
12 | #[test] | |
13 | fn block() { | |
14 | check(PrefixEntryPoint::Block, "{}, 92", "{}"); | |
15 | check(PrefixEntryPoint::Block, "{, 92)", "{, 92)"); | |
16 | check(PrefixEntryPoint::Block, "()", ""); | |
17 | } | |
18 | ||
19 | #[test] | |
20 | fn stmt() { | |
21 | check(PrefixEntryPoint::Stmt, "92; fn", "92"); | |
22 | check(PrefixEntryPoint::Stmt, "let _ = 92; 1", "let _ = 92"); | |
23 | check(PrefixEntryPoint::Stmt, "pub fn f() {} = 92", "pub fn f() {}"); | |
24 | check(PrefixEntryPoint::Stmt, "struct S;;", "struct S;"); | |
25 | check(PrefixEntryPoint::Stmt, "fn f() {};", "fn f() {}"); | |
26 | check(PrefixEntryPoint::Stmt, ";;;", ";"); | |
27 | check(PrefixEntryPoint::Stmt, "+", "+"); | |
28 | check(PrefixEntryPoint::Stmt, "@", "@"); | |
29 | check(PrefixEntryPoint::Stmt, "loop {} - 1", "loop {}"); | |
30 | } | |
31 | ||
32 | #[test] | |
33 | fn pat() { | |
34 | check(PrefixEntryPoint::Pat, "x y", "x"); | |
35 | check(PrefixEntryPoint::Pat, "fn f() {}", "fn"); | |
fe692bf9 | 36 | check(PrefixEntryPoint::Pat, ".. ..", ".."); |
064997fb FG |
37 | } |
38 | ||
39 | #[test] | |
40 | fn ty() { | |
41 | check(PrefixEntryPoint::Ty, "fn() foo", "fn()"); | |
42 | check(PrefixEntryPoint::Ty, "Clone + Copy + fn", "Clone + Copy +"); | |
43 | check(PrefixEntryPoint::Ty, "struct f", "struct"); | |
44 | } | |
45 | ||
46 | #[test] | |
47 | fn expr() { | |
48 | check(PrefixEntryPoint::Expr, "92 92", "92"); | |
49 | check(PrefixEntryPoint::Expr, "+1", "+"); | |
50 | check(PrefixEntryPoint::Expr, "-1", "-1"); | |
51 | check(PrefixEntryPoint::Expr, "fn foo() {}", "fn"); | |
52 | check(PrefixEntryPoint::Expr, "#[attr] ()", "#[attr] ()"); | |
9ffffee4 FG |
53 | check(PrefixEntryPoint::Expr, "foo.0", "foo.0"); |
54 | check(PrefixEntryPoint::Expr, "foo.0.1", "foo.0.1"); | |
55 | check(PrefixEntryPoint::Expr, "foo.0. foo", "foo.0. foo"); | |
064997fb FG |
56 | } |
57 | ||
58 | #[test] | |
59 | fn path() { | |
60 | check(PrefixEntryPoint::Path, "foo::bar baz", "foo::bar"); | |
61 | check(PrefixEntryPoint::Path, "foo::<> baz", "foo::<>"); | |
62 | check(PrefixEntryPoint::Path, "foo<> baz", "foo<>"); | |
63 | check(PrefixEntryPoint::Path, "Fn() -> i32?", "Fn() -> i32"); | |
64 | // FIXME: This shouldn't be accepted as path actually. | |
65 | check(PrefixEntryPoint::Path, "<_>::foo", "<_>::foo"); | |
66 | } | |
67 | ||
68 | #[test] | |
69 | fn item() { | |
70 | // FIXME: This shouldn't consume the semicolon. | |
71 | check(PrefixEntryPoint::Item, "fn foo() {};", "fn foo() {};"); | |
72 | check(PrefixEntryPoint::Item, "#[attr] pub struct S {} 92", "#[attr] pub struct S {}"); | |
73 | check(PrefixEntryPoint::Item, "item!{}?", "item!{}"); | |
74 | check(PrefixEntryPoint::Item, "????", "?"); | |
75 | } | |
76 | ||
77 | #[test] | |
78 | fn meta_item() { | |
79 | check(PrefixEntryPoint::MetaItem, "attr, ", "attr"); | |
80 | check(PrefixEntryPoint::MetaItem, "attr(some token {stream});", "attr(some token {stream})"); | |
81 | check(PrefixEntryPoint::MetaItem, "path::attr = 2 * 2!", "path::attr = 2 * 2"); | |
82 | } | |
83 | ||
84 | #[track_caller] | |
85 | fn check(entry: PrefixEntryPoint, input: &str, prefix: &str) { | |
86 | let lexed = LexedStr::new(input); | |
87 | let input = lexed.to_input(); | |
88 | ||
89 | let mut n_tokens = 0; | |
90 | for step in entry.parse(&input).iter() { | |
91 | match step { | |
92 | Step::Token { n_input_tokens, .. } => n_tokens += n_input_tokens as usize, | |
9ffffee4 | 93 | Step::FloatSplit { .. } => n_tokens += 1, |
064997fb FG |
94 | Step::Enter { .. } | Step::Exit | Step::Error { .. } => (), |
95 | } | |
96 | } | |
97 | ||
98 | let mut i = 0; | |
99 | loop { | |
100 | if n_tokens == 0 { | |
101 | break; | |
102 | } | |
103 | if !lexed.kind(i).is_trivia() { | |
104 | n_tokens -= 1; | |
105 | } | |
106 | i += 1; | |
107 | } | |
108 | let buf = &lexed.as_str()[..lexed.text_start(i)]; | |
109 | assert_eq!(buf, prefix); | |
110 | } |