class ScriptTest(unittest.TestCase): def setUp(self): test_module.configure() self._runner = ScriptRunner(self) def test_script(self): script = 'if {1 and 2 < 3} set all' self._runner.run_script(script) lifx = provide(i_controller.Lifx) for light in lifx.get_lights(): print(light.get_label(), light.call_list())
class ScriptTest(unittest.TestCase): def setUp(self): test_module.configure() self._runner = ScriptRunner(self) def test_script(self): script = """ repeat while {brightness < 50.0} begin brightness {brightness + 5} set all end """ self._runner.run_script(script) lifx = provide(i_controller.Lifx) for light in lifx.get_lights(): print(light.get_label(), light.call_list())
class PrintTest(unittest.TestCase): def setUp(self): test_module.configure() self._runner = ScriptRunner(self) @patch('builtins.print') def test_print(self, print_fn): script = 'print "hello"' self._runner.run_script(script) print_fn.assert_called_with('hello', end=' ') @patch('builtins.print') def test_println(self, print_fn): script = 'println "hello"' self._runner.run_script(script) print_fn.assert_called_with('hello', end='\n') @patch('builtins.print') def test_printf(self, print_fn): script = """ hue 456 printf "{} {hue}" 123 """ self._runner.run_script(script) print_fn.assert_called_with('123 456')
def setUp(self): test_module.configure() self._runner = ScriptRunner(self)
class DefineTest(unittest.TestCase): def setUp(self): test_module.configure() self._runner = ScriptRunner(self) def test_define_operand(self): script = """ units raw define light_name "Top" hue 1 saturation 2 brightness 3 kelvin 4 duration 5 set light_name on light_name """ self._runner.test_code(script, 'Top', [(Action.SET_COLOR, ([1, 2, 3, 4], 5)), (Action.SET_POWER, (65535, 5.0))]) def test_define_value(self): script = """ units raw define x 500 hue 1 saturation 2 brightness 3 kelvin 4 duration x time x set "Top" """ self._runner.test_code(script, 'Top', [(Action.SET_COLOR, ([1, 2, 3, 4], 500))]) def test_assign_registers(self): script = """ assign y 0 hue 10 assign y hue hue y set "Top" brightness 20 assign z {brightness * 5} hue z set "Top" """ self._runner.test_code(script, 'Top', [(Action.SET_COLOR, ([1820, 0, 0, 0], 0)), (Action.SET_COLOR, ([18204, 0, 13107, 0], 0))]) def test_nested_define(self): script = """ units raw define x 500 define y x hue 1 saturation 2 brightness 3 kelvin 4 duration y set "Top" """ self._runner.test_code(script, 'Top', [(Action.SET_COLOR, ([1, 2, 3, 4], 500))]) def test_routine_get_zone(self): script = """ units raw define get_z with x z get x zone z get_z "Strip" 5 """ self._runner.test_code(script, 'Strip', [(Action.GET_ZONE_COLOR, (5, 6))]) def test_define_zones(self): script = """ units raw hue 50 saturation 100 brightness 150 kelvin 200 duration 789 define z1 0 define z2 5 define light "Strip" set light zone z1 z2 set light zone z2 """ self._runner.test_code(script, 'Strip', [(Action.SET_ZONE_COLOR, (0, 6, [50, 100, 150, 200], 789)), (Action.SET_ZONE_COLOR, (5, 6, [50, 100, 150, 200], 789))]) def test_simple_routine(self): script = """ units raw hue 100 saturation 10 brightness 1 kelvin 1000 define do_set with light_name set light_name do_set "Table" """ self._runner.test_code(script, 'Table', [(Action.SET_COLOR, ([100, 10, 1, 1000], 0))]) def test_compound_routine(self): script = """ units raw hue 500 saturation 50 brightness 5 kelvin 5000 define do_set with light1 light2 the_hue begin hue the_hue set light1 and light2 end do_set "Table" "Bottom" 600 units logical """ self._runner.test_code(script, ('Table', 'Bottom'), [(Action.SET_COLOR, ([600, 50, 5, 5000], 0))]) def test_nested_param(self): script = """ units raw define inner with inner_hue hue inner_hue define outer with outer_hue inner outer_hue define outer_outer with outer_hue outer outer_hue hue 600 saturation 60 brightness 6 kelvin 6000 set "Table" outer_outer 700 set "Table" """ self._runner.test_code(script, 'Table', [(Action.SET_COLOR, ([600, 60, 6, 6000], 0)), (Action.SET_COLOR, ([700, 60, 6, 6000], 0))]) def test_variables(self): script = """ units raw saturation 1 brightness 2 kelvin 3 assign x 5 hue x set "Table" assign name "Chair" set name assign name2 name set name2 define use_var with x begin assign y x hue y set name end use_var 10 assign z 100 use_var z brightness z set "Chair" """ self._runner.run_script(script) self._runner.check_call_list('Table', [(Action.SET_COLOR, ([5, 1, 2, 3], 0))]) self._runner.check_call_list('Chair', [(Action.SET_COLOR, ([5, 1, 2, 3], 0)), (Action.SET_COLOR, ([5, 1, 2, 3], 0)), (Action.SET_COLOR, ([10, 1, 2, 3], 0)), (Action.SET_COLOR, ([100, 1, 2, 3], 0)), (Action.SET_COLOR, ([100, 1, 100, 3], 0))]) def test_nested_variables(self): script = """ units raw hue 1 saturation 2 brightness 3 kelvin 4 assign n 50 define inner1 with x begin kelvin x set "Chair" end define inner2 with y begin saturation y inner1 n end define outer1 with z inner2 z outer1 7500 """ self._runner.run_script(script) self._runner.check_call_list('Chair', [(Action.SET_COLOR, ([1, 7500, 3, 50], 0))]) def test_nested_assignment(self): script = """ # Assign to global variable inside routine units raw assign y 100 define set_global begin assign y 50 end hue y set "Top" set_global hue y set "Top" """ self._runner.run_script(script) self._runner.check_call_list('Top', [(Action.SET_COLOR, ([100, 0, 0, 0], 0)), (Action.SET_COLOR, ([50, 0, 0, 0], 0))]) def test_simple_routines(self): script = """ hue 180 saturation 50 brightness 50 duration 100 time 0 kelvin 0 define simple_no_params kelvin 50 simple_no_params set all define simple_one_param with x kelvin x simple_one_param 1000 set all hue 0 saturation 25 brightness 75 kelvin 100 define simple_three_params with x y z set x and y and z simple_three_params "Top" "Middle" "Bottom" """ self._runner.run_script(script) self._runner.check_global_call_list([ (Action.SET_COLOR, ([32768, 32768, 32768, 50], 100000)), (Action.SET_COLOR, ([32768, 32768, 32768, 1000], 100000)) ]) self._runner.check_call_list( ('Top', 'Middle', 'Bottom'), [(Action.SET_COLOR, ([0, 16384, 49151, 100], 100000))]) def test_complex_routines(self): script = """ hue 180 saturation 50 brightness 50 duration 100 time 0 kelvin 0 define complex_no_params begin kelvin 75 brightness 100 end complex_no_params set all define complex_one_param with x begin kelvin x end complex_one_param 85 set all hue 90 saturation 75 brightness 33.33 define complex_three_params with x y z begin set x set y set z end complex_three_params "Bottom" "Middle" "Top" """ self._runner.run_script(script) self._runner.check_global_call_list([ (Action.SET_COLOR, ([32768, 32768, 65535, 75], 100000)), (Action.SET_COLOR, ([32768, 32768, 65535, 85], 100000)) ]) self._runner.check_call_list( ('Top', 'Middle', 'Bottom'), [(Action.SET_COLOR, ([16384, 49151, 21843, 85], 100000))]) def test_routine_namespace(self): script = """ hue 180 saturation 50 brightness 75 duration 100 time 0 kelvin 0 define simple_no_params kelvin 75 define simple_1_param with x kelvin x define use_1_param with x simple_1_param x use_1_param 90 set all define set_2 with x y set x and y hue 90 saturation 75 brightness 33.33 kelvin 85 define simple_2_params with x y set_2 x y define use_2_params simple_2_params "Top" "Middle" use_2_params """ self._runner.run_script(script) self._runner.check_global_call_list([ (Action.SET_COLOR, ([32768, 32768, 49151, 90], 100000)) ]) self._runner.check_call_list( ('Top', 'Middle'), [(Action.SET_COLOR, ([16384, 49151, 21843, 85], 100000))])
class EndToEndTest(unittest.TestCase): def setUp(self): test_module.configure() self._runner = ScriptRunner(self) def test_individual(self): script = """ units raw hue 11 saturation 22 brightness 33 kelvin 2500 set "Top" hue 44 saturation 55 brightness 66 set "Bottom" """ self._runner.run_script(script) lifx = provide(i_controller.Lifx) for light in lifx.get_lights(): if light.get_label() == 'Top': expected = [(Action.SET_COLOR, ([11, 22, 33, 2500], 0))] elif light.get_label() == 'Bottom': expected = [(Action.SET_COLOR, ([44, 55, 66, 2500], 0))] else: expected = [] self.assertListEqual(light.get_call_list(), expected) def test_power(self): script = 'on "Top" off "Bottom"' self._runner.run_script(script) lifx = provide(i_controller.Lifx) for light in lifx.get_lights(): if light.get_label() == "Top": expected = [(Action.SET_POWER, (65535, 0))] elif light.get_label() == "Bottom": expected = [(Action.SET_POWER, (0, 0))] else: expected = [] self.assertListEqual(light.get_call_list(), expected) def test_and(self): script = """ units raw hue 1 saturation 2 brightness 3 kelvin 4 duration 5 set "Bottom" and "Top" and "Middle" """ self._runner.run_script(script) lifx = provide(i_controller.Lifx) for light in lifx.get_lights(): if light.get_label() in ('Bottom', 'Middle', 'Top'): expected = [(Action.SET_COLOR, ([1, 2, 3, 4], 5))] else: expected = [] self.assertListEqual(light.get_call_list(), expected) def test_mixed_and(self): script = """ units raw hue 10 saturation 20 brightness 30 kelvin 40 duration 50 set "Table" and group "Pole" """ self._runner.test_code(script, ('Top', 'Middle', 'Bottom', 'Table'), [(Action.SET_COLOR, ([10, 20, 30, 40], 50))]) def test_routine_get_zone(self): script = """ units raw define get_z with x z get x zone z get_z "Strip" 5 """ self._runner.test_code(script, 'Strip', [(Action.GET_ZONE_COLOR, (5, 6))]) def test_group(self): script = """ units raw hue 100 saturation 10 brightness 1 kelvin 1000 set group "Pole" on group "Furniture" """ self._runner.run_script(script) self._runner.check_call_list( ('Top', 'Middle', 'Bottom'), [(Action.SET_COLOR, ([100, 10, 1, 1000], 0))]) self._runner.check_call_list(('Table', 'Chair', 'Strip'), [(Action.SET_POWER, (65535, 0))]) def test_location(self): script = """ units raw hue 100 saturation 10 brightness 1 kelvin 1000 set location "Home" on location "Home" """ self._runner.test_code_all(script, [(Action.SET_COLOR, ([100, 10, 1, 1000], 0)), (Action.SET_POWER, (65535, 0))]) def test_expression(self): script = """ assign x {5 + 6} """ self._runner.run_script(script) def test_if_expr(self): script = """ units raw hue 1 saturation 2 brightness 3 kelvin 4 duration 5 assign x 10 assign y 20 if {8 >= 7} hue 5 if {9 <= 10} begin saturation 55 end if {8 > 7} brightness 555 if {not x > y} kelvin 5555 if {8 != 9} duration {50 / 5} if {100 == 10 * 10} set "Top" """ self._runner.test_code(script, 'Top', [(Action.SET_COLOR, ([5, 55, 555, 5555], 10.0))]) def test_if_else(self): script = """ units raw hue 1 saturation 2 brightness 3 kelvin 4 assign five 5 assign two 2 if {five < two} hue 0 else hue 1000 set "Top" if {five < two} begin hue 0 end else begin hue 2000 end set "Top" if {five < two} hue 0 else begin hue 3000 end set "Top" if {five > two} begin hue 4000 end else hue 0 set "Top" if {five > two} begin hue 5000 end else hue 0 set "Top" """ self._runner.test_code(script, 'Top', [(Action.SET_COLOR, ([1000, 2, 3, 4], 0.0)), (Action.SET_COLOR, ([2000, 2, 3, 4], 0.0)), (Action.SET_COLOR, ([3000, 2, 3, 4], 0.0)), (Action.SET_COLOR, ([4000, 2, 3, 4], 0.0)), (Action.SET_COLOR, ([5000, 2, 3, 4], 0.0))]) def test_multiple_get_logical(self): script = """ duration 1 hue 30 saturation 75 brightness 100 set "Top" time 1 hue 60 set all get "Top" hue 30 set all get "Top" hue 60 set all get "Top" hue 30 set all units raw brightness 10000 units logical saturation 50 set all """ self._runner.run_script(script) self._runner.check_call_list( 'Top', [(Action.SET_COLOR, ([5461, 49151, 65535, 0], 1000)), (Action.GET_COLOR, ([10922, 49151, 65535, 0])), (Action.GET_COLOR, ([5461, 49151, 65535, 0])), (Action.GET_COLOR, ([10922, 49151, 65535, 0]))]) self._runner.check_global_call_list([ (Action.SET_COLOR, ([10922, 49151, 65535, 0], 1000)), (Action.SET_COLOR, ([5461, 49151, 65535, 0], 1000)), (Action.SET_COLOR, ([10922, 49151, 65535, 0], 1000)), (Action.SET_COLOR, ([5461, 49151, 65535, 0], 1000)), (Action.SET_COLOR, ([5461, 32768, 10000, 0], 1000)) ]) def test_multiple_get_raw(self): script = """ units raw duration 1 hue 1000 saturation 2000 brightness 5000 set "Top" time 1 hue 3000 set all get "Top" hue 4000 set all get "Top" hue 3000 set all get "Top" hue 4000 set all units logical brightness 50 units raw hue 6000 set all """ self._runner.run_script(script) self._runner.check_call_list( 'Top', [(Action.SET_COLOR, ([1000, 2000, 5000, 0], 1)), (Action.GET_COLOR, ([3000, 2000, 5000, 0])), (Action.GET_COLOR, ([4000, 2000, 5000, 0])), (Action.GET_COLOR, ([3000, 2000, 5000, 0]))]) self._runner.check_global_call_list([ (Action.SET_COLOR, ([3000, 2000, 5000, 0], 1)), (Action.SET_COLOR, ([4000, 2000, 5000, 0], 1)), (Action.SET_COLOR, ([3000, 2000, 5000, 0], 1)), (Action.SET_COLOR, ([4000, 2000, 5000, 0], 1)), (Action.SET_COLOR, ([6000, 2000, 32768, 0], 1)) ])
def setUp(self): test_module.configure() fake_lifx.using_small_set().configure() light_set.configure() self._runner = ScriptRunner(self)
class LoopTest(unittest.TestCase): """ repeat-loop syntax: repeat [(all | in <light_list>) as <light_name>] [with <numeric_var> (from <start> to <end> | cycle [<start>)] light list syntax: <light_name> | group <group_name> | location <location_name> [and <light_list>] """ def setUp(self): test_module.configure() fake_lifx.using_small_set().configure() light_set.configure() self._runner = ScriptRunner(self) def test_all(self): script = """ hue 180 saturation 50 brightness 50 kelvin 1000 repeat all as the_light set the_light repeat all as the_light with brt from 0 to 100 begin brightness brt set the_light end repeat all as a_light with the_hue cycle begin hue the_hue set a_light end """ self._runner.run_script(script) self._runner.check_call_list( 'light_0', [(Action.SET_COLOR, ([32768, 32768, 32768, 1000], 0)), (Action.SET_COLOR, ([32768, 32768, 0, 1000], 0)), (Action.SET_COLOR, ([0, 32768, 65535, 1000], 0))]) self._runner.check_call_list( 'light_1', [(Action.SET_COLOR, ([32768, 32768, 32768, 1000], 0)), (Action.SET_COLOR, ([32768, 32768, 32768, 1000], 0)), (Action.SET_COLOR, ([21845, 32768, 65535, 1000], 0))]) self._runner.check_call_list( 'light_2', [(Action.SET_COLOR, ([32768, 32768, 32768, 1000], 0)), (Action.SET_COLOR, ([32768, 32768, 65535, 1000], 0)), (Action.SET_COLOR, ([43690, 32768, 65535, 1000], 0))]) def test_bare_list(self): script = """ hue 45 saturation 25 brightness 75 kelvin 2000 duration 9 define light_0 "light_0" assign light_1 "light_1" repeat in light_0 and light_1 and "light_2" as the_light set the_light """ self._runner.run_script(script) self._runner.check_call_list( ('light_0', 'light_1', 'light_2'), (Action.SET_COLOR, ([8192, 16384, 49151, 2000], 9000))) def test_list_range(self): script = """ hue 180 saturation 50 brightness 50 kelvin 1000 define l0 "light_0" assign l1 "light_1" repeat in l0 and l1 and "light_2" as the_light with brt from {saturation - brightness} to {hue - brightness - 30} begin brightness brt set the_light end """ self._runner.run_script(script) self._runner.check_call_list('light_0', (Action.SET_COLOR, ([32768, 32768, 0, 1000], 0))) self._runner.check_call_list('light_1', (Action.SET_COLOR, ([32768, 32768, 32768, 1000], 0))) self._runner.check_call_list('light_2', (Action.SET_COLOR, ([32768, 32768, 65535, 1000], 0))) def test_list_cycle(self): script = """ hue 180 saturation 50 brightness 50 kelvin 1000 repeat in "light_0" and "light_1" and "light_2" as the_light with the_hue cycle begin hue the_hue set the_light end """ self._runner.run_script(script) self._runner.check_call_list('light_0', (Action.SET_COLOR, ([0, 32768, 32768, 1000], 0))) self._runner.check_call_list('light_1', (Action.SET_COLOR, ([21845, 32768, 32768, 1000], 0))) self._runner.check_call_list('light_2', (Action.SET_COLOR, ([43690, 32768, 32768, 1000], 0))) def test_const_count(self): script = """ hue 180 saturation 50 brightness 50 kelvin 1000 repeat 4 with brt from 0 to 100 begin brightness brt set all end """ self._runner.run_script(script) self._runner.check_global_call_list([ (Action.SET_COLOR, ([32768, 32768, 0, 1000], 0)), (Action.SET_COLOR, ([32768, 32768, 21845, 1000], 0)), (Action.SET_COLOR, ([32768, 32768, 43690, 1000], 0)), (Action.SET_COLOR, ([32768, 32768, 65535, 1000], 0)) ]) def test_expr_count(self): script = """ assign x 16 define y 5 assign z 10 assign thou 1000 hue {36 * y} saturation {y * z} brightness {50 * 1} kelvin thou repeat {16 / (y - 1)} with brt from {-z+z} to {thou / 10} begin brightness brt set all end """ self._runner.run_script(script) self._runner.check_global_call_list([ (Action.SET_COLOR, ([32768, 32768, 0, 1000], 0)), (Action.SET_COLOR, ([32768, 32768, 21845, 1000], 0)), (Action.SET_COLOR, ([32768, 32768, 43690, 1000], 0)), (Action.SET_COLOR, ([32768, 32768, 65535, 1000], 0)) ]) def test_cycle_count(self): script = """ repeat 5 with the_hue cycle 180 begin hue the_hue set all end """ self._runner.run_script(script) self._runner.check_global_call_list([ (Action.SET_COLOR, ([32768, 0, 0, 0], 0)), (Action.SET_COLOR, ([45874, 0, 0, 0], 0)), (Action.SET_COLOR, ([58982, 0, 0, 0], 0)), (Action.SET_COLOR, ([6554, 0, 0, 0], 0)), (Action.SET_COLOR, ([19660, 0, 0, 0], 0)) ]) def test_nested_cycle(self): script = """ saturation 90 brightness 75 kelvin 2700 repeat 5 with base_hue cycle begin time 0 repeat all as the_light with the_hue cycle base_hue begin hue the_hue set the_light end time 3 wait end """ self._runner.run_script(script) def test_while(self): script = """ hue 180 saturation 10 brightness 10 kelvin 500 assign y 0 define x 100 repeat while {y < 4 and x == 100} begin set all assign y {y + 1} end """ self._runner.run_script(script) self._runner.check_global_call_list( [(Action.SET_COLOR, ([32768, 6554, 6554, 500], 0))] * 4) def test_bare_with(self): script = """ units raw repeat with i from 3 to 1 begin hue i set all end repeat with i from 1 to 3 begin hue i set all end """ self._runner.run_script(script) self._runner.check_global_call_list([ (Action.SET_COLOR, ([3, 0, 0, 0], 0)), (Action.SET_COLOR, ([2, 0, 0, 0], 0)), (Action.SET_COLOR, ([1, 0, 0, 0], 0)), (Action.SET_COLOR, ([1, 0, 0, 0], 0)), (Action.SET_COLOR, ([2, 0, 0, 0], 0)), (Action.SET_COLOR, ([3, 0, 0, 0], 0)) ]) def test_group(self): script = """ hue 180 saturation 50 brightness 50 kelvin 1000 repeat in group "group" as the_light with brt from 100 to 0 begin brightness brt set the_light end """ self._runner.run_script(script) self._runner.check_call_list('light_0', (Action.SET_COLOR, ([32768, 32768, 65535, 1000], 0))) self._runner.check_call_list('light_2', (Action.SET_COLOR, ([32768, 32768, 0, 1000], 0))) def test_group_cycle(self): script = """ saturation 75 brightness 25 kelvin 1234 repeat in group "group" as the_light with the_hue cycle begin hue the_hue set the_light end """ self._runner.run_script(script) self._runner.check_call_list('light_0', (Action.SET_COLOR, ([0, 49151, 16384, 1234], 0))) self._runner.check_call_list('light_2', (Action.SET_COLOR, ([32768, 49151, 16384, 1234], 0))) def test_group_no_with(self): script = """ hue 90 saturation 50 brightness 75 kelvin 2000 repeat in group "group" as the_light set the_light """ self._runner.run_script(script) self._runner.check_call_list('light_0', (Action.SET_COLOR, ([16384, 32768, 49151, 2000], 0))) self._runner.check_call_list('light_2', (Action.SET_COLOR, ([16384, 32768, 49151, 2000], 0))) def test_all_groups(self): script = """ hue 180 saturation 50 brightness 50 kelvin 1000 repeat group as grp with brt from 0 to 100 begin brightness brt set group grp end """ fake_lifx.using_large_set().configure() light_set.configure() self._runner.run_script(script) self._runner.check_call_list( ('Chair', 'Strip', 'Table'), (Action.SET_COLOR, ([32768, 32768, 0, 1000], 0))) self._runner.check_call_list( ('Bottom', 'Middle', 'Top'), (Action.SET_COLOR, ([32768, 32768, 65535, 1000], 0))) def test_all_locations(self): script = """ hue 180 saturation 50 brightness 50 kelvin 1000 repeat location as loc with brt from 0 to 100 begin brightness brt set location loc end """ self._runner.run_script(script) self._runner.check_call_list( ('light_0', 'light_2'), (Action.SET_COLOR, ([32768, 32768, 0, 1000], 0))) self._runner.check_call_list('light_1', (Action.SET_COLOR, ([32768, 32768, 65535, 1000], 0))) def test_mixture(self): fake_lifx.using_large_set().configure() light_set.configure() script = """ hue 180 saturation 50 brightness 50 kelvin 1000 repeat in "Table" and group "Pole" and "Chair" and location "Home" as the_light begin set the_light end """ self._runner.run_script(script) self._runner.check_call_list( ('Top', 'Middle', 'Bottom', 'Table', 'Chair'), [(Action.SET_COLOR, ([32768, 32768, 32768, 1000], 0))] * 2) def test_break(self): light_set.configure() script = """ units raw hue 100 saturation 200 brightness 300 kelvin 400 assign i 0 repeat begin if {i >= 2} break hue i set all assign i {i + 1} end """ self._runner.run_script(script) self._runner.check_global_call_list([ (Action.SET_COLOR, ([0, 200, 300, 400], 0.0)), (Action.SET_COLOR, ([1, 200, 300, 400], 0.0)) ]) def test_nested_break(self): light_set.configure() script = """ units raw hue 100 saturation 200 brightness 300 kelvin 400 assign i 0 repeat begin if {i >= 2} break hue i set all assign i {i + 1} assign j 1000 repeat begin if {j > 1002} break hue j set all assign j {j + 1} end end """ self._runner.run_script(script) self._runner.check_global_call_list([ (Action.SET_COLOR, ([0, 200, 300, 400], 0.0)), (Action.SET_COLOR, ([1000, 200, 300, 400], 0.0)), (Action.SET_COLOR, ([1001, 200, 300, 400], 0.0)), (Action.SET_COLOR, ([1002, 200, 300, 400], 0.0)), (Action.SET_COLOR, ([1, 200, 300, 400], 0.0)), (Action.SET_COLOR, ([1000, 200, 300, 400], 0.0)), (Action.SET_COLOR, ([1001, 200, 300, 400], 0.0)), (Action.SET_COLOR, ([1002, 200, 300, 400], 0.0)) ]) def test_while_break(self): script = """ assign y 0 units raw hue 180 saturation 190 brightness 200 kelvin 210 repeat while {y < 4} begin set all if {y == 2} break assign y {y + 1} end """ self._runner.run_script(script) self._runner.check_global_call_list( [(Action.SET_COLOR, ([180, 190, 200, 210], 0.0))] * 3) def test_count_break(self): script = """ units raw hue 180 saturation 190 brightness 200 kelvin 210 repeat with i from 0 to 10 begin set all if {i == 2} break end """ self._runner.run_script(script) self._runner.check_global_call_list( [(Action.SET_COLOR, ([180, 190, 200, 210], 0.0))] * 3) def test_list_break(self): script = """ units raw hue 500 saturation 600 brightness 700 kelvin 800 duration 900 assign i 0 repeat in "light_0" and "light_1" and "light_2" as the_light begin if {i == 2} break set the_light assign i {i + 1} end """ self._runner.run_script(script) self._runner.check_call_list( ('light_0', 'light_1'), [(Action.SET_COLOR, ([500, 600, 700, 800], 900))]) self._runner.check_no_others('light_0', 'light_1') def test_bad_break(self): script = "hue 5 saturation 6 break set all" parser = Parser() self.assertFalse(parser.parse(script)) self.assertEqual(parser.get_errors(), 'Line 1: Encountered "break" not inside loop.\n')
class ExampleTest(unittest.TestCase): def setUp(self): test_module.configure() self._runner = ScriptRunner(self) def test_individual(self): script = """ units raw hue 120 saturation 100 brightness 75 kelvin 2700 set "Table" """ self._runner.test_code(script, ('Table'), [(Action.SET_COLOR, ([120, 100, 75, 2700], 0))]) def test_multizone(self): script = """ units raw hue 150 saturation 100 brightness 50 kelvin 2700 duration 1.5 set "Strip" set "Strip" zone 5 set "Strip" zone 0 8 """ self._runner.test_code( script, 'Strip', [(Action.SET_COLOR, ([150, 100, 50, 2700], 1.5)), (Action.SET_ZONE_COLOR, (5, 6, [150, 100, 50, 2700], 1.5)), (Action.SET_ZONE_COLOR, (0, 9, [150, 100, 50, 2700], 1.5))]) def test_and(self): script = """ units raw hue 120 saturation 75 brightness 75 kelvin 2700 duration 1.5 set "Strip" zone 0 5 and "Table" """ self._runner.run_script(script) self._runner.check_call_list('Strip', [ (Action.SET_ZONE_COLOR, (0, 6, [120, 75, 75, 2700], 1.5)), ]) self._runner.check_call_list('Table', [(Action.SET_COLOR, ([120, 75, 75, 2700], 1.5))]) def test_group(self): script = """ units raw on location "Home" hue 120 saturation 80 brightness 75 kelvin 2700 set location "Home" """ self._runner.test_code_all(script, [(Action.SET_POWER, (65535, 0)), (Action.SET_COLOR, ([120, 80, 75, 2700], 0))]) def test_macro_definition(self): script = """ units raw define my_light "Chair" hue 120 saturation 80 brightness 50 kelvin 2700 set my_light define zone_1 5 define zone_2 10 set "Strip" zone zone_1 zone_2 """ self._runner.run_script(script) self._runner.check_call_list('Chair', [(Action.SET_COLOR, ([120, 80, 50, 2700], 0))]) self._runner.check_call_list('Strip', [ (Action.SET_ZONE_COLOR, (5, 11, [120, 80, 50, 2700], 0)), ])
class ExprTest(unittest.TestCase): def setUp(self): test_module.configure() self._runner = ScriptRunner(self) def _verify_vars(self, *pairs): it = iter(pairs) for pair in it: self._runner.assert_var_equal(pair, next(it)) def test_cmp_and(self): script = """ hue 0 if {1 <= 2 and not 3 < 4} assign x 5 else assign x 6 hue x """ self._runner.run_script(script) self._runner.assert_reg_equal(Register.HUE, 6) self._runner.assert_var_equal('x', 6) def test_prec(self): script = """ assign a {12 + 3 * 4} assign b {15 - 4 / 2} assign c {5 + 6^3*2} assign d {2 ^ 1 + 6} assign e {2 ^ (1 + 6)} """ self._runner.run_script(script) self._verify_vars('a', 24, 'b', 13, 'c', 437, 'd', 8, 'e', 128) def test_and_or_prec(self): script = """ assign a 20 assign b 0 assign c 0 if {5 > 1 or 10 < 100 and a == 30 } assign b 1 if {(5 > 1 or 10 < 100) and a == 30 } assign c 1 """ self._runner.run_script(script) self._verify_vars('b', 1, 'c', 0) def test_paren(self): script = """ assign a {(12 + 3) * 4} assign b {(16 - 4) / 2} assign c {(5 + 6)^3*2} """ self._runner.run_script(script) self._verify_vars('a', 60, 'b', 6, 'c', 2662) def test_nested_paren(self): script = """ assign a {(12 + (3*4)) * (4 + 5)} assign b {(16 - 4) / ((2))} assign c {(5 + 6)^((3)*2)} """ self._runner.run_script(script) self._verify_vars('a', 216, 'b', 6, 'c', 1771561) def test_unary(self): script = """ assign a {2-2 + -3 - -4} assign b {-(35 - 7)} assign c {+(-100 + 45)} assign d {-1 * -1} assign e {-2.0^-3} assign f {-2.0^+3} """ self._runner.run_script(script) self._verify_vars('a', 1, 'b', -28, 'c', -55, 'd', 1, 'e', -0.125, 'f', -8.0) def test_var_reference(self): script = """ assign a 25 assign b 3 assign c {a * 2 + 180 / b} """ self._runner.run_script(script) self._verify_vars('c', 110) def test_divide_by_zero(self): memory_handler = handlers.MemoryHandler(256) root_logger = logging.getLogger() root_logger.handlers = [] root_logger.addHandler(memory_handler) script = """ assign a {1 / 0} """ self._runner.run_script(script) self.assertTrue('division by zero' in memory_handler.buffer[0].msg)