| // Copyright 2017-2020 The Verible Authors. | |
| // | |
| // Licensed under the Apache License, Version 2.0 (the "License"); | |
| // you may not use this file except in compliance with the License. | |
| // You may obtain a copy of the License at | |
| // | |
| // http://www.apache.org/licenses/LICENSE-2.0 | |
| // | |
| // Unless required by applicable law or agreed to in writing, software | |
| // distributed under the License is distributed on an "AS IS" BASIS, | |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| // See the License for the specific language governing permissions and | |
| // limitations under the License. | |
| // Unit tests for bison_parser_common. | |
| #include "common/parser/bison_parser_common.h" | |
| #include <memory> | |
| #include "absl/strings/string_view.h" | |
| #include "common/lexer/lexer.h" | |
| #include "common/lexer/token_stream_adapter.h" | |
| #include "common/parser/parser_param.h" | |
| #include "common/text/concrete_syntax_leaf.h" | |
| #include "common/text/concrete_syntax_tree.h" | |
| #include "common/text/symbol.h" | |
| #include "common/text/token_info.h" | |
| #include "gtest/gtest.h" | |
| namespace verible { | |
| namespace { | |
| // MockLexer is just for testing, and returns a fixed token. | |
| class MockLexer : public Lexer { | |
| public: | |
| MockLexer() : token_(13, "foo") {} | |
| const TokenInfo& GetLastToken() const final { return token_; } | |
| const TokenInfo& DoNextToken() final { return token_; } | |
| void Restart(absl::string_view) final {} | |
| bool TokenIsError(const TokenInfo&) const final { return false; } | |
| private: | |
| TokenInfo token_; | |
| }; | |
| // Test that Lex fetches a token of the expected value. | |
| TEST(BisonParserCommonTest, LexTest) { | |
| MockLexer lexer; | |
| auto generator = MakeTokenGenerator(&lexer); | |
| ParserParam parser_param(&generator); | |
| SymbolPtr value; | |
| const int token_enum = verible::LexAdapter(&value, &parser_param); | |
| const TokenInfo& t(parser_param.GetLastToken()); | |
| EXPECT_EQ(13, token_enum); | |
| EXPECT_EQ(13, t.token_enum()); | |
| EXPECT_EQ(t.text(), "foo"); | |
| EXPECT_EQ(value->Kind(), SymbolKind::kLeaf); | |
| const auto* value_ptr = down_cast<const SyntaxTreeLeaf*>(value.get()); | |
| ASSERT_NE(value_ptr, nullptr); | |
| const TokenInfo& tref(value_ptr->get()); | |
| EXPECT_EQ(13, tref.token_enum()); | |
| EXPECT_EQ(tref.text(), "foo"); | |
| } | |
| } // namespace | |
| } // namespace verible |