Skip to content

Commit

Permalink
feat: add rust test
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Jun 27, 2024
1 parent 7c81d25 commit 07141a0
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use oxc_allocator::Allocator;
use oxc_ast::{ast::{Function}, visit::walk, Visit};
use oxc_ast::ast::{Directive, ImportDeclaration};
use oxc_parser::Parser;
use oxc_span::SourceType;
use oxc_span::{SourceType};
use oxc_syntax::scope::ScopeFlags;

#[napi]
Expand All @@ -25,8 +25,15 @@ pub async fn validate(
.await
.map_err(|e| Error::new(Status::Unknown, format!("Failed to read file: {}", e)))?
).unwrap();
let allocator = Allocator::default();
let source_type = SourceType::from_path(path).unwrap();
validate_string(source_text, source_type)
}

pub fn validate_string(
source_text: String,
source_type: SourceType,
) -> Result<ReactServerComponent> {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, &source_text, source_type).parse();

for error in ret.errors {
Expand Down Expand Up @@ -117,4 +124,40 @@ impl<'a> Visit<'a> for ReactServerComponent {
}
walk::walk_function(self, func, flags);
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_use_server() {
let source_text = r#"
"use server"
"#;
let rsc = validate_string(source_text.to_string(), SourceType::from_path("test.js").unwrap()).unwrap();
assert_eq!(rsc.file_type, FileType::ServerComponent);
assert_eq!(rsc.error, None);
}

#[test]
fn test_use_client() {
let source_text = r#"
"use client"
"#;
let rsc = validate_string(source_text.to_string(), SourceType::from_path("test.js").unwrap()).unwrap();
assert_eq!(rsc.file_type, FileType::Client);
assert_eq!(rsc.error, None);
}

#[test]
fn test_use_server_and_client() {
let source_text = r#"
"use server"
"use client"
"#;
let rsc = validate_string(source_text.to_string(), SourceType::from_path("test.js").unwrap()).unwrap();
assert_eq!(rsc.file_type, FileType::Client);
assert_eq!(rsc.error, Some("Cannot use both client and server".to_string()));
}
}

0 comments on commit 07141a0

Please sign in to comment.