#!/usr/bin/python
# -*-*- encoding: utf-8 -*-*-
#
# Copyright (C) 2006 Google Inc.
#
# 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.
__author__ = 'j.s@google.com (Jeff Scudder)'
import sys
import unittest
try:
  from xml.etree import ElementTree
except ImportError:
  from elementtree import ElementTree
import atom
from gdata import test_data
import gdata.test_config as conf
class AuthorTest(unittest.TestCase):
  
  def setUp(self):
    self.author = atom.Author()
    
  def testEmptyAuthorShouldHaveEmptyExtensionsList(self):
    self.assert_(isinstance(self.author.extension_elements, list))
    self.assert_(len(self.author.extension_elements) == 0)
    
  def testNormalAuthorShouldHaveNoExtensionElements(self):
    self.author.name = atom.Name(text='Jeff Scudder')
    self.assert_(self.author.name.text == 'Jeff Scudder')
    self.assert_(len(self.author.extension_elements) == 0)
    new_author = atom.AuthorFromString(self.author.ToString())
    self.assert_(len(self.author.extension_elements) == 0)
    
    self.author.extension_elements.append(atom.ExtensionElement(
        'foo', text='bar'))
    self.assert_(len(self.author.extension_elements) == 1)
    self.assert_(self.author.name.text == 'Jeff Scudder')
    new_author = atom.AuthorFromString(self.author.ToString())
    self.assert_(len(self.author.extension_elements) == 1)
    self.assert_(new_author.name.text == 'Jeff Scudder')
  def testEmptyAuthorToAndFromStringShouldMatch(self):
    string_from_author = self.author.ToString()
    new_author = atom.AuthorFromString(string_from_author)
    string_from_new_author = new_author.ToString()
    self.assert_(string_from_author == string_from_new_author)
    
  def testAuthorWithNameToAndFromStringShouldMatch(self):
    self.author.name = atom.Name()
    self.author.name.text = 'Jeff Scudder'
    string_from_author = self.author.ToString()
    new_author = atom.AuthorFromString(string_from_author)
    string_from_new_author = new_author.ToString()
    self.assert_(string_from_author == string_from_new_author)
    self.assert_(self.author.name.text == new_author.name.text)
  
  def testExtensionElements(self):
    self.author.extension_attributes['foo1'] = 'bar'
    self.author.extension_attributes['foo2'] = 'rab'
    self.assert_(self.author.extension_attributes['foo1'] == 'bar')
    self.assert_(self.author.extension_attributes['foo2'] == 'rab')
    new_author = atom.AuthorFromString(self.author.ToString())
    self.assert_(new_author.extension_attributes['foo1'] == 'bar')
    self.assert_(new_author.extension_attributes['foo2'] == 'rab')
    
  def testConvertFullAuthorToAndFromString(self):
    author = atom.AuthorFromString(test_data.TEST_AUTHOR)
    self.assert_(author.name.text == 'John Doe')
    self.assert_(author.email.text == 'johndoes@someemailadress.com')
    self.assert_(author.uri.text == 'http://www.google.com')
    
    
class EmailTest(unittest.TestCase):
  
  def setUp(self):
    self.email = atom.Email()
    
  def testEmailToAndFromString(self):
    self.email.text = 'This is a test'
    new_email = atom.EmailFromString(self.email.ToString())
    self.assert_(self.email.text == new_email.text)
    self.assert_(self.email.extension_elements == 
        new_email.extension_elements)
    
  
class NameTest(unittest.TestCase):
  def setUp(self):
    self.name = atom.Name()
    
  def testEmptyNameToAndFromStringShouldMatch(self):
    string_from_name = self.name.ToString()
    new_name = atom.NameFromString(string_from_name)
    string_from_new_name = new_name.ToString()
    self.assert_(string_from_name == string_from_new_name)
    
  def testText(self):
    self.assert_(self.name.text is None)
    self.name.text = 'Jeff Scudder'
    self.assert_(self.name.text == 'Jeff Scudder')
    new_name = atom.NameFromString(self.name.ToString())
    self.assert_(new_name.text == self.name.text)
    
  def testExtensionElements(self):
    self.name.extension_attributes['foo'] = 'bar'
    self.assert_(self.name.extension_attributes['foo'] == 'bar')
    new_name = atom.NameFromString(self.name.ToString())
    self.assert_(new_name.extension_attributes['foo'] == 'bar')
    
    
