#region Copyright (c) 2004 Ian Davis and James Carlyle /*------------------------------------------------------------------------------ COPYRIGHT AND PERMISSION NOTICE Copyright (c) 2004 Ian Davis and James Carlyle Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ------------------------------------------------------------------------------*/ #endregion namespace SemPlan.Spiral.Tests.Sparql { using NUnit.Framework; using SemPlan.Spiral.Core; using SemPlan.Spiral.Expressions; using SemPlan.Spiral.Sparql; using System; using System.Collections; /// /// Programmer tests for QueryParser class /// /// /// $Id: QueryParserTest.cs,v 1.11 2006/02/10 22:10:12 ian Exp $ /// [TestFixture] public class QueryParserTest { private Query ParseQuery(string sparql) { QueryParser parser = new QueryParser(); return parser.Parse( sparql ); } private Query ExplainParseQuery(string sparql) { QueryParser parser = new QueryParser(); parser.Explain = true; return parser.Parse( sparql ); } [Test] public void simpleSelectOneVariable() { Query query = ParseQuery("SELECT ?var WHERE { ?var }"); IList variables = query.Variables; Assert.AreEqual( 1, variables.Count ); Assert.AreEqual( "var", ((Variable)variables[0]).Name ); } [Test] public void simpleSelectTwoVariables() { Query query = ParseQuery("SELECT ?var1 , ?var2 WHERE { ?var }"); IList variables = query.Variables; Assert.AreEqual( 2, variables.Count ); Assert.AreEqual( "var1", ((Variable)variables[0]).Name ); Assert.AreEqual( "var2", ((Variable)variables[1]).Name ); } [Test] public void simpleSelectOnePattern() { SparqlQuery expected = new SparqlQuery(); expected.AddVariable( new Variable("var") ); expected.PatternGroup.AddPattern( new Pattern( new UriRef("http://example.com/subject"), new UriRef("http://example.com/predicate"), new Variable("var") ) ); AssertParse( expected, "SELECT ?var WHERE { ?var }"); } [Test] public void simpleSelectOnePatternWithTerminatingDot() { SparqlQuery expected = new SparqlQuery(); expected.AddVariable( new Variable("var") ); expected.PatternGroup.AddPattern( new Pattern( new UriRef("http://example.com/subject"), new UriRef("http://example.com/predicate"), new Variable("var") ) ); AssertParse( expected, "SELECT ?var WHERE { ?var .}" ); } [Test] public void simpleSelectTwoPatterns() { Query query = ParseQuery("SELECT ?var WHERE { ?var . ?var .}"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 2, patterns.Count ); Assert.AreEqual( new Pattern( new UriRef("http://example.com/subject"), new UriRef("http://example.com/predicate"), new Variable("var")).ToString() , patterns[0].ToString() ); Assert.AreEqual( new Pattern( new UriRef("http://example.com/subject2"), new UriRef("http://example.com/predicate"), new Variable("var")).ToString() , patterns[1].ToString() ); } [Test] public void simpleSelectThreeVariables() { Query query = ParseQuery("SELECT ?s , ?p , ?v WHERE { ?s ?p ?v }"); IList variables = query.Variables; Assert.AreEqual( 3, variables.Count ); Assert.AreEqual( "s", ((Variable)variables[0]).Name ); Assert.AreEqual( "p", ((Variable)variables[1]).Name ); Assert.AreEqual( "v", ((Variable)variables[2]).Name ); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("s"), new Variable("p"), new Variable("v")).ToString() , patterns[0].ToString() ); } [Test] public void simpleSelectThreePatterns() { Query query = ParseQuery("SELECT ?var WHERE { ?var . ?var . ?var .}"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 3, patterns.Count ); Assert.AreEqual( new Pattern( new UriRef("http://example.com/subject"), new UriRef("http://example.com/predicate"), new Variable("var")).ToString() , patterns[0].ToString() ); Assert.AreEqual( new Pattern( new UriRef("http://example.com/subject2"), new UriRef("http://example.com/predicate"), new Variable("var")).ToString() , patterns[1].ToString() ); Assert.AreEqual( new Pattern( new UriRef("http://example.com/subject2"), new UriRef("http://example.com/predicate"), new Variable("var")).ToString() , patterns[2].ToString() ); } [Test] public void simpleSelectOneVariableWildcard() { Query query = ParseQuery("SELECT * WHERE { ?var }"); Assert.AreEqual( true, query.SelectAll ); } [Test] public void simpleSelectThreeVariablesWildcard() { Query query = ParseQuery("SELECT * WHERE { ?s ?p ?v }"); Assert.AreEqual( true, query.SelectAll ); } [Test] public void simpleSelectDuplicatedVariableWildcard() { Query query = ParseQuery("SELECT * WHERE { ?s ?s ?s }"); Assert.AreEqual( true, query.SelectAll ); } [Test] public void simpleSelectOnePatternPlainLiteralValueDoubleQuoted() { Query query = ParseQuery("SELECT ?var WHERE { ?var \"foo\" }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new PlainLiteral("foo")).ToString() , patterns[0].ToString() ); } [Test] public void simpleSelectOnePatternPlainLiteralValueSingleQuoted() { Query query = ParseQuery("SELECT ?var WHERE { ?var 'foo' }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new PlainLiteral("foo")).ToString() , patterns[0].ToString() ); } [Test] public void simpleSelectOnePatternLanguageLiteralValueDoubleQuoted() { Query query = ParseQuery("SELECT ?var WHERE { ?var \"foo\"@en }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual("en", ((PlainLiteral)((Pattern)patterns[0]).GetObject()).GetLanguage() ) ; } [Test] public void simpleSelectOnePatternLanguageLiteralValueSingleQuoted() { Query query = ParseQuery("SELECT ?var WHERE { ?var \'foo\'@en }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual("en", ((PlainLiteral)((Pattern)patterns[0]).GetObject()).GetLanguage() ) ; } [Test] public void simpleSelectOnePatternTypedLiteralValueDoubleQuoted() { Query query = ParseQuery("SELECT ?var WHERE { ?var \"foo\"^^ }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new TypedLiteral("foo", "http://example.com/datatype")).ToString() , patterns[0].ToString() ); } [Test] public void simpleSelectOnePatternTypedLiteralValueSingleQuoted() { Query query = ParseQuery("SELECT ?var WHERE { ?var 'foo'^^ }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new TypedLiteral("foo", "http://example.com/datatype")).ToString() , patterns[0].ToString() ); } [Test] public void simpleSelectWithPrefix() { Query query = ParseQuery("PREFIX eg: SELECT ?var WHERE { ?var eg:predicate 'foo'^^ }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new TypedLiteral("foo", "http://example.com/datatype")).ToString() , patterns[0].ToString() ); } [Test] public void simpleSelectWithMultiplePrefixes() { Query query = ParseQuery("PREFIX eg: PREFIX ex: SELECT ?var WHERE { ?var eg:predicate ex:subject }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new UriRef("http://example.org/subject")).ToString() , patterns[0].ToString() ); } [Test] public void whereIsOptional() { Query query = ParseQuery("PREFIX eg: SELECT ?var { ?var eg:predicate 'foo'^^ }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new TypedLiteral("foo", "http://example.com/datatype")).ToString() , patterns[0].ToString() ); } [Test] public void numericInteger() { Query query = ParseQuery("SELECT ?var WHERE { ?var 1 }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new TypedLiteral("1", "http://www.w3.org/2001/XMLSchema#integer")).ToString() , patterns[0].ToString() ); } [Test] public void numericDecimal() { Query query = ParseQuery("SELECT ?var WHERE { ?var 1.3 }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new TypedLiteral("1.3", "http://www.w3.org/2001/XMLSchema#decimal")).ToString() , patterns[0].ToString() ); } [Test] public void numericDouble() { Query query = ParseQuery("SELECT ?var WHERE { ?var 1.3e5 }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new TypedLiteral("1.3e5", "http://www.w3.org/2001/XMLSchema#double")).ToString() , patterns[0].ToString() ); } [Test] public void booleanTrue() { Query query = ParseQuery("SELECT ?var WHERE { ?var true }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new TypedLiteral("true", "http://www.w3.org/2001/XMLSchema#boolean")).ToString() , patterns[0].ToString() ); } [Test] public void booleanFalse() { Query query = ParseQuery("SELECT ?var WHERE { ?var false }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new TypedLiteral("false", "http://www.w3.org/2001/XMLSchema#boolean")).ToString() , patterns[0].ToString() ); } [Test] public void simpleSelectTwoPatternsWithValueDelimiter() { Query query = ParseQuery("SELECT ?var WHERE { ?var 'x' ; 'y' }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 2, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new PlainLiteral("x")).ToString() , patterns[0].ToString() ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new PlainLiteral("y")).ToString() , patterns[1].ToString() ); } [Test] public void simpleSelectThreePatternsWithValueDelimiter() { Query query = ParseQuery("SELECT ?var WHERE { ?var 'x' ; 'y' ; }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 3, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new PlainLiteral("x")).ToString() , patterns[0].ToString() ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new PlainLiteral("y")).ToString() , patterns[1].ToString() ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new UriRef("http://example.com/obj") ).ToString() , patterns[2].ToString() ); } [Test] public void simpleSelectTwoPatternsWithPredicateDelimiter() { Query query = ParseQuery("SELECT ?var WHERE { ?var 'x' , 'y' }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 2, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new PlainLiteral("x")).ToString() , patterns[0].ToString() ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate2"), new PlainLiteral("y")).ToString() , patterns[1].ToString() ); } [Test] public void simpleSelectPatternsWithPredicateAndValueDelimiters() { Query query = ParseQuery("SELECT ?var WHERE { ?var 'x' ; 'y' , 'z' }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 3, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new PlainLiteral("x")).ToString() , patterns[0].ToString() ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate"), new PlainLiteral("y")).ToString() , patterns[1].ToString() ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://example.com/predicate2"), new PlainLiteral("z")).ToString() , patterns[2].ToString() ); } [Test] public void simpleSelectAShorthand() { Query query = ParseQuery("SELECT ?var WHERE { ?var a . }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.AreEqual( new Pattern( new Variable("var"), new UriRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), new UriRef("http://example.com/obj")).ToString() , patterns[0].ToString() ); } [Test] public void simpleSelectBlankNode() { Query query = ParseQuery("SELECT ?var WHERE { ?var _:a . }"); IList patterns = query.PatternGroup.Patterns; Assert.AreEqual( 1, patterns.Count ); Assert.IsTrue( ((Pattern)patterns[0]).GetObject() is BlankNode ); } [Test] [ExpectedException(typeof(SemPlan.Spiral.Sparql.SparqlException))] public void PrefixRequiresTextAfterToBeUri() { Query query = ParseQuery("PREFIX eg: foo:bar SELECT ?var WHERE { ?var eg:predicate 'foo'^^ }"); } [Test] [ExpectedException(typeof(SemPlan.Spiral.Sparql.SparqlException))] public void PrefixRequiresUri() { Query query = ParseQuery("PREFIX eg:"); } [Test] [ExpectedException(typeof(SemPlan.Spiral.Sparql.SparqlException))] public void PrefixKeywordExpectsPrefixes() { Query query = ParseQuery("PREFIX foo SELECT * WHERE { ?x ?y ?z}"); } [Test] public void DollarSignsForVariables() { Query query = ParseQuery("SELECT $var WHERE { $var }"); IList variables = query.Variables; Assert.AreEqual( 1, variables.Count ); Assert.AreEqual( "var", ((Variable)variables[0]).Name ); } //~ [Test] //~ [ExpectedException(typeof(SemPlan.Spiral.Sparql.SparqlException))] //~ public void MissingVariablesInSelect() { //~ Query query = ParseQuery("SELECT WHERE { ?var _:a . }"; //~ QueryParser parser = new QueryParser(); //~ Query query = parser.parse( sparql ); //~ } private void AssertIsParseable(string queryText) { try { Query query = ParseQuery(queryText); Assert.IsNotNull( query ); } catch(Exception e) { Assert.Fail( queryText + " was not parseable because " + e.ToString() ); } } private void AssertIsNotParseable(string queryText) { try { Query query = ParseQuery(queryText); Assert.Fail( "Invalid query should fail parse: " + query ); } catch(Exception) { } } private void AssertParse(Query expectedQuery, string queryText) { AssertParse( expectedQuery, queryText, false); } private void AssertParse(Query expectedQuery, string queryText, bool explain) { QueryParser parser = new QueryParser(); parser.Explain = explain; Query query = null; try { query = parser.Parse( queryText ); } catch(Exception e) { Assert.Fail( queryText + " was not parseable because " + e.ToString() ); } Assert.IsNotNull( query , "Parsed query should not be null" ); Assert.AreEqual( expectedQuery, query, "Parsed query should equal expected query"); } /// syntax-basic-01.rq [Test] public void syntax_basic_01_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; AssertParse(expected, @"SELECT * WHERE { }"); } /// syntax-basic-02.rq [Test] public void syntax_basic_02_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; AssertParse(expected, @"SELECT * {}"); } /// syntax-basic-03.rq [Test] public void syntax_basic_03_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern( new Pattern( new Variable("x"), new Variable("y"), new Variable("z") ) ); AssertParse(expected, @"# No trailing dot PREFIX : SELECT * WHERE { ?x ?y ?z }"); } /// syntax-basic-04.rq [Test] public void syntax_basic_04_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern( new Pattern( new Variable("x"), new Variable("y"), new Variable("z") ) ); AssertParse(expected, @"# With trailing dot SELECT * WHERE { ?x ?y ?z . }"); } /// syntax-basic-05.rq [Test] public void syntax_basic_05_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern( new Pattern( new Variable("x"), new Variable("y"), new Variable("z") ) ); expected.PatternGroup.AddPattern( new Pattern( new Variable("a"), new Variable("b"), new Variable("c") ) ); AssertParse(expected, @"# Two triples : no trailing dot SELECT * WHERE { ?x ?y ?z . ?a ?b ?c }"); } /// syntax-basic-06.rq [Test] public void syntax_basic_06_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern( new Pattern( new Variable("x"), new Variable("y"), new Variable("z") ) ); expected.PatternGroup.AddPattern( new Pattern( new Variable("a"), new Variable("b"), new Variable("c") ) ); AssertParse(expected, @"# Two triples : with trailing dot SELECT * WHERE { ?x ?y ?z . ?a ?b ?c . }"); } /// syntax-bnodes-01.rq [Test] public void syntax_bnodes_01_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern( new Pattern( new BlankNode(), new UriRef("http://example.org/ns#p"), new UriRef("http://example.org/ns#q") ) ); AssertParse(expected, @"PREFIX : SELECT * WHERE { [:p :q ] }"); } /// syntax-bnodes-02.rq [Test] public void syntax_bnodes_02_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern( new Pattern( new BlankNode(), new UriRef("http://example.org/ns#p"), new UriRef("http://example.org/ns#q") ) ); AssertParse(expected, @"PREFIX : SELECT * WHERE { [] :p :q }"); } /// syntax-bnodes-03.rq [Test] public void syntax_bnodes_03_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern( new Pattern( new UriRef("http://example.org/ns#x"), new BlankNode(), new UriRef("http://example.org/ns#q") ) ); AssertParse(expected, @"PREFIX : SELECT * WHERE { :x [] :q }"); } /// syntax-bnodes-04.rq [Test] public void syntax_bnodes_04_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern( new Pattern( new UriRef("http://example.org/ns#x"), new BlankNode(), new UriRef("http://example.org/ns#q") ) ); AssertParse(expected, @"PREFIX : SELECT * WHERE { :x _:a :q }"); } /// syntax-bnodes-05.rq [Test] public void syntax_bnodes_05_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; BlankNode blank1 = new BlankNode(); BlankNode blank2 = new BlankNode(); expected.PatternGroup.AddPattern( new Pattern(blank1, new Variable("x"), new Variable("y") ) ); expected.PatternGroup.AddPattern( new Pattern(blank1, new UriRef("http://example.org/ns#p"), blank2) ); expected.PatternGroup.AddPattern( new Pattern(blank2, new Variable("pa"), new Variable("b") ) ); AssertParse(expected, @"PREFIX : SELECT * WHERE { [ ?x ?y ] :p [ ?pa ?b ] }"); } /// syntax-bnodes-06.rq [Test] public void syntax_bnodes_06_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new BlankNode(), new UriRef("http://example.org/ns#p"), new UriRef("http://example.org/ns#q") ) ); AssertParse(expected, @"PREFIX : SELECT * WHERE { [ :p :q ; ] }"); } /// syntax-bnodes-07.rq [Test] public void syntax_bnodes_07_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; BlankNode blank1 = new BlankNode(); expected.PatternGroup.AddPattern(new Pattern( blank1, new UriRef("http://example.org/ns#p1"), new UriRef("http://example.org/ns#q1") ) ); expected.PatternGroup.AddPattern(new Pattern( blank1, new UriRef("http://example.org/ns#p2"), new UriRef("http://example.org/ns#q2") ) ); AssertParse(expected, @"PREFIX : SELECT * WHERE { _:a :p1 :q1 . _:a :p2 :q2 . }"); } /// syntax-bnodes-08.rq [Test] public void syntax_bnodes_08_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; BlankNode blank1 = new BlankNode(); BlankNode blank2 = new BlankNode(); expected.PatternGroup.AddPattern( new Pattern(blank1, new Variable("x"), new Variable("y") ) ); expected.PatternGroup.AddPattern( new Pattern(blank1, new UriRef("http://example.org/ns#p"), blank2) ); expected.PatternGroup.AddPattern( new Pattern(blank2, new Variable("pa"), new Variable("b") ) ); AssertParse(expected, @"PREFIX : SELECT * WHERE { [ ?x ?y ] :p [ ?pa ?b ] }"); } /// syntax-bnodes-09.rq [Test] public void syntax_bnodes_09_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new BlankNode(), new UriRef("http://example.org/ns#p"), new UriRef("http://example.org/ns#q") ) ); AssertParse(expected, @"PREFIX : SELECT * WHERE { [ :p :q ; ] }"); } /// syntax-expr-01.rq [Test] public void syntax_expr_01_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("s"), new Variable("p"), new Variable("o") ) ); expected.AddConstraint( new Constraint( new VariableExpression( new Variable("o") ) ) ); AssertParse(expected, @"SELECT * WHERE { ?s ?p ?o . FILTER (?o) }"); } /// syntax-expr-02.rq [Test] public void syntax_expr_02_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("s"), new Variable("p"), new Variable("o") ) ); expected.AddConstraint( new Constraint( new VariableExpression( new Variable("o") ) ) ); AssertParse(expected, @"SELECT * WHERE { ?s ?p ?o . FILTER (?o) }"); } /// syntax-expr-03.rq [Test] public void syntax_expr_03_rq() { AssertIsParseable(@"SELECT * WHERE { ?s ?p ?o . FILTER REGEX(?o, ""foo"") }"); } /// syntax-expr-04.rq [Test] public void syntax_expr_04_rq() { AssertIsParseable(@"SELECT * WHERE { ?s ?p ?o . FILTER REGEX(?o, ""foo"", ""i"") }"); } /// syntax-expr-05.rq [Test] public void syntax_expr_05_rq() { AssertIsParseable(@"PREFIX xsd: SELECT * WHERE { ?s ?p ?o . FILTER (xsd:integer(?o)) }"); } /// syntax-expr-06.rq [Test] public void syntax_expr_06_rq() { AssertIsParseable(@"PREFIX : PREFIX xsd: SELECT * WHERE { ?s ?p ?o . FILTER (:myFunc(?s,?o)) }"); } /// syntax-forms-01.rq [Test] public void syntax_forms_01_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { ( [ ?x ?y ] ) :p ( [ ?pa ?b ] 57 ) }"); } /// syntax-forms-02.rq [Test] public void syntax_forms_02_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { ( [] [] ) }"); } /// syntax-keywords-01.rq [Test] public void syntax_keywords_01_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("x"), new UriRef("http://example.org/ns#foo"), new Variable("z") ) ); expected.AddConstraint( new Constraint( new VariableExpression( new Variable("z") ) ) ); AssertParse(expected, @"# use keyword FILTER as a namespace prefix PREFIX FILTER: SELECT * WHERE { ?x FILTER:foo ?z FILTER (?z) }"); } /// syntax-keywords-02.rq [Test] public void syntax_keywords_02_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("x"), new UriRef("http://example.org/ns#FILTER"), new Variable("z") ) ); expected.AddConstraint( new Constraint( new VariableExpression( new Variable("z") ) ) ); AssertParse(expected, @"# use keyword FILTER as a local name PREFIX : SELECT * WHERE { ?x :FILTER ?z FILTER (?z) }"); } /// syntax-keywords-03.rq [Test] public void syntax_keywords_03_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("x"), new UriRef("http://example.org/ns#foo"), new Variable("z") ) ); AssertParse(expected, @"# use keyword UNION as a namespace prefix PREFIX UNION: SELECT * WHERE { ?x UNION:foo ?z }"); } /// syntax-limit-offset-07.rq [Test] public void syntax_limit_offset_07_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("s"), new Variable("p"), new Variable("o") ) ); expected.OrderDirection = Query.SortOrder.Ascending; expected.OrderBy = new VariableExpression( new Variable("o") ); expected.ResultLimit = 5; AssertParse(expected, @"PREFIX : SELECT * { ?s ?p ?o } ORDER BY ?o LIMIT 5"); } /// syntax-limit-offset-08.rq [Test] public void syntax_limit_offset_08_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("s"), new Variable("p"), new Variable("o") ) ); expected.OrderDirection = Query.SortOrder.Ascending; expected.OrderBy = new VariableExpression( new Variable("o") ); expected.ResultLimit = 5; expected.ResultOffset = 3; AssertParse(expected, @"PREFIX : SELECT * { ?s ?p ?o } ORDER BY ?o LIMIT 5 OFFSET 3"); } /// syntax-limit-offset-09.rq [Test] public void syntax_limit_offset_09_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("s"), new Variable("p"), new Variable("o") ) ); expected.OrderDirection = Query.SortOrder.Ascending; expected.OrderBy = new VariableExpression( new Variable("o") ); expected.ResultOffset = 3; AssertParse(expected, @"PREFIX : SELECT * { ?s ?p ?o } ORDER BY ?o OFFSET 3"); } /// syntax-lists-01.rq [Test] public void syntax_lists_01_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { ( ?x ) :p ?z }"); } /// syntax-lists-02.rq [Test] public void syntax_lists_02_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { ?x :p ( ?z ) }"); } /// syntax-lists-03.rq [Test] public void syntax_lists_03_rq() { AssertIsParseable(@"SELECT * WHERE { ( ?z ) }"); } /// syntax-lists-04.rq [Test] public void syntax_lists_04_rq() { AssertIsParseable(@"SELECT * WHERE { ( ( ?z ) ) }"); } /// syntax-lists-05.rq [Test] public void syntax_lists_05_rq() { AssertIsParseable(@"SELECT * WHERE { ( ( ) ) }"); } /// syntax-lit-01.rq [Test] public void syntax_lit_01_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral("x") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p ""x"" }"); } /// syntax-lit-02.rq [Test] public void syntax_lit_02_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral("x") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p 'x' }"); } /// syntax-lit-03.rq [Test] public void syntax_lit_03_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral("x\"y'z") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p ""x\""y'z"" }"); } /// syntax-lit-04.rq [Test] public void syntax_lit_04_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral("x\"y'z") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p 'x""y\'z' }"); } /// syntax-lit-05.rq [Test] public void syntax_lit_05_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral("x\"") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p ""x\"""" }"); } /// syntax-lit-06.rq [Test] public void syntax_lit_06_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral("x'") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p 'x\'' }"); } /// syntax-lit-07.rq [Test] public void syntax_lit_07_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new TypedLiteral("123", "http://www.w3.org/2001/XMLSchema#integer") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p 123 }"); } /// syntax-lit-08.rq [Test] public void syntax_lit_08_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new TypedLiteral("123.", "http://www.w3.org/2001/XMLSchema#decimal") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p 123. . }"); } /// syntax-lit-09.rq [Test] public void syntax_lit_09_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral(@"Long """" Literal ") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p """"""Long """" Literal """""" }"); } /// syntax-lit-10.rq [Test] public void syntax_lit_10_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral(@"Long '' """""" Literal") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p '''Long '' """""" Literal''' }"); } /// syntax-lit-11.rq [Test] public void syntax_lit_11_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral(@"Long""""""Literal") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p """"""Long""""\""Literal"""""" }"); } /// syntax-lit-12.rq [Test] public void syntax_lit_12_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral(@"Long'''Literal") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p '''Long''\'Literal''' }"); } /// syntax-lit-13.rq [Test] public void syntax_lit_13_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral(@"Long""""""Literal") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p """"""Long\""""""Literal"""""" }"); } /// syntax-lit-14.rq [Test] public void syntax_lit_14_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral(@"Long'''Literal") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p '''Long\'''Literal''' }"); } /// syntax-lit-15.rq [Test] public void syntax_lit_15_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral(@"Long '' Literal") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p '''Long '' Literal''' }"); } /// syntax-lit-16.rq [Test] public void syntax_lit_16_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral(@"Long ' Literal") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p '''Long ' Literal''' }"); } /// syntax-lit-17.rq [Test] public void syntax_lit_17_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral(@"Long''\Literal with '\ single quotes ") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p '''Long''\\Literal with '\\ single quotes ''' }"); } /// syntax-lit-18.rq [Test] public void syntax_lit_18_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral(@"Long """" Literal") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p """"""Long """" Literal"""""" }"); } /// syntax-lit-19.rq [Test] public void syntax_lit_19_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral(@"Long "" Literal") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p """"""Long "" Literal"""""" }"); } /// syntax-lit-20.rq [Test] public void syntax_lit_20_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.Base = "http://example.org/"; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/#x"), new UriRef("http://example.org/#p"), new PlainLiteral(@"Long""""\Literal with ""\ single quotes") ) ); AssertParse(expected, @"BASE PREFIX : <#> SELECT * WHERE { :x :p """"""Long""""\\Literal with ""\\ single quotes"""""" }"); } /// syntax-order-01.rq [Test] public void syntax_order_01_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("s"), new Variable("p"), new Variable("o") ) ); expected.OrderDirection = Query.SortOrder.Ascending; expected.OrderBy = new VariableExpression( new Variable("o") ); AssertParse(expected,@"PREFIX : SELECT * { ?s ?p ?o } ORDER BY ?o"); } /// syntax-order-02.rq [Test] public void syntax_order_02_rq() { AssertIsParseable(@"PREFIX : SELECT * { ?s ?p ?o } ORDER BY (?o+5)"); } /// syntax-order-03.rq [Test] public void syntax_order_03_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("s"), new Variable("p"), new Variable("o") ) ); expected.OrderDirection = Query.SortOrder.Ascending; expected.OrderBy = new VariableExpression( new Variable("o") ); AssertParse(expected,@"PREFIX : SELECT * { ?s ?p ?o } ORDER BY ASC(?o)"); } /// syntax-order-04.rq [Test] public void syntax_order_04_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("s"), new Variable("p"), new Variable("o") ) ); expected.OrderDirection = Query.SortOrder.Descending; expected.OrderBy = new VariableExpression( new Variable("o") ); AssertParse(expected,@"PREFIX : SELECT * { ?s ?p ?o } ORDER BY DESC(?o)"); } /// syntax-order-05.rq [Test] public void syntax_order_05_rq() { AssertIsParseable(@"PREFIX : SELECT * { ?s ?p ?o } ORDER BY DESC(:func(?s, ?o))"); } /// syntax-order-06.rq [Test] public void syntax_order_06_rq() { AssertIsParseable(@"PREFIX : SELECT * { ?s ?p ?o } ORDER BY DESC(?o+57) :func2(?o) ASC(?s)"); } /// syntax-pat-01.rq [Test] public void syntax_pat_01_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; AssertParse(expected, @"PREFIX : SELECT * { }"); } /// syntax-pat-02.rq [Test] public void syntax_pat_02_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("a"), new UriRef("http://example.org/ns#b"), new UriRef("http://example.org/ns#c") ) ); expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#x"), new Variable("y"), new Variable("z") ) ); expected.PatternGroup.OptionalGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#x"), new UriRef("http://example.org/ns#y"), new UriRef("http://example.org/ns#z") ) ); AssertParse(expected, @"# No DOT after optional PREFIX : SELECT * { ?a :b :c OPTIONAL{:x :y :z} :x ?y ?z }"); } /// syntax-pat-03.rq [Test] public void syntax_pat_03_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; PatternGroup group1 = new PatternGroup(); PatternGroup group2 = new PatternGroup(); PatternGroup group3 = new PatternGroup(); group1.AddPattern(new Pattern( new Variable("a"), new UriRef("http://example.org/ns#b"), new UriRef("http://example.org/ns#c") ) ); group1.AddPattern(new Pattern( new UriRef("http://example.org/ns#x1"), new UriRef("http://example.org/ns#y1"), new UriRef("http://example.org/ns#z1") ) ); group2.AddPattern(new Pattern( new UriRef("http://example.org/ns#x"), new UriRef("http://example.org/ns#y"), new UriRef("http://example.org/ns#z") ) ); group3.AddPattern(new Pattern( new UriRef("http://example.org/ns#x2"), new UriRef("http://example.org/ns#y2"), new UriRef("http://example.org/ns#z2") ) ); group1.OptionalGroup = group2; group1.AddAlternateGroup( group3 ); expected.PatternGroup= group1; AssertParse(expected, @"# No DOT between non-triples patterns PREFIX : SELECT * { ?a :b :c OPTIONAL{:x :y :z} { :x1 :y1 :z1 } UNION { :x2 :y2 :z2 } }"); } /// syntax-pat-04.rq [Test] public void syntax_pat_04_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; PatternGroup group1 = new PatternGroup(); PatternGroup group2 = new PatternGroup(); PatternGroup group3 = new PatternGroup(); group1.AddPattern(new Pattern( new Variable("a"), new UriRef("http://example.org/ns#b"), new UriRef("http://example.org/ns#c") ) ); group1.AddPattern(new Pattern( new UriRef("http://example.org/ns#x1"), new UriRef("http://example.org/ns#y1"), new UriRef("http://example.org/ns#z1") ) ); group2.AddPattern(new Pattern( new UriRef("http://example.org/ns#x"), new UriRef("http://example.org/ns#y"), new UriRef("http://example.org/ns#z") ) ); group3.AddPattern(new Pattern( new UriRef("http://example.org/ns#x2"), new UriRef("http://example.org/ns#y2"), new UriRef("http://example.org/ns#z2") ) ); group1.OptionalGroup = group2; group1.AddAlternateGroup( group3 ); expected.PatternGroup= group1; AssertParse(expected, @"# No DOT between non-triples patterns PREFIX : SELECT * { OPTIONAL{:x :y :z} ?a :b :c { :x1 :y1 :z1 } UNION { :x2 :y2 :z2 } }"); } /// syntax-qname-01.rq [Test] public void syntax_qname_01_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("x"), new UriRef("http://example.org/ns#p"), new Variable("z") ) ); AssertParse(expected,@"PREFIX : SELECT * { ?x :p ?z }"); } /// syntax-qname-02.rq [Test] public void syntax_qname_02_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#x"), new UriRef("http://example.org/ns#p"), new UriRef("http://example.org/ns#z") ) ); AssertParse(expected,@"PREFIX : SELECT * WHERE { :x :p :z . }"); } /// syntax-qname-03.rq [Test] public void syntax_qname_03_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new BlankNode(), new UriRef("http://example.org/ns#p.rdf"), new UriRef("http://example.org/ns#z.z") ) ); AssertParse(expected,@"PREFIX : SELECT * WHERE { :_1 :p.rdf :z.z . }"); } /// syntax-qname-04.rq [Test] public void syntax_qname_04_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#"), new UriRef("http://example.org/ns2#"), new UriRef("http://example.org/ns#a") ) ); expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#"), new UriRef("http://example.org/ns#"), new UriRef("http://example.org/ns#") ) ); AssertParse(expected,@"PREFIX : PREFIX a: SELECT * WHERE { : a: :a . : : : . }"); } /// syntax-qname-05.rq [Test] public void syntax_qname_05_rq() { AssertIsParseable(@"PREFIX : <> SELECT * WHERE { : : : . }"); } /// syntax-qname-06.rq [Test] public void syntax_qname_06_rq() { AssertIsParseable(@"PREFIX : <#> SELECT * WHERE { : : : . }"); } /// syntax-qname-07.rq [Test] public void syntax_qname_07_rq() { AssertIsParseable(@"BASE PREFIX : <#> SELECT * WHERE { : : : . }"); } /// syntax-qname-08.rq [Test] public void syntax_qname_08_rq() { AssertIsParseable(@"BASE PREFIX : <#> PREFIX x.y: SELECT * WHERE { :a.b x.y: : . }"); } /// syntax-qname-09.rq [Test] public void syntax_qname_09_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { :_1 :p.rdf :z.z . }"); } /// syntax-qname-10.rq [Test] public void syntax_qname_10_rq() { AssertIsParseable(@"PREFIX : PREFIX a: SELECT * WHERE { : a: :a . : : : . }"); } /// syntax-qname-11.rq [Test] public void syntax_qname_11_rq() { AssertIsParseable(@"PREFIX : <> SELECT * WHERE { : : : . }"); } /// syntax-qname-12.rq [Test] public void syntax_qname_12_rq() { AssertIsParseable(@"PREFIX : <#> SELECT * WHERE { : : : . }"); } /// syntax-qname-13.rq [Test] public void syntax_qname_13_rq() { AssertIsParseable(@"BASE PREFIX : <#> SELECT * WHERE { : : : . }"); } /// syntax-qname-14.rq [Test] public void syntax_qname_14_rq() { AssertIsParseable(@"BASE PREFIX : <#> PREFIX x.y: SELECT * WHERE { :a.b x.y: : . }"); } /// syntax-struct-01.rq [Test] public void syntax_struct_01_rq() { AssertIsParseable(@"# Operator PREFIX : SELECT * { OPTIONAL { } }"); } /// syntax-struct-02.rq [Test] public void syntax_struct_02_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.OptionalGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#a"), new UriRef("http://example.org/ns#b"), new UriRef("http://example.org/ns#c") ) ); AssertParse(expected, @"# Operator PREFIX : SELECT * { OPTIONAL { :a :b :c } }"); } /// syntax-struct-03.rq [Test] public void syntax_struct_03_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#p"), new UriRef("http://example.org/ns#q"), new UriRef("http://example.org/ns#r") ) ); expected.PatternGroup.OptionalGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#a"), new UriRef("http://example.org/ns#b"), new UriRef("http://example.org/ns#c") ) ); AssertParse(expected, @"# Triple, no DOT, operator PREFIX : SELECT * { :p :q :r OPTIONAL { :a :b :c } }"); } /// syntax-struct-04.rq [Test] public void syntax_struct_04_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#p"), new UriRef("http://example.org/ns#q"), new UriRef("http://example.org/ns#r") ) ); expected.PatternGroup.OptionalGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#a"), new UriRef("http://example.org/ns#b"), new UriRef("http://example.org/ns#c") ) ); AssertParse(expected, @"# Triple, DOT, operator PREFIX : SELECT * { :p :q :r . OPTIONAL { :a :b :c } }"); } /// syntax-struct-05.rq [Test] public void syntax_struct_05_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#p"), new UriRef("http://example.org/ns#q"), new UriRef("http://example.org/ns#r") ) ); expected.PatternGroup.OptionalGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#a"), new UriRef("http://example.org/ns#b"), new UriRef("http://example.org/ns#c") ) ); AssertParse(expected, @"# Triple, DOT, operator PREFIX : SELECT * { :p :q :r . OPTIONAL { :a :b :c } }"); } /// syntax-struct-06.rq [Test] public void syntax_struct_06_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#p"), new UriRef("http://example.org/ns#q"), new UriRef("http://example.org/ns#r") ) ); expected.PatternGroup.OptionalGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#a"), new UriRef("http://example.org/ns#b"), new UriRef("http://example.org/ns#c") ) ); AssertParse(expected,@"# Triple, DOT, operator, DOT PREFIX : SELECT * { :p :q :r . OPTIONAL { :a :b :c } . }"); } /// syntax-struct-07.rq [Test] public void syntax_struct_07_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.OptionalGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#a"), new UriRef("http://example.org/ns#b"), new UriRef("http://example.org/ns#c") ) ); AssertParse(expected, @"# Operator, no DOT PREFIX : SELECT * { OPTIONAL { :a :b :c } }"); } /// syntax-struct-08.rq [Test] public void syntax_struct_08_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.OptionalGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#a"), new UriRef("http://example.org/ns#b"), new UriRef("http://example.org/ns#c") ) ); AssertParse(expected, @"# Operator, DOT PREFIX : SELECT * { OPTIONAL { :a :b :c } . }"); } /// syntax-struct-09.rq [Test] public void syntax_struct_09_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("x"), new Variable("y"), new Variable("z") ) ); expected.PatternGroup.OptionalGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#a"), new UriRef("http://example.org/ns#b"), new UriRef("http://example.org/ns#c") ) ); AssertParse(expected, @"# Operator, triple PREFIX : SELECT * { OPTIONAL { :a :b :c } ?x ?y ?z }"); } /// syntax-struct-10.rq [Test] public void syntax_struct_10_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new Variable("x"), new Variable("y"), new Variable("z") ) ); expected.PatternGroup.OptionalGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#a"), new UriRef("http://example.org/ns#b"), new UriRef("http://example.org/ns#c") ) ); AssertParse(expected, @"# Operator, DOT triple PREFIX : SELECT * { OPTIONAL { :a :b :c } . ?x ?y ?z }"); } /// syntax-struct-11.rq [Test] public void syntax_struct_11_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#p"), new UriRef("http://example.org/ns#q"), new UriRef("http://example.org/ns#r") ) ); expected.PatternGroup.OptionalGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#a"), new UriRef("http://example.org/ns#b"), new UriRef("http://example.org/ns#c") ) ); AssertParse(expected, @"# Triple, semi, operator PREFIX : SELECT * { :p :q :r ; OPTIONAL { :a :b :c } }"); } /// syntax-struct-12.rq [Test] public void syntax_struct_12_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#p"), new UriRef("http://example.org/ns#q"), new UriRef("http://example.org/ns#r") ) ); expected.PatternGroup.OptionalGroup.AddPattern(new Pattern( new UriRef("http://example.org/ns#a"), new UriRef("http://example.org/ns#b"), new UriRef("http://example.org/ns#c") ) ); AssertParse(expected, @"# Triple, semi, DOT, operator PREFIX : SELECT * { :p :q :r ; . OPTIONAL { :a :b :c } }"); } /// syntax-union-01.rq [Test] public void syntax_union_01_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; PatternGroup group1 = new PatternGroup(); group1.AddPattern(new Pattern( new Variable("s"), new Variable("p"), new Variable("o") ) ); PatternGroup group2 = new PatternGroup(); group2.AddPattern(new Pattern( new Variable("a"), new Variable("b"), new Variable("c") ) ); group1.AddAlternateGroup( group2 ); expected.PatternGroup = group1; AssertParse(expected, @"PREFIX : SELECT * { { ?s ?p ?o } UNION { ?a ?b ?c } }"); } /// syntax-union-02.rq [Test] public void syntax_union_02_rq() { SparqlQuery expected = new SparqlQuery(); expected.SelectAll = true; PatternGroup group1 = new PatternGroup(); group1.AddPattern(new Pattern( new Variable("s"), new Variable("p"), new Variable("o") ) ); PatternGroup group2 = new PatternGroup(); group2.AddPattern(new Pattern( new Variable("a"), new Variable("b"), new Variable("c") ) ); PatternGroup group3 = new PatternGroup(); group3.AddPattern(new Pattern( new Variable("r"), new Variable("s"), new Variable("t") ) ); group1.AddAlternateGroup( group2 ); group1.AddAlternateGroup( group3 ); expected.PatternGroup = group1; AssertParse(expected, @"PREFIX : SELECT * { { ?s ?p ?o } UNION { ?a ?b ?c } UNION { ?r ?s ?t } }"); } [Test] public void syntax_general_01_rq() { AssertIsParseable(@"SELECT * WHERE { }"); } [Test] public void syntax_general_02_rq() { AssertIsParseable(@"SELECT * WHERE { _:x }"); } [Test] public void syntax_general_03_rq() { AssertIsParseable(@"SELECT * WHERE { _:x FILTER(_:x < 3) }"); } [Test] public void syntax_general_04_rq() { AssertIsParseable(@"SELECT * WHERE { 1 }"); } [Test] public void syntax_general_05_rq() { AssertIsParseable(@"SELECT * WHERE { +11 }"); } [Test] public void syntax_general_06_rq() { AssertIsParseable(@"SELECT * WHERE { -1 }"); } [Test] public void syntax_general_07_rq() { AssertIsParseable(@"SELECT * WHERE { 1.0 }"); } [Test] public void syntax_general_08_rq() { AssertIsParseable(@"SELECT * WHERE { +1.0 }"); } [Test] public void syntax_general_09_rq() { AssertIsParseable(@"SELECT * WHERE { -1.0 }"); } [Test] public void syntax_general_10_rq() { AssertIsParseable(@"SELECT * WHERE { 1.0e0 }"); } [Test] public void syntax_general_11_rq() { AssertIsParseable(@"SELECT * WHERE { +1.0e+1 }"); } [Test] public void syntax_general_12_rq() { AssertIsParseable(@"SELECT * WHERE { -1.0e-1 }"); } [Test] public void syntax_lists_01_2_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { () :p 1 }"); } [Test] public void syntax_lists_02_2_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { ( ) :p 1 }"); } [Test] public void syntax_lists_03_2_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { ( ) :p 1 }"); } [Test] public void syntax_lists_04_2_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { ( 1 2 ) :p 1 }"); } [Test] public void syntax_lists_05_2_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { ( 1 2 ) }"); } [Test] public void syntax_bnode_01_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { [] :p [] }"); } [Test] public void syntax_bnode_02_rq() { AssertIsParseable(@"PREFIX : # Tab SELECT * WHERE { [ ] :p [ ] }"); } [Test] public void syntax_bnode_03_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { [ :p :q ] }"); } [Test] public void syntax_function_01_rq() { AssertIsParseable(@"PREFIX q: SELECT * WHERE { FILTER (q:name()) }"); } [Test] public void syntax_function_02_rq() { AssertIsParseable(@"PREFIX q: SELECT * WHERE { FILTER (q:name( )) }"); } [Test] public void syntax_function_03_rq() { AssertIsParseable(@"PREFIX q: SELECT * WHERE { FILTER (q:name( )) }"); } [Test] public void syntax_function_04_rq() { AssertIsParseable(@"PREFIX q: SELECT * WHERE { FILTER (q:name(1 )) . FILTER (q:name(1,2)) . FILTER (q:name(1 ,2))}"); } [Test] public void syntax_form_select_01_rq() { AssertIsParseable(@"SELECT * WHERE { }"); } [Test] public void syntax_form_select_02_rq() { AssertIsParseable(@"SELECT * { }"); } [Test] public void syntax_form_ask_02_rq() { AssertIsParseable(@"ASK {}"); } [Test] public void syntax_form_construct_01_rq() { AssertIsParseable(@"CONSTRUCT { ?s . ?s ?o } WHERE {?s ?p ?o}"); } [Test] public void syntax_form_construct_02_rq() { AssertIsParseable(@"CONSTRUCT { ?s . ?s ?o .} WHERE {?s ?p ?o}"); } [Test] public void syntax_form_construct_03_rq() { AssertIsParseable(@"PREFIX rdf: CONSTRUCT { [] rdf:subject ?s ; rdf:predicate ?p ; rdf:object ?o } WHERE {?s ?p ?o}"); } [Test] public void syntax_form_construct_04_rq() { AssertIsParseable(@"PREFIX rdf: CONSTRUCT { [] rdf:subject ?s ; rdf:predicate ?p ; rdf:object ?o . } WHERE {?s ?p ?o}"); } [Test] public void syntax_form_construct_06_rq() { AssertIsParseable(@"CONSTRUCT {} WHERE {}"); } [Test] public void syntax_form_describe_01_rq() { SparqlQuery expected = new SparqlQuery(); expected.ResultForm = SparqlQuery.ResultFormType.Describe; expected.AddDescribeTerm( new UriRef("u") ); AssertParse(expected, @"DESCRIBE "); } [Test] public void syntax_form_describe_02_rq() { SparqlQuery expected = new SparqlQuery(); expected.ResultForm = SparqlQuery.ResultFormType.Describe; expected.AddDescribeTerm( new UriRef("u") ); expected.AddDescribeTerm( new Variable("u") ); expected.AddVariable( new Variable("u") ); expected.PatternGroup.AddPattern(new Pattern( new UriRef("http://localhost/x"), new UriRef("http://localhost/q"), new Variable("u") ) ); AssertParse(expected, @"DESCRIBE ?u WHERE { ?u . }"); } [Test] public void syntax_dataset_01_rq() { AssertIsParseable(@"PREFIX : SELECT ?x FROM WHERE {}"); } [Test] public void syntax_dataset_02_rq() { AssertIsParseable(@"PREFIX : SELECT ?x FROM NAMED WHERE {}"); } [Test] public void syntax_dataset_03_rq() { AssertIsParseable(@"PREFIX : SELECT ?x FROM NAMED :graph1 FROM NAMED :graph2 WHERE {}"); } [Test] public void syntax_dataset_04_rq() { AssertIsParseable(@"PREFIX : SELECT ?x FROM :g1 FROM :g2 FROM NAMED :graph1 FROM NAMED :graph2 WHERE {}"); } [Test] public void syntax_graph_01_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { GRAPH ?g { } }"); } [Test] public void syntax_graph_02_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { GRAPH [] { } }"); } [Test] public void syntax_graph_03_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { GRAPH :a { } }"); } [Test] public void syntax_graph_04_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { GRAPH ?g { :x :b ?a } }"); } [Test] public void syntax_graph_05_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { :x :p :z GRAPH ?g { :x :b ?a } }"); } [Test] public void syntax_graph_06_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { :x :p :z GRAPH ?g { :x :b ?a . GRAPH [] { :x :p ?x } } }"); } [Test] public void syntax_esc_01_rq() { AssertIsParseable(@"SELECT * WHERE {

