aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/skylarkdebug/server/DebuggerSerializationTest.java
blob: 525f1ce0051be6136fbf89ffb0db8652d342ed30 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// 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.

package com.google.devtools.build.lib.skylarkdebug.server;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.NestedSetView;
import com.google.devtools.build.lib.skylarkdebugging.SkylarkDebuggingProtos.Value;
import com.google.devtools.build.lib.syntax.EvalUtils;
import com.google.devtools.build.lib.syntax.Printer;
import com.google.devtools.build.lib.syntax.SkylarkNestedSet;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link DebuggerSerialization}. */
@RunWith(JUnit4.class)
public final class DebuggerSerializationTest {

  @Test
  public void testSimpleNestedSet() {
    Set<String> children = ImmutableSet.of("a", "b");
    SkylarkNestedSet set =
        SkylarkNestedSet.of(Object.class, NestedSetBuilder.stableOrder().addAll(children).build());

    Value value = DebuggerSerialization.getValueProto("name", set);

    assertTypeAndDescription(set, value);
    assertThat(value.getChildList()).hasSize(3);
    assertThat(value.getChild(0))
        .isEqualTo(
            Value.newBuilder()
                .setLabel("order")
                .setType("Traversal order")
                .setDescription("default")
                .build());
    assertEqualIgnoringTypeAndDescription(
        value.getChild(1), DebuggerSerialization.getValueProto("directs", children));
    assertEqualIgnoringTypeAndDescription(
        value.getChild(2), DebuggerSerialization.getValueProto("transitives", ImmutableList.of()));
  }

  @Test
  public void testNestedSetWithNestedChildren() {
    NestedSet<String> innerNestedSet =
        NestedSetBuilder.<String>stableOrder().add("inner1").add("inner2").build();
    ImmutableSet<String> directChildren = ImmutableSet.of("a", "b");
    SkylarkNestedSet outerSet =
        SkylarkNestedSet.of(
            String.class,
            NestedSetBuilder.<String>linkOrder()
                .addAll(directChildren)
                .addTransitive(innerNestedSet)
                .build());

    Value value = DebuggerSerialization.getValueProto("name", outerSet);

    assertTypeAndDescription(outerSet, value);
    assertThat(value.getChildList()).hasSize(3);
    assertThat(value.getChild(0))
        .isEqualTo(
            Value.newBuilder()
                .setLabel("order")
                .setType("Traversal order")
                .setDescription("topological")
                .build());
    assertEqualIgnoringTypeAndDescription(
        value.getChild(1), DebuggerSerialization.getValueProto("directs", directChildren));
    assertEqualIgnoringTypeAndDescription(
        value.getChild(2),
        DebuggerSerialization.getValueProto(
            "transitives", ImmutableList.of(new NestedSetView<>(innerNestedSet))));
  }

  @Test
  public void testSimpleMap() {
    Map<String, Integer> map = ImmutableMap.of("a", 1, "b", 2);

    Value value = DebuggerSerialization.getValueProto("name", map);

    assertTypeAndDescription(map, value);
    assertThat(value.getChildList()).hasSize(2);
    assertThat(value.getChild(0).getLabel()).isEqualTo("[0]");
    assertThat(value.getChild(0).getChildList())
        .isEqualTo(
            ImmutableList.of(
                DebuggerSerialization.getValueProto("key", "a"),
                DebuggerSerialization.getValueProto("value", 1)));
    assertThat(value.getChild(1).getLabel()).isEqualTo("[1]");
    assertThat(value.getChild(1).getChildList())
        .isEqualTo(
            ImmutableList.of(
                DebuggerSerialization.getValueProto("key", "b"),
                DebuggerSerialization.getValueProto("value", 2)));
  }

  @Test
  public void testNestedMap() {
    Set<String> set = ImmutableSet.of("a", "b");
    Map<String, Object> map = ImmutableMap.of("a", set);

    Value value = DebuggerSerialization.getValueProto("name", map);

    assertTypeAndDescription(map, value);
    assertThat(value.getChildList()).hasSize(1);
    assertThat(value.getChild(0).getLabel()).isEqualTo("[0]");
    assertThat(value.getChild(0).getChildList())
        .isEqualTo(
            ImmutableList.of(
                DebuggerSerialization.getValueProto("key", "a"),
                DebuggerSerialization.getValueProto("value", set)));
  }

  @Test
  public void testSimpleIterable() {
    Iterable<Integer> iter = ImmutableList.of(1, 2);

    Value value = DebuggerSerialization.getValueProto("name", iter);

    assertTypeAndDescription(iter, value);
    assertThat(value.getChildList()).hasSize(2);
    assertThat(value.getChild(0)).isEqualTo(DebuggerSerialization.getValueProto("[0]", 1));
    assertThat(value.getChild(1)).isEqualTo(DebuggerSerialization.getValueProto("[1]", 2));
  }

  @Test
  public void testNestedIterable() {
    Iterable<Object> iter = ImmutableList.of(ImmutableList.of(1, 2));

    Value value = DebuggerSerialization.getValueProto("name", iter);

    assertTypeAndDescription(iter, value);
    assertThat(value.getChildList()).hasSize(1);
    assertThat(value.getChild(0))
        .isEqualTo(DebuggerSerialization.getValueProto("[0]", ImmutableList.of(1, 2)));
  }

  @Test
  public void testSimpleArray() {
    int[] array = new int[] {1, 2};

    Value value = DebuggerSerialization.getValueProto("name", array);

    assertTypeAndDescription(array, value);
    assertThat(value.getChildList()).hasSize(2);
    assertThat(value.getChild(0)).isEqualTo(DebuggerSerialization.getValueProto("[0]", 1));
    assertThat(value.getChild(1)).isEqualTo(DebuggerSerialization.getValueProto("[1]", 2));
  }

  @Test
  public void testNestedArray() {
    Object[] array = new Object[] {1, ImmutableList.of(2, 3)};

    Value value = DebuggerSerialization.getValueProto("name", array);

    assertTypeAndDescription(array, value);
    assertThat(value.getChildList()).hasSize(2);
    assertThat(value.getChild(0)).isEqualTo(DebuggerSerialization.getValueProto("[0]", 1));
    assertThat(value.getChild(1))
        .isEqualTo(DebuggerSerialization.getValueProto("[1]", ImmutableList.of(2, 3)));
  }

  @Test
  public void testUnrecognizedObjectOrSkylarkPrimitiveHasNoChildren() {
    assertThat(DebuggerSerialization.getValueProto("name", 1).getChildList()).isEmpty();
    assertThat(DebuggerSerialization.getValueProto("name", "string").getChildList()).isEmpty();
    assertThat(DebuggerSerialization.getValueProto("name", new Object()).getChildList()).isEmpty();
  }

  private static void assertTypeAndDescription(Object object, Value value) {
    assertThat(value.getType()).isEqualTo(EvalUtils.getDataTypeName(object));
    assertThat(value.getDescription()).isEqualTo(Printer.repr(object));
  }

  /**
   * Type and description are implementation dependent (e.g. NestedSetView#directs returns a list
   * instead of a set if there are no duplicates, which changes both 'type' and 'description').
   */
  private static void assertEqualIgnoringTypeAndDescription(Value value1, Value value2) {
    assertThat(value1.getLabel()).isEqualTo(value2.getLabel());
    assertThat(value1.getChildCount()).isEqualTo(value2.getChildCount());
    for (int i = 0; i < value1.getChildCount(); i++) {
      assertEqualIgnoringTypeAndDescription(value1.getChild(i), value2.getChild(i));
    }
  }
}