Skip to content

Commit f35e705

Browse files
committed
test(linter): add test for Message::from_tsgo_lint_diagnostic
1 parent a1b52c8 commit f35e705

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

crates/oxc_linter/src/tsgolint.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,3 +506,91 @@ pub fn try_find_tsgolint_executable(cwd: &Path) -> Option<PathBuf> {
506506

507507
None
508508
}
509+
510+
#[cfg(test)]
511+
mod test {
512+
use oxc_diagnostics::{LabeledSpan, OxcCode, Severity};
513+
use oxc_span::{GetSpan, Span};
514+
515+
use crate::{
516+
fixer::{Message, PossibleFixes},
517+
tsgolint::{Fix, MessageType, Range, RuleMessage, TsGoLintDiagnostic},
518+
};
519+
520+
impl PartialEq for PossibleFixes<'_> {
521+
fn eq(&self, other: &Self) -> bool {
522+
match (self, other) {
523+
(Self::None, Self::None) => true,
524+
(Self::Single(fix1), Self::Single(fix2)) => fix1 == fix2,
525+
(Self::Multiple(fixes1), Self::Multiple(fixes2)) => {
526+
fixes1.iter().zip(fixes2.iter()).all(|(f1, f2)| f1 == f2)
527+
}
528+
_ => false,
529+
}
530+
}
531+
}
532+
533+
#[test]
534+
fn test_message_from_tsgo_lint_diagnostic_basic() {
535+
let diagnostic = TsGoLintDiagnostic {
536+
r#type: MessageType::Diagnostic,
537+
range: Range { pos: 0, end: 10 },
538+
rule: "some_rule".into(),
539+
message: RuleMessage {
540+
id: "some_id".into(),
541+
description: "Some description".into(),
542+
help: Some("Some help".into()),
543+
},
544+
fixes: vec![],
545+
suggestions: vec![],
546+
file_path: "some/file/path".into(),
547+
};
548+
549+
let message = Message::from_tsgo_lint_diagnostic(diagnostic, "Some text over 10 bytes.");
550+
551+
assert_eq!(message.error.message, "Some description");
552+
assert_eq!(message.error.severity, Severity::Warning);
553+
assert_eq!(message.span(), Span::new(0, 10));
554+
assert_eq!(
555+
message.error.code,
556+
OxcCode { scope: Some("typescript-eslint".into()), number: Some("some_rule".into()) }
557+
);
558+
assert!(message.error.labels.as_ref().is_some());
559+
assert_eq!(message.error.labels.as_ref().unwrap().len(), 1);
560+
assert_eq!(message.error.labels.as_ref().unwrap()[0], LabeledSpan::new(None, 0, 10));
561+
assert_eq!(message.error.help, Some("Some help".into()));
562+
assert!(message.fixes.is_empty());
563+
}
564+
565+
#[test]
566+
fn test_message_from_tsgo_lint_diagnostic_with_fixes() {
567+
let diagnostic = TsGoLintDiagnostic {
568+
r#type: MessageType::Diagnostic,
569+
range: Range { pos: 0, end: 10 },
570+
rule: "some_rule".into(),
571+
message: RuleMessage {
572+
id: "some_id".into(),
573+
description: "Some description".into(),
574+
help: None,
575+
},
576+
fixes: vec![
577+
Fix { text: "fixed".into(), range: Range { pos: 0, end: 5 } },
578+
Fix { text: "hello".into(), range: Range { pos: 5, end: 10 } },
579+
],
580+
suggestions: vec![],
581+
file_path: "some/file/path".into(),
582+
};
583+
584+
let message = Message::from_tsgo_lint_diagnostic(diagnostic, "Some text over 10 bytes.");
585+
586+
assert_eq!(message.fixes.len(), 1);
587+
assert_eq!(
588+
message.fixes,
589+
PossibleFixes::Single(crate::fixer::Fix {
590+
content: "fixedhello".into(),
591+
span: Span::new(0, 10),
592+
message: None,
593+
})
594+
);
595+
}
596+
}

0 commit comments

Comments
 (0)