def test_create_sketch_from_string(self): sketch_content = 'test string sketch' sketch_dir = actions.create_sketch_from_string(sketch_content) self.assertTrue(os.path.isfile(sketch_dir)) f = codecs.open(sketch_dir, "r", "utf-8") self.assertEqual(f.read(), sketch_content) # Test for unicode file sketch_content = 'いろはにほへとちり Γαζέες καὶ μυρτς Âne aéquo au whist' sketch_dir = actions.create_sketch_from_string(sketch_content) self.assertTrue(os.path.isfile(sketch_dir)) f = codecs.open(sketch_dir, "r", "utf-8") self.assertEqual(f.read(), sketch_content)
def test_create_sketch_from_string_content(self, mock_settings): """Test the create_sketch_from_string creates the file correctly. :param mock_settings: Mock for ServerCompilerSettings class. :return: None. """ sketch_code = 'いろはにほへとちり Γαζέες καὶ μυρτς Âne aéquo au whist' mock_settings.return_value.sketch_dir = self.temp_folder mock_settings.return_value.sketch_name = 'test_sketch' returned_path = actions.create_sketch_from_string(sketch_code) with codecs.open(returned_path, 'r', encoding='utf-8') as sketch: self.assertEqual(sketch.read(), sketch_code)
def test_create_sketch_from_string_file(self, mock_settings): """Test the create_sketch_from_string creates the file correctly. :param mock_settings: Mock for ServerCompilerSettings class. :return: None. """ sketch_name = 'test_sketch' mock_settings.return_value.sketch_dir = self.temp_folder mock_settings.return_value.sketch_name = sketch_name sketch_path = os.path.join( self.temp_folder, sketch_name, sketch_name + '.ino') self.assertFalse(os.path.exists(sketch_path)) returned_path = actions.create_sketch_from_string('') self.assertEqual(returned_path, sketch_path) self.assertTrue(os.path.exists(returned_path))
def handle_sketch(sketch_code): """ Creates an Arduino Sketch and invokes the Arduino CLI. Creates a JSON string to return to the page with the following format: {"response_type": "ide_output", "element" : "div_ide_output", "success" : "true", "conclusion" : Short text as main conclusion, "output" : Output string, "error_output" : Output string, "exit_code": Exit code} """ sketch_path = actions.create_sketch_from_string(sketch_code) success, conclusion, out, error, exit_code =\ actions.load_arduino_cli(sketch_path) json_data = \ {'response_type': 'ide_output', 'element': 'div_ide_output', 'success': success, 'conclusion': conclusion, 'output': out, 'error_output': error, 'exit_code': exit_code} return json.dumps(json_data)