Skip to content

Commit 91bf45c

Browse files
Add convenience wrapper for getting strings from ValueMismatch.
1 parent 9d662fe commit 91bf45c

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ allprojects {
5454
apply plugin: 'idea'
5555

5656
group = 'org.spine3'
57-
version = '0.5.18-SNAPSHOT'
57+
version = '0.5.19-SNAPSHOT'
5858
}
5959

6060
ext {

client/src/main/java/org/spine3/base/Mismatch.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020

2121
package org.spine3.base;
2222

23+
import com.google.protobuf.InvalidProtocolBufferException;
2324
import com.google.protobuf.Message;
25+
import com.google.protobuf.StringValue;
2426
import org.spine3.protobuf.AnyPacker;
2527

2628
import javax.annotation.Nullable;
2729

30+
import static com.google.common.base.Throwables.propagate;
2831
import static org.spine3.protobuf.Values.pack;
2932

3033
/**
@@ -157,4 +160,35 @@ public static ValueMismatch of(@Nullable Message expected, @Nullable Message act
157160
builder.setVersion(version);
158161
return builder.build();
159162
}
163+
164+
165+
/**
166+
* Obtains expected string from the passed mismatch.
167+
*
168+
* @throws RuntimeException if the passed instance represent a mismatch of non-string values
169+
*/
170+
public static String getExpectedString(ValueMismatch mismatch) {
171+
try {
172+
final StringValue result = mismatch.getExpected()
173+
.unpack(StringValue.class);
174+
return result.getValue();
175+
} catch (InvalidProtocolBufferException e) {
176+
throw propagate(e);
177+
}
178+
}
179+
180+
/**
181+
* Obtains actual string from the passed mismatch.
182+
*
183+
* @throws RuntimeException if the passed instance represent a mismatch of non-string values
184+
*/
185+
public static String getActualString(ValueMismatch mismatch) {
186+
try {
187+
final StringValue result = mismatch.getActual()
188+
.unpack(StringValue.class);
189+
return result.getValue();
190+
} catch (InvalidProtocolBufferException e) {
191+
throw propagate(e);
192+
}
193+
}
160194
}

client/src/test/java/org/spine3/base/MismatchShould.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
import static org.junit.Assert.assertEquals;
3232
import static org.junit.Assert.assertTrue;
33+
import static org.spine3.base.Mismatch.getActualString;
34+
import static org.spine3.base.Mismatch.getExpectedString;
3335
import static org.spine3.protobuf.AnyPacker.unpack;
3436
import static org.spine3.protobuf.Values.newStringValue;
3537
import static org.spine3.test.Tests.hasPrivateUtilityConstructor;
@@ -83,7 +85,6 @@ public void return_mismatch_object_with_string_values() {
8385
public void return_mismatch_object_with_int32_values() {
8486
final int expected = 0;
8587
final int actual = 1;
86-
final int requested = 2;
8788
final ValueMismatch mismatch = Mismatch.of(expected, actual, VERSION);
8889
final Int32Value expectedWrapper = unpack(mismatch.getExpected());
8990
final Int32Value actualWrapper = unpack(mismatch.getActual());
@@ -96,7 +97,6 @@ public void return_mismatch_object_with_int32_values() {
9697
public void return_mismatch_object_with_int64_values() {
9798
final long expected = 0L;
9899
final long actual = 1L;
99-
final long requested = 2L;
100100
final ValueMismatch mismatch = Mismatch.of(expected, actual, VERSION);
101101
final Int64Value expectedWrapped = unpack(mismatch.getExpected());
102102
final Int64Value actualWrapped = unpack(mismatch.getActual());
@@ -109,7 +109,6 @@ public void return_mismatch_object_with_int64_values() {
109109
public void return_mismatch_object_with_float_values() {
110110
final float expected = 0.0F;
111111
final float actual = 1.0F;
112-
final float requested = 2.0F;
113112
final ValueMismatch mismatch = Mismatch.of(expected, actual, VERSION);
114113
final FloatValue expectedWrapped = unpack(mismatch.getExpected());
115114
final FloatValue actualWrapped = unpack(mismatch.getActual());
@@ -122,7 +121,6 @@ public void return_mismatch_object_with_float_values() {
122121
public void return_mismatch_object_with_double_values() {
123122
final double expected = 0.1;
124123
final double actual = 0.2;
125-
final double requested = 0.3;
126124
final ValueMismatch mismatch = Mismatch.of(expected, actual, VERSION);
127125
final DoubleValue expectedWrapped = unpack(mismatch.getExpected());
128126
final DoubleValue actualWrapped = unpack(mismatch.getActual());
@@ -135,7 +133,6 @@ public void return_mismatch_object_with_double_values() {
135133
public void return_mismatch_object_with_boolean_values() {
136134
final boolean expected = true;
137135
final boolean actual = false;
138-
final boolean requested = true;
139136
final ValueMismatch mismatch = Mismatch.of(expected, actual, VERSION);
140137
final BoolValue expectedWrapped = unpack(mismatch.getExpected());
141138
final BoolValue actualWrapped = unpack(mismatch.getActual());
@@ -171,4 +168,16 @@ public void return_mismatch_object_with_message_values() {
171168
assertEquals(EXPECTED, expected.getValue());
172169
assertEquals(ACTUAL, actual.getValue());
173170
}
171+
172+
@Test
173+
public void return_expected_string() {
174+
final ValueMismatch mismatch = Mismatch.of(newStringValue(EXPECTED), newStringValue(ACTUAL), VERSION);
175+
assertEquals(EXPECTED, getExpectedString(mismatch));
176+
}
177+
178+
@Test
179+
public void return_actual_string() {
180+
final ValueMismatch mismatch = Mismatch.of(newStringValue(EXPECTED), newStringValue(ACTUAL), VERSION);
181+
assertEquals(ACTUAL, getActualString(mismatch));
182+
}
174183
}

0 commit comments

Comments
 (0)