This repository was archived by the owner on May 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtest_parser.py
112 lines (95 loc) · 4.46 KB
/
test_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright 2017 Google Inc.
#
# 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.
from __future__ import absolute_import
import io
import os
import unittest
import pytest
from google.protobuf.compiler.plugin_pb2 import CodeGeneratorRequest
from protoc_docs import parser
curdir = os.path.realpath(os.path.dirname(__file__))
class TestCodeGeneratorParser(unittest.TestCase):
def test_from_input_file(self):
with io.open('%s/data/input_buffer' % curdir, 'rb') as file_:
cgp = parser.CodeGeneratorParser.from_input_file(file_)
assert isinstance(cgp._request, CodeGeneratorRequest)
def test_constructor_bad_argument(self):
with pytest.raises(TypeError):
parser.CodeGeneratorParser(request='foo')
def test_find_docs(self):
# Read in a valid request from disk.
with io.open('%s/data/input_buffer' % curdir, 'rb') as file_:
cgp = parser.CodeGeneratorParser.from_input_file(file_)
# Parse the request.
answer = {}
for filename, message_structure in cgp.find_docs():
answer.setdefault(filename, set())
answer[filename].add(message_structure)
# Make incredibly basic assertions about the collected data.
assert len(answer) == 1
assert {i.name for i in answer['protos/descriptor.proto']} == {
'google.protobuf.FileDescriptorSet',
'google.protobuf.FieldOptions',
'google.protobuf.FileOptions',
'google.protobuf.MessageOptions',
'google.protobuf.FileDescriptorProto',
'google.protobuf.FieldDescriptorProto',
'google.protobuf.ServiceOptions',
'google.protobuf.MethodDescriptorProto',
'google.protobuf.EnumDescriptorProto',
'google.protobuf.EnumValueDescriptorProto',
'google.protobuf.UninterpretedOption.NamePart',
'google.protobuf.SourceCodeInfo',
'google.protobuf.SourceCodeInfo.Location',
'google.protobuf.GeneratedCodeInfo.Annotation',
'google.protobuf.MethodOptions',
'google.protobuf.DescriptorProto',
'google.protobuf.GeneratedCodeInfo',
'google.protobuf.EnumValueOptions',
'google.protobuf.UninterpretedOption',
'google.protobuf.ServiceDescriptorProto',
'google.protobuf.OneofOptions',
'google.protobuf.DescriptorProto.ReservedRange',
'google.protobuf.OneofDescriptorProto',
'google.protobuf.EnumOptions',
}
def test_find_docs_no_output_files(self):
# Read the file, but this time wipe out the list of target output
# files; this should make no files be written.
with io.open('%s/data/input_buffer' % curdir, 'rb') as file_:
cgp = parser.CodeGeneratorParser.from_input_file(file_)
cgp._request.ClearField('file_to_generate')
# There should be no data this time.
answer = {}
for filename, message_structure in cgp.find_docs():
answer.setdefault(filename, set())
answer[filename].add(message_structure)
assert len(answer) == 0
def test_find_docs_no_source_info(self):
# Read the file, but this time wipe out the source code info;
# the proto specification says this is optional.
with io.open('%s/data/input_buffer' % curdir, 'rb') as file_:
cgp = parser.CodeGeneratorParser.from_input_file(file_)
cgp._request.proto_file[0].ClearField('source_code_info')
# There should be no data this time.
answer = {}
for filename, message_structure in cgp.find_docs():
answer.setdefault(filename, set())
answer[filename].add(message_structure)
assert len(answer) == 0
def test_is_mixed_case(self):
cgp = parser.CodeGeneratorParser(CodeGeneratorRequest())
assert cgp._is_mixed_case('foo') is False
assert cgp._is_mixed_case('FOO') is False
assert cgp._is_mixed_case('Foo') is True