Skip to content

Commit d3a3e86

Browse files
Merge pull request #191 from SpineEventEngine/improve-mismatch
Add convenience wrapper for getting strings from `ValueMismatch`.
2 parents 9d662fe + 2e17c9b commit d3a3e86

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
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 & 6 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;
@@ -39,7 +41,6 @@
3941
*/
4042
public class MismatchShould {
4143

42-
private static final String REQUESTED = "requested";
4344
private static final String EXPECTED = "expected";
4445
private static final String ACTUAL = "ACTUAL";
4546
private static final int VERSION = 0;
@@ -83,7 +84,6 @@ public void return_mismatch_object_with_string_values() {
8384
public void return_mismatch_object_with_int32_values() {
8485
final int expected = 0;
8586
final int actual = 1;
86-
final int requested = 2;
8787
final ValueMismatch mismatch = Mismatch.of(expected, actual, VERSION);
8888
final Int32Value expectedWrapper = unpack(mismatch.getExpected());
8989
final Int32Value actualWrapper = unpack(mismatch.getActual());
@@ -96,7 +96,6 @@ public void return_mismatch_object_with_int32_values() {
9696
public void return_mismatch_object_with_int64_values() {
9797
final long expected = 0L;
9898
final long actual = 1L;
99-
final long requested = 2L;
10099
final ValueMismatch mismatch = Mismatch.of(expected, actual, VERSION);
101100
final Int64Value expectedWrapped = unpack(mismatch.getExpected());
102101
final Int64Value actualWrapped = unpack(mismatch.getActual());
@@ -109,7 +108,6 @@ public void return_mismatch_object_with_int64_values() {
109108
public void return_mismatch_object_with_float_values() {
110109
final float expected = 0.0F;
111110
final float actual = 1.0F;
112-
final float requested = 2.0F;
113111
final ValueMismatch mismatch = Mismatch.of(expected, actual, VERSION);
114112
final FloatValue expectedWrapped = unpack(mismatch.getExpected());
115113
final FloatValue actualWrapped = unpack(mismatch.getActual());
@@ -122,7 +120,6 @@ public void return_mismatch_object_with_float_values() {
122120
public void return_mismatch_object_with_double_values() {
123121
final double expected = 0.1;
124122
final double actual = 0.2;
125-
final double requested = 0.3;
126123
final ValueMismatch mismatch = Mismatch.of(expected, actual, VERSION);
127124
final DoubleValue expectedWrapped = unpack(mismatch.getExpected());
128125
final DoubleValue actualWrapped = unpack(mismatch.getActual());
@@ -135,7 +132,6 @@ public void return_mismatch_object_with_double_values() {
135132
public void return_mismatch_object_with_boolean_values() {
136133
final boolean expected = true;
137134
final boolean actual = false;
138-
final boolean requested = true;
139135
final ValueMismatch mismatch = Mismatch.of(expected, actual, VERSION);
140136
final BoolValue expectedWrapped = unpack(mismatch.getExpected());
141137
final BoolValue actualWrapped = unpack(mismatch.getActual());
@@ -171,4 +167,16 @@ public void return_mismatch_object_with_message_values() {
171167
assertEquals(EXPECTED, expected.getValue());
172168
assertEquals(ACTUAL, actual.getValue());
173169
}
170+
171+
@Test
172+
public void return_expected_string() {
173+
final ValueMismatch mismatch = Mismatch.of(newStringValue(EXPECTED), newStringValue(ACTUAL), VERSION);
174+
assertEquals(EXPECTED, getExpectedString(mismatch));
175+
}
176+
177+
@Test
178+
public void return_actual_string() {
179+
final ValueMismatch mismatch = Mismatch.of(newStringValue(EXPECTED), newStringValue(ACTUAL), VERSION);
180+
assertEquals(ACTUAL, getActualString(mismatch));
181+
}
174182
}

0 commit comments

Comments
 (0)