/*
(c) Copyright 2008, 2009 Hewlett-Packard Development Company, LP
All rights reserved.
$Id$
*/
package com.hp.jena.ymris.grammar.tests;
import static org.junit.Assert.*;
import java.io.StringReader;
import org.junit.*;
import com.hp.jena.ymris.grammar.ParseException;
import com.hp.jena.ymris.grammar.Ymris;
import com.hp.jena.ymris.yast.*;
import com.hp.jena.ymris.yast.YAST.RawTerms;
public class TestYmrisGrammarRagbag
{
protected Ymris parserOn( String text )
{ return new Ymris( new StringReader( text ) ); }
@Test public void ensure_various() throws ParseException
{
parserOn( "" ).ymris();
parserOn( "ruleset" ).ymris();
parserOn( "application" ).ymris();
parserOn( "ruleset application" ).ymris();
parserOn( "application ruleset" ).ymris();
parserOn( "ruleset ruleset application ruleset ruleset" ).ymris();
parserOn( "application <some:uri>" ).ymris();
parserOn( "application prefix a: <other:uri>" ).ymris();
parserOn( "application base <base:uri>" ).ymris();
parserOn( "application prefix a: <other:uri> base <base:b>" ).ymris();
parserOn( "application prefix a: <other:uri> prefix b: <another:uri>" ).ymris();
parserOn( "application <other:uri>" ).ymris();
parserOn( "application 'hello'" ).ymris();
parserOn( "application 'hello', 'there'" ).ymris();
parserOn( "application 'hello'@en" ).ymris();
parserOn( "application 'hello', 'bonjour'@fr" ).ymris();
parserOn( "application <my:uri> 'my name'@en" ).ymris();
}
protected final RawTerms raw = new RawTerms();
@Test public void ensureObjectAcceptsLegalExpressions() throws ParseException
{
parserOn( "<eh:/object>" ).itemOrExpr( raw );
parserOn( ":object" ).itemOrExpr( raw );
parserOn( "my:" ).itemOrExpr( raw );
parserOn( "my:object" ).itemOrExpr( raw );
parserOn( "true" ).itemOrExpr( raw );
parserOn( "false" ).itemOrExpr( raw );
parserOn( "17" ).itemOrExpr( raw );
parserOn( "17.0" ).itemOrExpr( raw );
parserOn( "+17" ).itemOrExpr( raw );
parserOn( "-17" ).itemOrExpr( raw );
parserOn( "+17.0" ).itemOrExpr( raw );
parserOn( "-17.0" ).itemOrExpr( raw );
parserOn( "+17.0e2" ).itemOrExpr( raw );
parserOn( "-17.0e2" ).itemOrExpr( raw );
parserOn( "'confabulation'" ).itemOrExpr( raw );
parserOn( "'confabulation'@en" ).itemOrExpr( raw );
parserOn( "'confabulation'^^xsd:integer" ).itemOrExpr( raw );
parserOn( "f(?x)" ).itemOrExpr( raw );
parserOn( "f(?x + 1)" ).itemOrExpr( raw );
}
@Test public void ensureAcceptsFilterExpressions() throws ParseException
{
parseFilter( "true" );
parseFilter( "false" );
parseFilter( "1" );
parseFilter( "'2'" );
parseFilter( "1.0" );
parseFilter( "-1" );
parseFilter( "+1" );
parseFilter( "-1.0" );
parseFilter( "+1.0" );
parseFilter( "-1.0e1" );
parseFilter( "+1.0e-1" );
parseFilter( "'spelling'xsd:string" );
parseFilter( "1 = 2" );
parseFilter( "1 < 2" );
parseFilter( "1 > 2" );
parseFilter( "1 <= 2" );
parseFilter( "1 >= 2" );
parseFilter( "1 != 2" );
parseFilter( "?x" );
parseFilter( "true && true" );
parseFilter( "true || true" );
parseFilter( "f(true)" );
parseFilter( "1 + 2" );
parseFilter( "1 - 2" );
parseFilter( "1 * 2" );
parseFilter( "1 / 2" );
}
private void parseFilter( String source ) throws ParseException
{ parserOn( "filter " + source ).filter( raw ); }
@Test public void ensureAllowsListsAsSubjects() throws ParseException
{
parserOn( "{ () my:P my:y }" ).bracedTriplePatterns( raw );
parserOn( "{ (my:a) my:P my:y }" ).bracedTriplePatterns( raw );
parserOn( "{ (my:a my:b) my:P my:y }" ).bracedTriplePatterns( raw );
}
@Test public void ensureParsesBracedTriplesPatterns() throws ParseException
{
parserOn( "{ my:x my:P $f(?x) }" ).bracedTriplePatterns( raw );
parserOn( "{ _:spoo my:P _:flarn }" ).bracedTriplePatterns( raw );
parserOn( "{ my:x my:P my:y }" ).bracedTriplePatterns( raw );
parserOn( "{ my:x my:P my:y, my:z }" ).bracedTriplePatterns( raw );
parserOn( "{ my:x my:P my:y; my:Q my:z }" ).bracedTriplePatterns( raw );
parserOn( "{ my:x my:P 17 }" ).bracedTriplePatterns( raw );
parserOn( "{ my:x my:P 'helloWorld' }" ).bracedTriplePatterns( raw );
}
@Test public void ensureParsesListsAsOjects() throws ParseException
{
parserOn( "{ my:a my:P () }" ).bracedTriplePatterns( raw );
parserOn( "{ my:a my:P (my:y) }" ).bracedTriplePatterns( raw );
parserOn( "{ my:a my:P (my:y my:z) }" ).bracedTriplePatterns( raw );
}
@Test public void ensureParsesUNLESS() throws ParseException
{
parserOn( "{ my:x my:P my:y UNLESS ?x my:Q ?y }" ).bracedTriplePatterns( raw );
parserOn( "{ UNLESS my:x my:P my:y ?x my:P ?y }" ).bracedTriplePatterns( raw );
parserOn( "{ my:x my:P my:y UNLESS ?x my:Q ?y }" ).bracedTriplePatterns( raw );
}
@Test public void ensureParsesUNLESSwithFilter() throws ParseException
{
parserOn( "{ my:x my:P my:y UNLESS { ?x my:Q ?y FILTER ?y > 0 } }" ).bracedTriplePatterns( raw );
}
@Test public void ensureParsesLETs() throws ParseException
{
parserOn( "{ LET ?x = 1 }" ).bracedTriplePatterns( raw );
parserOn( "{ ?a my:P ?b LET ?x = 1 }" ).bracedTriplePatterns( raw );
parserOn( "{ LET ?x = 1 ?a my:P ?b }" ).bracedTriplePatterns( raw );
parserOn( "{ LET ?x = 1 + 2 ?a my:P ?b }" ).bracedTriplePatterns( raw );
}
@Test public void ensureParseALL() throws ParseException
{
parserOn( "{ALL ?x {} => {}}" ).bracedTriplePatterns( raw );
parserOn( "{ALL ?x. {} => {}}" ).bracedTriplePatterns( raw );
parserOn( "{ALL ?x. {?x <eh:/P> ?y} => {<eh:?P> <my:count> count(?x)}}" ).bracedTriplePatterns( raw );
}
@Test public void ensureEnptyStringsParsedCorrectly() throws ParseException
{
assertEquals( "", parseLiteral( "''" ) );
assertEquals( "", parseLiteral( "\"\"" ) );
assertEquals( "", parseLiteral( "''''''" ) );
assertEquals( "", parseLiteral( "\"\"\"\"\"\"" ) );
}
@Test public void ensureNonEmptyStringsWithoutEscapesParsedCorrectly() throws ParseException
{
assertEquals( "abc", parseLiteral( "'abc'" ) );
assertEquals( "def", parseLiteral( "\"def\"" ) );
assertEquals( "ghi\njkl", parseLiteral( "'''ghi\njkl'''" ) );
assertEquals( "mno\npqr", parseLiteral( "\"\"\"mno\npqr\"\"\"" ) );
}
@Test public void ensureEscapesParsedCorrectly() throws ParseException
{ // TODO hex escapes
assertEquals( "\n", parseLiteral( "'\\n'" ) );
assertEquals( "\t", parseLiteral( "'\\t'" ) );
assertEquals( "\r", parseLiteral( "'\\r'" ) );
assertEquals( "\f", parseLiteral( "'\\f'" ) );
assertEquals( "\b", parseLiteral( "'\\b'" ) );
assertEquals( "\\", parseLiteral( "'\\\\'" ) );
assertEquals( "\"", parseLiteral( "'\\\"'" ) );
assertEquals( "\'", parseLiteral( "'\\\''" ) );
}
private String parseLiteral( String toParse ) throws ParseException
{ return parserOn( toParse ).string(); }
@Test public void ensureParsesTaggedStrings() throws ParseException
{
assertEquals( sil( "chat", "fr" ), parseTagged( "'chat'@fr" ) );
assertEquals( sil( "chat", "en" ), parseTagged( "'chat'@en" ) );
assertEquals( sil( "chat", "fr" ), parseTagged( "'''chat'''@fr" ) );
assertEquals( sil( "chat", "fr" ), parseTagged( "\"chat\"@fr" ) );
}
private YNode sil( String spelling, String lang )
{ return YLiteral.lang( spelling, "@" + lang ); }
private YNode parseTagged( String toParse ) throws ParseException
{ return parserOn( toParse ).typedString(); }
}
/*
(c) Copyright 2009 Hewlett-Packard Development Company, LP
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/