Skip to content

Commit

Permalink
Add tests for python version compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Nov 18, 2024
1 parent a57503d commit 57cd678
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
14 changes: 14 additions & 0 deletions crates/red_knot/src/target_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ impl From<TargetVersion> for red_knot_python_semantic::PythonVersion {
}
}
}

#[cfg(test)]
mod tests {
use crate::target_version::TargetVersion;
use red_knot_python_semantic::PythonVersion;

#[test]
fn same_default_as_python_version() {
assert_eq!(
PythonVersion::from(TargetVersion::default()),
PythonVersion::default()
);
}
}
14 changes: 14 additions & 0 deletions crates/red_knot_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,17 @@ impl System for WasmSystem {
fn not_found() -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::NotFound, "No such file or directory")
}

#[cfg(test)]
mod tests {
use crate::TargetVersion;
use red_knot_python_semantic::PythonVersion;

#[test]
fn same_default_as_python_version() {
assert_eq!(
PythonVersion::from(TargetVersion::default()),
PythonVersion::default()
);
}
}
4 changes: 4 additions & 0 deletions crates/ruff_linter/src/settings/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ impl PythonVersion {
Self::Py313
}

pub const fn minimal_supported() -> Self {
Self::Py37
}

pub const fn as_tuple(&self) -> (u8, u8) {
match self {
Self::Py37 => (3, 7),
Expand Down
20 changes: 20 additions & 0 deletions crates/ruff_python_formatter/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,24 @@ impl PythonVersion {
pub fn supports_pep_701(self) -> bool {
self >= Self::Py312
}

pub fn as_tuple(self) -> (u8, u8) {
match self {
Self::Py37 => (3, 7),
Self::Py38 => (3, 8),
Self::Py39 => (3, 9),
Self::Py310 => (3, 10),
Self::Py311 => (3, 11),
Self::Py312 => (3, 12),
Self::Py313 => (3, 13),
}
}

pub fn latest() -> Self {
Self::Py313
}

pub fn minimal_supported() -> Self {
Self::Py37
}
}
26 changes: 26 additions & 0 deletions crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3408,7 +3408,9 @@ pub struct AnalyzeOptions {
mod tests {
use crate::options::Flake8SelfOptions;
use ruff_linter::rules::flake8_self;
use ruff_linter::settings::types::PythonVersion as LinterPythonVersion;
use ruff_python_ast::name::Name;
use ruff_python_formatter::PythonVersion as FormatterPythonVersion;

#[test]
fn flake8_self_options() {
Expand Down Expand Up @@ -3456,4 +3458,28 @@ mod tests {
vec![Name::new_static("_foo"), Name::new_static("_bar")]
);
}

#[test]
fn formatter_and_linter_target_version_have_same_default() {
assert_eq!(
FormatterPythonVersion::default().as_tuple(),
LinterPythonVersion::default().as_tuple()
);
}

#[test]
fn formatter_and_linter_target_version_have_same_latest() {
assert_eq!(
FormatterPythonVersion::latest().as_tuple(),
LinterPythonVersion::latest().as_tuple()
);
}

#[test]
fn formatter_and_linter_target_version_have_same_minimal_supported() {
assert_eq!(
FormatterPythonVersion::minimal_supported().as_tuple(),
LinterPythonVersion::minimal_supported().as_tuple()
);
}
}

0 comments on commit 57cd678

Please sign in to comment.