File: enums.rs

package info (click to toggle)
rust-arg-enum-proc-macro 0.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 120 kB
  • sloc: makefile: 2
file content (115 lines) | stat: -rw-r--r-- 2,458 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use arg_enum_proc_macro::ArgEnum;

#[derive(ArgEnum, PartialEq, Debug)]
pub enum Foo {
    Bar,
    /// Foo
    Baz,
}

#[test]
fn parse() {
    let v: Foo = "Baz".parse().unwrap();

    assert_eq!(v, Foo::Baz);
}

#[test]
fn variants() {
    assert_eq!(&Foo::variants(), &["Bar", "Baz"]);
}

mod alias {
    use arg_enum_proc_macro::ArgEnum;

    #[derive(ArgEnum, PartialEq, Debug)]
    pub enum Bar {
        A,
        B,
        #[arg_enum(alias = "Cat")]
        C,
    }

    #[test]
    fn parse() {
        let v: Bar = "Cat".parse().unwrap();

        assert_eq!(v, Bar::C);
    }

    #[test]
    fn variants() {
        assert_eq!(&Bar::variants(), &["A", "B", "C", "Cat"]);
    }
}

mod name {
    use arg_enum_proc_macro::ArgEnum;

    #[derive(ArgEnum, PartialEq, Debug)]
    pub enum Bar {
        A,
        B,
        #[arg_enum(name = "Cat", alias = "Feline")]
        C,
    }

    #[test]
    fn parse() {
        let v: Bar = "Cat".parse().unwrap();

        assert_eq!(v, Bar::C);
    }

    #[test]
    fn variants() {
        assert_eq!(&Bar::variants(), &["A", "B", "Cat", "Feline"]);
    }
}

mod description {
    use arg_enum_proc_macro::ArgEnum;

    #[derive(ArgEnum, PartialEq, Debug)]
    pub enum Bar {
        /// This is A and it's description is a single line
        A,
        /// This is B and it's description contains " for no specific reason
        /// and is in two lines.
        B,
        /// This is C, normally known as "Cat" or "Feline"
        #[arg_enum(name = "Cat", alias = "Feline")]
        C,
    }

    #[test]
    fn descriptions() {
        let expected: [(&'static [&'static str], &'static [&'static str]); 3usize] = [
            (
                &["A"],
                &[" This is A and it's description is a single line"],
            ),
            (
                &["B"],
                &[
                    " This is B and it's description contains \" for no specific reason",
                    " and is in two lines.",
                ],
            ),
            (
                &["Cat", "Feline"],
                &[" This is C, normally known as \"Cat\" or \"Feline\""],
            ),
        ];
        assert_eq!(&Bar::descriptions(), &expected);
    }
}

mod ui {
    #[test]
    fn invalid_applications() {
        let t = trybuild::TestCases::new();
        t.compile_fail("tests/ui/complex-enum.rs");
        t.compile_fail("tests/ui/derive-not-on-enum.rs");
    }
}