use super::{
ReleaseBump, UnsupportedVersion, json_quote_units, patch_bump_version, unsupported_message,
};
fn units(text: &str) -> Vec<u16> {
return text.encode_utf16().collect();
}
fn bump(version: &str) -> Result<String, Vec<u16>> {
return patch_bump_version("@scope/a", &units(version))
.map(|done: ReleaseBump| {
assert_eq!(done.from, version);
return done.to;
})
.map_err(|unsupported: UnsupportedVersion| {
assert_eq!(unsupported.name, "@scope/a");
return unsupported.version;
});
}
#[test]
fn increments_the_patch_component() {
assert_eq!(bump("1.2.3"), Ok(String::from("1.2.4")));
assert_eq!(bump("0.0.9"), Ok(String::from("0.0.10")));
assert_eq!(bump("0.0.0"), Ok(String::from("0.0.1")));
assert_eq!(bump("10.20.199"), Ok(String::from("10.20.200")));
assert_eq!(bump("1.0.909"), Ok(String::from("1.0.910")));
}
#[test]
fn increments_exactly_at_any_size() {
assert_eq!(
bump("1.0.9007199254740993"),
Ok(String::from("1.0.9007199254740994"))
);
assert_eq!(
bump("1.0.999999999999999999999"),
Ok(String::from("1.0.1000000000000000000000"))
);
}
#[test]
fn rejects_versions_that_are_not_plain_releases() {
for version in ["1.0.0-alpha.1", "1.0.0+build", "01.0.0", "1.0", "1.0.x", ""] {
assert_eq!(bump(version), Err(units(version)), "{version}");
}
}
#[test]
fn checks_every_component() {
for version in [
"1..0",
".1.0",
"1.0.",
"1.01.0",
"1.0.01",
"1.0.0.0",
"1.0.\u{663}",
"1./.0",
"1.:.0",
] {
assert_eq!(bump(version), Err(units(version)), "{version}");
}
assert_eq!(bump("1.0.0"), Ok(String::from("1.0.1")));
assert_eq!(bump("10.0.0"), Ok(String::from("10.0.1")));
}
#[test]
fn states_the_unsupported_message() {
assert_eq!(
unsupported_message(&UnsupportedVersion {
name: String::from("@s/runtime"),
version: units("2.0.0-rc.1"),
}),
"@s/runtime has version \"2.0.0-rc.1\", which is not a plain major.minor.patch release; \
bump it by hand in the same commit."
);
}
#[test]
fn quotes_as_json_stringify() {
assert_eq!(json_quote_units(&units("a\"b\\c")), "\"a\\\"b\\\\c\"");
assert_eq!(
json_quote_units(&units("\u{8}\u{c}\n\r\t")),
"\"\\b\\f\\n\\r\\t\""
);
assert_eq!(
json_quote_units(&units("\u{1}\u{1f} ")),
"\"\\u0001\\u001f \""
);
assert_eq!(
json_quote_units(&units("\u{7f}\u{2028}\u{1f600}")),
"\"\u{7f}\u{2028}\u{1f600}\""
);
assert_eq!(
json_quote_units(&[0xd800, 0x41, 0xdc0f]),
"\"\\ud800A\\udc0f\""
);
assert_eq!(json_quote_units(&[]), "\"\"");
}