ValuesTest.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf.util;
32 
33 import static com.google.common.truth.Truth.assertThat;
34 
36 import com.google.protobuf.NullValue;
37 import com.google.protobuf.Struct;
38 import com.google.protobuf.Value;
39 import java.util.ArrayList;
40 import java.util.List;
41 import junit.framework.TestCase;
42 
43 public final class ValuesTest extends TestCase {
44  public void testOfNull_IsNullValue() throws Exception {
45  assertThat(Values.ofNull())
46  .isEqualTo(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
47  }
48 
50  assertThat(Values.of(true)).isEqualTo(Value.newBuilder().setBoolValue(true).build());
51  assertThat(Values.of(false)).isEqualTo(Value.newBuilder().setBoolValue(false).build());
52  }
53 
55  assertThat(Values.of(100)).isEqualTo(Value.newBuilder().setNumberValue(100).build());
56  assertThat(Values.of(1000L)).isEqualTo(Value.newBuilder().setNumberValue(1000).build());
57  assertThat(Values.of(1000.23f)).isEqualTo(Value.newBuilder().setNumberValue(1000.23f).build());
58  assertThat(Values.of(10000.23)).isEqualTo(Value.newBuilder().setNumberValue(10000.23).build());
59  }
60 
62  assertThat(Values.of("")).isEqualTo(Value.newBuilder().setStringValue("").build());
63  assertThat(Values.of("foo")).isEqualTo(Value.newBuilder().setStringValue("foo").build());
64  }
65 
67  Struct.Builder builder = Struct.newBuilder();
68  builder.putFields("a", Values.of("a"));
69  builder.putFields("b", Values.of("b"));
70 
71  assertThat(Values.of(builder.build()))
72  .isEqualTo(Value.newBuilder().setStructValue(builder.build()).build());
73  }
74 
76  ListValue.Builder builder = ListValue.newBuilder();
77  builder.addValues(Values.of(1));
78  builder.addValues(Values.of(2));
79 
80  assertThat(Values.of(builder.build()))
81  .isEqualTo(Value.newBuilder().setListValue(builder.build()).build());
82  }
83 
85  ListValue.Builder builder = ListValue.newBuilder();
86  builder.addValues(Values.of(1));
87  builder.addValues(Values.of(2));
88  builder.addValues(Values.of(true));
89  builder.addValues(Value.newBuilder().setListValue(builder.build()).build());
90 
91  List<Value> list = new ArrayList<>();
92  list.add(Values.of(1));
93  list.add(Values.of(2));
94  list.add(Values.of(true));
95  List<Value> copyList = new ArrayList<>(list);
96  list.add(Values.of(copyList));
97 
98  assertThat(Values.of(list)).isEqualTo(Value.newBuilder().setListValue(builder).build());
99  assertThat(Values.of(new ArrayList<Value>()))
100  .isEqualTo(Value.newBuilder().setListValue(ListValue.getDefaultInstance()).build());
101  }
102 }
com.google.protobuf::ListValue
com.google.protobuf.util.ValuesTest.testOfNumeric_ConstructsValue
void testOfNumeric_ConstructsValue()
Definition: ValuesTest.java:54
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
com.google.protobuf.util.ValuesTest.testOfBoolean_ConstructsValue
void testOfBoolean_ConstructsValue()
Definition: ValuesTest.java:49
NullValue
NullValue
Definition: struct.pb.h:83
com.google.protobuf::Value
com.google.protobuf.util.Values
Definition: Values.java:39
com.google.protobuf.util.Values.of
static Value of(boolean value)
Definition: Values.java:49
ListValue
struct ListValue ListValue
Definition: php/ext/google/protobuf/protobuf.h:646
com.google.protobuf.util.ValuesTest.testOfStruct_ConstructsValue
void testOfStruct_ConstructsValue()
Definition: ValuesTest.java:66
com.google.protobuf.util.Values.ofNull
static Value ofNull()
Definition: Values.java:44
java
com.google.protobuf.util.ValuesTest.testOfIterable_ReturnsTheValue
void testOfIterable_ReturnsTheValue()
Definition: ValuesTest.java:84
com.google.protobuf.util.ValuesTest.testOfString_ConstructsValue
void testOfString_ConstructsValue()
Definition: ValuesTest.java:61
com.google.protobuf.util.ValuesTest.testOfListValue_ConstructsInstance
void testOfListValue_ConstructsInstance()
Definition: ValuesTest.java:75
com.google.protobuf.util.ValuesTest.testOfNull_IsNullValue
void testOfNull_IsNullValue()
Definition: ValuesTest.java:44
com.google
com
Struct
struct Struct Struct
Definition: php/ext/google/protobuf/protobuf.h:661
gmock_test_utils.TestCase
TestCase
Definition: gmock_test_utils.py:97
com.google.protobuf.util.ValuesTest
Definition: ValuesTest.java:43
com.google.protobuf::Struct
Value
struct Value Value
Definition: php/ext/google/protobuf/protobuf.h:667


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:01