Skip to main content

swls_lang_turtle/lang/
model.rs

1#[cfg(test)]
2mod test {
3    use swls_lang_rdf_base::traits::{Turtle, TurtleExt};
4
5    fn parse_turtle(inp: &str, base_url: &str) -> Turtle {
6        use rdf_parsers::turtle::{convert::convert, Lang, Rule, SyntaxKind};
7        let (parse, _) = rdf_parsers::parse_incremental(
8            Rule::new(SyntaxKind::TurtleDoc),
9            inp,
10            None,
11            rdf_parsers::IncrementalBias::default(),
12        );
13        let mut t = convert(&parse.syntax::<Lang>());
14        t.set_base = Some(base_url.to_string());
15        t
16    }
17
18    #[test]
19    fn easy_triples() {
20        let txt = r#"
21@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
22@prefix foaf: <http://xmlns.com/foaf/0.1/> .
23
24[] a foaf:Name;
25   foaf:knows <abc>;.
26"#;
27        let output = parse_turtle(txt, "http://example.com/ns#");
28        let triples = output.get_simple_triples().expect("Triples found");
29        assert_eq!(triples.triples.len(), 3);
30        println!("{:?}", triples);
31    }
32
33    #[test]
34    fn easy_triples_2() {
35        let txt = r#"
36@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
37@prefix foaf: <http://xmlns.com/foaf/0.1/> .
38
39[ foaf:knows <abc>; ]
40    a foaf:Name;
41    foaf:knows [
42        a foaf:Name;
43        foaf:knows [
44            a foaf:Name; ] ].
45"#;
46        let output = parse_turtle(txt, "http://example.com/ns#");
47        let triples = output.get_simple_triples().expect("Triples found");
48        assert_eq!(triples.triples.len(), 6);
49    }
50
51    #[test]
52    fn triple_spans_are_correct() {
53        // "@prefix foaf: <http://xmlns.com/foaf/0.1/>.\n<> foaf:name \"Arthur\"."
54        // Byte layout after the newline (offset 44):
55        //   44..46  "<>"
56        //   47..56  "foaf:name"
57        //   57..65  "\"Arthur\""
58        let txt = "@prefix foaf: <http://xmlns.com/foaf/0.1/>.\n<> foaf:name \"Arthur\".";
59        let output = parse_turtle(txt, "http://example.com/");
60        let triples = output.get_simple_triples().expect("Triples found");
61        assert_eq!(
62            triples.triples.len(),
63            1,
64            "expected exactly one triple, got: {:#?}",
65            triples.triples
66        );
67        let t = &triples.triples[0];
68        println!("triple span:    {:?}", t.span);
69        println!("subject span:   {:?}", t.subject.span);
70        println!("predicate span: {:?}", t.predicate.span);
71        println!("object span:    {:?}", t.object.span);
72
73        // Subject "<>" is at bytes 44..46
74        assert!(
75            t.subject.span.contains(&44),
76            "cursor at start of subject should be in subject span, span={:?}",
77            t.subject.span
78        );
79        // Predicate "foaf:name" is at bytes 47..56
80        assert!(
81            t.predicate.span.contains(&50),
82            "cursor in middle of predicate should be in predicate span, span={:?}",
83            t.predicate.span
84        );
85        // Object "\"Arthur\"" is at bytes 57..65
86        assert!(
87            t.object.span.contains(&60),
88            "cursor in middle of object should be in object span, span={:?}",
89            t.object.span
90        );
91
92        // The triple outer span must contain all positions
93        assert!(t.span.contains(&44));
94        assert!(t.span.contains(&50));
95        assert!(t.span.contains(&60));
96    }
97
98    #[test]
99    fn owl_is_valid() {
100        let txt = include_str!("../../../lov/prefixes/owl.ttl");
101        let output = parse_turtle(txt, "http://example.com/ns#");
102        output.get_simple_triples().expect("Triples found");
103    }
104
105    #[test]
106    fn owl_is_valid_2() {
107        let txt = r#"
108@prefix dc:    <http://purl.org/dc/elements/1.1/> .
109@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
110@prefix owl:   <http://www.w3.org/2002/07/owl#> .
111@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
112@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
113@prefix xml:   <http://www.w3.org/XML/1998/namespace> .
114@prefix grddl: <http://www.w3.org/2003/g/data-view#> .
115
116<http://www.w3.org/2002/07/owl>
117        a                              owl:Ontology ;
118        rdfs:comment                   "
119  This ontology partially describes the built-in " ; .
120            "#;
121        let output = parse_turtle(txt, "http://example.com/ns#");
122        output.get_simple_triples().expect("Triples found");
123    }
124}