File: test.rs

package info (click to toggle)
rust-enumn 0.1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 124 kB
  • sloc: makefile: 4
file content (52 lines) | stat: -rw-r--r-- 1,153 bytes parent folder | download
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
use enumn::N;

#[derive(Debug, N, PartialEq)]
enum EmptyEnum {}

#[test]
fn test_empty() {
    assert_eq!(EmptyEnum::n(0), None);
    assert_eq!(EmptyEnum::n(1), None);
    assert_eq!(EmptyEnum::n(-1), None);
}

#[derive(Debug, N, PartialEq)]
enum SimpleEnum {
    Case0,
    Case1,
}

#[test]
fn test_simple() {
    assert_eq!(SimpleEnum::n(0), Some(SimpleEnum::Case0));
    assert_eq!(SimpleEnum::n(1), Some(SimpleEnum::Case1));
    assert_eq!(SimpleEnum::n(4), None);
    assert_eq!(SimpleEnum::n(-1), None);
}

#[derive(Debug, N, PartialEq)]
#[repr(u8)]
enum EnumWithRepr {
    Case0,
}

#[test]
fn test_repr() {
    assert_eq!(EnumWithRepr::n(0), Some(EnumWithRepr::Case0));
    assert_eq!(EnumWithRepr::n(255), None);
}

#[derive(Debug, N, PartialEq)]
enum EnumWithDiscriminant {
    A = 10,
    B, // implicitly 11
    C = -80,
}

#[test]
fn test_discriminant() {
    assert_eq!(EnumWithDiscriminant::n(10), Some(EnumWithDiscriminant::A));
    assert_eq!(EnumWithDiscriminant::n(11), Some(EnumWithDiscriminant::B));
    assert_eq!(EnumWithDiscriminant::n(-80), Some(EnumWithDiscriminant::C));
    assert_eq!(EnumWithDiscriminant::n(12), None);
}