# -*- encoding : utf-8 -*- # # Copyright 2011 innoQ Deutschland GmbH # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. $LOAD_PATH << File.dirname(__FILE__) require 'test_helper' class NTriplesTest < Test::Unit::TestCase def test_basics document = IqRdf::Document.new('http://example.org/') document.namespaces :skos => 'http://www.w3.org/2004/02/skos/core#' document << IqRdf.foo do |node| node.skos.related(IqRdf.bar) end assert_equal(<<-rdf.strip, document.to_ntriples.strip) . rdf document = IqRdf::Document.new('http://www.test.de/', :lang => :de) document.namespaces :foaf => 'http://xmlns.com/foaf/0.1/' document << IqRdf::testemann do |t| t.Foaf::knows(IqRdf::testefrau) t.Foaf.nick("Testy") t.Foaf.lastname("Testemann", :lang => :none) end actual = document.to_ntriples assert_equal(<<-rdf.strip, actual.strip) . "Testy"@de . "Testemann" . rdf assert actual.end_with?("\n"), "trailing line break" end def test_full_uri_subject document = IqRdf::Document.new('http://www.test.de/') document << IqRdf::build_full_uri_subject(URI.parse('http://www.xyz.de/#test'), IqRdf::build_uri('SomeType')) do |t| t.sometest("testvalue") end assert_equal(<<-rdf.strip, document.to_ntriples.strip) . "testvalue" . rdf end def test_full_uri_predicate document = IqRdf::Document.new('http://www.test.de/') document << IqRdf::testemann. build_full_uri_predicate(URI.parse("http://www.test.org/hoho"), 42) assert_equal(<<-rdf.strip, document.to_ntriples.strip) "42"^^ . rdf end def test_blank_nodes document = IqRdf::Document.new('http://www.test.de/') document << IqRdf::testnode.test32 do |blank_node| blank_node.title("dies ist ein test") blank_node.build_predicate(:test, "Another test") blank_node.sub do |subnode| subnode.title("blubb") end end assert_equal(<<-rdf.strip, document.to_ntriples.strip) _:b1 . _:b1 "dies ist ein test" . _:b1 "Another test" . _:b1 _:b2 . _:b2 "blubb" . rdf end def test_collections document = IqRdf::Document.new('http://test.de/') document << IqRdf::testemann.testIt([IqRdf::hello, IqRdf::goodbye, "bla"]) assert_equal(<<-rdf.strip, document.to_ntriples.strip) _:b2 . _:b2 . _:b2 _:b3 . _:b3 . _:b3 . _:b3 _:b4 . _:b4 . _:b4 "bla" . _:b2 . rdf end def test_complex_features document = IqRdf::Document.new('http://www.umweltprobenbank.de/', :lang => :de) document.namespaces :skos => 'http://www.w3.org/2008/05/skos#', :foaf => 'http://xmlns.com/foaf/0.1/', :upb => 'http://www.upb.de/' document << IqRdf::testemann.myCustomNote("This is an example", :lang => :en) document << IqRdf::testemann(IqRdf::Foaf::build_uri("Person")). Foaf::name("Heinz Peter Testemann", :lang => :none) document << IqRdf::testemann.Foaf::knows(IqRdf::testefrau) document << IqRdf::testemann.Foaf::nick("Crash test dummy") ["u1023", "xkfkrl"].each do |id| document << IqRdf::Upb::build_uri(id, IqRdf::Skos::build_uri(:Concept)) do |doc| doc.Skos::prefLabel("Test", :lang => :en) doc.Skos::related(IqRdf::Rdf.anotherThing) doc.test1("bla") doc.testIt(:hello, :goodbye, "bla") doc.anotherTest(URI.parse("http://www.test.de/foo")) end end document << IqRdf::Skos::testnode.test32 do |blank_node| blank_node.title("dies ist ein test") blank_node.sub do |subnode| subnode.title("blubb") end end assert_equal(<<-rdf.strip, document.to_ntriples.strip) "This is an example"@en . . "Heinz Peter Testemann" . . "Crash test dummy"@de . . "Test"@en . . "bla"@de . . . "bla"@de . . . "Test"@en . . "bla"@de . . . "bla"@de . . _:b1 . _:b1 "dies ist ein test"@de . _:b1 _:b2 . _:b2 "blubb"@de . rdf end def test_literals document = IqRdf::Document.new('http://www.test.de/', :lang => :de) document.namespaces :foaf => 'http://xmlns.com/foaf/0.1/' document << IqRdf::testemann do |t| t.Foaf::knows(:testefrau) t.Foaf.nick("Testy") t.Foaf.lastname("Tästämann", :lang => :none) t.age(32) t.married(false) t.weight(65.8) t.complex(IqRdf::Literal.new("A very complex type", :none, URI.parse("http://this.com/is#complex"))) t.complex2(IqRdf::Literal.new("Shorter form", :none, IqRdf::myDatatype)) t.quotes("\"I'm \\quoted\"") t.line_breaks("I'm written\nover two lines") t.some_literal(IqRdf::Literal.new("text", :de)) end assert_equal(< . "Testy"@de . "Tästämann" . "32"^^ . "false"^^ . "65.8"^^ . "A very complex type"^^ . "Shorter form"^^ . "\\"I'm \\\\quoted\\""@de . """I'm written over two lines"""@de . "text"@de . rdf end def test_supress_if_empty_option document = IqRdf::Document.new('http://www.test.de/') document.namespaces :foaf => 'http://xmlns.com/foaf/0.1/' document << IqRdf::testemann.Foaf::knows(:suppress_if_empty => true) document << IqRdf::testemann.Foaf::knows(nil, :suppress_if_empty => true) document << IqRdf::testemann.Foaf::knows("", :suppress_if_empty => true) document << IqRdf::testemann.Foaf::knows([], :suppress_if_empty => true) assert_equal("", document.to_ntriples.strip) end end