// file: $isip/class/mmedia/xt_02.cc
// version: $Id: xt_02.cc 9929 2004-12-31 02:42:05Z duncan $

#include "XMLToken.h"
#include <Console.h>

// method: diagnose
//
// arguments:
//  Integral::DEBUG level: (input) debug level for diagnostics
//
// return: a boolean value indicating status
//
boolean XMLToken::diagnose(Integral::DEBUG level_a) {
  
  //---------------------------------------------------------------------------
  //
  // 0. preliminaries
  //
  //---------------------------------------------------------------------------
  
  // output the class name
  //
  if (level_a > Integral::NONE) {
    SysString output(L"diagnosing class ");
    output.concat(CLASS_NAME);
    output.concat(L": ");
    Console::put(output);
    Console::increaseIndention();
  }
  
  //--------------------------------------------------------------------------
  //
  // 1. required public methods
  //
  //--------------------------------------------------------------------------
  
  // set indentation
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing required public methods...\n");
    Console::increaseIndention();
  }
  
  // test debug methods
  //
  XMLToken xml_token;
  xml_token.setDebug(Integral::ALL);
  if(xml_token.getDebug() == Integral::ALL) {
    if(level_a > Integral::BRIEF) {
      
      Console::put(L"testing debug method...");
      Console::increaseIndention();
      xml_token.debug(L"debug");
      Console::decreaseIndention();
    }
  }
  else {
    return Error::handle(name(), L"setDebug", Error::TEST, __FILE__, __LINE__);
  }
  
  // resent indention
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  //--------------------------------------------------------------------------
  //
  // 2. class-specific public methods
  //
  //--------------------------------------------------------------------------
  
  // set indention
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing class-specific public methods...\n");
    Console::increaseIndention();
  }
  
  // declare and initialize two tokens to use for testing
  //
  XMLToken token1;
  XMLToken token2;
  Vector< Pair<String, String> > attributes;
  Long depth = 5;
  Pair <String, String> attribute(L"attribute1", L"value1");
  attributes.concat(attribute);
  token1.init(START_TAG, L"xml_tag", attributes, (long)depth);
  
  token2.init(token1.getType(), token1.getValue(),
	      token1.getAttributes(), token1.getDepth());
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing get/setType methods...");
  }
  
  if(token1.getType() != token2.getType()) {
    
    // error
    //
    return Error::handle(name(), L"get/setType",
			 Error::TEST, __FILE__, __LINE__);
  }
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing get/setValue methods...");
  }
  if(!token1.getValue().eq(token2.getValue())) {
    
    // error
    //
    return Error::handle(name(), L"get/setValue",
			 Error::TEST, __FILE__, __LINE__);
  }
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing get/setDepth methods...");
  }
  if(token1.getDepth() != token2.getDepth()) {
    
    // error
    //
    return Error::handle(name(), L"setDepth",
			 Error::TEST, __FILE__, __LINE__);
  }
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing get/set/hasAttribute methods...");
  }
  if(!token1.getAttributes().eq(token2.getAttributes())) {
    
    // error
    //
    return Error::handle(name(), L"get/set/hasAttributes",
			 Error::TEST, __FILE__, __LINE__);
  }
  
  if(!token1.hasAttributes()) {
    
    // error
    //
    return Error::handle(name(), L"get/set/hasAttributes",
			 Error::TEST, __FILE__, __LINE__);
  }
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing isA methods...");
  }
  if(!token1.isA(L"xml_tag")) {
    
    // error
    //
    return Error::handle(name(), L"isA", Error::TEST, __FILE__, __LINE__);
  }
  
  if(!token1.isA(START_TAG)) {
    
    // error
    //
    return Error::handle(name(), L"isA", Error::TEST, __FILE__, __LINE__);
  }
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing eq methods...");
  }
  if(!token2.eq(token1)) {
    
    // error
    //
    return Error::handle(name(), L"eq",
			 Error::TEST, __FILE__, __LINE__);
  }
  
  token1.setAttributeValue(L"attribute1", L"value1A"); 
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing get/setAttributeValue methods...");
  }
  if(!token1.getAttributeValue(L"attribute1").eq(L"value1A")) {
    
    // error
    //
    return Error::handle(name(), L"get/setAttributeValue",
			 Error::TEST, __FILE__, __LINE__);
  }
  
  token1.removeAttribute(L"attribute1");
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing removeAttribute method...");
  }
  if(!token1.getAttributeValue(L"attribute1").eq(String::EMPTY)) {
    
    // error
    //
    return Error::handle(name(), L"removeAttribute",
			 Error::TEST, __FILE__, __LINE__);
  }
  
  token1.addAttribute(L"attribute2", L"value2");
  
  if (level_a > Integral::BRIEF) {
    Console::put(L"testing addAttribute method...");
  }
  if(!token1.getAttributeValue(L"attribute2").eq(L"value2")) {
    
    // error
    //
    return Error::handle(name(), L"addAttribute",
			 Error::TEST, __FILE__, __LINE__);
  }
  
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  if (level_a > Integral::NONE) {
    SysString output(L"diagnostics passed for class ");
    output.concat(name());
    output.concat(L"\n");
    Console::put(output);
  }
  
  // indicate success
  //
  return true;
}
