1 /* 2 * Copyright 2014 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 #include "Parser.h" 17 18 #include <gtest/gtest.h> 19 20 #define ARRAYLEN(x) (sizeof(x) / sizeof(x[0])) 21 22 TEST(ParserTest, normalizeTypeDeclaration) { 23 static const struct { 24 const char* expected; 25 const char* input; 26 } kData[] = { 27 { "char", "char" }, 28 { "const unsigned int", " const unsigned\tint\n" }, 29 { "char* const**", "char *const* *" }, 30 }; 31 const size_t kDataSize = ARRAYLEN(kData); 32 for (size_t n = 0; n < kDataSize; ++n) { 33 std::string result; 34 std::string text = "When parsing '"; 35 text += kData[n].input; 36 text += "'"; 37 38 result = normalizeTypeDeclaration(kData[n].input); 39 EXPECT_STREQ(kData[n].expected, result.c_str()) << text; 40 } 41 } 42 43 TEST(ParserTest, parseTypeDeclaration) { 44 static const struct { 45 const char* input; 46 bool expected; 47 const char* expectedType; 48 const char* expectedError; 49 } kData[] = { 50 { "const", false, NULL, "Missing type name" }, 51 { "const const", false, NULL, "Missing type name" }, 52 { "foo", true, "foo", NULL }, 53 { "void", true, "void", NULL }, 54 { "const foo", true, "const foo", NULL }, 55 { "foo *", true, "foo*", NULL }, 56 { "char foo", true, "char foo", NULL }, 57 { "\tunsigned \t int\n", true, "unsigned int", NULL }, 58 { "const * char", false, NULL, "Unexpected '*' before type name" }, 59 { "const char * ", true, "const char*", NULL }, 60 { "const void*const * *", true, "const void* const**", NULL }, 61 }; 62 const size_t kDataSize = ARRAYLEN(kData); 63 for (size_t n = 0; n < kDataSize; ++n) { 64 std::string varname, vartype, error; 65 std::string text = "When parsing '"; 66 text += kData[n].input; 67 text += "'"; 68 69 EXPECT_EQ(kData[n].expected, 70 parseTypeDeclaration(kData[n].input, 71 &vartype, 72 &error)) << text; 73 if (kData[n].expected) { 74 EXPECT_STREQ(kData[n].expectedType, vartype.c_str()) << text; 75 } else { 76 EXPECT_STREQ(kData[n].expectedError, error.c_str()) << text; 77 } 78 } 79 } 80 81 TEST(ParserTest, parseParameterDeclaration) { 82 static const struct { 83 const char* input; 84 bool expected; 85 const char* expectedType; 86 const char* expectedVariable; 87 const char* expectedError; 88 } kData[] = { 89 { "foo", false, NULL, NULL, "Missing variable name" }, 90 { "const", false, NULL, NULL, "Missing type name" }, 91 { "const foo", false, NULL, NULL, "Missing variable name" }, 92 { "const const", false, NULL, NULL, "Missing type name" }, 93 { "char foo", true, "char", "foo", NULL }, 94 { "unsigned int\t bar\n", true, "unsigned int", "bar", NULL }, 95 { "const * char foo", false, NULL, NULL, "Unexpected '*' before type name" }, 96 { "const char * foo", true, "const char*", "foo", NULL }, 97 { "const void*const *data", true, "const void* const*", "data", NULL }, 98 { "char foo const", false, NULL, NULL, "Extra 'const' after variable name" }, 99 { "int bar*", false, NULL, NULL, "Extra '*' after variable name" }, 100 }; 101 const size_t kDataSize = ARRAYLEN(kData); 102 for (size_t n = 0; n < kDataSize; ++n) { 103 std::string varname, vartype, error; 104 std::string text = "When parsing '"; 105 text += kData[n].input; 106 text += "'"; 107 108 EXPECT_EQ(kData[n].expected, 109 parseParameterDeclaration(kData[n].input, 110 &vartype, 111 &varname, 112 &error)) << text; 113 if (kData[n].expected) { 114 EXPECT_STREQ(kData[n].expectedType, vartype.c_str()) << text; 115 EXPECT_STREQ(kData[n].expectedVariable, varname.c_str()) << text; 116 } else { 117 EXPECT_STREQ(kData[n].expectedError, error.c_str()) << text; 118 } 119 } 120 } 121