@@ -32,86 +32,40 @@ import io.kotest.matchers.shouldBe
32
32
import io.spine.base.FieldPath
33
33
import io.spine.protobuf.TypeConverter.toAny
34
34
import io.spine.protobuf.field
35
- import io.spine.test.options.setonce.TestEnv.CERT1
36
- import io.spine.test.options.setonce.TestEnv.CERT2
37
- import io.spine.test.options.setonce.TestEnv.DONALD
38
- import io.spine.test.options.setonce.TestEnv.TWENTY
39
- import io.spine.test.options.setonce.TestEnv.SEVENTY
40
- import io.spine.test.options.setonce.TestEnv.EIGHTY_KG
41
- import io.spine.test.options.setonce.TestEnv.FIFTY_KG
42
- import io.spine.test.options.setonce.TestEnv.FIRST_YEAR
43
- import io.spine.test.options.setonce.TestEnv.JACK
44
- import io.spine.test.options.setonce.TestEnv.NO
45
- import io.spine.test.options.setonce.TestEnv.SHORT_HEIGHT
46
- import io.spine.test.options.setonce.TestEnv.TWO
47
- import io.spine.test.options.setonce.TestEnv.EIGHT
48
- import io.spine.test.options.setonce.TestEnv.STUDENT1
49
- import io.spine.test.options.setonce.TestEnv.STUDENT2
50
- import io.spine.test.options.setonce.TestEnv.TALL_HEIGHT
51
- import io.spine.test.options.setonce.TestEnv.THIRD_YEAR
52
- import io.spine.test.options.setonce.TestEnv.YES
53
35
import io.spine.test.tools.validate.StudentCustomMessage
54
36
import io.spine.test.tools.validate.StudentDefaultMessage
55
37
import io.spine.test.tools.validate.YearOfStudy
56
38
import io.spine.validate.ValidationException
57
39
import org.junit.jupiter.api.DisplayName
58
- import org.junit.jupiter.api.Named.named
59
40
import org.junit.jupiter.api.assertThrows
60
41
import org.junit.jupiter.params.ParameterizedTest
61
- import org.junit.jupiter.params.provider.Arguments.arguments
62
42
import org.junit.jupiter.params.provider.MethodSource
63
43
64
44
@DisplayName(" `(set_once)` constraint should" )
65
45
internal class SetOnceErrorMessageITest {
66
46
67
- @MethodSource(" allFieldTypesWithTwoDistinctValues" )
47
+ @MethodSource(" io.spine.test.options.setonce.given.SetOnceErrorMessageTestEnv# allFieldTypesWithTwoDistinctValues" )
68
48
@ParameterizedTest(name = " show the default error message for `{0}` field" )
69
- fun <T : Any > defaultErrorMessage (fieldName : String , value1 : T , value2 : T ) =
70
- assertDefaultMessage(fieldName, value1, value2)
49
+ fun <T : Any > defaultErrorMessage (fieldName : String , value1 : T , value2 : T , type : String ) =
50
+ assertDefaultMessage(fieldName, value1, value2, type )
71
51
72
- @MethodSource(" allFieldTypesWithTwoDistinctValues" )
52
+ @MethodSource(" io.spine.test.options.setonce.given.SetOnceErrorMessageTestEnv# allFieldTypesWithTwoDistinctValues" )
73
53
@ParameterizedTest(name = " show the custom error message for `{0}` field" )
74
- fun <T : Any > customErrorMessage (fieldName : String , value1 : T , value2 : T ) =
75
- assertCustomMessage(fieldName, value1, value2)
76
-
77
- private companion object {
78
-
79
- @JvmStatic
80
- fun allFieldTypesWithTwoDistinctValues () = listOf (
81
- arguments(named(" message" , " name" ), JACK , DONALD ),
82
- arguments(named(" string" , " id" ), STUDENT1 , STUDENT2 ),
83
- arguments(named(" double" , " height" ), SHORT_HEIGHT , TALL_HEIGHT ),
84
- arguments(named(" float" , " weight" ), FIFTY_KG , EIGHTY_KG ),
85
- arguments(named(" int32" , " cash_USD" ), TWO , EIGHT ),
86
- arguments(named(" int64" , " cash_EUR" ), TWENTY , SEVENTY ),
87
- arguments(named(" uint32" , " cash_JPY" ), TWO , EIGHT ),
88
- arguments(named(" uint64" , " cash_GBP" ), TWENTY , SEVENTY ),
89
- arguments(named(" sint32" , " cash_AUD" ), TWO , EIGHT ),
90
- arguments(named(" sint64" , " cash_CAD" ), TWENTY , SEVENTY ),
91
- arguments(named(" fixed32" , " cash_CHF" ), TWO , EIGHT ),
92
- arguments(named(" fixed64" , " cash_CNY" ), TWENTY , SEVENTY ),
93
- arguments(named(" sfixed32" , " cash_PLN" ), TWO , EIGHT ),
94
- arguments(named(" sfixed64" , " cash_NZD" ), TWENTY , SEVENTY ),
95
- arguments(named(" bool" , " has_medals" ), YES , NO ),
96
- arguments(named(" enum" , " signature" ), CERT1 , CERT2 ),
97
-
98
- // For some reason, for enums, `Message.Builder.setField()` expects value
99
- // descriptors instead of constants or their ordinal numbers.
100
- arguments(" year_of_study" , FIRST_YEAR .valueDescriptor, THIRD_YEAR .valueDescriptor)
101
- )
102
- }
54
+ fun <T : Any > customErrorMessage (fieldName : String , value1 : T , value2 : T , type : String ) =
55
+ assertCustomMessage(fieldName, value1, value2, type)
103
56
}
104
57
105
- private fun <T : Any > assertDefaultMessage (fieldName : String , value1 : T , value2 : T ) {
58
+ private fun <T : Any > assertDefaultMessage (fieldName : String , value1 : T , value2 : T , type : String ) {
106
59
val builder = StudentDefaultMessage .newBuilder()
107
- val expectedParams = listOf (fieldName, " $value1 " , " $value2 " )
60
+ val descriptor = StudentDefaultMessage .getDescriptor()
61
+ val expectedParams = listOf (descriptor.fullName, fieldName, type, " $value1 " , " $value2 " )
108
62
val expectedFormat = { _: Int -> DEFAULT_MESSAGE_FORMAT }
109
63
return builder.assertErrorMessage(fieldName, value1, value2, expectedParams, expectedFormat)
110
64
}
111
65
112
- private fun <T : Any > assertCustomMessage (fieldName : String , value1 : T , value2 : T ) {
66
+ private fun <T : Any > assertCustomMessage (fieldName : String , value1 : T , value2 : T , type : String ) {
113
67
val builder = StudentCustomMessage .newBuilder()
114
- val expectedParams = listOf (" $value1 " , fieldName, " $value2 " )
68
+ val expectedParams = listOf (" $value1 " , fieldName, " $value2 " , type )
115
69
val expectedFormat = ::customMessageFormat
116
70
return builder.assertErrorMessage(fieldName, value1, value2, expectedParams, expectedFormat)
117
71
}
@@ -121,7 +75,7 @@ private fun <T : Any> assertCustomMessage(fieldName: String, value1: T, value2:
121
75
* the expected parameters when [fieldName] is set twice.
122
76
*
123
77
* Notice on enum fields: we have to pass enums as value descriptors
124
- * (see [SetOnceErrorMessageITest .allFieldTypesWithTwoDistinctValues]),
78
+ * (see [io.spine.test.options.setonce.given.SetOnceErrorMessageTestEnv .allFieldTypesWithTwoDistinctValues]),
125
79
* so we also have to take this into account during assertions because
126
80
* in `ConstraintViolation` they still arrive as Java enum constants.
127
81
*
@@ -166,7 +120,7 @@ private fun <T : Any> Builder.assertErrorMessage(
166
120
}
167
121
168
122
private const val DEFAULT_MESSAGE_FORMAT =
169
- " The field `%s` already has the value `%s` and cannot be reassigned to `%s`."
123
+ " The field `%s.%s` of the type `%s ` already has the value `%s` and cannot be reassigned to `%s`."
170
124
171
125
private fun customMessageFormat (fieldNumber : Int ) =
172
- " Field_$fieldNumber : `%s`, `%s`, `%s`."
126
+ " Field_$fieldNumber : `%s`, `%s`, `%s`, `%s` ."
0 commit comments