Skip to main content

rdf_parsers/turtle/
mod.rs

1pub mod parser;
2
3pub mod convert;
4pub use parser::*;
5
6#[cfg(test)]
7mod format_tests {
8    use crate::{
9        parse,
10        turtle::{Lang, Rule, SyntaxKind},
11    };
12
13    fn format_turtle(input: &str, width: usize) -> String {
14        let rule = Rule::new(SyntaxKind::TurtleDoc);
15        let (result, _) = parse(rule, input);
16        let root = result.syntax::<Lang>();
17        crate::turtle::parser::format::format(&root, width)
18    }
19
20    #[test]
21    fn format_multiple_objects() {
22        // Both inner and outer fit at wide width — both stay flat.
23        let input = r#"<a> <b> <c>,<d>  ."#;
24        let out = format_turtle(input, 80);
25        assert_eq!(out, "<a> <b> <c>, <d>.\n");
26    }
27    #[test]
28    fn format_multiple_predicate_objects() {
29        // Both inner and outer fit at wide width — both stay flat.
30        let input = r#"<a> <b> <c>; <d> <e> ."#;
31        let out = format_turtle(input, 80);
32        assert_eq!(out, "<a> <b> <c>; <d> <e>.\n");
33    }
34
35    #[test]
36    fn format_multiple_predicate_objects_with_newlines() {
37        // Too wide for one line: break after ';', indenting the second clause.
38        let input = r#"<a> <b> <c>; <d> <e> ."#;
39        let out = format_turtle(input, 16);
40        assert_eq!(out, "<a> <b> <c>;\n  <d> <e>.\n");
41
42        let input = r#"<a> <b> <c>; <d> <e>; <f> <g> ."#;
43        let out = format_turtle(input, 16);
44        assert_eq!(out, "<a> <b> <c>;\n  <d> <e>;\n  <f> <g>.\n");
45    }
46
47    #[test]
48    fn format_multiple_predicate_objects_with_newlines_bn_subject() {
49        let input = r#"[ ] <b> <c>; <d> <e>; <f> <g> ."#;
50        let out = format_turtle(input, 16);
51        assert_eq!(out, "[ ] <b> <c>;\n  <d> <e>;\n  <f> <g>.\n");
52    }
53
54    #[test]
55    fn format_multiple_triples_with_newlines() {
56        let input = r#"<a> <b> <c>; <d> <e>; <f> <g> . <a> <b> <c>; <d> <e>; <f> <g> ."#;
57        let out = format_turtle(input, 16);
58        assert_eq!(
59            out,
60            "<a> <b> <c>;\n  <d> <e>;\n  <f> <g>.\n\n<a> <b> <c>;\n  <d> <e>;\n  <f> <g>.\n"
61        );
62    }
63
64    #[test]
65    fn format_multiple_objects_with_newlines() {
66        let input = r#"<a> <b> <c>, <d>, <e>, <f>, <g> . "#;
67        let out = format_turtle(input, 16);
68        assert_eq!(out, "<a> <b> <c>,\n  <d>,\n  <e>,\n  <f>,\n  <g>.\n");
69    }
70
71    #[test]
72    fn format_directies() {
73        // Too wide for one line: break after ';', indenting the second clause.
74        let input = r#"@prefix foaf: <> ."#;
75        let out = format_turtle(input, 16);
76        assert_eq!(out, "@prefix foaf: <>.\n");
77    }
78
79    #[test]
80    fn format_multiple_triples() {
81        // Too wide for one line: break after ';', indenting the second clause.
82        let input = r#"<a> <b> <c>. <d> <e> <f> ."#;
83        let out = format_turtle(input, 16);
84        assert_eq!(out, "<a> <b> <c>.\n\n<d> <e> <f>.\n");
85    }
86    #[test]
87    fn format_multiple_directives() {
88        // Too wide for one line: break after ';', indenting the second clause.
89        let input = r#"@prefix foaf: <> . @prefix foaf2: <> . "#;
90        let out = format_turtle(input, 16);
91        assert_eq!(out, "@prefix foaf: <>.\n@prefix foaf2: <>.\n");
92    }
93
94    #[test]
95    fn format_directive_then_triple() {
96        // Too wide for one line: break after ';', indenting the second clause.
97        let input = r#"@prefix foaf: <> . <a> <b> <c>. "#;
98        let out = format_turtle(input, 16);
99        assert_eq!(out, "@prefix foaf: <>.\n\n<a> <b> <c>.\n");
100    }
101
102    #[test]
103    fn format_bn_triple() {
104        // Too wide for one line: break after ';', indenting the second clause.
105        let input = r#"[ <a> <b> ]."#;
106        let out = format_turtle(input, 16);
107        assert_eq!(out, "[ <a> <b> ].\n");
108    }
109
110    #[test]
111    fn format_bn_triple_big() {
112        // Too wide for one line: break after ';', indenting the second clause.
113        let input = r#"[ <a> <b>; <c> <d> ]."#;
114        let out = format_turtle(input, 24);
115        assert_eq!(out, "[ <a> <b>; <c> <d> ].\n");
116    }
117
118    #[test]
119    fn format_bn_triple_big_multi_line() {
120        // Too wide for one line: break after ';', indenting the second clause.
121        let input = r#"[ <a> <b>; <c> <d>; <e> <f> ]."#;
122        let out = format_turtle(input, 16);
123        assert_eq!(out, "[\n  <a> <b>;\n  <c> <d>;\n  <e> <f>\n].\n");
124    }
125}