Graydon Hoare | d1affff | 2012-12-11 01:32:48 | [diff] [blame] | 1 | // Copyright 2012 The Rust Project Developers. See the COPYRIGHT |
| 2 | // file at the top-level directory of this distribution and at |
| 3 | // http://rust-lang.org/COPYRIGHT. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | // option. This file may not be copied, modified, or distributed |
| 9 | // except according to those terms. |
| 10 | |
Graydon Hoare | ce72993 | 2011-06-15 18:19:50 | [diff] [blame] | 11 | |
| 12 | |
| 13 | |
Brian Anderson | 1326d42 | 2011-04-02 23:32:34 | [diff] [blame] | 14 | // -*- rust -*- |
| 15 | |
Tim Chevalier | 6d4907a | 2013-01-26 06:46:32 | [diff] [blame] | 16 | // Tests for match as expressions resulting in struct types |
| 17 | struct R { i: int } |
| 18 | |
Brian Anderson | 1326d42 | 2011-04-02 23:32:34 | [diff] [blame] | 19 | fn test_rec() { |
Nick Desaulniers | 4445b38 | 2013-02-12 03:26:38 | [diff] [blame] | 20 | let rs = match true { true => R {i: 100}, _ => fail!() }; |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 21 | assert_eq!(rs.i, 100); |
Patrick Walton | 9653436 | 2012-08-27 23:26:35 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | enum mood { happy, sad, } |
Patrick Walton | 9117dcb | 2012-09-20 01:00:26 | [diff] [blame] | 25 | |
Patrick Walton | 206ab89 | 2013-05-25 02:35:29 | [diff] [blame] | 26 | impl Eq for mood { |
Patrick Walton | 3eda11a | 2013-03-22 18:23:21 | [diff] [blame] | 27 | fn eq(&self, other: &mood) -> bool { |
Patrick Walton | 318e534 | 2012-11-15 02:59:30 | [diff] [blame] | 28 | ((*self) as uint) == ((*other) as uint) |
Patrick Walton | 9653436 | 2012-08-27 23:26:35 | [diff] [blame] | 29 | } |
Patrick Walton | 3eda11a | 2013-03-22 18:23:21 | [diff] [blame] | 30 | fn ne(&self, other: &mood) -> bool { !(*self).eq(other) } |
Brian Anderson | 1326d42 | 2011-04-02 23:32:34 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | fn test_tag() { |
Brian Anderson | ecaf9e3 | 2012-08-06 19:34:08 | [diff] [blame] | 34 | let rs = match true { true => { happy } false => { sad } }; |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 35 | assert_eq!(rs, happy); |
Brian Anderson | 1326d42 | 2011-04-02 23:32:34 | [diff] [blame] | 36 | } |
| 37 | |
Graydon Hoare | 89c8ef7 | 2013-02-02 03:43:17 | [diff] [blame] | 38 | pub fn main() { test_rec(); test_tag(); } |