|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 | 11 | // ignore-pretty
|
12 |
| -// ignore-test |
13 | 12 |
|
14 |
| -#![feature(quote)] |
| 13 | +#![feature(quote, rustc_private)] |
15 | 14 |
|
16 | 15 | extern crate syntax;
|
17 | 16 |
|
18 |
| -use std::io::*; |
19 |
| - |
20 |
| -use syntax::diagnostic; |
21 | 17 | use syntax::ast;
|
22 | 18 | use syntax::codemap;
|
23 |
| -use syntax::codemap::span; |
24 | 19 | use syntax::parse;
|
25 |
| -use syntax::print::*; |
26 |
| - |
27 |
| - |
28 |
| -trait fake_ext_ctxt { |
29 |
| - fn cfg() -> ast::CrateConfig; |
30 |
| - fn parse_sess() -> parse::parse_sess; |
31 |
| - fn call_site() -> span; |
32 |
| - fn ident_of(st: &str) -> ast::ident; |
| 20 | +use syntax::print::pprust; |
| 21 | + |
| 22 | +trait FakeExtCtxt { |
| 23 | + fn call_site(&self) -> codemap::Span; |
| 24 | + fn cfg(&self) -> ast::CrateConfig; |
| 25 | + fn ident_of(&self, st: &str) -> ast::Ident; |
| 26 | + fn name_of(&self, st: &str) -> ast::Name; |
| 27 | + fn parse_sess(&self) -> &parse::ParseSess; |
33 | 28 | }
|
34 | 29 |
|
35 |
| -type fake_session = parse::parse_sess; |
36 |
| - |
37 |
| -impl fake_ext_ctxt for fake_session { |
38 |
| - fn cfg() -> ast::CrateConfig { Vec::new() } |
39 |
| - fn parse_sess() -> parse::parse_sess { self } |
40 |
| - fn call_site() -> span { |
41 |
| - codemap::span { |
| 30 | +impl FakeExtCtxt for parse::ParseSess { |
| 31 | + fn call_site(&self) -> codemap::Span { |
| 32 | + codemap::Span { |
42 | 33 | lo: codemap::BytePos(0),
|
43 | 34 | hi: codemap::BytePos(0),
|
44 |
| - expn_id: codemap::NO_EXPANSION |
| 35 | + expn_id: codemap::NO_EXPANSION, |
45 | 36 | }
|
46 | 37 | }
|
47 |
| - fn ident_of(st: &str) -> ast::ident { |
48 |
| - self.interner.intern(st) |
| 38 | + fn cfg(&self) -> ast::CrateConfig { Vec::new() } |
| 39 | + fn ident_of(&self, st: &str) -> ast::Ident { |
| 40 | + parse::token::str_to_ident(st) |
49 | 41 | }
|
50 |
| -} |
51 |
| - |
52 |
| -fn mk_ctxt() -> fake_ext_ctxt { |
53 |
| - parse::new_parse_sess(None) as fake_ext_ctxt |
| 42 | + fn name_of(&self, st: &str) -> ast::Name { |
| 43 | + parse::token::intern(st) |
| 44 | + } |
| 45 | + fn parse_sess(&self) -> &parse::ParseSess { self } |
54 | 46 | }
|
55 | 47 |
|
56 | 48 | fn main() {
|
57 |
| - let cx = mk_ctxt(); |
58 |
| - |
59 |
| - let abc = quote_expr!(cx, 23); |
60 |
| - check_pp(ext_cx, abc, pprust::print_expr, "23".to_string()); |
| 49 | + let cx = parse::new_parse_sess(); |
61 | 50 |
|
| 51 | + quote_ty!(&cx, isize).and_then(|ty| { |
| 52 | + assert_eq!(pprust::ty_to_string(&ty), "isize") |
| 53 | + }); |
62 | 54 |
|
63 |
| - let ty = quote_ty!(cx, isize); |
64 |
| - check_pp(ext_cx, ty, pprust::print_type, "isize".to_string()); |
65 |
| - |
66 |
| - let item = quote_item!(cx, static x : isize = 10;).get(); |
67 |
| - check_pp(ext_cx, item, pprust::print_item, "static x: isize = 10;".to_string()); |
| 55 | + quote_pat!(&cx, Some(_)).and_then(|pat| { |
| 56 | + assert_eq!(pprust::pat_to_string(&pat), "Some(_)") |
| 57 | + }); |
68 | 58 |
|
69 |
| - let stmt = quote_stmt!(cx, let x = 20;); |
70 |
| - check_pp(ext_cx, *stmt, pprust::print_stmt, "let x = 20;".to_string()); |
| 59 | + let arm = quote_arm!(&cx, (ref x, ref y) => (x, y),); |
| 60 | + assert_eq!(pprust::arm_to_string(&arm), " (ref x, ref y) => (x, y),"); |
71 | 61 |
|
72 |
| - let pat = quote_pat!(cx, Some(_)); |
73 |
| - check_pp(ext_cx, pat, pprust::print_pat, "Some(_)".to_string()); |
| 62 | + quote_expr!(&cx, 23).and_then(|expr| { |
| 63 | + assert_eq!(pprust::expr_to_string(&expr), "23") |
| 64 | + }); |
74 | 65 |
|
75 |
| - let arm = quote_arm!(cx, (ref x, ref y) => (x, y)); |
76 |
| - check_pp(ext_cx, arm, pprust::print_stmt, "(ref x, ref y) = (x, y)".to_string()); |
| 66 | + quote_stmt!(&cx, let x = 20;).unwrap().and_then(|stmt| { |
| 67 | + assert_eq!(pprust::stmt_to_string(&stmt), "let x = 20;") |
| 68 | + }); |
77 | 69 |
|
78 |
| - let attr = quote_attr!(cx, #![cfg(foo = "bar")]); |
79 |
| - check_pp(ext_cx, attr, pprust::print_attribute, "#![cfg(foo = "bar")]".to_string()); |
80 |
| -} |
| 70 | + let attr = quote_attr!(&cx, #![cfg(foo = "bar")]); |
| 71 | + assert_eq!(pprust::attr_to_string(&attr), "#![cfg(foo = \"bar\")]"); |
81 | 72 |
|
82 |
| -fn check_pp<T>(cx: fake_ext_ctxt, |
83 |
| - expr: T, f: |pprust::ps, T|, expect: String) { |
84 |
| - let s = io::with_str_writer(|wr| { |
85 |
| - let pp = pprust::rust_printer(wr, cx.parse_sess().interner); |
86 |
| - f(pp, expr); |
87 |
| - pp::eof(pp.s); |
| 73 | + quote_item!(&cx, static x : isize = 10;).unwrap().and_then(|item| { |
| 74 | + assert_eq!(pprust::item_to_string(&item), "static x: isize = 10;") |
88 | 75 | });
|
89 |
| - stdout().write_line(s); |
90 |
| - if expect != "".to_string() { |
91 |
| - println!("expect: '%s', got: '%s'", expect, s); |
92 |
| - assert_eq!(s, expect); |
93 |
| - } |
94 | 76 | }
|
0 commit comments