Skip to main content

rdf_parsers/trig/
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        trig::{Lang, Rule, SyntaxKind},
11    };
12
13    fn format_trig(input: &str, width: usize) -> String {
14        let rule = Rule::new(SyntaxKind::TrigDoc);
15        let (result, _) = parse(rule, input);
16        let root = result.syntax::<Lang>();
17        crate::trig::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_trig(input, 80);
25        assert_eq!(out, "<a> <b> <c>, <d>.\n");
26    }
27
28    #[test]
29    fn format_multiple_predicate_objects() {
30        // Both inner and outer fit at wide width — both stay flat.
31        let input = r#"<a> <b> <c>; <d> <e> ."#;
32        let out = format_trig(input, 80);
33        assert_eq!(out, "<a> <b> <c>; <d> <e>.\n");
34    }
35
36    #[test]
37    fn format_triple_long_bn_subject() {
38        let input =
39            r#"[ ] a foaf:Agent; foaf:account "test"; rml:constant 5; rml:template "Testing". "#;
40        let output = "[ ] a foaf:Agent;\n  foaf:account \"test\";\n  rml:constant 5;\n  rml:template \"Testing\".\n";
41        assert_eq!(&format_trig(input, 70), output);
42    }
43
44    #[test]
45    fn format_multiple_predicate_objects_with_newlines() {
46        // Too wide for one line: break after ';', indenting the second clause.
47        let input = r#"<a> <b> <c>; <d> <e> ."#;
48        let out = format_trig(input, 16);
49        assert_eq!(out, "<a> <b> <c>;\n  <d> <e>.\n");
50
51        let input = r#"<a> <b> <c>; <d> <e>; <f> <g> ."#;
52        let out = format_trig(input, 16);
53        assert_eq!(out, "<a> <b> <c>;\n  <d> <e>;\n  <f> <g>.\n");
54    }
55
56    #[test]
57    fn format_multiple_triples_with_newlines() {
58        let input = r#"<a> <b> <c>; <d> <e>; <f> <g> . <a> <b> <c>; <d> <e>; <f> <g> ."#;
59        let out = format_trig(input, 16);
60        assert_eq!(
61            out,
62            "<a> <b> <c>;\n  <d> <e>;\n  <f> <g>.\n\n<a> <b> <c>;\n  <d> <e>;\n  <f> <g>.\n"
63        );
64    }
65
66    #[test]
67    fn format_directies() {
68        // Too wide for one line: break after ';', indenting the second clause.
69        let input = r#"@prefix foaf: <> ."#;
70        let out = format_trig(input, 16);
71        assert_eq!(out, "@prefix foaf: <>.\n");
72    }
73
74    #[test]
75    fn format_multiple_triples() {
76        // Too wide for one line: break after ';', indenting the second clause.
77        let input = r#"<a> <b> <c>. <d> <e> <f> ."#;
78        let out = format_trig(input, 16);
79        assert_eq!(out, "<a> <b> <c>.\n\n<d> <e> <f>.\n");
80    }
81
82    #[test]
83    fn format_multiple_directives() {
84        // Too wide for one line: break after ';', indenting the second clause.
85        let input = r#"@prefix foaf: <> . @prefix foaf2: <> . "#;
86        let out = format_trig(input, 16);
87        assert_eq!(out, "@prefix foaf: <>.\n@prefix foaf2: <>.\n");
88    }
89
90    #[test]
91    fn format_directive_then_triple() {
92        // Too wide for one line: break after ';', indenting the second clause.
93        let input = r#"@prefix foaf: <> . <a> <b> <c>. "#;
94        let out = format_trig(input, 16);
95        assert_eq!(out, "@prefix foaf: <>.\n\n<a> <b> <c>.\n");
96    }
97
98    #[test]
99    fn format_bn_triple() {
100        // Too wide for one line: break after ';', indenting the second clause.
101        let input = r#"[ <a> <b> ]."#;
102        let out = format_trig(input, 16);
103        assert_eq!(out, "[ <a> <b> ].\n");
104    }
105
106    #[test]
107    fn format_bn_triple_big() {
108        // Too wide for one line: break after ';', indenting the second clause.
109        let input = r#"[ <a> <b>; <c> <d> ]."#;
110        let out = format_trig(input, 24);
111        assert_eq!(out, "[ <a> <b>; <c> <d> ].\n");
112    }
113
114    #[test]
115    fn format_bn_triple_big_multi_line() {
116        // Too wide for one line: break after ';', indenting the second clause.
117        let input = r#"[ <a> <b>; <c> <d>; <e> <f> ]."#;
118        let out = format_trig(input, 16);
119        assert_eq!(out, "[\n  <a> <b>;\n  <c> <d>;\n  <e> <f>\n].\n");
120    }
121
122    #[test]
123    fn format_triples_inside_graph() {
124        // Too wide for one line: break after ';', indenting the second clause.
125        let input = r#"GRAPH <b> { <a> <b> <c> }"#;
126        let out = format_trig(input, 64);
127        assert_eq!(out, "GRAPH <b> { <a> <b> <c> }\n");
128    }
129
130    #[test]
131    fn format_triples_inside_graph_long() {
132        let input = r#"GRAPH <b> { <a> <b> <c>. <a> <b> <c> }"#;
133        let out = format_trig(input, 16);
134        assert_eq!(out, "GRAPH <b> {\n  <a> <b> <c>.\n\n  <a> <b> <c>\n\n}\n");
135    }
136
137    #[test]
138    fn format_triples_inside_graph_bn() {
139        let input = r#"GRAPH <b> { [ <a> <b> ; <a> <b> ] a foaf:Friend }"#;
140        let out = format_trig(input, 16);
141        assert_eq!(
142            out,
143            "GRAPH <b> {\n  [\n    <a> <b>;\n    <a> <b>\n  ] a foaf:Friend\n}\n"
144        );
145    }
146}