1# Copyright 2018 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import proto
16
17
18def test_oneof():
19    class Foo(proto.Message):
20        bar = proto.Field(proto.INT32, number=1, oneof="bacon")
21        baz = proto.Field(proto.STRING, number=2, oneof="bacon")
22
23    foo = Foo(bar=42)
24    assert foo.bar == 42
25    assert not foo.baz
26    foo.baz = "the answer"
27    assert not foo.bar
28    assert foo.baz == "the answer"
29
30
31def test_multiple_oneofs():
32    class Foo(proto.Message):
33        bar = proto.Field(proto.INT32, number=1, oneof="spam")
34        baz = proto.Field(proto.STRING, number=2, oneof="spam")
35        bacon = proto.Field(proto.FLOAT, number=3, oneof="eggs")
36        ham = proto.Field(proto.STRING, number=4, oneof="eggs")
37
38    foo = Foo()
39    foo.bar = 42
40    foo.bacon = 42.0
41    assert foo.bar == 42
42    assert foo.bacon == 42.0
43    assert not foo.baz
44    assert not foo.ham
45    foo.ham = "this one gets assigned"
46    assert not foo.bacon
47    assert foo.ham == "this one gets assigned"
48    assert foo.bar == 42
49    assert not foo.baz
50