aboutsummaryrefslogtreecommitdiff
path: root/tests/test_parse_boolean.rs
blob: 9cf865cfdfb57a93b1c7eef9601663e6eed4d4cf (plain)
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
use codecrafters_redis::resp_parser::*;

#[test]
fn test_valid_booleans() {
    // Basic true value
    assert_eq!(parse_boolean(b"#t\r\n").unwrap().0, RespType::Boolean(true));

    // Basic false value
    assert_eq!(
        parse_boolean(b"#f\r\n").unwrap().0,
        RespType::Boolean(false)
    );
}

#[test]
fn test_invalid_booleans() {
    // Wrong data type marker
    assert_eq!(
        parse_boolean(b":t\r\n").err().unwrap().message(),
        "ERR Invalid data type"
    );

    // Invalid boolean value
    assert_eq!(
        parse_boolean(b"#x\r\n").err().unwrap().message(),
        "ERR invalid value"
    );

    // Missing \r\n terminator
    assert_eq!(
        parse_boolean(b"#t").err().unwrap().message(),
        "ERR Unexpected end of input"
    );

    // Only \r without \n
    assert_eq!(
        parse_boolean(b"#t\r").err().unwrap().message(),
        "ERR Unexpected end of input"
    );

    // Empty input
    assert_eq!(
        parse_boolean(b"").err().unwrap().message(),
        "ERR Empty data"
    );

    // Just the marker
    assert_eq!(
        parse_boolean(b"#").err().unwrap().message(),
        "ERR Unexpected end of input"
    );

    // Case sensitivity
    assert_eq!(
        parse_boolean(b"#T\r\n").err().unwrap().message(),
        "ERR invalid value"
    );

    // Extra content
    assert_eq!(
        parse_boolean(b"#ttrue\r\n").err().unwrap().message(),
        "ERR Unexpected end of input"
    );
}

#[test]
fn test_boolean_remaining_bytes() {
    // Test with remaining data
    let (value, remaining) = parse_boolean(b"#t\r\n+OK\r\n").unwrap();
    assert_eq!(value, RespType::Boolean(true));
    assert_eq!(remaining, b"+OK\r\n");

    // Test with no remaining data
    let (value, remaining) = parse_boolean(b"#f\r\n").unwrap();
    assert_eq!(value, RespType::Boolean(false));
    assert_eq!(remaining, b"");

    // Test with multiple commands
    let (value, remaining) = parse_boolean(b"#t\r\n:42\r\n").unwrap();
    assert_eq!(value, RespType::Boolean(true));
    assert_eq!(remaining, b":42\r\n");

    // Test with false and remaining data
    let (value, remaining) = parse_boolean(b"#f\r\n-ERR test\r\n").unwrap();
    assert_eq!(value, RespType::Boolean(false));
    assert_eq!(remaining, b"-ERR test\r\n");
}