1
Fork 0

Fix fallout in unit tests.

This commit is contained in:
Jeffrey Seyfried 2017-03-02 01:29:40 +00:00
parent a02c18aa52
commit 0d554139ad
7 changed files with 45 additions and 53 deletions

View file

@ -598,7 +598,6 @@ pub fn integer_lit(s: &str, suffix: Option<Symbol>, sd: &Handler, sp: Span) -> a
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use std::rc::Rc;
use syntax_pos::{self, Span, BytePos, Pos, NO_EXPANSION}; use syntax_pos::{self, Span, BytePos, Pos, NO_EXPANSION};
use codemap::Spanned; use codemap::Spanned;
use ast::{self, Ident, PatKind}; use ast::{self, Ident, PatKind};
@ -609,7 +608,7 @@ mod tests {
use print::pprust::item_to_string; use print::pprust::item_to_string;
use ptr::P; use ptr::P;
use tokenstream::{self, TokenTree}; use tokenstream::{self, TokenTree};
use util::parser_testing::{string_to_tts, string_to_parser}; use util::parser_testing::{string_to_stream, string_to_parser};
use util::parser_testing::{string_to_expr, string_to_item, string_to_stmt}; use util::parser_testing::{string_to_expr, string_to_item, string_to_stmt};
use util::ThinVec; use util::ThinVec;
@ -654,7 +653,8 @@ mod tests {
// check the token-tree-ization of macros // check the token-tree-ization of macros
#[test] #[test]
fn string_to_tts_macro () { fn string_to_tts_macro () {
let tts = string_to_tts("macro_rules! zip (($a)=>($a))".to_string()); let tts: Vec<_> =
string_to_stream("macro_rules! zip (($a)=>($a))".to_string()).trees().collect();
let tts: &[TokenTree] = &tts[..]; let tts: &[TokenTree] = &tts[..];
match (tts.len(), tts.get(0), tts.get(1), tts.get(2), tts.get(3)) { match (tts.len(), tts.get(0), tts.get(1), tts.get(2), tts.get(3)) {
@ -667,7 +667,7 @@ mod tests {
) )
if name_macro_rules.name == "macro_rules" if name_macro_rules.name == "macro_rules"
&& name_zip.name == "zip" => { && name_zip.name == "zip" => {
let tts = &macro_delimed.tts[..]; let tts = &macro_delimed.stream().trees().collect::<Vec<_>>();
match (tts.len(), tts.get(0), tts.get(1), tts.get(2)) { match (tts.len(), tts.get(0), tts.get(1), tts.get(2)) {
( (
3, 3,
@ -676,7 +676,7 @@ mod tests {
Some(&TokenTree::Delimited(_, ref second_delimed)), Some(&TokenTree::Delimited(_, ref second_delimed)),
) )
if macro_delimed.delim == token::Paren => { if macro_delimed.delim == token::Paren => {
let tts = &first_delimed.tts[..]; let tts = &first_delimed.stream().trees().collect::<Vec<_>>();
match (tts.len(), tts.get(0), tts.get(1)) { match (tts.len(), tts.get(0), tts.get(1)) {
( (
2, 2,
@ -684,9 +684,9 @@ mod tests {
Some(&TokenTree::Token(_, token::Ident(ident))), Some(&TokenTree::Token(_, token::Ident(ident))),
) )
if first_delimed.delim == token::Paren && ident.name == "a" => {}, if first_delimed.delim == token::Paren && ident.name == "a" => {},
_ => panic!("value 3: {:?}", **first_delimed), _ => panic!("value 3: {:?}", *first_delimed),
} }
let tts = &second_delimed.tts[..]; let tts = &second_delimed.stream().trees().collect::<Vec<_>>();
match (tts.len(), tts.get(0), tts.get(1)) { match (tts.len(), tts.get(0), tts.get(1)) {
( (
2, 2,
@ -695,10 +695,10 @@ mod tests {
) )
if second_delimed.delim == token::Paren if second_delimed.delim == token::Paren
&& ident.name == "a" => {}, && ident.name == "a" => {},
_ => panic!("value 4: {:?}", **second_delimed), _ => panic!("value 4: {:?}", *second_delimed),
} }
}, },
_ => panic!("value 2: {:?}", **macro_delimed), _ => panic!("value 2: {:?}", *macro_delimed),
} }
}, },
_ => panic!("value: {:?}",tts), _ => panic!("value: {:?}",tts),
@ -707,31 +707,31 @@ mod tests {
#[test] #[test]
fn string_to_tts_1() { fn string_to_tts_1() {
let tts = string_to_tts("fn a (b : i32) { b; }".to_string()); let tts = string_to_stream("fn a (b : i32) { b; }".to_string());
let expected = vec![ let expected = TokenStream::concat(vec![
TokenTree::Token(sp(0, 2), token::Ident(Ident::from_str("fn"))), TokenTree::Token(sp(0, 2), token::Ident(Ident::from_str("fn"))).into(),
TokenTree::Token(sp(3, 4), token::Ident(Ident::from_str("a"))), TokenTree::Token(sp(3, 4), token::Ident(Ident::from_str("a"))).into(),
TokenTree::Delimited( TokenTree::Delimited(
sp(5, 14), sp(5, 14),
Rc::new(tokenstream::Delimited { tokenstream::Delimited {
delim: token::DelimToken::Paren, delim: token::DelimToken::Paren,
tts: vec![ tts: TokenStream::concat(vec![
TokenTree::Token(sp(6, 7), token::Ident(Ident::from_str("b"))), TokenTree::Token(sp(6, 7), token::Ident(Ident::from_str("b"))).into(),
TokenTree::Token(sp(8, 9), token::Colon), TokenTree::Token(sp(8, 9), token::Colon).into(),
TokenTree::Token(sp(10, 13), token::Ident(Ident::from_str("i32"))), TokenTree::Token(sp(10, 13), token::Ident(Ident::from_str("i32"))).into(),
], ]).into(),
})), }).into(),
TokenTree::Delimited( TokenTree::Delimited(
sp(15, 21), sp(15, 21),
Rc::new(tokenstream::Delimited { tokenstream::Delimited {
delim: token::DelimToken::Brace, delim: token::DelimToken::Brace,
tts: vec![ tts: TokenStream::concat(vec![
TokenTree::Token(sp(17, 18), token::Ident(Ident::from_str("b"))), TokenTree::Token(sp(17, 18), token::Ident(Ident::from_str("b"))).into(),
TokenTree::Token(sp(18, 19), token::Semi), TokenTree::Token(sp(18, 19), token::Semi).into(),
], ]).into(),
})) }).into()
]; ]);
assert_eq!(tts, expected); assert_eq!(tts, expected);
} }
@ -974,8 +974,8 @@ mod tests {
let expr = parse::parse_expr_from_source_str("foo".to_string(), let expr = parse::parse_expr_from_source_str("foo".to_string(),
"foo!( fn main() { body } )".to_string(), &sess).unwrap(); "foo!( fn main() { body } )".to_string(), &sess).unwrap();
let tts = match expr.node { let tts: Vec<_> = match expr.node {
ast::ExprKind::Mac(ref mac) => mac.node.tts.clone(), ast::ExprKind::Mac(ref mac) => mac.node.stream().trees().collect(),
_ => panic!("not a macro"), _ => panic!("not a macro"),
}; };

View file

@ -409,10 +409,10 @@ mod tests {
use syntax::ast::Ident; use syntax::ast::Ident;
use syntax_pos::{Span, BytePos, NO_EXPANSION}; use syntax_pos::{Span, BytePos, NO_EXPANSION};
use parse::token::Token; use parse::token::Token;
use util::parser_testing::string_to_tts; use util::parser_testing::string_to_stream;
fn string_to_ts(string: &str) -> TokenStream { fn string_to_ts(string: &str) -> TokenStream {
string_to_tts(string.to_owned()).into_iter().collect() string_to_stream(string.to_owned())
} }
fn sp(a: u32, b: u32) -> Span { fn sp(a: u32, b: u32) -> Span {
@ -428,20 +428,12 @@ mod tests {
let test_res = string_to_ts("foo::bar::baz"); let test_res = string_to_ts("foo::bar::baz");
let test_fst = string_to_ts("foo::bar"); let test_fst = string_to_ts("foo::bar");
let test_snd = string_to_ts("::baz"); let test_snd = string_to_ts("::baz");
let eq_res = TokenStream::concat([test_fst, test_snd].iter().cloned()); let eq_res = TokenStream::concat(vec![test_fst, test_snd]);
assert_eq!(test_res.trees().count(), 5); assert_eq!(test_res.trees().count(), 5);
assert_eq!(eq_res.trees().count(), 5); assert_eq!(eq_res.trees().count(), 5);
assert_eq!(test_res.eq_unspanned(&eq_res), true); assert_eq!(test_res.eq_unspanned(&eq_res), true);
} }
#[test]
fn test_from_to_bijection() {
let test_start = string_to_tts("foo::bar(baz)".to_string());
let ts = test_start.iter().cloned().collect::<TokenStream>();
let test_end: Vec<TokenTree> = ts.trees().collect();
assert_eq!(test_start, test_end)
}
#[test] #[test]
fn test_to_from_bijection() { fn test_to_from_bijection() {
let test_start = string_to_ts("foo::bar(baz)"); let test_start = string_to_ts("foo::bar(baz)");

View file

@ -9,17 +9,17 @@
// except according to those terms. // except according to those terms.
use ast::{self, Ident}; use ast::{self, Ident};
use parse::{ParseSess,PResult,filemap_to_tts}; use parse::{ParseSess, PResult, filemap_to_stream};
use parse::{lexer, new_parser_from_source_str}; use parse::{lexer, new_parser_from_source_str};
use parse::parser::Parser; use parse::parser::Parser;
use ptr::P; use ptr::P;
use tokenstream; use tokenstream::TokenStream;
use std::iter::Peekable; use std::iter::Peekable;
/// Map a string to tts, using a made-up filename: /// Map a string to tts, using a made-up filename:
pub fn string_to_tts(source_str: String) -> Vec<tokenstream::TokenTree> { pub fn string_to_stream(source_str: String) -> TokenStream {
let ps = ParseSess::new(); let ps = ParseSess::new();
filemap_to_tts(&ps, ps.codemap().new_filemap("bogofile".to_string(), None, source_str)) filemap_to_stream(&ps, ps.codemap().new_filemap("bogofile".to_string(), None, source_str))
} }
/// Map string to parser (via tts) /// Map string to parser (via tts)

View file

@ -18,7 +18,7 @@ use syntax::ast::*;
use syntax::attr::*; use syntax::attr::*;
use syntax::ast; use syntax::ast;
use syntax::parse; use syntax::parse;
use syntax::parse::{ParseSess,filemap_to_tts, PResult}; use syntax::parse::{ParseSess, PResult};
use syntax::parse::new_parser_from_source_str; use syntax::parse::new_parser_from_source_str;
use syntax::parse::parser::Parser; use syntax::parse::parser::Parser;
use syntax::parse::token; use syntax::parse::token;

View file

@ -32,13 +32,13 @@ pub fn plugin_registrar(reg: &mut Registry) {
fn cond(input: TokenStream) -> TokenStream { fn cond(input: TokenStream) -> TokenStream {
let mut conds = Vec::new(); let mut conds = Vec::new();
let mut input = input.trees(); let mut input = input.trees().peekable();
while let Some(tree) = input.next() { while let Some(tree) = input.next() {
let cond: TokenStream = match *tree { let mut cond = match tree {
TokenTree::Delimited(_, ref delimited) => delimited.tts.iter().cloned().collect(), TokenTree::Delimited(_, ref delimited) => delimited.stream(),
_ => panic!("Invalid input"), _ => panic!("Invalid input"),
}; };
let mut trees = cond.trees().cloned(); let mut trees = cond.trees();
let test = trees.next(); let test = trees.next();
let rhs = trees.collect::<TokenStream>(); let rhs = trees.collect::<TokenStream>();
if rhs.is_empty() { if rhs.is_empty() {

View file

@ -26,7 +26,7 @@ use syntax::print::pprust;
use syntax::ptr::P; use syntax::ptr::P;
use syntax::symbol::Symbol; use syntax::symbol::Symbol;
use syntax_pos::Span; use syntax_pos::Span;
use syntax::tokenstream; use syntax::tokenstream::TokenStream;
use rustc_plugin::Registry; use rustc_plugin::Registry;
struct Expander { struct Expander {
@ -37,7 +37,7 @@ impl TTMacroExpander for Expander {
fn expand<'cx>(&self, fn expand<'cx>(&self,
ecx: &'cx mut ExtCtxt, ecx: &'cx mut ExtCtxt,
sp: Span, sp: Span,
_: &[tokenstream::TokenTree]) -> Box<MacResult+'cx> { _: TokenStream) -> Box<MacResult+'cx> {
let args = self.args.iter().map(|i| pprust::meta_list_item_to_string(i)) let args = self.args.iter().map(|i| pprust::meta_list_item_to_string(i))
.collect::<Vec<_>>().join(", "); .collect::<Vec<_>>().join(", ");
MacEager::expr(ecx.expr_str(sp, Symbol::intern(&args))) MacEager::expr(ecx.expr_str(sp, Symbol::intern(&args)))

View file

@ -35,8 +35,8 @@ fn expand_mbe_matches(cx: &mut ExtCtxt, _: Span, args: &[TokenTree])
-> Box<MacResult + 'static> { -> Box<MacResult + 'static> {
let mbe_matcher = quote_tokens!(cx, $$matched:expr, $$($$pat:pat)|+); let mbe_matcher = quote_tokens!(cx, $$matched:expr, $$($$pat:pat)|+);
let mbe_matcher = quoted::parse(&mbe_matcher, true, cx.parse_sess); let mbe_matcher = quoted::parse(mbe_matcher.into_iter().collect(), true, cx.parse_sess);
let map = match TokenTree::parse(cx, &mbe_matcher, args) { let map = match TokenTree::parse(cx, &mbe_matcher, args.iter().cloned().collect()) {
Success(map) => map, Success(map) => map,
Failure(_, tok) => { Failure(_, tok) => {
panic!("expected Success, but got Failure: {}", parse_failure_msg(tok)); panic!("expected Success, but got Failure: {}", parse_failure_msg(tok));