blob: 9035e43fed8eb42c2b2217e65282970e8c64c60f [file] [log] [blame]
Christopher Wileyfdeb0f42015-09-11 15:38:22 -07001/*
2 * Copyright (C) 2015, The Android Open Source Project
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
17#include <string>
18
19#include <gtest/gtest.h>
20
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070021#include "ast_java.h"
22#include "code_writer.h"
Christopher Wiley775fa1f2015-09-22 15:00:12 -070023#include "type_java.h"
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070024
Christopher Wiley12e894a2016-01-29 11:55:07 -080025using std::string;
26
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070027namespace android {
28namespace aidl {
Christopher Wileydb154a52015-09-28 16:32:25 -070029namespace java {
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070030namespace {
31
32const char kExpectedClassOutput[] =
33R"(// class comment
34final class TestClass extends SuperClass
35{
36}
37)";
38
39} // namespace
40
41TEST(AstJavaTests, GeneratesClass) {
Christopher Wiley8b2d3ee2015-09-23 15:43:01 -070042 JavaTypeNamespace types;
Christopher Wiley56799522015-10-31 10:17:04 -070043 types.Init();
Jiyong Park1d2df7d2018-07-23 15:22:50 +090044 Type class_type(&types, "TestClass", ValidatableType::KIND_GENERATED, false);
45 Type extend_type(&types, "SuperClass", ValidatableType::KIND_BUILT_IN, false);
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070046 Class a_class;
47 a_class.comment = "// class comment";
48 a_class.modifiers = FINAL;
49 a_class.what = Class::CLASS;
50 a_class.type = &class_type;
51 a_class.extends = &extend_type;
52
53 string actual_output;
Jiyong Parkb78e15b2018-07-04 20:31:03 +090054 a_class.Write(CodeWriter::ForString(&actual_output).get());
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070055 EXPECT_EQ(string(kExpectedClassOutput), actual_output);
56}
57
Jiyong Park176905e2018-07-04 22:29:41 +090058TEST(AstJavaTests, ToString) {
59 std::string literal = "public void foo() {}";
60 LiteralClassElement ce(literal);
61 std::string actual = ce.ToString();
62 EXPECT_EQ(literal, actual);
63
64 std::string written;
65 ce.Write(CodeWriter::ForString(&written).get());
66 EXPECT_EQ(literal, written);
67}
68
Christopher Wileydb154a52015-09-28 16:32:25 -070069} // namespace java
Christopher Wileyfdeb0f42015-09-11 15:38:22 -070070} // namespace aidl
71} // namespace android