Commit 1606a66c authored by Clara Engler's avatar Clara Engler
Browse files

tor-netdoc: Refactor RelayPlatform tests

This refactors the RelayPlatform test to store the test vectors in an
array and iterate over it, comparing it with the expected output.  This
is a lot better than the current version, where there is not just a lot
of copy and pasted code but also some tests that only check for an okay
value.

Unfortunately, there is not an easy way to review this with
--color-moved or something.  Personally, I would recommend to review
each original test vector (i.e. a line starting with `let p =` followed
by a string literal) and verify that the exact same string literal is
still present within the new test vector.  Afterwards, verifying the
assertion logic should be easy, as it is a one-liner.
parent c8e4ff10
Loading
Loading
Loading
Loading
+28 −21
Original line number Diff line number Diff line
@@ -1273,27 +1273,34 @@ mod test {

    #[test]
    fn test_platform() {
        let p = "Tor 0.4.4.4-alpha on a flying bison".parse::<RelayPlatform>();
        assert!(p.is_ok());
        assert_eq!(
            p.unwrap(),
        let tests = [
            // Test with platform.
            (
                "Tor 0.4.4.4-alpha on a flying bison",
                RelayPlatform::Tor(
                    "0.4.4.4-alpha".parse().unwrap(),
                "a flying bison".to_string()
            )
        );

        let p = "Tor 0.4.4.4-alpha on".parse::<RelayPlatform>();
        assert!(p.is_ok());

        let p = "Tor 0.4.4.4-alpha ".parse::<RelayPlatform>();
        assert!(p.is_ok());
        let p = "Tor 0.4.4.4-alpha".parse::<RelayPlatform>();
        assert!(p.is_ok());

        let p = "arti 0.0.0".parse::<RelayPlatform>();
        assert!(p.is_ok());
        assert_eq!(p.unwrap(), RelayPlatform::Other("arti 0.0.0".to_string()));
                    "a flying bison".to_string(),
                ),
            ),
            // Test without platform but potentially weird spacing.
            (
                "Tor 0.4.4.4-alpha on",
                RelayPlatform::Tor("0.4.4.4-alpha".parse().unwrap(), "".to_string()),
            ),
            (
                "Tor 0.4.4.4-alpha ",
                RelayPlatform::Tor("0.4.4.4-alpha".parse().unwrap(), "".to_string()),
            ),
            (
                "Tor 0.4.4.4-alpha",
                RelayPlatform::Tor("0.4.4.4-alpha".parse().unwrap(), "".to_string()),
            ),
            // Test other.
            ("arti 0.0.0", RelayPlatform::Other("arti 0.0.0".to_string())),
        ];
        for (input, output) in tests {
            assert_eq!(input.parse::<RelayPlatform>().unwrap(), output);
        }
    }

    #[test]