From 13a60e44208158387c10b32b9bd851efbd085907 Mon Sep 17 00:00:00 2001 From: Nordine Bittich Date: Sun, 24 Nov 2024 10:35:10 +0100 Subject: [PATCH] all tests are in separate files --- src/tests/mod.rs | 1 + src/tests/turtle_doc_test_misc.rs | 106 ++++++++++++++++++++++++++ src/tests/turtle_parser_test_misc.rs | 4 +- src/turtle/turtle_doc.rs | 110 --------------------------- 4 files changed, 109 insertions(+), 112 deletions(-) create mode 100644 src/tests/turtle_doc_test_misc.rs diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 1e9e2d0..278996f 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -91,4 +91,5 @@ fn cmp_input_file( } mod triple_common_parser_test_misc; mod turtle_doc_test; +mod turtle_doc_test_misc; mod turtle_parser_test_misc; diff --git a/src/tests/turtle_doc_test_misc.rs b/src/tests/turtle_doc_test_misc.rs new file mode 100644 index 0000000..10866f1 --- /dev/null +++ b/src/tests/turtle_doc_test_misc.rs @@ -0,0 +1,106 @@ +use crate::{ + shared::XSD_STRING, + turtle::turtle_doc::{ + Literal, Node, RdfJsonNode, RdfJsonNodeResult, RdfJsonTriple, Statement, TurtleDoc, + }, +}; +use serial_test::serial; +use std::borrow::Cow; +use Cow::Borrowed; +use Node::Iri; + +#[test] +#[serial] +fn turtle_doc_add_test() { + let doc1 = r#" + @prefix : . + :a :b ( "apple" "banana" ) . + "#; + let doc2 = r#" + @prefix foaf: . + [ foaf:name "Alice" ] foaf:knows [ + foaf:name "Bob" ; + foaf:lastName "George", "Joshua" ; + foaf:knows [ + foaf:name "Eve" ] ; + foaf:mbox ] . + + "#; + let turtle1: TurtleDoc = (doc1, None).try_into().unwrap(); + assert_eq!(5, turtle1.list_statements(None, None, None).len()); + + let turtle2: TurtleDoc = (doc2, None).try_into().unwrap(); + assert_eq!(8, turtle2.list_statements(None, None, None).len()); + + let turtle3 = turtle1 + turtle2; + assert_eq!(13, turtle3.list_statements(None, None, None).len()); + let mut turtle = TurtleDoc::default(); + turtle.add_statement( + Iri(Borrowed("http://xxx.com/123")), + Iri(Borrowed("http://bar.com/345")), + Node::Literal(Literal::Decimal(123f32)), + ); + let turtle4 = turtle + turtle3; + assert_eq!(14, turtle4.list_statements(None, None, None).len()); +} + +#[test] +#[serial] +fn turtle_doc_list_statements_test() { + let doc = r#" + @prefix foaf: . + [ foaf:name "Alice" ] foaf:knows [ + foaf:name "Bob" ; + foaf:lastName "George", "Joshua" ; + foaf:knows [ + foaf:name "Eve" ] ; + foaf:mbox ] . + + "#; + let turtle: TurtleDoc = (doc, None).try_into().unwrap(); + let statements = turtle.list_statements(None, None, Some(&Iri(Borrowed("bob@example.com")))); + assert_eq!(1, statements.len()); + println!("{statements:?}"); + let statement = statements[0]; + let statements = turtle.list_statements(Some(&statement.subject), None, None); + assert_eq!(5, statements.len()); +} + +#[test] +#[serial] +fn test_convert_rdf_triple_to_doc() { + let triple = RdfJsonTriple { + subject: RdfJsonNodeResult::SingleNode(RdfJsonNode { + typ: "uri".into(), + datatype: None, + lang: None, + value: "http://xx.com/xxx".into(), + }), + predicate: RdfJsonNodeResult::SingleNode(RdfJsonNode { + typ: "uri".into(), + datatype: None, + lang: None, + value: "http://xx.com/pred".into(), + }), + object: RdfJsonNodeResult::SingleNode(RdfJsonNode { + typ: "literal".into(), + datatype: Some(XSD_STRING.into()), + lang: None, + value: "hello".into(), + }), + }; + let stmt: Statement = (&triple).try_into().unwrap(); + + let expected = Statement { + subject: Node::Iri(Cow::Borrowed("http://xx.com/xxx")), + predicate: Node::Iri(Cow::Borrowed("http://xx.com/pred")), + object: Node::Literal(Literal::Quoted { + datatype: Some(Box::new(Node::Iri(Cow::Borrowed( + "http://www.w3.org/2001/XMLSchema#string", + )))), + value: Cow::Borrowed("hello"), + lang: None, + }), + }; + assert_eq!(stmt, expected); +} diff --git a/src/tests/turtle_parser_test_misc.rs b/src/tests/turtle_parser_test_misc.rs index 3c84432..39f0150 100644 --- a/src/tests/turtle_parser_test_misc.rs +++ b/src/tests/turtle_parser_test_misc.rs @@ -723,7 +723,7 @@ fn predicate_lists_testx() { _:helium "2". "#; let (_, res) = triples(s).unwrap(); - dbg!( + assert_eq!( res, TurtleValue::Statement { subject: Box::new(TurtleValue::BNode(BlankNode::Labeled("helium",),)), @@ -746,7 +746,7 @@ fn predicate_lists_testx() { "2". "#; let (_, res) = triples(s).unwrap(); - dbg!( + assert_eq!( res, TurtleValue::Statement { subject: Box::new(TurtleValue::Iri(Iri::Enclosed( diff --git a/src/turtle/turtle_doc.rs b/src/turtle/turtle_doc.rs index f2c552a..048b6b7 100644 --- a/src/turtle/turtle_doc.rs +++ b/src/turtle/turtle_doc.rs @@ -1098,113 +1098,3 @@ impl Display for TurtleDocError { write!(f, "error: {}", self.message) } } -#[cfg(test)] -mod test { - use crate::{ - shared::XSD_STRING, - turtle::turtle_doc::{Literal, Node, RdfJsonTriple, Statement, TurtleDoc}, - }; - use serial_test::serial; - use std::borrow::Cow; - use Cow::Borrowed; - use Node::Iri; - - use super::RdfJsonNodeResult; - - #[test] - #[serial] - fn turtle_doc_add_test() { - let doc1 = r#" - @prefix : . - :a :b ( "apple" "banana" ) . - "#; - let doc2 = r#" - @prefix foaf: . - [ foaf:name "Alice" ] foaf:knows [ - foaf:name "Bob" ; - foaf:lastName "George", "Joshua" ; - foaf:knows [ - foaf:name "Eve" ] ; - foaf:mbox ] . - - "#; - let turtle1: TurtleDoc = (doc1, None).try_into().unwrap(); - assert_eq!(5, turtle1.statements.len()); - - let turtle2: TurtleDoc = (doc2, None).try_into().unwrap(); - assert_eq!(8, turtle2.statements.len()); - - let turtle3 = turtle1 + turtle2; - assert_eq!(13, turtle3.statements.len()); - let mut turtle = TurtleDoc::default(); - turtle.add_statement( - Iri(Borrowed("http://xxx.com/123")), - Iri(Borrowed("http://bar.com/345")), - Node::Literal(Literal::Decimal(123f32)), - ); - let turtle4 = turtle + turtle3; - assert_eq!(14, turtle4.statements.len()); - } - - #[test] - #[serial] - fn turtle_doc_list_statements_test() { - let doc = r#" - @prefix foaf: . - [ foaf:name "Alice" ] foaf:knows [ - foaf:name "Bob" ; - foaf:lastName "George", "Joshua" ; - foaf:knows [ - foaf:name "Eve" ] ; - foaf:mbox ] . - - "#; - let turtle: TurtleDoc = (doc, None).try_into().unwrap(); - let statements = - turtle.list_statements(None, None, Some(&Iri(Borrowed("bob@example.com")))); - assert_eq!(1, statements.len()); - println!("{statements:?}"); - let statement = statements[0]; - let statements = turtle.list_statements(Some(&statement.subject), None, None); - assert_eq!(5, statements.len()); - } - - #[test] - #[serial] - fn test_convert_rdf_triple_to_doc() { - let triple = RdfJsonTriple { - subject: RdfJsonNodeResult::SingleNode(super::RdfJsonNode { - typ: "uri".into(), - datatype: None, - lang: None, - value: "http://xx.com/xxx".into(), - }), - predicate: RdfJsonNodeResult::SingleNode(super::RdfJsonNode { - typ: "uri".into(), - datatype: None, - lang: None, - value: "http://xx.com/pred".into(), - }), - object: RdfJsonNodeResult::SingleNode(super::RdfJsonNode { - typ: "literal".into(), - datatype: Some(XSD_STRING.into()), - lang: None, - value: "hello".into(), - }), - }; - let stmt: Statement = (&triple).try_into().unwrap(); - - let expected = Statement { - subject: Node::Iri(Cow::Borrowed("http://xx.com/xxx")), - predicate: Node::Iri(Cow::Borrowed("http://xx.com/pred")), - object: Node::Literal(Literal::Quoted { - datatype: Some(Box::new(Node::Iri(Cow::Borrowed( - "http://www.w3.org/2001/XMLSchema#string", - )))), - value: Cow::Borrowed("hello"), - lang: None, - }), - }; - assert_eq!(stmt, expected); - } -}