1 /** 2 * Copyright 2020 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #include <iostream> 17 #include <string> 18 #include "common/common_test.h" 19 #include "common/py_func_graph_fetcher.h" 20 21 #include "utils/log_adapter.h" 22 #include "pipeline/jit/parse/parse.h" 23 #include "debug/draw.h" 24 25 namespace mindspore { 26 namespace parse { 27 28 class TestParserClass : public UT::Common { 29 public: 30 TestParserClass() {} 31 virtual void SetUp(); 32 virtual void TearDown(); 33 }; 34 35 void TestParserClass::SetUp() { UT::InitPythonPath(); } 36 37 void TestParserClass::TearDown() {} 38 39 // Test case1 : test class method 40 TEST_F(TestParserClass, TestParseDataClassApi) { 41 py::function fn_ = python_adapter::GetPyFn("gtest_input.pipeline.parse.parser_test", "test_class_fn"); 42 Parser::InitParserEnvironment(fn_); 43 FuncGraphPtr func_graph = ParsePythonCode(fn_); 44 ASSERT_TRUE(nullptr != func_graph); 45 46 // save the func func_graph to manager 47 std::shared_ptr<FuncGraphManager> manager = Manage(func_graph); 48 49 // call resolve 50 bool ret_ = ResolveAll(manager); 51 52 ASSERT_TRUE(ret_); 53 54 // check the dataclass 55 bool is_dataclass = false; 56 py::object dataclass_obj; 57 // check the dataclass 58 for (auto node : manager->all_nodes()) { 59 if (node->isa<ValueNode>()) { 60 ValuePtr value = node->cast<ValueNodePtr>()->value(); 61 if (value->isa<PyObjectWrapper>()) { 62 if (IsValueNode<ClassObject>(node)) { 63 is_dataclass = true; 64 dataclass_obj = value->cast<std::shared_ptr<PyObjectWrapper>>()->obj(); 65 } 66 } 67 } 68 } 69 70 ASSERT_TRUE(is_dataclass); 71 72 // parse data class method 73 py::object inf_method = python_adapter::GetPyObjAttr(dataclass_obj, "inf"); 74 FuncGraphPtr graph_inf = ParsePythonCode(inf_method); 75 ASSERT_TRUE(nullptr != graph_inf); 76 manager->AddFuncGraph(graph_inf); 77 } 78 79 /* # skip ut test cases temporarily 80 // Test case 2: test parse object, transfore the CELL instance to api. 81 TEST_F(TestParserClass, TestParseMethod) { 82 py::object obj_ = python_adapter::CallPyFn("gtest_input.pipeline.parse.parse_class", "test_parse_object_instance"); 83 Parser::InitParserEnvironment(obj_); 84 FuncGraphPtr func_graph = ParsePythonCode(obj_); 85 ASSERT_TRUE(nullptr != func_graph); 86 87 // save the func_graph to manager 88 std::shared_ptr<FuncGraphManager> manager = Manage(func_graph); 89 90 // call resolve 91 bool ret_ = ResolveAll(manager); 92 93 ASSERT_TRUE(ret_); 94 } 95 96 // Test case 3: common test for debug ptest case 97 TEST_F(TestParserClass, TestParseCompileAPI) { 98 python_adapter::CallPyFn("gtest_input.pipeline.parse.parse_compile", "test_build"); 99 MS_LOG(DEBUG) << "Test end"; 100 } 101 */ 102 103 } // namespace parse 104 } // namespace mindspore 105