Debian Codegraph @

src/cfg_expr/tests/lexer.rs

raw
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 use alloc::{vec, vec::Vec};
4
5 use crate::cfg_expr::expr::lexer::{Lexer, Token};
6
7 macro_rules! test_lex {
8 ($text:expr, [$($token:expr),+$(,)?]) => {
9 let lexed: Vec<_> = Lexer::new($text).map(|lt| lt.unwrap().token).collect();
10 let expected = vec![$($token),+];
11
12 assert_eq!(lexed, expected);
13 }
14 }
15
16 #[test]
17 fn handles_raw() {
18 test_lex!("key", [Token::Key("key")]);
19 }
20
21 #[test]
22 fn strips_attribute() {
23 test_lex!("cfg(key)", [Token::Key("key")]);
24 }
25
26 #[test]
27 fn handle_key_value() {
28 test_lex!("key = \"value\"", [Token::Key("key"), Token::Equals, Token::Value("value"),]);
29 }
30
31 #[test]
32 fn handle_empty_value() {
33 test_lex!("key = \"\"", [Token::Key("key"), Token::Equals, Token::Value(""),]);
34 }
35
36 #[test]
37 fn handle_short_key() {
38 test_lex!("k", [Token::Key("k"),]);
39 }
40
41 #[test]
42 fn handle_cfg_keywords() {
43 test_lex!("all(any(not(any_blah,all_nope,not_any)))", [
44 Token::All,
45 Token::OpenParen,
46 Token::Any,
47 Token::OpenParen,
48 Token::Not,
49 Token::OpenParen,
50 Token::Key("any_blah"),
51 Token::Comma,
52 Token::Key("all_nope"),
53 Token::Comma,
54 Token::Key("not_any"),
55 Token::CloseParen,
56 Token::CloseParen,
57 Token::CloseParen,
58 ]);
59 }