pytorch/test/cpp/jit/test_class_parser.cpp
Michael Suo dfdb86a595 big cpp test reorg (#24801)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/24801

This is to fix the ODR-violations in fbcode static builds, which have been broken for several months.

This PR is unfortunately quite large, but the changes are only mechanical:
1. Tests defined in header files -> tests defined in cpp files
2. Remove the `torch::jit::testing` namespace -> `torch::jit`.
3. Single `test.h` file that aggregates all tests.
4. Separate out files for gtest and python versions of the tests instead of using a build flag
5. Add a readme for how to add a new test, and explaining a bit about why the cpp tests are the way they are.

Test Plan: Imported from OSS

Differential Revision: D16878605

Pulled By: suo

fbshipit-source-id: 27b5c077dadd990a5f74e25d01731f9c1f491603
2019-08-18 16:49:56 -07:00

38 lines
1.0 KiB
C++

#include <test/cpp/jit/test_base.h>
#include <torch/csrc/jit/script/parser.h>
#include <torch/csrc/jit/script/resolver.h>
namespace torch {
namespace jit {
using namespace torch::jit::script;
const auto testSource = R"JIT(
class FooTest:
def __init__(self, x):
self.x = x
def get_x(self):
return self.x
an_attribute : Tensor
)JIT";
void testClassParser() {
Parser p(std::make_shared<Source>(testSource));
std::vector<Def> definitions;
std::vector<Resolver> resolvers;
const auto classDef = ClassDef(p.parseClass());
p.lexer().expect(TK_EOF);
ASSERT_EQ(classDef.name().name(), "FooTest");
ASSERT_EQ(classDef.body().size(), 3);
ASSERT_EQ(Def(classDef.body()[0]).name().name(), "__init__");
ASSERT_EQ(Def(classDef.body()[1]).name().name(), "get_x");
ASSERT_EQ(
Var(Assign(classDef.body()[2]).lhs()).name().name(), "an_attribute");
ASSERT_FALSE(Assign(classDef.body()[2]).rhs().present());
ASSERT_TRUE(Assign(classDef.body()[2]).type().present());
}
} // namespace jit
} // namespace torch