Hi,
I have a pretty simple test which compares two strings representing software versions, one for a client and one for a server. Let's consider the server to be always correct. I wrote three unit tests to cover the three possible cases:
- Current versions match
- Client is older than Server
- Client is (somehow) newer than Server
Two of the three cases pass, but one always fails. What I do not understand is why this fails at all given that the other two cases pass.
Fun fact: On my PC it always fails for test case '2', but on Rust Playground it fails for case '3'.
I was reading a little bit and some people pointed to using lazy_static!{}
but I couldn't get it working. On my actual code base, SYSTEM_VERSION
is defined in a non-root-crate module used by two different binaries.
src\
|-- client.rs <--- defines a `main()`
|-- net.rs
|__ server.rs <--- defines a `main()`
Both import net.rs
Code Snippet:
pub static SYSTEM_VERSION: &'static str = env!("CARGO_PKG_VERSION");
fn validate_version(version: String) -> bool {
return version <= SYSTEM_VERSION.to_owned()
}
fn main () {
assert!(validate_version( env!("CARGO_PKG_VERSION").to_owned()), true);
assert!(validate_version(format!("{}.{}.{}", 0, 0, 1).to_owned()), true);
assert!(validate_version(format!("{}.{}.{}", <i32>::max_value(), <i32>::max_value(), <i32>::max_value()).to_owned()), false);
println!("All cases passed");
}
Any help would be appreciated. Thanks.