<s> import unittest <EOL> import random <EOL> from stix . test import EntityTestCase , TypedListTestCase <EOL> from stix import common <EOL> class StructuredTextTests ( unittest . TestCase , EntityTestCase ) : <EOL> klass = common . StructuredText <EOL> _full_dict = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:value>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> class StructuredTextListTests ( unittest . TestCase , TypedListTestCase ) : <EOL> klass = common . StructuredTextList <EOL> _full_dict = [ <EOL> StructuredTextTests . _full_dict , <EOL> { <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT:value>' : '<STR_LIT>' , <EOL> '<STR_LIT:id>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> ] <EOL> @ classmethod <EOL> def _get_text_list ( cls ) : <EOL> slist = [ ] <EOL> for ordinality in xrange ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> text = common . StructuredText ( "<STR_LIT>" % ordinality ) <EOL> text . ordinality = ordinality <EOL> slist . append ( text ) <EOL> random . shuffle ( slist ) <EOL> return slist <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> slist = cls . _get_text_list ( ) <EOL> cls . slist = common . StructuredTextList ( slist ) <EOL> def test_iter ( self ) : <EOL> for idx , text in enumerate ( self . slist , <NUM_LIT:1> ) : <EOL> self . assertEqual ( text . ordinality , idx ) <EOL> def test_reset ( self ) : <EOL> ords = ( <NUM_LIT:5> , <NUM_LIT> , <NUM_LIT> ) <EOL> slist = common . StructuredTextList ( ) <EOL> for o in ords : <EOL> text = common . StructuredText ( "<STR_LIT>" % o , o ) <EOL> slist . add ( text ) <EOL> for o in ords : <EOL> self . assertEqual ( o , slist [ o ] . ordinality ) <EOL> slist . reset ( ) <EOL> for o in xrange ( <NUM_LIT:1> , len ( ords ) + <NUM_LIT:1> ) : <EOL> self . assertEqual ( o , slist [ o ] . ordinality ) <EOL> def test_ordinalities ( self ) : <EOL> all_ords = range ( <NUM_LIT:1> , <NUM_LIT:10> ) <EOL> self . assertTrue ( all ( x in self . slist . ordinalities for x in all_ords ) ) <EOL> def test_add ( self ) : <EOL> slist = common . StructuredTextList ( ) <EOL> st1 = common . StructuredText ( "<STR_LIT:foo>" , ordinality = <NUM_LIT:1> ) <EOL> st2 = common . StructuredText ( "<STR_LIT:bar>" , ordinality = <NUM_LIT:2> ) <EOL> slist . add ( st1 ) <EOL> slist . add ( st2 ) <EOL> self . assertTrue ( st1 in slist ) <EOL> self . assertTrue ( st2 in slist ) <EOL> def test_insert ( self ) : <EOL> st1 = common . StructuredText ( "<STR_LIT:foo>" , ordinality = <NUM_LIT:1> ) <EOL> st2 = common . StructuredText ( "<STR_LIT:bar>" , ordinality = <NUM_LIT:2> ) <EOL> slist = common . StructuredTextList ( st1 , st2 ) <EOL> new_st1 = common . StructuredText ( "<STR_LIT>" , ordinality = <NUM_LIT:1> ) <EOL> slist . insert ( new_st1 ) <EOL> self . assertEqual ( new_st1 . ordinality , <NUM_LIT:1> ) <EOL> self . assertEqual ( st1 . ordinality , <NUM_LIT:2> ) <EOL> self . assertEqual ( st2 . ordinality , <NUM_LIT:3> ) <EOL> def test_insert_str ( self ) : <EOL> slist = common . StructuredTextList ( "<STR_LIT:foo>" , "<STR_LIT:bar>" ) <EOL> slist . insert ( "<STR_LIT:test>" ) <EOL> self . assertEqual ( str ( slist [ <NUM_LIT:3> ] ) , "<STR_LIT:test>" ) <EOL> def test_delitem ( self ) : <EOL> slist = common . StructuredTextList ( ) <EOL> st1 = common . StructuredText ( "<STR_LIT:foo>" , ordinality = <NUM_LIT:1> ) <EOL> st2 = common . StructuredText ( "<STR_LIT:bar>" , ordinality = <NUM_LIT:2> ) <EOL> slist . add ( st1 ) <EOL> slist . add ( st2 ) <EOL> del slist [ <NUM_LIT:1> ] <EOL> self . assertTrue ( st1 not in slist ) <EOL> self . assertTrue ( st2 in slist ) <EOL> self . assertRaises ( <EOL> KeyError , <EOL> slist . __getitem__ , <EOL> <NUM_LIT:1> <EOL> ) <EOL> def test_remove ( self ) : <EOL> slist = common . StructuredTextList ( ) <EOL> st1 = common . StructuredText ( "<STR_LIT:foo>" , ordinality = <NUM_LIT:1> ) <EOL> st2 = common . StructuredText ( "<STR_LIT:bar>" , ordinality = <NUM_LIT:2> ) <EOL> slist . add ( st1 ) <EOL> slist . add ( st2 ) <EOL> slist . remove ( st1 ) <EOL> self . assertTrue ( st1 not in slist ) <EOL> self . assertTrue ( st2 in slist ) <EOL> self . assertRaises ( <EOL> KeyError , <EOL> slist . __getitem__ , <EOL> <NUM_LIT:1> <EOL> ) <EOL> def test_update ( self ) : <EOL> slist = common . StructuredTextList ( ) <EOL> st1 = common . StructuredText ( "<STR_LIT:foo>" , ordinality = <NUM_LIT:1> ) <EOL> st2 = common . StructuredText ( "<STR_LIT:bar>" , ordinality = <NUM_LIT:2> ) <EOL> slist . add ( st1 ) <EOL> slist . add ( st2 ) <EOL> new_st1 = common . StructuredText ( "<STR_LIT>" , ordinality = <NUM_LIT:1> ) <EOL> new_st2 = common . StructuredText ( "<STR_LIT>" , ordinality = <NUM_LIT:2> ) <EOL> newlist = [ new_st1 , new_st2 ] <EOL> slist . update ( newlist ) <EOL> self . assertEqual ( slist [ <NUM_LIT:1> ] , new_st1 ) <EOL> self . assertEqual ( slist [ <NUM_LIT:2> ] , new_st2 ) <EOL> self . assertTrue ( st1 not in slist ) <EOL> self . assertTrue ( st2 not in slist ) <EOL> def test_len ( self ) : <EOL> slist = common . StructuredTextList ( ) <EOL> st1 = common . StructuredText ( "<STR_LIT:foo>" , ordinality = <NUM_LIT:1> ) <EOL> st2 = common . StructuredText ( "<STR_LIT:bar>" , ordinality = <NUM_LIT:2> ) <EOL> slist . add ( st1 ) <EOL> slist . add ( st2 ) <EOL> self . assertEqual ( len ( slist ) , <NUM_LIT:2> ) <EOL> del slist [ <NUM_LIT:1> ] <EOL> self . assertEqual ( len ( slist ) , <NUM_LIT:1> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
