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