""\t"" }"); } [Test] public void syntax_esc_02_rq() { AssertIsParseable(@"SELECT * WHERE {

""x\t"" }"); } [Test] public void syntax_esc_03_rq() { AssertIsParseable(@"SELECT * WHERE {

""\tx"" }"); } [Test] [Category("KnownFailures")] public void syntax_esc_04_rq() { AssertIsParseable(@"PREFIX : SELECT * WHERE { <\u0078> :\u0070 ?xx\u0078 }"); } [Test] public void syntax_bad_01_rq() { AssertIsNotParseable(@"# More a test that bad syntax tests work! PREFIX ex: SELECT * "); } [Test] public void syntax_bad_02_rq() { AssertIsNotParseable(@"# Missing DOT between triples PREFIX : SELECT * { :s1 :p1 :o1 :s2 :p2 :o2 . } "); } [Test] public void syntax_bad_03_rq() { AssertIsNotParseable(@"# DOT, no triples SELECT * WHERE { . }"); } [Test] public void syntax_bad_04_rq() { AssertIsNotParseable(@"# DOT, no triples SELECT * WHERE { . FILTER(?x) } "); } [Test] public void syntax_bad_05_rq() { AssertIsNotParseable(@"# Broken ; SELECT * WHERE { ; FILTER(?x) }"); } [Test] public void syntax_bad_06_rq() { AssertIsNotParseable(@"# Broken ; PREFIX : SELECT * WHERE { :s ; :p :o }"); } [Test] public void syntax_bad_07_rq() { AssertIsNotParseable(@"# Broken ; PREFIX : SELECT * WHERE { :s :p ; } "); } [Test] public void syntax_bad_08_rq() { AssertIsNotParseable(@"# Broken ; PREFIX : SELECT * WHERE { :s :p ; FILTER(?x) } "); } [Test] public void syntax_bad_09_rq() { AssertIsNotParseable(@"# Broken ; PREFIX : SELECT * WHERE { :s :p :o . ; }"); } [Test] public void syntax_bad_10_rq() { AssertIsNotParseable(@"CONSTRUCT"); } [Test] public void DescribeVariablesAreAddedToVariablesCollection() { Query query = ParseQuery("DESCRIBE ?var1 ,?var2 WHERE { ?var }"); IList variables = query.Variables; Assert.AreEqual( 2, variables.Count ); Assert.AreEqual( "var1", ((Variable)variables[0]).Name ); Assert.AreEqual( "var2", ((Variable)variables[1]).Name ); } } }