diff options
author | Christopher Chambers <chris.chambers@peanutcode.com> | 2014-12-19 07:29:33 -0600 |
---|---|---|
committer | Christopher Chambers <chris.chambers@peanutcode.com> | 2014-12-19 07:34:42 -0600 |
commit | 010ce680336355af484ef2e171c2bb6cc4c0c34b (patch) | |
tree | 4b72cb568e04b662459b06370828d4f614085877 | |
parent | ce879aacbc77de5523042ddead8aee1d6a47c15c (diff) |
Adds utilities that allow text comparison of bindings.
This is unused in this branch. Just preparation for upcoming branches
that will feature tests.
-rw-r--r-- | tests/util.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/util.rs b/tests/util.rs new file mode 100644 index 00000000..360fbb8d --- /dev/null +++ b/tests/util.rs @@ -0,0 +1,58 @@ +extern crate bindgen; +extern crate regex; +extern crate syntax; + +use bindgen::{Logger, BindgenOptions}; +use regex::Regex; +use std::default::Default; +use syntax::ast; +use syntax::codemap::DUMMY_SP; +use syntax::print::pp; +use syntax::print::pprust; +use syntax::ptr::P; + +struct TestLogger; + +impl Logger for TestLogger { + fn error(&self, msg: &str) { + println!("err: {}", msg); + } + + fn warn(&self, msg: &str) { + println!("warn: {}", msg); + } +} + +pub fn generate_bindings(filename: &str) -> Result<Vec<P<ast::Item>>, ()> { + let mut options:BindgenOptions = Default::default(); + options.clang_args.push(filename.to_string()); + + let logger = TestLogger; + bindgen::generate_bindings(options, Some(&logger as &Logger), DUMMY_SP) +} + +pub fn generate_unpretty_output(filename: &str) -> String { + let output = pprust::to_string(|s| { + // HACK: Replace the default printer with a very wide printer. This + // makes it easier to reason about how the unpretty'd output + // will look, at the cost of tying us to the implementation + // details of pprust::to_string. + s.s = pp::mk_printer(box Vec::new(), 4096); + + let items = generate_bindings(filename).unwrap(); + + let module = ast::Mod { + inner: DUMMY_SP, + view_items: Vec::new(), + items: items, + }; + s.print_mod(&module, &[]) + }); + + unpretty(output.as_slice()) +} + +pub fn unpretty(s: &str) -> String { + let re = Regex::new(r"\s+").unwrap(); + re.replace_all(s, " ") +} |