From 064ec35c18f396c2c26c05517290fdc1a4379586 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Sat, 26 Mar 2016 12:10:16 -0500 Subject: [PATCH] add regression test for try! --- src/test/run-pass/try-macro.rs | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/test/run-pass/try-macro.rs diff --git a/src/test/run-pass/try-macro.rs b/src/test/run-pass/try-macro.rs new file mode 100644 index 00000000000..a12e20702d2 --- /dev/null +++ b/src/test/run-pass/try-macro.rs @@ -0,0 +1,57 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::num::{ParseFloatError, ParseIntError}; + +fn main() { + assert_eq!(simple(), Ok(1)); + assert_eq!(nested(), Ok(2)); + assert_eq!(merge_ok(), Ok(3.0)); + assert_eq!(merge_int_err(), Err(Error::Int)); + assert_eq!(merge_float_err(), Err(Error::Float)); +} + +fn simple() -> Result { + Ok(try!("1".parse())) +} + +fn nested() -> Result { + Ok(try!(try!("2".parse::()).to_string().parse::())) +} + +fn merge_ok() -> Result { + Ok(try!("1".parse::()) as f32 + try!("2.0".parse::())) +} + +fn merge_int_err() -> Result { + Ok(try!("a".parse::()) as f32 + try!("2.0".parse::())) +} + +fn merge_float_err() -> Result { + Ok(try!("1".parse::()) as f32 + try!("b".parse::())) +} + +#[derive(Debug, PartialEq)] +enum Error { + Int, + Float, +} + +impl From for Error { + fn from(_: ParseIntError) -> Error { + Error::Int + } +} + +impl From for Error { + fn from(_: ParseFloatError) -> Error { + Error::Float + } +}