# Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig. # License: Apache License, Version 2.0 require File.dirname(__FILE__) + '/../../../spec_helper' module RTM::Sugar::Topic::HashAccess describe self do before(:each) do @tm = get_used_tm_sys_tm end after(:each) do @tm.close end describe "#[]" do before(:each) do @topic1 = @tm.get!("Hans") @name1 = @topic1.create_name("Hans") @name2 = @topic1.create_name("first","George",["en","always"]) @name3 = @topic1.create_name("last","Meyer",["underground"]) @occ1 = @topic1.create_occurrence("Heimat","Sachsen",:scope => ["de"]) @occ2 = @topic1.create_occurrence("Heimat","Germany",:scope => ["de","always"]) @occ3 = @topic1.create_occurrence("Birthyear","1969",:scope => ["always"]) @occ4 = @topic1.create_occurrence("parents","none") end it "should give back an Occurrence of the specified type, in an Array, if the argument is a Topic" do @topic1[@tm.get("Something")].size.should == 0 @topic1[@tm.get("Heimat")].size.should == 2 @topic1[@tm.get("Heimat")].should include(@occ1) @topic1[@tm.get("Heimat")].should include(@occ2) end it "should give back an empty Array if the argument is nil" do @topic1[nil].should be_empty @topic1.[](nil).should be_empty end it "should give back all occurrences of this Topic if no argument is given" do @topic1[].size.should == 4 @topic1[].should include(@occ1) @topic1[].should include(@occ2) @topic1[].should include(@occ3) @topic1[].should include(@occ4) end it "should return an array of names with default nametype if the argument equals '-'" do @topic1["-"].size.should == 1 @topic1["-"].should include(@name1) end it "should give back all names that have the given name type if argument equals '-type'" do @topic1["-first"].size.should == 1 @topic1["-first"].should include(@name2) end it "should give back all names that have the given name type and scope if argument equals '-type @scope" do @topic1["-last @underground"].size.should == 1 @topic1["-last @underground"].should include(@name3) end it "should give back an empty Array if argument equals '-type' or '-type @scope' or '-type @scope1 scope2' but type or scope/1/2 does not exist" do @topic1["-nothing"].should be_empty @topic1["-first @nothing"].should be_empty @topic1["-nothing @nothing"].should be_empty @topic1["-first @en nothing"].should be_empty @topic1["-first @nothing en"].should be_empty @topic1["-nothing @nothing nothing"].should be_empty end it "should give back all names that have the given name type and scope if argument equals '-type @scope1 scope2" do @topic1["-first @en always"].size.should == 1 @topic1["-first @en always"].should include(@name2) @topic1["-first @en, always"].should == @topic1["-first @en always"] end it "should give back all occurrences that have the given occurrence type if argument equals 'type'" do @topic1["Heimat"].size.should == 2 @topic1["Heimat"].should include(@occ1) @topic1["Heimat"].should include(@occ2) end it "should give back all occurrences that have the given occurrence type and scope if argument equals 'type @scope'" do @topic1["Heimat @de"].size.should == 2 @topic1["Heimat @de"].should include(@occ1) @topic1["Heimat @de"].should include(@occ2) end it "should give back all occurrences that have the given occurrence type and scope if argument equals 'type @scope1 scope2'" do @topic1["Heimat @de always"].size.should == 1 @topic1["Heimat @de always"].should include(@occ2) @topic1["Heimat @de, always"].should == @topic1["Heimat @de always"] end it "should give back an empty Array if argument equals 'type' or 'type @scope' or 'type @scope1 scope2' but type or scope/1/2 does not exist" do @topic1["nothing"].should be_empty @topic1["first @nothing"].should be_empty @topic1["nothing @nothing"].should be_empty @topic1["parents @en nothing"].should be_empty @topic1["parents @nothing en"].should be_empty @topic1["nothing @nothing nothing"].should be_empty end it "should give back all name and occurrences of this Topic if argument equals '*'" do @topic1["*"].size.should == 7 end it "should give back all name and occurrences that have the given type if argument equals '*type'" do name = @topic1.create_name("dummy_type","dummy_name") occ = @topic1.create_occurrence("dummy_type","dummy_occ") @topic1["*dummy_type"].size.should == 2 @topic1["*dummy_type"].should include(name) @topic1["*dummy_type"].should include(occ) @topic1["*first"].size.should == 1 @topic1["*first"].should include(@name2) @topic1["*parents"].size.should == 1 @topic1["*parents"].should include(@occ4) end it "should give back all name and occurrences that have the given type and scope if argument equals '*type @scope'" do name1 = @topic1.create_name("dummy_type","dummy_name1",["theme0"]) name2 = @topic1.create_name("dummy_type","dummy_name2",["theme1"]) occ1 = @topic1.create_occurrence("dummy_type","dummy_occ1", :scope => ["theme0","theme1"]) occ2 = @topic1.create_occurrence("dummy_type","dummy_occ2",:scope => ["theme1"]) @topic1["*dummy_type @theme0"].size.should == 2 @topic1["*dummy_type @theme0"].should include(occ1) @topic1["*dummy_type @theme0"].should include(name1) @topic1["*dummy_type @theme1"].size.should == 3 @topic1["*dummy_type @theme1"].should include(name2) @topic1["*dummy_type @theme1"].should include(occ1) @topic1["*dummy_type @theme1"].should include(occ2) end it "should give back all name and occurrences that have the given type and scope if argument equals '*type @scope1 scope2'" do name1 = @topic1.create_name("dummy_type","dummy_name1",["theme0","theme1"]) name2 = @topic1.create_name("dummy_type","dummy_name2",["theme1"]) occ1 = @topic1.create_occurrence("dummy_type","dummy_occ1",:scope => ["theme0"]) occ2 = @topic1.create_occurrence("dummy_type","dummy_occ2",:scope => ["theme0","theme1"]) @topic1["*dummy_type @theme0 theme1"].size.should == 2 @topic1["*dummy_type @theme0 theme1"].should include(name1) @topic1["*dummy_type @theme0 theme1"].should include(occ2) end it "should handle spaces if argument is a String" do @topic1[" - "].should == @topic1["-"] @topic1[" - first "].should == @topic1["-first"] @topic1[" - last @ underground"].should == @topic1["-last @underground"] @topic1[" - first @ en always"].should == @topic1["-first @en always"] @topic1[" - first @ en, always"].should == @topic1["-first @en, always"] @topic1[" Heimat "].should == @topic1["Heimat"] @topic1[" Heimat @ de "].should == @topic1["Heimat @de"] @topic1[" Heimat @ de always "].should == @topic1["Heimat @de always"] @topic1[" * "].should == @topic1["*"] end it "should give back occurrences and names whose type have item identifier" do topic = @tm.get!("name_ii_test") name = topic.create_name("ii:firstname_ii_test","Hans") @tm.get("^firstname_ii_test").should_not be_nil occ = topic.create_occurrence("ii:age_ii_test","12") @tm.get("^age_ii_test").should_not be_nil topic["-ii:firstname_ii_test"].size.should == 1 topic["-ii:firstname_ii_test"].should include name topic["ii:age_ii_test"].size.should == 1 topic["ii:age_ii_test"].should include occ topic["-firstname_ii_test"].should be_empty topic["age_ii_test"].should be_empty topic["*"].size.should == 2 end it "should give back occurrences and names whose type have subject locator" end describe "#[]=" do before(:each) do @topic2 = @tm.get!("Peter") end it "should set a name with standard name type given '-'" do @topic2["-"] = "Peter" @topic2["Hausnummer"] = "1" @topic2.names.size.should == 1 @topic2.names.first.type.should == @tm.get(RTM::PSI[:name_type]) @topic2.names.first.value.should == "Peter" end it "should set a name with standard name type and scope given '- @scope'" do scope = RTM::PREFIX[:language] + "german" @topic2["- @" + scope] = "Peter" @topic2["Hausnummer"] = "1" @topic2.names.size.should == 1 @topic2.names.first.type.should == @tm.get(RTM::PSI[:name_type]) @topic2.names.first.value.should == "Peter" @topic2.names.first.scope.first.should == @tm.get(scope) end it "should set a name with standard name type and scope (which is a qname) given '-@qname'" do @tm.add_prefix("lang",RTM::PREFIX[:language]) @topic2["- @lang:german"] = "Peter" @topic2["Hausnummer"] = "1" @topic2.names.size.should == 1 @topic2.names.first.type.should == @tm.get(RTM::PSI[:name_type]) @topic2.names.first.value.should == "Peter" scope = RTM::PREFIX[:language] + "german" @topic2.names.first.scope.first.should == @tm.get(scope) end it "should set a name and nametype type given '-nametype'" do @topic2["-first"] = "Peter" @topic2["Hausnummer"] = "1" @topic2.names.size.should == 1 @topic2.names.first.type.should == @tm.get("first") @topic2.names.first.value.should == "Peter" end it "should set an Occurrence and Occurrence type given 'occurrencetype'" do @topic2["-first"] = "Peter" @topic2["Hausnummer"] = "1" @topic2.occurrences.size.should == 1 @topic2.occurrences.first.type.should == @tm.get("Hausnummer") @topic2.occurrences.first.value.should == "1" end it "should set the Scope given [...@ scope] or [...@ scope1, scope2 scope3 ...]" do @topic2["-first @deutsch"] = "Peter" @topic2["Hausnummer @scope1, scope2 scope3"] = "1" @topic2.names.first.type.should == @tm.get("first") @topic2.names.first.value.should == "Peter" @topic2.names.first.scope.first.should == @tm.get("deutsch") @topic2.occurrences.first.type.should == @tm.get("Hausnummer") @topic2.occurrences.first.value.should == "1" @topic2.occurrences.first.scope.size.should == 3 @topic2.occurrences.first.scope.should include(@tm.get("scope1")) @topic2.occurrences.first.scope.should include(@tm.get("scope2")) @topic2.occurrences.first.scope.should include(@tm.get("scope3")) end it "should allow spaces before and after and between - and Nametype and @" do @topic2[" - first @ scope1 scope2 scope3"] = "Peter" @topic2.names.size.should == 1 @topic2.names.first.type.should == @tm.get("first") @topic2.names.first.scope.size.should == 3 @topic2.names.first.scope.should include(@tm.get("scope1")) @topic2.names.first.scope.should include(@tm.get("scope2")) @topic2.names.first.scope.should include(@tm.get("scope3")) end it "should create names whose types have item identifier" do @topic2["-ii:a_name"] = "Billy" @tm.get("^a_name").should_not be_nil @topic2.names.first.type.should == @tm.get("^a_name") end it "should create occurrences whose types have item identifier" do @topic2["ii:an_occ"] = "42" @tm.get("^an_occ").should_not be_nil @topic2.occurrences.first.type.should == @tm.get("^an_occ") end end end end