def test_plot_undefined_axis_no_source(self): """ Should return without error """ print("Testing undefined variable in Axis - no source") program = simple_plot_example() e = Evaluator(graphics=False) code, err = e.evaluate(program, duration=4000) self.assertEqual(0, code)
def test_invalid_host_no_trigger(self): e = Evaluator() p = ast.Program( ast.Body([ ast.Loader(ast.Var("source"), ast.Source("https://covid-apiiii.com/api/reports")), ])) code, err = e.evaluate(p, duration=4000) self.assertNotEqual(0, code) self.assertTrue(isinstance(err, DataLoaderError))
def test_plot_manual_define(self): """ Should successfully create a plot and update with a constant value """ print("Testing hard-coded variable value") program = simple_plot_example() e = Evaluator(graphics=False) e.env.extend('t', values.FloatValue(0.5)) code, err = e.evaluate(program, duration=7000) self.assertEqual(0, code)
def test_duplicate_name(self): e = Evaluator() p = ast.Program( ast.Body([ ast.Loader(ast.Var("source"), ast.Source("www.source.com")), ast.Loader(ast.Var("source"), ast.Source("www.source2.com")), ])) code, err = e.evaluate(p, duration=4000) self.assertNotEqual(0, code) self.assertTrue(isinstance(err, DataLoaderError))
def test_simple_func_baseline(self): e = Evaluator() p = ast.Program( ast.Body([ ast.Loader(ast.Var("source"), ast.Source("https://covid-api.com/api/reports")), ast.Assigner(ast.Declare(ast.Type(Types.NUMBER), ast.Var("count")), ast.Value(IntegerValue(20))), ast.Trigger(ast.Var("source"), ast.MathFuncs([])), ])) code, err = e.evaluate(p, duration=7000) self.assertEqual(0, code) self.assertEqual(20, cast(IntegerValue, e.env.get_val("count")).value)
def test_valid_source(self): e = Evaluator() p = ast.Program( ast.Body([ ast.Loader(ast.Var("source"), ast.Source("https://covid-api.com/api/reports")), ast.Assigner(ast.Declare(ast.Type(Types.NUMBER), ast.Var("count")), ast.Value(IntegerValue(0))), ast.Trigger(ast.Var("source"), ast.MathFuncs([ast.Increment(ast.Var( "count"))])), ])) code, err = e.evaluate(p, duration=4000) self.assertEqual(0, code)
def test_pow_overflow(self): e = Evaluator() p = ast.Program( ast.Body([ ast.Loader(ast.Var("source"), ast.Source("https://covid-api.com/api/reports")), ast.Assigner(ast.Declare(ast.Type(Types.NUMBER), ast.Var("count")), ast.Value(FloatValue(1.1))), ast.Trigger(ast.Var("source"), ast.MathFuncs([ast.SimpleFunc( ast.Var("count"), ast.Operand(ConcreteNumOp.EXP), ast.Value(IntegerValue(2)))])), ])) code, err = e.evaluate(p, duration=10000) self.assertNotEqual(0, code)
def run_program(filename: str, graphics=False, duration=5000): try: return read_program_file( filename, lambda c: process( c, Evaluator(graphics=graphics), duration=duration)) except FileNotFoundError as e: print("\nERROR: Could not read file '%s'\n" % filename) return 3, e
def process(content: str, e: Evaluator, duration: int): try: t = Tokenizer(content) t.tokenize() p = Parser(t).parseProgram() return e.evaluate(p, duration=duration) except (TokenizationError, ParseError) as e: print("\nERROR: %s\n" % str(e)) return 2, e
def test(self): content = "NOT A REAL PROGRAM" try: t = Tokenizer(content) t.tokenize() program: ast.Program = Parser(t).parseProgram() result: int = Evaluator(graphics=False).evaluate(program, duration=5000) self.fail() except TokenizationError: pass
def test_plot_undefined_axis(self): """ Should briefly create a plot then error """ print("Testing undefined variable in Axis") program = ast.Program( ast.Body([ ast.Loader(ast.Var("source"), ast.Source("https://covid-api.com/api/reports")), ast.Mapper( ast.Var("source"), "confirmed", ast.Declare(ast.Type(Types.NUMBER), ast.Var("confirmed"))), ast.Plotter( ast.Graph(ScatterXYGraph()), ast.VarAxis(ast.Var("t")), ast.FuncAxis(ast.BuiltinFunc(NumFunction.SIN, ast.Var("t"))), "sine_wave"), ])) e = Evaluator(graphics=False) code, err = e.evaluate(program, duration=4000) self.assertNotEqual(0, code) self.assertTrue(isinstance(err, UndefinedVariableError))
def test_regular_program(self): """ Should successfully create a plot and update with new values """ print("Testing a full program") p = ast.Program( ast.Body([ # source = live remote "https://covid-api.com/api/reports" ast.Loader(ast.Var("source"), ast.Source("https://covid-api.com/api/reports")), # map source "confirmed" to number confirmed ast.Mapper( ast.Var("source"), "confirmed", ast.Declare(ast.Type(Types.NUMBER), ast.Var("confirmed"))), # number count = 0 ast.Assigner( ast.Declare(ast.Type(Types.NUMBER), ast.Var("count")), ast.Value(values.IntegerValue(0))), # observe(source) do count++ ast.Trigger(ast.Var("source"), ast.MathFuncs([ast.Increment(ast.Var("count"))])), # plot line_xy(count,confirmed) called "confirmed_cases" ast.Plotter(ast.Graph(graphs.LineXYGraph()), ast.VarAxis(ast.Var("count")), ast.VarAxis(ast.Var("confirmed")), "confirmed_cases"), ])) e = Evaluator(graphics=True) code, err = e.evaluate(p, duration=7000) self.assertEqual(0, code)