<s> from __future__ import absolute_import , print_function <EOL> import unittest <EOL> import tempfile <EOL> from bokeh . document import Document <EOL> from bokeh . model import Model <EOL> from bokeh . core . property_mixins import FillProps , LineProps , TextProps <EOL> from bokeh . core . properties import Int , String <EOL> from bokeh . themes import Theme <EOL> class ThemedModel ( Model ) : <EOL> number = Int ( <NUM_LIT> ) <EOL> string = String ( "<STR_LIT:hello>" ) <EOL> class SubOfThemedModel ( ThemedModel ) : <EOL> another_string = String ( "<STR_LIT>" ) <EOL> class TestThemes ( unittest . TestCase ) : <EOL> def test_construct_empty_theme_from_file ( self ) : <EOL> with ( tempfile . NamedTemporaryFile ( ) ) as file : <EOL> file . file . write ( "<STR_LIT>" . encode ( '<STR_LIT:utf-8>' ) ) <EOL> file . file . flush ( ) <EOL> theme = Theme ( filename = file . name ) <EOL> theme . apply_to_model ( ThemedModel ( ) ) <EOL> def test_construct_empty_theme_from_json ( self ) : <EOL> theme = Theme ( json = dict ( ) ) <EOL> theme . apply_to_model ( ThemedModel ( ) ) <EOL> def test_construct_no_json_or_filename ( self ) : <EOL> with self . assertRaises ( ValueError ) as manager : <EOL> Theme ( ) <EOL> self . assertTrue ( "<STR_LIT>" in repr ( manager . exception ) ) <EOL> def test_construct_json_and_filename ( self ) : <EOL> with self . assertRaises ( ValueError ) as manager : <EOL> Theme ( filename = "<STR_LIT>" , json = { } ) <EOL> self . assertTrue ( "<STR_LIT>" in repr ( manager . exception ) ) <EOL> def test_construct_bad_attrs ( self ) : <EOL> with self . assertRaises ( ValueError ) as manager : <EOL> Theme ( json = dict ( attrs = <NUM_LIT> ) ) <EOL> self . assertTrue ( "<STR_LIT>" in repr ( manager . exception ) ) <EOL> def test_construct_bad_class_props ( self ) : <EOL> with self . assertRaises ( ValueError ) as manager : <EOL> Theme ( json = dict ( attrs = dict ( SomeClass = <NUM_LIT> ) ) ) <EOL> self . assertTrue ( "<STR_LIT>" in repr ( manager . exception ) ) <EOL> def test_construct_nonempty_theme_from_file ( self ) : <EOL> with ( tempfile . NamedTemporaryFile ( ) ) as file : <EOL> file . file . write ( """<STR_LIT>""" . encode ( '<STR_LIT:utf-8>' ) ) <EOL> file . file . flush ( ) <EOL> theme = Theme ( filename = file . name ) <EOL> self . assertDictEqual ( dict ( number = <NUM_LIT> ) , theme . _for_class ( ThemedModel ) ) <EOL> self . assertDictEqual ( dict ( number = <NUM_LIT> , another_string = "<STR_LIT>" ) , theme . _for_class ( SubOfThemedModel ) ) <EOL> def test_theming_a_model ( self ) : <EOL> theme = Theme ( json = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:string>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> } ) <EOL> obj = ThemedModel ( ) <EOL> changes = dict ( calls = [ ] ) <EOL> self . assertEqual ( '<STR_LIT:hello>' , obj . string ) <EOL> def record_trigger ( attr , old , new_ ) : <EOL> changes [ '<STR_LIT>' ] . append ( ( attr , old , new_ ) ) <EOL> obj . on_change ( '<STR_LIT:string>' , record_trigger ) <EOL> theme . apply_to_model ( obj ) <EOL> self . assertEqual ( '<STR_LIT>' , obj . string ) <EOL> self . assertEqual ( [ ( '<STR_LIT:string>' , '<STR_LIT:hello>' , '<STR_LIT>' ) ] , changes [ '<STR_LIT>' ] ) <EOL> def test_theming_a_model_via_base ( self ) : <EOL> theme = Theme ( json = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:string>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> } ) <EOL> obj = SubOfThemedModel ( ) <EOL> changes = dict ( calls = [ ] ) <EOL> def record_trigger ( attr , old , new_ ) : <EOL> changes [ '<STR_LIT>' ] . append ( ( attr , old , new_ ) ) <EOL> obj . on_change ( '<STR_LIT:string>' , record_trigger ) <EOL> self . assertEqual ( '<STR_LIT:hello>' , obj . string ) <EOL> theme . apply_to_model ( obj ) <EOL> self . assertEqual ( '<STR_LIT>' , obj . string ) <EOL> self . assertEqual ( [ ( '<STR_LIT:string>' , '<STR_LIT:hello>' , '<STR_LIT>' ) ] , changes [ '<STR_LIT>' ] ) <EOL> def test_subclass_theme_used_rather_than_base ( self ) : <EOL> theme = Theme ( json = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:string>' : '<STR_LIT>' <EOL> } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:string>' : '<STR_LIT:bar>' <EOL> } <EOL> } <EOL> } ) <EOL> obj = SubOfThemedModel ( ) <EOL> self . assertEqual ( '<STR_LIT:hello>' , obj . string ) <EOL> changes = dict ( calls = [ ] ) <EOL> def record_trigger ( attr , old , new_ ) : <EOL> changes [ '<STR_LIT>' ] . append ( ( attr , old , new_ ) ) <EOL> obj . on_change ( '<STR_LIT:string>' , record_trigger ) <EOL> theme . apply_to_model ( obj ) <EOL> self . assertEqual ( '<STR_LIT:bar>' , obj . string ) <EOL> self . assertEqual ( [ ( '<STR_LIT:string>' , '<STR_LIT:hello>' , '<STR_LIT:bar>' ) ] , changes [ '<STR_LIT>' ] ) <EOL> def test_theming_a_document_after_adding_root ( self ) : <EOL> theme = Theme ( json = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:string>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> } ) <EOL> obj = ThemedModel ( ) <EOL> doc = Document ( ) <EOL> doc . add_root ( obj ) <EOL> self . assertEqual ( '<STR_LIT:hello>' , obj . string ) <EOL> changes = dict ( calls = [ ] ) <EOL> def record_trigger ( attr , old , new_ ) : <EOL> changes [ '<STR_LIT>' ] . append ( ( attr , old , new_ ) ) <EOL> obj . on_change ( '<STR_LIT:string>' , record_trigger ) <EOL> doc . theme = theme <EOL> self . assertIs ( doc . theme , theme ) <EOL> self . assertEqual ( '<STR_LIT>' , obj . string ) <EOL> doc . remove_root ( obj ) <EOL> self . assertEqual ( '<STR_LIT:hello>' , obj . string ) <EOL> self . assertEqual ( [ ( '<STR_LIT:string>' , '<STR_LIT:hello>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:string>' , '<STR_LIT>' , '<STR_LIT:hello>' ) ] , changes [ '<STR_LIT>' ] ) <EOL> def test_theming_a_document_before_adding_root ( self ) : <EOL> theme = Theme ( json = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:string>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> } ) <EOL> obj = ThemedModel ( ) <EOL> doc = Document ( ) <EOL> self . assertEqual ( '<STR_LIT:hello>' , obj . string ) <EOL> doc . theme = theme <EOL> self . assertIs ( doc . theme , theme ) <EOL> changes = dict ( calls = [ ] ) <EOL> def record_trigger ( attr , old , new_ ) : <EOL> changes [ '<STR_LIT>' ] . append ( ( attr , old , new_ ) ) <EOL> obj . on_change ( '<STR_LIT:string>' , record_trigger ) <EOL> doc . add_root ( obj ) <EOL> self . assertEqual ( '<STR_LIT>' , obj . string ) <EOL> doc . remove_root ( obj ) <EOL> self . assertEqual ( '<STR_LIT:hello>' , obj . string ) <EOL> self . assertEqual ( [ ( '<STR_LIT:string>' , '<STR_LIT:hello>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:string>' , '<STR_LIT>' , '<STR_LIT:hello>' ) ] , changes [ '<STR_LIT>' ] ) <EOL> def test_setting_document_theme_to_none ( self ) : <EOL> theme = Theme ( json = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:string>' : '<STR_LIT>' <EOL> } <EOL> } <EOL> } ) <EOL> obj = ThemedModel ( ) <EOL> doc = Document ( ) <EOL> doc . add_root ( obj ) <EOL> changes = dict ( calls = [ ] ) <EOL> def record_trigger ( attr , old , new_ ) : <EOL> changes [ '<STR_LIT>' ] . append ( ( attr , old , new_ ) ) <EOL> obj . on_change ( '<STR_LIT:string>' , record_trigger ) <EOL> doc . theme = theme <EOL> self . assertEqual ( '<STR_LIT>' , obj . string ) <EOL> doc . theme = None <EOL> self . assertIsNot ( doc . theme , None ) <EOL> self . assertEqual ( '<STR_LIT:hello>' , obj . string ) <EOL> self . assertEqual ( [ ( '<STR_LIT:string>' , '<STR_LIT:hello>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:string>' , '<STR_LIT>' , '<STR_LIT:hello>' ) ] , changes [ '<STR_LIT>' ] ) <EOL> def _compare_dict_to_model_class_defaults ( self , props , model_class ) : <EOL> model = model_class ( ) <EOL> for name , value in props . items ( ) : <EOL> property = model . lookup ( name ) <EOL> if property is None : <EOL> raise RuntimeError ( "<STR_LIT>" % ( model , name ) ) <EOL> default = property . class_default ( model_class ) <EOL> if default != value : <EOL> print ( "<STR_LIT>" % ( model_class . __name__ , name , default , value ) ) <EOL> else : <EOL> print ( "<STR_LIT>" % ( model_class . __name__ , name , default ) ) <EOL> def _compare_dict_to_model_defaults ( self , props , model_name ) : <EOL> import bokeh . models as models <EOL> import bokeh . models . widgets as widgets <EOL> if hasattr ( models , model_name ) : <EOL> self . _compare_dict_to_model_class_defaults ( props , getattr ( models , model_name ) ) <EOL> elif hasattr ( widgets , model_name ) : <EOL> self . _compare_dict_to_model_class_defaults ( props , getattr ( widgets , model_name ) ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" + model_name ) <EOL> def test_default_theme_is_empty ( self ) : <EOL> doc = Document ( ) <EOL> for class_name , props in doc . theme . _json [ '<STR_LIT>' ] . items ( ) : <EOL> self . _compare_dict_to_model_defaults ( props , class_name ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( doc . theme . _json [ '<STR_LIT>' ] ) ) <EOL> self . _compare_dict_to_model_class_defaults ( doc . theme . _fill_defaults , FillProps ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( doc . theme . _fill_defaults ) ) <EOL> self . _compare_dict_to_model_class_defaults ( doc . theme . _text_defaults , TextProps ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( doc . theme . _text_defaults ) ) <EOL> self . _compare_dict_to_model_class_defaults ( doc . theme . _line_defaults , LineProps ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( doc . theme . _line_defaults ) ) </s>
