# 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::TopicRef
  describe self do
    before(:each) do
      @tm = get_used_tm_sys_tm
      @topic_with_ii = @tm.get!("ii:uni-leipzig")
      @topic_with_si = @tm.get!("si:uni-berlin")
      @topic_with_sl = @tm.get!("sl:uni-munich")
    end
    after(:each) do
      @tm.close
    end
    describe "#references" do
      it "should not give back an empty Array" do
        @topic_with_si.references.should_not be_empty
      end
      it "should give back an item identifier that starts with an ^" do
        @topic_with_ii.references.first[0].should == ?^
        @topic_with_ii.references.should include("^http://www.topicmapslab.de/uni-leipzig")
      end
      it "should give back a subject locator that starts with a ^=" do
        @topic_with_sl.references.first[0].should == ?=
        @topic_with_sl.references.should include("=http://www.topicmapslab.de/uni-munich")
      end
      it "should give back a subject identifier start does not start with a special character" do
        @topic_with_si.references.first[0].should == ?h
        @topic_with_si.references.should include("http://www.topicmapslab.de/uni-berlin")
      end
    end

    describe ":resolve_qnames => :true" do
      it "should resolve the qnames" do
        @tm.add_prefix("tml", "http://www.topicmapslab.de/")
        @topic_with_ii.references(:resolve_qnames => :true).first[0].should == ?^
        @topic_with_ii.references(:resolve_qnames => :true).should include("^tml:uni-leipzig")
        @topic_with_sl.references(:resolve_qnames => :true).first[0].should == ?=
        @topic_with_sl.references(:resolve_qnames => :true).should include("=tml:uni-munich")
        @topic_with_si.references(:resolve_qnames => :true).first[0].should == ?t
        @topic_with_si.references(:resolve_qnames => :true).should include("tml:uni-berlin")
      end
      it "should resolve the qnames for a topic with many identifiers" do
        topic = @tm.get!("http://www.example.org/si")
        topic.add_subject_identifier("http://www.example.net/si")
        topic.add_item_identifier("http://www.example.org/i i")
        topic.add_item_identifier("http://www.example.net/i i")
        topic.add_subject_locator("http://www.example.org/there#sl")
        topic.add_subject_locator("http://www.example.net/sl")
        @tm.add_prefix("example", "http://www.example.org/")
        topic.references(:outputstyle => :yaml, :resolve_qnames => :true).size.should == 6
        topic.references(:outputstyle => :yaml, :resolve_qnames => :true).should include("si:example:si", "si:http://www.example.net/si", "ii:example:i i", "ii:http://www.example.net/i i", "sl:example:there#sl", "sl:http://www.example.net/sl")
      end
    end

    describe "#unresolve" do
      it "should unresolve an iri that starts with the base iri of the parent topic map" do
        topic = @tm.get!("Leipzig")
        identifier = "http://www.topicmapslab.de/Leipzig"
        topic.reference.should == identifier
        topic.unresolve(identifier).should == "Leipzig"
      end
      it "should not raise an error"
    end

    describe "questioning identifiers with" do
      before(:each) do
        @tm.add_prefix("test","http://example.com/test/")
        @topic = @tm.get!("relativ_si")
        @topic.add_si("http://example.org/absolute_si")
        @topic.add_si("test:test_prefix_si")
        @topic.add_sl("relativs_sl")
        @topic.add_sl("http://example.org/absolute_sl")
        @topic.add_sl("test:test_prefix_sl")
        @topic.add_ii("relativs_ii")
        @topic.add_ii("http://example.org/absolute_ii")
        @topic.add_ii("test:test_prefix_il")
      end
      describe "#has_si?" do
        it "shoud give back true if the topic has the given subject identifier" do
          @topic_with_si.has_si?("http://www.topicmapslab.de/uni-berlin").should be_true

        end
        describe "if the topic does not have the given identifier" do
          it "should give back false" do
            @topic_with_ii.has_si?("http://www.topicmapslab.de/uni-berlin").should be_false
            @topic_with_sl.has_si?("http://www.topicmapslab.de/uni-berlin").should be_false
          end
          describe "and also if this identifier is an item identifier" do
            it "should give back false" do
              @topic_with_ii.has_si?("http://www.topicmapslab.de/uni-leipzig").should be_false
            end
          end
          describe "and also if this identifier is a suject locator" do
            it "should give back false" do
              @topic_with_sl.has_si?("http://www.topicmapslab.de/uni-munich").should be_false
            end
          end
        end
        it "should resolve prefixes" do
          @topic.has_si?("http://example.com/test/test_prefix_si").should be_true
          @topic.has_si?("test:test_prefix_si").should be_true
        end

        it "should be tested"
      end

      describe "#has_ii?" do
        it "should be tested"
      end

      describe "#has_sl?" do
        it "should be tested"
      end

      describe "#has_type_with_si?" do
        it "should give back true if the si belongs to a type of the topic and false otherwise" do
          instance = @tm.get!("Instance")
          instance.add_type(@topic)
          instance.add_type("something")
          instance.has_type_with_si?("http://example.org/absolute_si").should be_true
          instance.has_type_with_si?("test:test_prefix_si").should be_true
          instance.has_type_with_si?("http://example.org/absolute_sl").should be_false
          instance.has_type_with_si?("http://example.org/absolute_ii").should be_false
        end
      end

      describe "#has_type_with_ii?" do
        it "should be tested"

      end

      describe "#has_type_with_sl?" do
        it "should be tested"

      end
    end
  end #of describe self
end