Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 48dbe52

Browse files
oppiliappanHendrik van Antwerpen
authored andcommitted
move tests to test/it/serde.rs
1 parent aaf8c52 commit 48dbe52

File tree

3 files changed

+283
-279
lines changed

3 files changed

+283
-279
lines changed

stack-graphs/src/serde.rs

Lines changed: 16 additions & 279 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ mod filter {
103103

104104
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Default, Clone)]
105105
pub struct StackGraph {
106-
files: Files,
107-
nodes: Nodes,
108-
edges: Edges,
106+
pub files: Files,
107+
pub nodes: Nodes,
108+
pub edges: Edges,
109109
}
110110

111111
#[derive(Debug, Error, PartialEq, Eq)]
@@ -288,13 +288,13 @@ impl StackGraph {
288288
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Default, Clone)]
289289
#[serde(transparent)]
290290
pub struct Files {
291-
data: Vec<String>,
291+
pub data: Vec<String>,
292292
}
293293

294294
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Default, Clone)]
295295
#[serde(transparent)]
296296
pub struct Nodes {
297-
data: Vec<Node>,
297+
pub data: Vec<Node>,
298298
}
299299

300300
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
@@ -391,26 +391,26 @@ impl Node {
391391

392392
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
393393
pub struct SourceInfo {
394-
span: lsp_positions::Span,
395-
syntax_type: Option<String>,
394+
pub span: lsp_positions::Span,
395+
pub syntax_type: Option<String>,
396396
}
397397

398398
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
399399
#[serde(transparent)]
400400
pub struct DebugInfo {
401-
data: Vec<DebugEntry>,
401+
pub data: Vec<DebugEntry>,
402402
}
403403

404404
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
405405
pub struct DebugEntry {
406-
key: String,
407-
value: String,
406+
pub key: String,
407+
pub value: String,
408408
}
409409

410410
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
411411
pub struct NodeID {
412-
file: Option<String>,
413-
local_id: u32,
412+
pub file: Option<String>,
413+
pub local_id: u32,
414414
}
415415

416416
impl NodeID {
@@ -446,14 +446,14 @@ impl NodeID {
446446
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Default, Clone)]
447447
#[serde(transparent)]
448448
pub struct Edges {
449-
data: Vec<Edge>,
449+
pub data: Vec<Edge>,
450450
}
451451

452452
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
453453
pub struct Edge {
454-
source: NodeID,
455-
sink: NodeID,
456-
precedence: i32,
454+
pub source: NodeID,
455+
pub sink: NodeID,
456+
pub precedence: i32,
457457
}
458458

459459
impl crate::graph::StackGraph {
@@ -586,266 +586,3 @@ impl crate::graph::StackGraph {
586586
}
587587
}
588588
}
589-
590-
#[cfg(test)]
591-
mod test {
592-
use super::*;
593-
594-
#[test]
595-
#[cfg(feature = "json")]
596-
fn serde_json_stack_graph() {
597-
let expected = StackGraph {
598-
files: Files {
599-
data: vec!["index.ts".to_owned()],
600-
},
601-
nodes: Nodes {
602-
data: vec![Node::Root {
603-
id: NodeID {
604-
local_id: 1,
605-
file: None,
606-
},
607-
source_info: Some(SourceInfo {
608-
span: lsp_positions::Span {
609-
start: lsp_positions::Position {
610-
line: 0,
611-
column: lsp_positions::Offset {
612-
utf8_offset: 0,
613-
utf16_offset: 0,
614-
grapheme_offset: 0,
615-
},
616-
containing_line: 0..0,
617-
trimmed_line: 0..0,
618-
},
619-
end: lsp_positions::Position {
620-
line: 0,
621-
column: lsp_positions::Offset {
622-
utf8_offset: 0,
623-
utf16_offset: 0,
624-
grapheme_offset: 0,
625-
},
626-
containing_line: 0..0,
627-
trimmed_line: 0..0,
628-
},
629-
},
630-
syntax_type: None,
631-
}),
632-
debug_info: Some(DebugInfo { data: vec![] }),
633-
}],
634-
},
635-
edges: Edges {
636-
data: vec![Edge {
637-
source: NodeID {
638-
file: None,
639-
local_id: 1,
640-
},
641-
sink: NodeID {
642-
file: Some("index.ts".to_owned()),
643-
local_id: 0,
644-
},
645-
precedence: 0,
646-
}],
647-
},
648-
};
649-
650-
let json_data = serde_json::json!({
651-
"files": [
652-
"index.ts"
653-
],
654-
"nodes": [{
655-
"type": "root",
656-
"id": {
657-
"local_id": 1
658-
},
659-
"source_info": {
660-
"span": {
661-
"start": {
662-
"line": 0,
663-
"column": {
664-
"utf8_offset": 0,
665-
"utf16_offset": 0,
666-
"grapheme_offset": 0
667-
}
668-
},
669-
"end": {
670-
"line": 0,
671-
"column": {
672-
"utf8_offset": 0,
673-
"utf16_offset": 0,
674-
"grapheme_offset": 0
675-
}
676-
}
677-
}
678-
},
679-
"debug_info": []
680-
}],
681-
"edges": [{
682-
"source": {
683-
"local_id": 1
684-
},
685-
"sink": {
686-
"file": "index.ts",
687-
"local_id": 0
688-
},
689-
"precedence": 0
690-
}]
691-
692-
});
693-
694-
let observed = serde_json::from_value::<super::StackGraph>(json_data).unwrap();
695-
696-
assert_eq!(observed, expected);
697-
}
698-
699-
#[test]
700-
#[cfg(feature = "json")]
701-
fn reconstruct() {
702-
let json_data = serde_json::json!(
703-
{
704-
"files": [
705-
"index.ts"
706-
],
707-
"nodes": [
708-
{
709-
"type": "root",
710-
"id": {
711-
"local_id": 1
712-
},
713-
"source_info": {
714-
"span": {
715-
"start": {
716-
"line": 0,
717-
"column": {
718-
"utf8_offset": 0,
719-
"utf16_offset": 0,
720-
"grapheme_offset": 0
721-
}
722-
},
723-
"end": {
724-
"line": 0,
725-
"column": {
726-
"utf8_offset": 0,
727-
"utf16_offset": 0,
728-
"grapheme_offset": 0
729-
}
730-
}
731-
}
732-
},
733-
"debug_info": []
734-
},
735-
{
736-
"type": "jump_to_scope",
737-
"id": {
738-
"local_id": 2
739-
},
740-
"source_info": {
741-
"span": {
742-
"start": {
743-
"line": 0,
744-
"column": {
745-
"utf8_offset": 0,
746-
"utf16_offset": 0,
747-
"grapheme_offset": 0
748-
}
749-
},
750-
"end": {
751-
"line": 0,
752-
"column": {
753-
"utf8_offset": 0,
754-
"utf16_offset": 0,
755-
"grapheme_offset": 0
756-
}
757-
}
758-
}
759-
},
760-
"debug_info": []
761-
},
762-
{
763-
"type": "scope",
764-
"is_exported": false,
765-
"id": {
766-
"file": "index.ts",
767-
"local_id": 0
768-
},
769-
"source_info": {
770-
"span": {
771-
"start": {
772-
"line": 0,
773-
"column": {
774-
"utf8_offset": 0,
775-
"utf16_offset": 0,
776-
"grapheme_offset": 0
777-
}
778-
},
779-
"end": {
780-
"line": 0,
781-
"column": {
782-
"utf8_offset": 0,
783-
"utf16_offset": 0,
784-
"grapheme_offset": 0
785-
}
786-
}
787-
}
788-
},
789-
"debug_info": [
790-
{
791-
"key": "tsg_variable",
792-
"value": "@prog.defs"
793-
},
794-
{
795-
"key": "tsg_location",
796-
"value": "(225, 14)"
797-
}
798-
]
799-
}
800-
],
801-
"edges": [
802-
{
803-
"source": {
804-
"local_id": 1
805-
},
806-
"sink": {
807-
"file": "index.ts",
808-
"local_id": 0
809-
},
810-
"precedence": 0
811-
}
812-
]
813-
}
814-
);
815-
let observed = serde_json::from_value::<super::StackGraph>(json_data).unwrap();
816-
let mut sg = crate::graph::StackGraph::new();
817-
observed.load_into(&mut sg).unwrap();
818-
819-
assert_eq!(sg.iter_nodes().count(), 3);
820-
assert_eq!(sg.iter_files().count(), 1);
821-
822-
// the scope node should contain debug and source info
823-
let handle = sg
824-
.iter_nodes()
825-
.find(|handle| matches!(sg[*handle], crate::graph::Node::Scope(..)))
826-
.unwrap();
827-
assert!(sg.source_info(handle).is_some());
828-
assert!(sg.debug_info(handle).is_some());
829-
}
830-
831-
#[test]
832-
fn load_fail_accidental_merge() {
833-
let source = StackGraph {
834-
files: Files {
835-
data: vec!["index.ts".to_owned(), "App.tsx".to_owned()],
836-
},
837-
..Default::default()
838-
};
839-
840-
let mut target = crate::graph::StackGraph::new();
841-
target.add_file("App.tsx").unwrap();
842-
843-
assert_eq!(
844-
source.load_into(&mut target).unwrap_err(),
845-
Error::FileAlreadyPresent("App.tsx".to_owned())
846-
);
847-
848-
// ensure that source and target graphs were not partially merged
849-
assert_eq!(target.iter_files().count(), 1);
850-
}
851-
}

stack-graphs/tests/it/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ mod graph;
2424
#[cfg(feature = "json")]
2525
mod json;
2626
mod partial;
27+
#[cfg(feature = "serde")]
28+
mod serde;
2729
mod util;

0 commit comments

Comments
 (0)