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