diff options
Diffstat (limited to 'src/libsyntax_expand/parse')
| -rw-r--r-- | src/libsyntax_expand/parse/lexer/tests.rs | 38 | ||||
| -rw-r--r-- | src/libsyntax_expand/parse/tests.rs | 195 |
2 files changed, 116 insertions, 117 deletions
diff --git a/src/libsyntax_expand/parse/lexer/tests.rs b/src/libsyntax_expand/parse/lexer/tests.rs index 75e4ee805b2..2ca0224812b 100644 --- a/src/libsyntax_expand/parse/lexer/tests.rs +++ b/src/libsyntax_expand/parse/lexer/tests.rs @@ -1,14 +1,14 @@ use rustc_data_structures::sync::Lrc; use rustc_parse::lexer::StringReader; -use syntax::token::{self, Token, TokenKind}; use syntax::sess::ParseSess; -use syntax::source_map::{SourceMap, FilePathMapping}; +use syntax::source_map::{FilePathMapping, SourceMap}; +use syntax::token::{self, Token, TokenKind}; use syntax::util::comments::is_doc_comment; use syntax::with_default_globals; use syntax_pos::symbol::Symbol; use syntax_pos::{BytePos, Span}; -use errors::{Handler, emitter::EmitterWriter}; +use errors::{emitter::EmitterWriter, Handler}; use std::io; use std::path::PathBuf; @@ -22,17 +22,11 @@ fn mk_sess(sm: Lrc<SourceMap>) -> ParseSess { None, false, ); - ParseSess::with_span_handler( - Handler::with_emitter(true, None, Box::new(emitter)), - sm, - ) + ParseSess::with_span_handler(Handler::with_emitter(true, None, Box::new(emitter)), sm) } // Creates a string reader for the given string. -fn setup<'a>(sm: &SourceMap, - sess: &'a ParseSess, - teststr: String) - -> StringReader<'a> { +fn setup<'a>(sm: &SourceMap, sess: &'a ParseSess, teststr: String) -> StringReader<'a> { let sf = sm.new_source_file(PathBuf::from(teststr.clone()).into(), teststr); StringReader::new(sess, sf, None) } @@ -50,20 +44,14 @@ fn t1() { assert_eq!(string_reader.next_token(), token::Comment); assert_eq!(string_reader.next_token(), token::Whitespace); let tok1 = string_reader.next_token(); - let tok2 = Token::new( - mk_ident("fn"), - Span::with_root_ctxt(BytePos(21), BytePos(23)), - ); + let tok2 = Token::new(mk_ident("fn"), Span::with_root_ctxt(BytePos(21), BytePos(23))); assert_eq!(tok1.kind, tok2.kind); assert_eq!(tok1.span, tok2.span); assert_eq!(string_reader.next_token(), token::Whitespace); // Read another token. let tok3 = string_reader.next_token(); assert_eq!(string_reader.pos.clone(), BytePos(28)); - let tok4 = Token::new( - mk_ident("main"), - Span::with_root_ctxt(BytePos(24), BytePos(28)), - ); + let tok4 = Token::new(mk_ident("main"), Span::with_root_ctxt(BytePos(24), BytePos(28))); assert_eq!(tok3.kind, tok4.kind); assert_eq!(tok3.span, tok4.span); @@ -142,10 +130,7 @@ fn character_a() { with_default_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); - assert_eq!( - setup(&sm, &sh, "'a'".to_string()).next_token(), - mk_lit(token::Char, "a", None), - ); + assert_eq!(setup(&sm, &sh, "'a'".to_string()).next_token(), mk_lit(token::Char, "a", None),); }) } @@ -154,10 +139,7 @@ fn character_space() { with_default_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); - assert_eq!( - setup(&sm, &sh, "' '".to_string()).next_token(), - mk_lit(token::Char, " ", None), - ); + assert_eq!(setup(&sm, &sh, "' '".to_string()).next_token(), mk_lit(token::Char, " ", None),); }) } @@ -213,7 +195,7 @@ fn literal_suffixes() { setup(&sm, &sh, format!("{} suffix", $input)).next_token(), mk_lit(token::$tok_type, $tok_contents, None), ); - }} + }}; } test!("'a'", Char, "a"); diff --git a/src/libsyntax_expand/parse/tests.rs b/src/libsyntax_expand/parse/tests.rs index 30e83c151e2..154ccb25621 100644 --- a/src/libsyntax_expand/parse/tests.rs +++ b/src/libsyntax_expand/parse/tests.rs @@ -1,19 +1,19 @@ use crate::tests::{matches_codepattern, string_to_stream, with_error_checking_parse}; +use errors::PResult; use rustc_parse::new_parser_from_source_str; use syntax::ast::{self, Name, PatKind}; use syntax::attr::first_attr_value_str_by_name; -use syntax::sess::ParseSess; -use syntax::token::{self, Token}; use syntax::print::pprust::item_to_string; use syntax::ptr::P; +use syntax::sess::ParseSess; use syntax::source_map::FilePathMapping; use syntax::symbol::{kw, sym}; -use syntax::tokenstream::{DelimSpan, TokenTree, TokenStream}; +use syntax::token::{self, Token}; +use syntax::tokenstream::{DelimSpan, TokenStream, TokenTree}; use syntax::visit; use syntax::with_default_globals; -use syntax_pos::{Span, BytePos, Pos, FileName}; -use errors::PResult; +use syntax_pos::{BytePos, FileName, Pos, Span}; use std::path::PathBuf; @@ -25,8 +25,11 @@ fn sess() -> ParseSess { /// /// Returns `Ok(Some(item))` when successful, `Ok(None)` when no item was found, and `Err` /// when a syntax error occurred. -fn parse_item_from_source_str(name: FileName, source: String, sess: &ParseSess) - -> PResult<'_, Option<P<ast::Item>>> { +fn parse_item_from_source_str( + name: FileName, + source: String, + sess: &ParseSess, +) -> PResult<'_, Option<P<ast::Item>>> { new_parser_from_source_str(sess, name, source).parse_item() } @@ -36,17 +39,18 @@ fn sp(a: u32, b: u32) -> Span { } /// Parses a string, return an expression. -fn string_to_expr(source_str : String) -> P<ast::Expr> { +fn string_to_expr(source_str: String) -> P<ast::Expr> { with_error_checking_parse(source_str, &sess(), |p| p.parse_expr()) } /// Parses a string, returns an item. -fn string_to_item(source_str : String) -> Option<P<ast::Item>> { +fn string_to_item(source_str: String) -> Option<P<ast::Item>> { with_error_checking_parse(source_str, &sess(), |p| p.parse_item()) } #[should_panic] -#[test] fn bad_path_expr_1() { +#[test] +fn bad_path_expr_1() { with_default_globals(|| { string_to_expr("::abc::def::return".to_string()); }) @@ -54,51 +58,38 @@ fn string_to_item(source_str : String) -> Option<P<ast::Item>> { // Checks the token-tree-ization of macros. #[test] -fn string_to_tts_macro () { +fn string_to_tts_macro() { with_default_globals(|| { let tts: Vec<_> = string_to_stream("macro_rules! zip (($a)=>($a))".to_string()).trees().collect(); let tts: &[TokenTree] = &tts[..]; match tts { - [ - TokenTree::Token(Token { kind: token::Ident(name_macro_rules, false), .. }), - TokenTree::Token(Token { kind: token::Not, .. }), - TokenTree::Token(Token { kind: token::Ident(name_zip, false), .. }), - TokenTree::Delimited(_, macro_delim, macro_tts) - ] - if name_macro_rules == &sym::macro_rules && name_zip.as_str() == "zip" => { + [TokenTree::Token(Token { kind: token::Ident(name_macro_rules, false), .. }), TokenTree::Token(Token { kind: token::Not, .. }), TokenTree::Token(Token { kind: token::Ident(name_zip, false), .. }), TokenTree::Delimited(_, macro_delim, macro_tts)] + if name_macro_rules == &sym::macro_rules && name_zip.as_str() == "zip" => + { let tts = ¯o_tts.trees().collect::<Vec<_>>(); match &tts[..] { - [ - TokenTree::Delimited(_, first_delim, first_tts), - TokenTree::Token(Token { kind: token::FatArrow, .. }), - TokenTree::Delimited(_, second_delim, second_tts), - ] - if macro_delim == &token::Paren => { + [TokenTree::Delimited(_, first_delim, first_tts), TokenTree::Token(Token { kind: token::FatArrow, .. }), TokenTree::Delimited(_, second_delim, second_tts)] + if macro_delim == &token::Paren => + { let tts = &first_tts.trees().collect::<Vec<_>>(); match &tts[..] { - [ - TokenTree::Token(Token { kind: token::Dollar, .. }), - TokenTree::Token(Token { kind: token::Ident(name, false), .. }), - ] - if first_delim == &token::Paren && name.as_str() == "a" => {}, + [TokenTree::Token(Token { kind: token::Dollar, .. }), TokenTree::Token(Token { kind: token::Ident(name, false), .. })] + if first_delim == &token::Paren && name.as_str() == "a" => {} _ => panic!("value 3: {:?} {:?}", first_delim, first_tts), } let tts = &second_tts.trees().collect::<Vec<_>>(); match &tts[..] { - [ - TokenTree::Token(Token { kind: token::Dollar, .. }), - TokenTree::Token(Token { kind: token::Ident(name, false), .. }), - ] - if second_delim == &token::Paren && name.as_str() == "a" => {}, + [TokenTree::Token(Token { kind: token::Dollar, .. }), TokenTree::Token(Token { kind: token::Ident(name, false), .. })] + if second_delim == &token::Paren && name.as_str() == "a" => {} _ => panic!("value 4: {:?} {:?}", second_delim, second_tts), } - }, + } _ => panic!("value 2: {:?} {:?}", macro_delim, macro_tts), } - }, - _ => panic!("value: {:?}",tts), + } + _ => panic!("value: {:?}", tts), } }) } @@ -118,23 +109,28 @@ fn string_to_tts_1() { TokenTree::token(token::Ident(Name::intern("b"), false), sp(6, 7)).into(), TokenTree::token(token::Colon, sp(8, 9)).into(), TokenTree::token(token::Ident(sym::i32, false), sp(10, 13)).into(), - ]).into(), - ).into(), + ]) + .into(), + ) + .into(), TokenTree::Delimited( DelimSpan::from_pair(sp(15, 16), sp(20, 21)), token::DelimToken::Brace, TokenStream::new(vec![ TokenTree::token(token::Ident(Name::intern("b"), false), sp(17, 18)).into(), TokenTree::token(token::Semi, sp(18, 19)).into(), - ]).into(), - ).into() + ]) + .into(), + ) + .into(), ]); assert_eq!(tts, expected); }) } -#[test] fn parse_use() { +#[test] +fn parse_use() { with_default_globals(|| { let use_s = "use foo::bar::baz;"; let vitem = string_to_item(use_s.to_string()).unwrap(); @@ -148,7 +144,8 @@ fn string_to_tts_1() { }) } -#[test] fn parse_extern_crate() { +#[test] +fn parse_extern_crate() { with_default_globals(|| { let ex_s = "extern crate foo;"; let vitem = string_to_item(ex_s.to_string()).unwrap(); @@ -166,12 +163,12 @@ fn get_spans_of_pat_idents(src: &str) -> Vec<Span> { let item = string_to_item(src.to_string()).unwrap(); struct PatIdentVisitor { - spans: Vec<Span> + spans: Vec<Span>, } impl<'a> visit::Visitor<'a> for PatIdentVisitor { fn visit_pat(&mut self, p: &'a ast::Pat) { match p.kind { - PatKind::Ident(_ , ref ident, _) => { + PatKind::Ident(_, ref ident, _) => { self.spans.push(ident.span.clone()); } _ => { @@ -185,27 +182,32 @@ fn get_spans_of_pat_idents(src: &str) -> Vec<Span> { return v.spans; } -#[test] fn span_of_self_arg_pat_idents_are_correct() { +#[test] +fn span_of_self_arg_pat_idents_are_correct() { with_default_globals(|| { - - let srcs = ["impl z { fn a (&self, &myarg: i32) {} }", - "impl z { fn a (&mut self, &myarg: i32) {} }", - "impl z { fn a (&'a self, &myarg: i32) {} }", - "impl z { fn a (self, &myarg: i32) {} }", - "impl z { fn a (self: Foo, &myarg: i32) {} }", - ]; + let srcs = [ + "impl z { fn a (&self, &myarg: i32) {} }", + "impl z { fn a (&mut self, &myarg: i32) {} }", + "impl z { fn a (&'a self, &myarg: i32) {} }", + "impl z { fn a (self, &myarg: i32) {} }", + "impl z { fn a (self: Foo, &myarg: i32) {} }", + ]; for &src in &srcs { let spans = get_spans_of_pat_idents(src); let (lo, hi) = (spans[0].lo(), spans[0].hi()); - assert!("self" == &src[lo.to_usize()..hi.to_usize()], - "\"{}\" != \"self\". src=\"{}\"", - &src[lo.to_usize()..hi.to_usize()], src) + assert!( + "self" == &src[lo.to_usize()..hi.to_usize()], + "\"{}\" != \"self\". src=\"{}\"", + &src[lo.to_usize()..hi.to_usize()], + src + ) } }) } -#[test] fn parse_exprs () { +#[test] +fn parse_exprs() { with_default_globals(|| { // just make sure that they parse.... string_to_expr("3 + 4".to_string()); @@ -213,9 +215,11 @@ fn get_spans_of_pat_idents(src: &str) -> Vec<Span> { }) } -#[test] fn attrs_fix_bug () { +#[test] +fn attrs_fix_bug() { with_default_globals(|| { - string_to_item("pub fn mk_file_writer(path: &Path, flags: &[FileFlag]) + string_to_item( + "pub fn mk_file_writer(path: &Path, flags: &[FileFlag]) -> Result<Box<Writer>, String> { #[cfg(windows)] fn wb() -> c_int { @@ -226,27 +230,32 @@ fn wb() -> c_int { fn wb() -> c_int { O_WRONLY as c_int } let mut fflags: c_int = wb(); -}".to_string()); +}" + .to_string(), + ); }) } -#[test] fn crlf_doc_comments() { +#[test] +fn crlf_doc_comments() { with_default_globals(|| { let sess = sess(); let name_1 = FileName::Custom("crlf_source_1".to_string()); let source = "/// doc comment\r\nfn foo() {}".to_string(); - let item = parse_item_from_source_str(name_1, source, &sess) - .unwrap().unwrap(); + let item = parse_item_from_source_str(name_1, source, &sess).unwrap().unwrap(); let doc = first_attr_value_str_by_name(&item.attrs, sym::doc).unwrap(); assert_eq!(doc.as_str(), "/// doc comment"); let name_2 = FileName::Custom("crlf_source_2".to_string()); let source = "/// doc comment\r\n/// line 2\r\nfn foo() {}".to_string(); - let item = parse_item_from_source_str(name_2, source, &sess) - .unwrap().unwrap(); - let docs = item.attrs.iter().filter(|a| a.has_name(sym::doc)) - .map(|a| a.value_str().unwrap().to_string()).collect::<Vec<_>>(); + let item = parse_item_from_source_str(name_2, source, &sess).unwrap().unwrap(); + let docs = item + .attrs + .iter() + .filter(|a| a.has_name(sym::doc)) + .map(|a| a.value_str().unwrap().to_string()) + .collect::<Vec<_>>(); let b: &[_] = &["/// doc comment".to_string(), "/// line 2".to_string()]; assert_eq!(&docs[..], b); @@ -261,15 +270,21 @@ let mut fflags: c_int = wb(); #[test] fn ttdelim_span() { fn parse_expr_from_source_str( - name: FileName, source: String, sess: &ParseSess + name: FileName, + source: String, + sess: &ParseSess, ) -> PResult<'_, P<ast::Expr>> { new_parser_from_source_str(sess, name, source).parse_expr() } with_default_globals(|| { let sess = sess(); - let expr = parse_expr_from_source_str(PathBuf::from("foo").into(), - "foo!( fn main() { body } )".to_string(), &sess).unwrap(); + let expr = parse_expr_from_source_str( + PathBuf::from("foo").into(), + "foo!( fn main() { body } )".to_string(), + &sess, + ) + .unwrap(); let tts: Vec<_> = match expr.kind { ast::ExprKind::Mac(ref mac) => mac.args.inner_tokens().trees().collect(), @@ -295,7 +310,9 @@ fn out_of_line_mod() { PathBuf::from("foo").into(), "mod foo { struct S; mod this_does_not_exist; }".to_owned(), &sess(), - ).unwrap().unwrap(); + ) + .unwrap() + .unwrap(); if let ast::ItemKind::Mod(ref m) = item.kind { assert!(m.items.len() == 2); @@ -307,31 +324,31 @@ fn out_of_line_mod() { #[test] fn eqmodws() { - assert_eq!(matches_codepattern("",""),true); - assert_eq!(matches_codepattern("","a"),false); - assert_eq!(matches_codepattern("a",""),false); - assert_eq!(matches_codepattern("a","a"),true); - assert_eq!(matches_codepattern("a b","a \n\t\r b"),true); - assert_eq!(matches_codepattern("a b ","a \n\t\r b"),true); - assert_eq!(matches_codepattern("a b","a \n\t\r b "),false); - assert_eq!(matches_codepattern("a b","a b"),true); - assert_eq!(matches_codepattern("ab","a b"),false); - assert_eq!(matches_codepattern("a b","ab"),true); - assert_eq!(matches_codepattern(" a b","ab"),true); + assert_eq!(matches_codepattern("", ""), true); + assert_eq!(matches_codepattern("", "a"), false); + assert_eq!(matches_codepattern("a", ""), false); + assert_eq!(matches_codepattern("a", "a"), true); + assert_eq!(matches_codepattern("a b", "a \n\t\r b"), true); + assert_eq!(matches_codepattern("a b ", "a \n\t\r b"), true); + assert_eq!(matches_codepattern("a b", "a \n\t\r b "), false); + assert_eq!(matches_codepattern("a b", "a b"), true); + assert_eq!(matches_codepattern("ab", "a b"), false); + assert_eq!(matches_codepattern("a b", "ab"), true); + assert_eq!(matches_codepattern(" a b", "ab"), true); } #[test] fn pattern_whitespace() { - assert_eq!(matches_codepattern("","\x0C"), false); - assert_eq!(matches_codepattern("a b ","a \u{0085}\n\t\r b"),true); - assert_eq!(matches_codepattern("a b","a \u{0085}\n\t\r b "),false); + assert_eq!(matches_codepattern("", "\x0C"), false); + assert_eq!(matches_codepattern("a b ", "a \u{0085}\n\t\r b"), true); + assert_eq!(matches_codepattern("a b", "a \u{0085}\n\t\r b "), false); } #[test] fn non_pattern_whitespace() { // These have the property 'White_Space' but not 'Pattern_White_Space' - assert_eq!(matches_codepattern("a b","a\u{2002}b"), false); - assert_eq!(matches_codepattern("a b","a\u{2002}b"), false); - assert_eq!(matches_codepattern("\u{205F}a b","ab"), false); - assert_eq!(matches_codepattern("a \u{3000}b","ab"), false); + assert_eq!(matches_codepattern("a b", "a\u{2002}b"), false); + assert_eq!(matches_codepattern("a b", "a\u{2002}b"), false); + assert_eq!(matches_codepattern("\u{205F}a b", "ab"), false); + assert_eq!(matches_codepattern("a \u{3000}b", "ab"), false); } |
