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 
35 import com.google.protobuf.ListValue;
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 {
testOfNull_IsNullValue()44   public void testOfNull_IsNullValue() throws Exception {
45     assertThat(Values.ofNull())
46         .isEqualTo(Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
47   }
48 
testOfBoolean_ConstructsValue()49   public void testOfBoolean_ConstructsValue() {
50     assertThat(Values.of(true)).isEqualTo(Value.newBuilder().setBoolValue(true).build());
51     assertThat(Values.of(false)).isEqualTo(Value.newBuilder().setBoolValue(false).build());
52   }
53 
testOfNumeric_ConstructsValue()54   public void testOfNumeric_ConstructsValue() {
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 
testOfString_ConstructsValue()61   public void testOfString_ConstructsValue() {
62     assertThat(Values.of("")).isEqualTo(Value.newBuilder().setStringValue("").build());
63     assertThat(Values.of("foo")).isEqualTo(Value.newBuilder().setStringValue("foo").build());
64   }
65 
testOfStruct_ConstructsValue()66   public void testOfStruct_ConstructsValue() {
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 
testOfListValue_ConstructsInstance()75   public void testOfListValue_ConstructsInstance() {
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 
testOfIterable_ReturnsTheValue()84   public void testOfIterable_ReturnsTheValue() {
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 }
103