]> git.lizzy.rs Git - rust.git/blob - src/librustc_lexer/src/tests.rs
Fix font color for help button in ayu and dark themes
[rust.git] / src / librustc_lexer / src / tests.rs
1 #[cfg(test)]
2 mod tests {
3     use crate::*;
4
5     fn check_raw_str(s: &str, expected_hashes: u16, expected_err: Option<RawStrError>) {
6         let s = &format!("r{}", s);
7         let mut cursor = Cursor::new(s);
8         cursor.bump();
9         let (n_hashes, err) = cursor.raw_double_quoted_string(0);
10         assert_eq!(n_hashes, expected_hashes);
11         assert_eq!(err, expected_err);
12     }
13
14     #[test]
15     fn test_naked_raw_str() {
16         check_raw_str(r#""abc""#, 0, None);
17     }
18
19     #[test]
20     fn test_raw_no_start() {
21         check_raw_str(r##""abc"#"##, 0, None);
22     }
23
24     #[test]
25     fn test_too_many_terminators() {
26         // this error is handled in the parser later
27         check_raw_str(r###"#"abc"##"###, 1, None);
28     }
29
30     #[test]
31     fn test_unterminated() {
32         check_raw_str(
33             r#"#"abc"#,
34             1,
35             Some(RawStrError::NoTerminator {
36                 expected: 1,
37                 found: 0,
38                 possible_terminator_offset: None,
39             }),
40         );
41         check_raw_str(
42             r###"##"abc"#"###,
43             2,
44             Some(RawStrError::NoTerminator {
45                 expected: 2,
46                 found: 1,
47                 possible_terminator_offset: Some(7),
48             }),
49         );
50         // We're looking for "# not just any #
51         check_raw_str(
52             r###"##"abc#"###,
53             2,
54             Some(RawStrError::NoTerminator {
55                 expected: 2,
56                 found: 0,
57                 possible_terminator_offset: None,
58             }),
59         )
60     }
61
62     #[test]
63     fn test_invalid_start() {
64         check_raw_str(r##"#~"abc"#"##, 1, Some(RawStrError::InvalidStarter { bad_char: '~' }));
65     }
66
67     #[test]
68     fn test_unterminated_no_pound() {
69         // https://github.com/rust-lang/rust/issues/70677
70         check_raw_str(
71             r#"""#,
72             0,
73             Some(RawStrError::NoTerminator {
74                 expected: 0,
75                 found: 0,
76                 possible_terminator_offset: None,
77             }),
78         );
79     }
80
81     #[test]
82     fn test_valid_shebang() {
83         // https://github.com/rust-lang/rust/issues/70528
84         let input = "#!/usr/bin/rustrun\nlet x = 5;";
85         assert_eq!(strip_shebang(input), Some(18));
86     }
87
88     #[test]
89     fn test_invalid_shebang_valid_rust_syntax() {
90         // https://github.com/rust-lang/rust/issues/70528
91         let input = "#!    [bad_attribute]";
92         assert_eq!(strip_shebang(input), None);
93     }
94
95     #[test]
96     fn test_shebang_second_line() {
97         // Because shebangs are interpreted by the kernel, they must be on the first line
98         let input = "\n#!/bin/bash";
99         assert_eq!(strip_shebang(input), None);
100     }
101
102     #[test]
103     fn test_shebang_space() {
104         let input = "#!    /bin/bash";
105         assert_eq!(strip_shebang(input), Some(input.len()));
106     }
107
108     #[test]
109     fn test_shebang_empty_shebang() {
110         let input = "#!    \n[attribute(foo)]";
111         assert_eq!(strip_shebang(input), None);
112     }
113
114     #[test]
115     fn test_invalid_shebang_comment() {
116         let input = "#!//bin/ami/a/comment\n[";
117         assert_eq!(strip_shebang(input), None)
118     }
119
120     #[test]
121     fn test_invalid_shebang_another_comment() {
122         let input = "#!/*bin/ami/a/comment*/\n[attribute";
123         assert_eq!(strip_shebang(input), None)
124     }
125
126     #[test]
127     fn test_shebang_valid_rust_after() {
128         let input = "#!/*bin/ami/a/comment*/\npub fn main() {}";
129         assert_eq!(strip_shebang(input), Some(23))
130     }
131
132     #[test]
133     fn test_shebang_followed_by_attrib() {
134         let input = "#!/bin/rust-scripts\n#![allow_unused(true)]";
135         assert_eq!(strip_shebang(input), Some(19));
136     }
137 }