|
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 |
| - } |
| 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)); |
137 | 122 | }
|
0 commit comments