class ExtensionElementTest(unittest.TestCase):
  
  def setUp(self):
    self.ee = atom.ExtensionElement('foo')
    
  def testEmptyEEShouldProduceEmptyString(self):
    pass
    
  def testEEParsesTreeCorrectly(self):
    deep_tree = atom.ExtensionElementFromString(test_data.EXTENSION_TREE)
    self.assert_(deep_tree.tag == 'feed')
    self.assert_(deep_tree.namespace == 'http://www.w3.org/2005/Atom')
    self.assert_(deep_tree.children[0].tag == 'author')
    self.assert_(deep_tree.children[0].namespace == 'http://www.google.com')
    self.assert_(deep_tree.children[0].children[0].tag == 'name')
    self.assert_(deep_tree.children[0].children[0].namespace == 
        'http://www.google.com')
    self.assert_(deep_tree.children[0].children[0].text.strip() == 'John Doe')
    self.assert_(deep_tree.children[0].children[0].children[0].text.strip() ==
        'Bar')
    foo = deep_tree.children[0].children[0].children[0]
    self.assert_(foo.tag == 'foo')
    self.assert_(foo.namespace == 'http://www.google.com')
    self.assert_(foo.attributes['up'] == 'down')
    self.assert_(foo.attributes['yes'] == 'no')
    self.assert_(foo.children == [])
  
  def testEEToAndFromStringShouldMatch(self):
    string_from_ee = self.ee.ToString()
    new_ee = atom.ExtensionElementFromString(string_from_ee)
    string_from_new_ee = new_ee.ToString()
    self.assert_(string_from_ee == string_from_new_ee)
    
    deep_tree = atom.ExtensionElementFromString(test_data.EXTENSION_TREE)    
    string_from_deep_tree = deep_tree.ToString()
    new_deep_tree = atom.ExtensionElementFromString(string_from_deep_tree)
    string_from_new_deep_tree = new_deep_tree.ToString()
    self.assert_(string_from_deep_tree == string_from_new_deep_tree)
    
    
class LinkTest(unittest.TestCase):
  
  def setUp(self):
    self.link = atom.Link()
    
  def testLinkToAndFromString(self):
    self.link.href = 'test href'
    self.link.hreflang = 'english'
    self.link.type = 'text/html'
    self.link.extension_attributes['foo'] = 'bar'
    self.assert_(self.link.href == 'test href')
    self.assert_(self.link.hreflang == 'english')
    self.assert_(self.link.type == 'text/html')
    self.assert_(self.link.extension_attributes['foo'] == 'bar')
    new_link = atom.LinkFromString(self.link.ToString())
    self.assert_(self.link.href == new_link.href)
    self.assert_(self.link.type == new_link.type)
    self.assert_(self.link.hreflang == new_link.hreflang)
    self.assert_(self.link.extension_attributes['foo'] == 
        new_link.extension_attributes['foo'])
  def testLinkType(self):
    test_link = atom.Link(link_type='text/html')
    self.assert_(test_link.type == 'text/html')
class GeneratorTest(unittest.TestCase):
  def setUp(self):
    self.generator = atom.Generator()
  def testGeneratorToAndFromString(self):
    self.generator.uri = 'www.google.com'
    self.generator.version = '1.0'
    self.generator.extension_attributes['foo'] = 'bar'
    self.assert_(self.generator.uri == 'www.google.com')
    self.assert_(self.generator.version == '1.0')
    self.assert_(self.generator.extension_attributes['foo'] == 'bar')
    new_generator = atom.GeneratorFromString(self.generator.ToString())
    self.assert_(self.generator.uri == new_generator.uri)
    self.assert_(self.generator.version == new_generator.version)
    self.assert_(self.generator.extension_attributes['foo'] ==
        new_generator.extension_attributes['foo'])
class TitleTest(unittest.TestCase):
  def setUp(self):
    self.title = atom.Title()
  def testTitleToAndFromString(self):
    self.title.type = 'text'
    self.title.text = 'Less: &lt;'
    self.assert_(self.title.type == 'text')
    self.assert_(self.title.text == 'Less: &lt;')
    new_title = atom.TitleFromString(self.title.ToString())
    self.assert_(self.title.type == new_title.type)
    self.assert_(self.title.text == new_title.text)
class SubtitleTest(unittest.TestCase):
  def setUp(self):
    self.subtitle = atom.Subtitle()
  def testTitleToAndFromString(self):
    self.subtitle.type = 'text'
    self.subtitle.text = 'sub & title'
    self.assert_(self.subtitle.type == 'text')
    self.assert_(self.subtitle.text == 'sub & title')
    new_subtitle = atom.SubtitleFromString(self.subtitle.ToString())
    self.assert_(self.subtitle.type == new_subtitle.type)
    self.assert_(self.subtitle.text == new_subtitle.text)
class SummaryTest(unittest.TestCase):
  def setUp(self):
    self.summary = atom.Summary()
  def testTitleToAndFromString(self):
    self.summary.type = 'text'
    self.summary.text = 'Less: &lt;'
    self.assert_(self.summary.type == 'text')
    self.assert_(self.summary.text == 'Less: &lt;')
    new_summary = atom.SummaryFromString(self.summary.ToString())
    self.assert_(self.summary.type == new_summary.type)
    self.assert_(self.summary.text == new_summary.text)
class CategoryTest(unittest.TestCase):
  def setUp(self):


Answer:     self.category = atom.Category()