|
| 1 | +// Copyright 2026 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 | + |
| 15 | +package dev.cel.runtime; |
| 16 | + |
| 17 | +import static com.google.common.truth.Truth.assertThat; |
| 18 | +import static org.junit.Assert.assertThrows; |
| 19 | + |
| 20 | +import com.google.common.collect.ImmutableList; |
| 21 | +import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
| 22 | +import java.util.Set; |
| 23 | +import org.junit.Test; |
| 24 | +import org.junit.runner.RunWith; |
| 25 | + |
| 26 | +@RunWith(TestParameterInjector.class) |
| 27 | +public final class AccumulatedUnknownsTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + public void createForAsyncCall_success() { |
| 31 | + AccumulatedUnknowns unknowns = AccumulatedUnknowns.createForAsyncCall(7L, 42L); |
| 32 | + |
| 33 | + assertThat(unknowns.callIds()).containsExactly(42L); |
| 34 | + assertThat(unknowns.exprIds()).containsExactly(7L); |
| 35 | + assertThat(unknowns.attributes()).isEmpty(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void createForAsyncCall_adaptedToCelUnknownSet_retainsExprId() { |
| 40 | + AccumulatedUnknowns unknowns = AccumulatedUnknowns.createForAsyncCall(7L, 42L); |
| 41 | + |
| 42 | + Object adapted = InterpreterUtil.maybeAdaptToCelUnknownSet(unknowns); |
| 43 | + |
| 44 | + assertThat(adapted).isEqualTo(CelUnknownSet.create(7L)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void createForAsyncCall_distinctExprIds_adaptToDistinctUnknownSets() { |
| 49 | + Object first = |
| 50 | + InterpreterUtil.maybeAdaptToCelUnknownSet(AccumulatedUnknowns.createForAsyncCall(7L, 42L)); |
| 51 | + Object second = |
| 52 | + InterpreterUtil.maybeAdaptToCelUnknownSet(AccumulatedUnknowns.createForAsyncCall(8L, 43L)); |
| 53 | + |
| 54 | + assertThat(first).isNotEqualTo(second); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void callIds_returnsUnmodifiableSet() { |
| 59 | + AccumulatedUnknowns unknowns = AccumulatedUnknowns.createForAsyncCall(7L, 42L); |
| 60 | + Set<Long> callIds = unknowns.callIds(); |
| 61 | + |
| 62 | + assertThrows(UnsupportedOperationException.class, () -> callIds.add(99L)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void merge_mergesCallIdsAndExprIdsAndAttributes() { |
| 67 | + AccumulatedUnknowns u1 = |
| 68 | + AccumulatedUnknowns.create(ImmutableList.of(1L), ImmutableList.of(CelAttribute.EMPTY)); |
| 69 | + u1.merge(AccumulatedUnknowns.createForAsyncCall(1L, 100L)); |
| 70 | + AccumulatedUnknowns u2 = AccumulatedUnknowns.create(ImmutableList.of(2L), ImmutableList.of()); |
| 71 | + u2.merge(AccumulatedUnknowns.createForAsyncCall(2L, 200L)); |
| 72 | + |
| 73 | + AccumulatedUnknowns merged = u1.merge(u2); |
| 74 | + |
| 75 | + assertThat(merged).isSameInstanceAs(u1); |
| 76 | + assertThat(merged.exprIds()).containsExactly(1L, 2L); |
| 77 | + assertThat(merged.attributes()).containsExactly(CelAttribute.EMPTY); |
| 78 | + assertThat(merged.callIds()).containsExactly(100L, 200L); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void maybeMerge_withNullAccumulator_returnsNewUnknowns() { |
| 83 | + AccumulatedUnknowns u = AccumulatedUnknowns.createForAsyncCall(1L, 1L); |
| 84 | + |
| 85 | + AccumulatedUnknowns result = AccumulatedUnknowns.maybeMerge(null, u); |
| 86 | + |
| 87 | + assertThat(result).isSameInstanceAs(u); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void maybeMerge_withExistingAccumulator_mergesBoth() { |
| 92 | + AccumulatedUnknowns u1 = AccumulatedUnknowns.createForAsyncCall(1L, 1L); |
| 93 | + AccumulatedUnknowns u2 = AccumulatedUnknowns.createForAsyncCall(2L, 2L); |
| 94 | + |
| 95 | + AccumulatedUnknowns result = AccumulatedUnknowns.maybeMerge(u1, u2); |
| 96 | + |
| 97 | + assertThat(result).isSameInstanceAs(u1); |
| 98 | + assertThat(result.callIds()).containsExactly(1L, 2L); |
| 99 | + assertThat(result.exprIds()).containsExactly(1L, 2L); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void maybeMerge_withNonUnknownObject_returnsOriginalAccumulator() { |
| 104 | + AccumulatedUnknowns u = AccumulatedUnknowns.createForAsyncCall(1L, 1L); |
| 105 | + |
| 106 | + AccumulatedUnknowns result = AccumulatedUnknowns.maybeMerge(u, "not an unknown"); |
| 107 | + |
| 108 | + assertThat(result).isSameInstanceAs(u); |
| 109 | + assertThat(result.callIds()).containsExactly(1L); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void create_singleExprId_populatesExprIdsOnly() { |
| 114 | + AccumulatedUnknowns u = AccumulatedUnknowns.create(10L); |
| 115 | + |
| 116 | + assertThat(u.exprIds()).containsExactly(10L); |
| 117 | + assertThat(u.attributes()).isEmpty(); |
| 118 | + assertThat(u.callIds()).isEmpty(); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void create_exprIdsAndAttributes_leavesCallIdsEmpty() { |
| 123 | + AccumulatedUnknowns u = |
| 124 | + AccumulatedUnknowns.create( |
| 125 | + ImmutableList.of(10L, 20L), ImmutableList.of(CelAttribute.EMPTY)); |
| 126 | + |
| 127 | + assertThat(u.exprIds()).containsExactly(10L, 20L); |
| 128 | + assertThat(u.attributes()).containsExactly(CelAttribute.EMPTY); |
| 129 | + assertThat(u.callIds()).isEmpty(); |
| 130 | + } |
| 131 | +} |
0 commit comments