diff options
Diffstat (limited to 'crates/ra_syntax/src/tests.rs')
-rw-r--r-- | crates/ra_syntax/src/tests.rs | 79 |
1 files changed, 58 insertions, 21 deletions
diff --git a/crates/ra_syntax/src/tests.rs b/crates/ra_syntax/src/tests.rs index 458920607..fb22b9e54 100644 --- a/crates/ra_syntax/src/tests.rs +++ b/crates/ra_syntax/src/tests.rs | |||
@@ -1,18 +1,28 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | fmt::Write, | 2 | fmt::Write, |
3 | path::{Component, PathBuf}, | 3 | path::{Component, Path, PathBuf}, |
4 | }; | 4 | }; |
5 | 5 | ||
6 | use test_utils::{collect_tests, dir_tests, project_dir, read_text}; | 6 | use test_utils::{collect_tests, dir_tests, project_dir, read_text}; |
7 | 7 | ||
8 | use crate::{fuzz, SourceFile}; | 8 | use crate::{fuzz, tokenize, Location, SourceFile, SyntaxError, TextRange, Token}; |
9 | 9 | ||
10 | #[test] | 10 | #[test] |
11 | fn lexer_tests() { | 11 | fn lexer_tests() { |
12 | dir_tests(&test_data_dir(), &["lexer"], |text, _| { | 12 | // FIXME: |
13 | let tokens = crate::tokenize(text); | 13 | // * Add tests for unicode escapes in byte-character and [raw]-byte-string literals |
14 | dump_tokens(&tokens, text) | 14 | // * Add tests for unescape errors |
15 | }) | 15 | |
16 | dir_tests(&test_data_dir(), &["lexer/ok"], |text, path| { | ||
17 | let (tokens, errors) = tokenize(text); | ||
18 | assert_errors_are_absent(&errors, path); | ||
19 | dump_tokens_and_errors(&tokens, &errors, text) | ||
20 | }); | ||
21 | dir_tests(&test_data_dir(), &["lexer/err"], |text, path| { | ||
22 | let (tokens, errors) = tokenize(text); | ||
23 | assert_errors_are_present(&errors, path); | ||
24 | dump_tokens_and_errors(&tokens, &errors, text) | ||
25 | }); | ||
16 | } | 26 | } |
17 | 27 | ||
18 | #[test] | 28 | #[test] |
@@ -32,18 +42,13 @@ fn parser_tests() { | |||
32 | dir_tests(&test_data_dir(), &["parser/inline/ok", "parser/ok"], |text, path| { | 42 | dir_tests(&test_data_dir(), &["parser/inline/ok", "parser/ok"], |text, path| { |
33 | let parse = SourceFile::parse(text); | 43 | let parse = SourceFile::parse(text); |
34 | let errors = parse.errors(); | 44 | let errors = parse.errors(); |
35 | assert_eq!( | 45 | assert_errors_are_absent(&errors, path); |
36 | errors, | ||
37 | &[] as &[crate::SyntaxError], | ||
38 | "There should be no errors in the file {:?}", | ||
39 | path.display(), | ||
40 | ); | ||
41 | parse.debug_dump() | 46 | parse.debug_dump() |
42 | }); | 47 | }); |
43 | dir_tests(&test_data_dir(), &["parser/err", "parser/inline/err"], |text, path| { | 48 | dir_tests(&test_data_dir(), &["parser/err", "parser/inline/err"], |text, path| { |
44 | let parse = SourceFile::parse(text); | 49 | let parse = SourceFile::parse(text); |
45 | let errors = parse.errors(); | 50 | let errors = parse.errors(); |
46 | assert!(!errors.is_empty(), "There should be errors in the file {:?}", path.display()); | 51 | assert_errors_are_present(&errors, path); |
47 | parse.debug_dump() | 52 | parse.debug_dump() |
48 | }); | 53 | }); |
49 | } | 54 | } |
@@ -75,7 +80,7 @@ fn self_hosting_parsing() { | |||
75 | .into_iter() | 80 | .into_iter() |
76 | .filter_entry(|entry| { | 81 | .filter_entry(|entry| { |
77 | !entry.path().components().any(|component| { | 82 | !entry.path().components().any(|component| { |
78 | // Get all files which are not in the crates/ra_syntax/tests/data folder | 83 | // Get all files which are not in the crates/ra_syntax/test_data folder |
79 | component == Component::Normal(OsStr::new("test_data")) | 84 | component == Component::Normal(OsStr::new("test_data")) |
80 | }) | 85 | }) |
81 | }) | 86 | }) |
@@ -101,15 +106,47 @@ fn test_data_dir() -> PathBuf { | |||
101 | project_dir().join("crates/ra_syntax/test_data") | 106 | project_dir().join("crates/ra_syntax/test_data") |
102 | } | 107 | } |
103 | 108 | ||
104 | fn dump_tokens(tokens: &[crate::Token], text: &str) -> String { | 109 | fn assert_errors_are_present(errors: &[SyntaxError], path: &Path) { |
110 | assert!(!errors.is_empty(), "There should be errors in the file {:?}", path.display()); | ||
111 | } | ||
112 | fn assert_errors_are_absent(errors: &[SyntaxError], path: &Path) { | ||
113 | assert_eq!( | ||
114 | errors, | ||
115 | &[] as &[SyntaxError], | ||
116 | "There should be no errors in the file {:?}", | ||
117 | path.display(), | ||
118 | ); | ||
119 | } | ||
120 | |||
121 | fn dump_tokens_and_errors(tokens: &[Token], errors: &[SyntaxError], text: &str) -> String { | ||
105 | let mut acc = String::new(); | 122 | let mut acc = String::new(); |
106 | let mut offset = 0; | 123 | let mut offset = 0; |
107 | for token in tokens { | 124 | for token in tokens { |
108 | let len: u32 = token.len.into(); | 125 | let token_len = token.len.to_usize(); |
109 | let len = len as usize; | 126 | let token_text = &text[offset..offset + token_len]; |
110 | let token_text = &text[offset..offset + len]; | 127 | offset += token_len; |
111 | offset += len; | 128 | writeln!(acc, "{:?} {} {:?}", token.kind, token_len, token_text).unwrap(); |
112 | write!(acc, "{:?} {} {:?}\n", token.kind, token.len, token_text).unwrap() | 129 | } |
130 | for err in errors { | ||
131 | let err_range = location_to_range(err.location()); | ||
132 | writeln!( | ||
133 | acc, | ||
134 | "> error{:?} token({:?}) msg({})", | ||
135 | err.location(), | ||
136 | &text[err_range], | ||
137 | err.kind() | ||
138 | ) | ||
139 | .unwrap(); | ||
140 | } | ||
141 | return acc; | ||
142 | |||
143 | // FIXME: copy-pasted this from `ra_ide/src/diagnostics.rs` | ||
144 | // `Location` will be refactored soon in new PR, see todos here: | ||
145 | // https://github.com/rust-analyzer/rust-analyzer/issues/223 | ||
146 | fn location_to_range(location: Location) -> TextRange { | ||
147 | match location { | ||
148 | Location::Offset(offset) => TextRange::offset_len(offset, 1.into()), | ||
149 | Location::Range(range) => range, | ||
150 | } | ||
113 | } | 151 | } |
114 | acc | ||
115 | } | 152 | } |