-------------------------------------------------------------------------------- -- $Id: NamespaceTest.hs,v 1.2 2004/06/30 16:49:03 graham Exp $ -- -- Copyright (c) 2003, G. KLYNE. All rights reserved. -- See end of this file for licence information. -------------------------------------------------------------------------------- -- | -- Module : NamespaceTest -- Copyright : (c) 2003, Graham Klyne -- License : GPL V2 -- -- Maintainer : Graham Klyne -- Stability : provisional -- Portability : H98 -- -- This module contains test cases for namespace data -- {--------+---------+---------+---------+---------+---------+---------+---------} module NamespaceTest where import Namespace ( Namespace(..) , ScopedName(..) , getScopePrefix, getScopeURI , getScopedNameURI , makeUriScopedName, makeNsUriScopedName ) import HUnit ( Test(TestCase,TestList,TestLabel) , assertBool, assertEqual, assertString , runTestTT, runTestText, putTextToHandle ) import IO ( Handle, IOMode(WriteMode) , openFile, hClose, hPutStr, hPutStrLn ) ------------------------------------------------------------ -- Test case helpers ------------------------------------------------------------ testEq :: (Eq a, Show a) => String -> Bool -> a -> a -> Test testEq lab eq a1 a2 = TestCase ( assertEqual ("testEq:"++lab) eq (a1==a2) ) testStringEq :: String -> String -> String -> Test testStringEq lab s1 s2 = TestCase ( assertEqual ("testStringEq:"++lab) s1 s2 ) testSameNamespace :: String -> Namespace -> Namespace -> Test testSameNamespace lab n1 n2 = TestCase ( assertBool ("SameNsPrefix:"++lab) ((p1==p2)&&(u1==u2)) ) where p1 = nsPrefix n1 p2 = nsPrefix n2 u1 = nsURI n1 u2 = nsURI n2 testScopedNameEq :: String -> Bool -> ScopedName -> ScopedName -> Test testScopedNameEq lab eq n1 n2 = TestCase ( assertEqual ("testScopedNameEq:"++lab) eq (n1==n2) ) ------------------------------------------------------------ -- Define and manipulate namespaces ------------------------------------------------------------ base1 = "http://id.ninebynine.org/wip/2003/test/graph1/node#" base2 = "http://id.ninebynine.org/wip/2003/test/graph2/node/" base3 = "http://id.ninebynine.org/wip/2003/test/graph3/node" base4 = "http://id.ninebynine.org/wip/2003/test/graph1/node#more" ns1 = Namespace "base1" base1 ns2 = Namespace "base2" base2 ns3 = Namespace "base3" base3 ns4 = Namespace "alt1" base1 ns5 = Namespace "more" base4 ns6 = Namespace "" base1 ns7 = Namespace "?" base1 -- Namespaces are equivalent if they have the same URI. -- The prefix doesn't matter. testNsEq01 = testEq "testNsEq01" False ns1 ns2 testNsEq02 = testEq "testNsEq02" True ns1 ns4 testNsEq03 = testEq "testNsEq03" True ns1 ns6 -- Extract components of namespace testNsPref01 = testStringEq "testNsPref01" "base1" (nsPrefix ns1) testNsPref02 = testStringEq "testNsPref02" "base2" (nsPrefix ns2) testNsPref03 = testStringEq "testNsPref03" "base3" (nsPrefix ns3) testNsPref04 = testStringEq "testNsPref04" "alt1" (nsPrefix ns4) testNsPref05 = testStringEq "testNsPref05" "more" (nsPrefix ns5) testNsPref06 = testStringEq "testNsPref06" "" (nsPrefix ns6) testNsURI01 = testStringEq "testNsURI01" base1 (nsURI ns1) testNsURI02 = testStringEq "testNsURI02" base2 (nsURI ns2) testNsURI03 = testStringEq "testNsURI03" base3 (nsURI ns3) testNsURI04 = testStringEq "testNsURI04" base1 (nsURI ns4) testNsURI05 = testStringEq "testNsURI05" base4 (nsURI ns5) testNsURI06 = testStringEq "testNsURI06" base1 (nsURI ns6) testShowNs01 = testStringEq "testShowNs01" ("base1:<"++base1++">") (show ns1) testShowNs02 = testStringEq "testShowNs02" ("base2:<"++base2++">") (show ns2) testShowNs03 = testStringEq "testShowNs03" ("base3:<"++base3++">") (show ns3) testShowNs04 = testStringEq "testShowNs04" ("alt1:<"++base1++">") (show ns4) testShowNs05 = testStringEq "testShowNs05" ("more:<"++base4++">") (show ns5) testShowNs06 = testStringEq "testShowNs06" (":<"++base1++">") (show ns6) testShowNs07 = testStringEq "testShowNs07" ("<"++base1++">") (show ns7) testNamespaceSuite = TestList [ testNsEq01, testNsEq02, testNsEq03 , testNsPref01, testNsPref02, testNsPref03, testNsPref04, testNsPref05, testNsPref06 , testNsURI01, testNsURI02, testNsURI03, testNsURI04, testNsURI05, testNsURI06 , testShowNs01, testShowNs02, testShowNs03, testShowNs04, testShowNs05, testShowNs06 , testShowNs07 ] ------------------------------------------------------------ -- Create and compare scoped names ------------------------------------------------------------ -- Note: scoped names are equal if the corresponding QNames are equal -- (inheriting QName->URI logic from RDF.) sn1 = ScopedName ns1 "loc1" sn2 = ScopedName ns2 "loc1" sn3 = ScopedName ns3 "loc1" sn4 = ScopedName ns4 "loc1" sn5 = ScopedName ns1 "loc2" sn6 = ScopedName ns4 "loc2" sn7 = ScopedName ns1 "moreloc3" sn8 = ScopedName ns5 "loc3" sn9 = ScopedName ns6 "loc1" sn10 = ScopedName ns7 "loc1" sn11 = makeUriScopedName "http://id.ninebynine.org/wip/2003/test/graph1/node#loc1" snlist = [ ("sn1", sn1 ) , ("sn2", sn2 ) , ("sn3", sn3 ) , ("sn4", sn4 ) , ("sn5", sn5 ) , ("sn6", sn6 ) , ("sn9", sn9 ) , ("sn11", sn11) ] sneqlist = [ ("sn1", "sn4" ) , ("sn1", "sn9" ) , ("sn1", "sn11") , ("sn4", "sn9" ) , ("sn4", "sn11") , ("sn5", "sn6" ) , ("sn7", "sn8" ) , ("sn9", "sn11") ] testScopedNameEqSuite = TestList [ testScopedNameEq (testLab l1 l2) (testEq l1 l2) n1 n2 | (l1,n1) <- snlist , (l2,n2) <- snlist ] where testLab l1 l2 = l1 ++ "-" ++ l2 testEq l1 l2 = (l1 == l2) || (l1,l2) `elem` sneqlist || (l2,l1) `elem` sneqlist ------------------------------------------------------------ -- Pick components of scoped name ------------------------------------------------------------ -- Extract namespace testSnScope01 = testSameNamespace "testSnScope01" ns1 (snScope sn1) testSnScope02 = testSameNamespace "testSnScope02" ns2 (snScope sn2) testSnScope03 = testSameNamespace "testSnScope03" ns3 (snScope sn3) testSnScope04 = testSameNamespace "testSnScope04" ns4 (snScope sn4) testSnScope05 = testSameNamespace "testSnScope05" ns1 (snScope sn5) testSnScope06 = testSameNamespace "testSnScope06" ns4 (snScope sn6) testSnScope07 = testSameNamespace "testSnScope07" ns1 (snScope sn7) testSnScope08 = testSameNamespace "testSnScope08" ns5 (snScope sn8) testSnScope09 = testSameNamespace "testSnScope09" ns6 (snScope sn9) -- Extract prefix testSnPref01 = testStringEq "testSnPref01" "base1" (getScopePrefix sn1) testSnPref02 = testStringEq "testSnPref02" "base2" (getScopePrefix sn2) testSnPref03 = testStringEq "testSnPref03" "base3" (getScopePrefix sn3) testSnPref04 = testStringEq "testSnPref04" "alt1" (getScopePrefix sn4) testSnPref05 = testStringEq "testSnPref05" "base1" (getScopePrefix sn5) testSnPref06 = testStringEq "testSnPref06" "alt1" (getScopePrefix sn6) testSnPref07 = testStringEq "testSnPref07" "base1" (getScopePrefix sn7) testSnPref08 = testStringEq "testSnPref08" "more" (getScopePrefix sn8) testSnPref09 = testStringEq "testSnPref09" "" (getScopePrefix sn9) testSnPref10 = testStringEq "testSnPref10" "?" (getScopePrefix sn10) -- Extract namespace URI testSnNsURI01 = testStringEq "testSnNsURI01" base1 (getScopeURI sn1) testSnNsURI02 = testStringEq "testSnNsURI02" base2 (getScopeURI sn2) testSnNsURI03 = testStringEq "testSnNsURI03" base3 (getScopeURI sn3) testSnNsURI04 = testStringEq "testSnNsURI04" base1 (getScopeURI sn4) testSnNsURI05 = testStringEq "testSnNsURI05" base1 (getScopeURI sn5) testSnNsURI06 = testStringEq "testSnNsURI06" base1 (getScopeURI sn6) testSnNsURI07 = testStringEq "testSnNsURI07" base1 (getScopeURI sn7) testSnNsURI08 = testStringEq "testSnNsURI08" base4 (getScopeURI sn8) testSnNsURI09 = testStringEq "testSnNsURI09" base1 (getScopeURI sn9) testSnNsURI10 = testStringEq "testSnNsURI10" base1 (getScopeURI sn10) -- Extract local name testSnLocal01 = testStringEq "testSnLocal01" "loc1" (snLocal sn1) testSnLocal02 = testStringEq "testSnLocal02" "loc1" (snLocal sn2) testSnLocal03 = testStringEq "testSnLocal03" "loc1" (snLocal sn3) testSnLocal04 = testStringEq "testSnLocal04" "loc1" (snLocal sn4) testSnLocal05 = testStringEq "testSnLocal05" "loc2" (snLocal sn5) testSnLocal06 = testStringEq "testSnLocal06" "loc2" (snLocal sn6) testSnLocal07 = testStringEq "testSnLocal07" "moreloc3" (snLocal sn7) testSnLocal08 = testStringEq "testSnLocal08" "loc3" (snLocal sn8) testSnLocal09 = testStringEq "testSnLocal09" "loc1" (snLocal sn9) testSnLocal10 = testStringEq "testSnLocal10" "loc1" (snLocal sn10) testScopedNameComponentSuite = TestList [ testSnScope01, testSnScope02, testSnScope03, testSnScope04 , testSnScope05, testSnScope06, testSnScope07, testSnScope08 , testSnScope09 , testSnPref01, testSnPref02, testSnPref03, testSnPref04 , testSnPref05, testSnPref06, testSnPref07, testSnPref08 , testSnPref09, testSnPref10 , testSnNsURI01, testSnNsURI02, testSnNsURI03, testSnNsURI04 , testSnNsURI05, testSnNsURI06, testSnNsURI07, testSnNsURI08 , testSnNsURI09, testSnNsURI10 , testSnLocal01, testSnLocal02, testSnLocal03, testSnLocal04 , testSnLocal05, testSnLocal06, testSnLocal07, testSnLocal08 , testSnLocal09, testSnLocal10 ] ------------------------------------------------------------ -- Get URI ------------------------------------------------------------ testSnURI01 = testStringEq "testSnURI01" (base1++"loc1") (getScopedNameURI sn1) testSnURI02 = testStringEq "testSnURI02" (base2++"loc1") (getScopedNameURI sn2) testSnURI03 = testStringEq "testSnURI03" (base3++"loc1") (getScopedNameURI sn3) testSnURI04 = testStringEq "testSnURI04" (base1++"loc1") (getScopedNameURI sn4) testSnURI05 = testStringEq "testSnURI05" (base1++"loc2") (getScopedNameURI sn5) testSnURI06 = testStringEq "testSnURI06" (base1++"loc2") (getScopedNameURI sn6) testSnURI07 = testStringEq "testSnURI07" (base4++"loc3") (getScopedNameURI sn7) testSnURI08 = testStringEq "testSnURI08" (base4++"loc3") (getScopedNameURI sn8) testSnURI09 = testStringEq "testSnURI09" (base1++"loc1") (getScopedNameURI sn9) testScopedNameAssemblySuite = TestList [ testSnURI01, testSnURI02, testSnURI03, testSnURI04 , testSnURI05, testSnURI06, testSnURI07, testSnURI08 , testSnURI09 ] ------------------------------------------------------------ -- Show scoped name (as prefix:local) ------------------------------------------------------------ testShowName01 = testStringEq "testShowName01" "base1:loc1" (show sn1) testShowName02 = testStringEq "testShowName02" "base2:loc1" (show sn2) testShowName03 = testStringEq "testShowName03" "base3:loc1" (show sn3) testShowName04 = testStringEq "testShowName04" "alt1:loc1" (show sn4) testShowName05 = testStringEq "testShowName05" "base1:loc2" (show sn5) testShowName06 = testStringEq "testShowName06" "alt1:loc2" (show sn6) testShowName07 = testStringEq "testShowName07" "base1:moreloc3" (show sn7) testShowName08 = testStringEq "testShowName08" "more:loc3" (show sn8) testShowName09 = testStringEq "testShowName09" ":loc1" (show sn9) testShowName10 = testStringEq "testShowName10" ("<"++base1++"loc1>") (show sn10) testShowNameSuite = TestList [ testShowName01, testShowName02, testShowName03, testShowName04 , testShowName05, testShowName06, testShowName07, testShowName08 , testShowName09, testShowName10 ] ------------------------------------------------------------ -- Edge cases ------------------------------------------------------------ -- Null local name ec1 = ScopedName ns1 "" ec2 = ScopedName ns1 "" ec3 = ScopedName ns6 "" ec4 = ScopedName ns6 "" testNullLocalName01 = testScopedNameEq "testNullLocalName01" True ec1 ec2 testNullLocalName02 = testScopedNameEq "testNullLocalName02" True ec3 ec4 testNullLocalName03 = testScopedNameEq "testNullLocalName03" True ec1 ec4 testEdgeCaseSuite = TestList [ testNullLocalName01, testNullLocalName02, testNullLocalName03 ] ------------------------------------------------------------ -- Make scoped name using given namespace, if it matches ------------------------------------------------------------ -- makeNsUriScopedName :: Namespace -> String -> ScopedName nu11 = makeNsUriScopedName ns1 (getScopedNameURI sn1) nu12 = makeNsUriScopedName ns1 (getScopedNameURI sn2) nu13 = makeNsUriScopedName ns1 (getScopedNameURI sn3) nu18 = makeNsUriScopedName ns1 (getScopedNameURI sn8) nu21 = makeNsUriScopedName ns2 (getScopedNameURI sn1) nu22 = makeNsUriScopedName ns2 (getScopedNameURI sn2) nu31 = makeNsUriScopedName ns3 (getScopedNameURI sn1) nu32 = makeNsUriScopedName ns3 (getScopedNameURI sn2) nu33 = makeNsUriScopedName ns3 (getScopedNameURI sn3) nu37 = makeNsUriScopedName ns3 (getScopedNameURI sn7) nu38 = makeNsUriScopedName ns3 (getScopedNameURI sn8) nu51 = makeNsUriScopedName ns5 (getScopedNameURI sn1) nu52 = makeNsUriScopedName ns5 (getScopedNameURI sn2) nu53 = makeNsUriScopedName ns5 (getScopedNameURI sn3) nu57 = makeNsUriScopedName ns5 (getScopedNameURI sn7) nu58 = makeNsUriScopedName ns5 (getScopedNameURI sn8) showUri sn = "<"++getScopedNameURI sn++">" testNsUriName11 = testStringEq "testNsUriName11" "base1:loc1" (show nu11) testNsUriName12 = testStringEq "testNsUriName12" (showUri sn2) (show nu12) testNsUriName13 = testStringEq "testNsUriName13" (showUri sn3) (show nu13) testNsUriName18 = testStringEq "testNsUriName18" "base1:moreloc3" (show nu18) testNsUriName21 = testStringEq "testNsUriName21" (showUri sn1) (show nu21) testNsUriName22 = testStringEq "testNsUriName22" "base2:loc1" (show nu22) testNsUriName31 = testStringEq "testNsUriName31" (showUri sn1) (show nu31) testNsUriName32 = testStringEq "testNsUriName32" (showUri sn2) (show nu32) testNsUriName33 = testStringEq "testNsUriName33" "base3:loc1" (show nu33) testNsUriName37 = testStringEq "testNsUriName37" (showUri sn7) (show nu37) testNsUriName38 = testStringEq "testNsUriName38" (showUri sn8) (show nu38) testNsUriName51 = testStringEq "testNsUriName51" (showUri sn1) (show nu51) testNsUriName52 = testStringEq "testNsUriName52" (showUri sn2) (show nu52) testNsUriName53 = testStringEq "testNsUriName53" (showUri sn3) (show nu53) testNsUriName57 = testStringEq "testNsUriName57" "more:loc3" (show nu57) testNsUriName58 = testStringEq "testNsUriName58" "more:loc3" (show nu58) testNsUriSuite = TestList [ testNsUriName11, testNsUriName12, testNsUriName13, testNsUriName18 , testNsUriName21, testNsUriName22 , testNsUriName31, testNsUriName32, testNsUriName33, testNsUriName37, testNsUriName38 , testNsUriName51, testNsUriName52, testNsUriName53, testNsUriName57, testNsUriName58 ] ------------------------------------------------------------ -- All tests ------------------------------------------------------------ allTests = TestList [ testNamespaceSuite , testScopedNameEqSuite , testScopedNameComponentSuite , testScopedNameAssemblySuite , testShowNameSuite , testEdgeCaseSuite , testNsUriSuite ] main = runTestTT allTests runTestFile t = do h <- openFile "a.tmp" WriteMode runTestText (putTextToHandle h False) t hClose h tf = runTestFile tt = runTestTT -------------------------------------------------------------------------------- -- -- Copyright (c) 2003, G. KLYNE. All rights reserved. -- -- This file is part of Swish. -- -- Swish is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- -- Swish is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with Swish; if not, write to: -- The Free Software Foundation, Inc., -- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -- -------------------------------------------------------------------------------- -- $Source: /file/cvsdev/HaskellUtils/NamespaceTest.hs,v $ -- $Author: graham $ -- $Revision: 1.2 $ -- $Log: NamespaceTest.hs,v $ -- Revision 1.2 2004/06/30 16:49:03 graham -- Remove references to module QName from module Namespace. -- -- Revision 1.1 2004/01/13 12:31:24 graham -- Move modules from HaskellRDF to HaskellUtils project -- -- Revision 1.6 2004/01/07 19:49:12 graham -- Reorganized RDFLabel details to eliminate separate language field, -- and to use ScopedName rather than QName. -- Removed some duplicated functions from module Namespace. -- -- Revision 1.5 2004/01/06 13:53:10 graham -- Created consolidated test harness (SwishTestAll.hs) -- -- Revision 1.4 2003/12/08 23:55:36 graham -- Various enhancements to variable bindings and proof structure. -- New module BuiltInMap coded and tested. -- Script processor is yet to be completed. -- -- Revision 1.3 2003/11/24 17:20:35 graham -- Separate module Vocabulary from module Namespace. -- -- Revision 1.2 2003/11/12 20:44:24 graham -- Added some vocabulary to Namespace. -- Enhaced ScopedName to allow null namespace prefixes, -- following N3 display conventions. -- -- Revision 1.1 2003/09/24 18:51:36 graham -- Add module Namespace and test cases. -- -- Revision 1.2 2003/09/24 13:36:42 graham -- QName handling separated from RDFGraph module, and -- QName splitting moved from URI module to QName module. -- -- Revision 1.1 2003/09/24 12:51:00 graham -- Add separate QName module and test suite --