Skip to content

Commit 2d8758b

Browse files
committed
test: add more coverage
1 parent 6c3f66f commit 2d8758b

File tree

2 files changed

+67
-26
lines changed

2 files changed

+67
-26
lines changed

translations/id/id.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,7 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
15991599
return
16001600
}
16011601

1602-
if err = ut.Add("min-items", "panjang minimal {0} adalah {1}", false); err != nil {
1602+
if err = ut.Add("min-items", "{0} harus berisi minimal {1}", false); err != nil {
16031603
return
16041604
}
16051605

@@ -1833,7 +1833,7 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
18331833
},
18341834
{
18351835
tag: "excluded_without_all",
1836-
translation: "{0} tidak boleh diisi jika semua {1} tidak diisi",
1836+
translation: "{0} tidak boleh diisi jika {1} tidak diisi",
18371837
override: false,
18381838
customTransFunc: func(ut ut.Translator, fe validator.FieldError) string {
18391839
t, err := ut.T(fe.Tag(), fe.Field(), fe.Param())

translations/id/id_test.go

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -961,21 +961,25 @@ func TestComparisonTagsTranslations(t *testing.T) {
961961
GtString string `validate:"gt=5"` // length > 5
962962
GtNumber float64 `validate:"gt=10.5"`
963963
GtTime time.Time `validate:"gt"`
964+
GtSlice []string `validate:"gt=1"` // length > 1
964965

965966
// Greater than or equal comparisons
966967
GteString string `validate:"gte=5"` // length >= 5
967968
GteNumber float64 `validate:"gte=10.5"`
968969
GteTime time.Time `validate:"gte"`
970+
GteSlice []string `validate:"gte=1"` // length >= 1
969971

970972
// Less than comparisons
971973
LtString string `validate:"lt=5"` // length < 5
972974
LtNumber float64 `validate:"lt=10.5"`
973975
LtTime time.Time `validate:"lt"`
976+
LtSlice []string `validate:"lt=2"` // length < 1
974977

975978
// Less than or equal comparisons
976979
LteString string `validate:"lte=5"` // length <= 5
977980
LteNumber float64 `validate:"lte=10.5"`
978981
LteTime time.Time `validate:"lte"`
982+
LteSlice []string `validate:"lte=1"` // length <= 1
979983
}
980984

981985
// init test struct with invalid values
@@ -997,13 +1001,15 @@ func TestComparisonTagsTranslations(t *testing.T) {
9971001
GteNumber: 5.5, // should be >= 10.5
9981002
GteTime: now.Add(-time.Hour), // should be >= now
9991003

1000-
LtString: "toolong", // length = 7, should be < 5
1001-
LtNumber: 15.5, // should be < 10.5
1002-
LtTime: now.Add(time.Hour), // should be < now
1004+
LtString: "toolong", // length = 7, should be < 5
1005+
LtNumber: 15.5, // should be < 10.5
1006+
LtTime: now.Add(time.Hour), // should be < now
1007+
LtSlice: []string{"satu", "dua"}, // should be < 2
10031008

1004-
LteString: "toolong", // length = 7, should be <= 5
1005-
LteNumber: 15.5, // should be <= 10.5
1006-
LteTime: now.Add(time.Hour), // should be <= now
1009+
LteString: "toolong", // length = 7, should be <= 5
1010+
LteNumber: 15.5, // should be <= 10.5
1011+
LteTime: now.Add(time.Hour), // should be <= now
1012+
LteSlice: []string{"satu", "dua"}, // should be <= 1
10071013
}
10081014

10091015
// validate struct
@@ -1054,6 +1060,10 @@ func TestComparisonTagsTranslations(t *testing.T) {
10541060
ns: "TestComparisonTags.GtTime",
10551061
expected: "GtTime harus lebih besar dari tanggal & waktu saat ini",
10561062
},
1063+
{
1064+
ns: "TestComparisonTags.GtSlice",
1065+
expected: "GtSlice harus berisi lebih dari 1 item",
1066+
},
10571067
{
10581068
ns: "TestComparisonTags.GteString",
10591069
expected: "panjang minimal GteString adalah 5 karakter",
@@ -1066,6 +1076,10 @@ func TestComparisonTagsTranslations(t *testing.T) {
10661076
ns: "TestComparisonTags.GteTime",
10671077
expected: "GteTime harus lebih besar dari atau sama dengan tanggal & waktu saat ini",
10681078
},
1079+
{
1080+
ns: "TestComparisonTags.GteSlice",
1081+
expected: "GteSlice harus berisi setidaknya 1 item",
1082+
},
10691083
{
10701084
ns: "TestComparisonTags.LtString",
10711085
expected: "panjang LtString harus kurang dari 5 karakter",
@@ -1078,6 +1092,10 @@ func TestComparisonTagsTranslations(t *testing.T) {
10781092
ns: "TestComparisonTags.LtTime",
10791093
expected: "LtTime harus kurang dari tanggal & waktu saat ini",
10801094
},
1095+
{
1096+
ns: "TestComparisonTags.LtSlice",
1097+
expected: "LtSlice harus berisi kurang dari 2 item",
1098+
},
10811099
{
10821100
ns: "TestComparisonTags.LteString",
10831101
expected: "panjang maksimal LteString adalah 5 karakter",
@@ -1090,6 +1108,10 @@ func TestComparisonTagsTranslations(t *testing.T) {
10901108
ns: "TestComparisonTags.LteTime",
10911109
expected: "LteTime harus kurang dari atau sama dengan tanggal & waktu saat ini",
10921110
},
1111+
{
1112+
ns: "TestComparisonTags.LteSlice",
1113+
expected: "LteSlice harus berisi maksimal 1 item",
1114+
},
10931115
}
10941116

10951117
// verify each expected error message
@@ -1135,19 +1157,21 @@ func TestOtherTagsTranslations(t *testing.T) {
11351157
LenSlice []string `validate:"len=3"`
11361158
LenNumber int `validate:"len=10"`
11371159
MinString string `validate:"min=3"`
1160+
MinSlice []string `validate:"min=1"`
11381161
MaxString string `validate:"max=5"`
1162+
MaxSlice []string `validate:"max=1"`
11391163
IsDefault string `validate:"isdefault"`
11401164
Required string `validate:"required"`
11411165
RequiredIf string `validate:"required_if=Inner.RequiredWith value"`
11421166
RequiredUnless string `validate:"required_unless=Inner.RequiredWith values"`
11431167
RequiredWith string `validate:"required_with=Inner.RequiredWith"`
11441168
RequiredWithAll string `validate:"required_with_all=Inner.RequiredWith"`
1145-
RequiredWithout string `validate:"required_without=Inner.RequiredWith"`
1146-
RequiredWithoutAll string `validate:"required_without_all=Inner.RequiredWith"`
1169+
RequiredWithout string `validate:"required_without=Inner.ExcludedWith"`
1170+
RequiredWithoutAll string `validate:"required_without_all=Inner.ExcludedWith"`
11471171
ExcludedIf string `validate:"excluded_if=Inner.RequiredWith value"`
11481172
ExcludedUnless string `validate:"excluded_unless=Inner.ExcludedWith value"`
1149-
ExcludedWith string `validate:"excluded_with=Inner.Field"`
1150-
ExcludedWithAll string `validate:"excluded_with_all=Inner.Field"`
1173+
ExcludedWith string `validate:"excluded_with=Inner.RequiredWith"`
1174+
ExcludedWithAll string `validate:"excluded_with_all=Inner.RequiredWith"`
11511175
ExcludedWithout string `validate:"excluded_without=Inner.ExcludedWith"`
11521176
ExcludedWithoutAll string `validate:"excluded_without_all=Inner.ExcludedWith"`
11531177
OneOf string `validate:"oneof=red green blue"`
@@ -1162,11 +1186,13 @@ func TestOtherTagsTranslations(t *testing.T) {
11621186
File: "nonexistent.txt",
11631187
Image: "not-an-image.txt",
11641188

1165-
LenString: "toolong", // should be exactly 5 chars
1166-
LenSlice: []string{"a", "b"}, // should be exactly 3 items
1167-
LenNumber: 5, // should be 10
1168-
MinString: "ab", // should be min 3 chars
1169-
MaxString: "toolong", // should be max 5 chars
1189+
LenString: "toolong", // should be exactly 5 chars
1190+
LenSlice: []string{"a", "b"}, // should be exactly 3 items
1191+
LenNumber: 5, // should be 10
1192+
MinString: "ab", // should be min 3 chars
1193+
MinSlice: []string{}, // should be min 1 item
1194+
MaxString: "toolong", // should be max 5 chars
1195+
MaxSlice: []string{"satu", "dua"}, // should be max 1 item
11701196

11711197
IsDefault: "non-default",
11721198

@@ -1181,9 +1207,8 @@ func TestOtherTagsTranslations(t *testing.T) {
11811207
Unique: []string{"a", "a"}, // contains duplicate
11821208

11831209
Inner: Inner{
1184-
RequiredWith: "value", // triggers required_if validation
1185-
ExcludedWith: "", // triggers excluded_unless validation
1186-
Field: "populated", // triggers excluded_with validation
1210+
RequiredWith: "value", // triggers required_if validation
1211+
ExcludedWith: "", // triggers excluded_unless validation
11871212
},
11881213
}
11891214

@@ -1235,10 +1260,18 @@ func TestOtherTagsTranslations(t *testing.T) {
12351260
ns: "TestOtherTags.MinString",
12361261
expected: "panjang minimal MinString adalah 3 karakter",
12371262
},
1263+
{
1264+
ns: "TestOtherTags.MinSlice",
1265+
expected: "MinSlice harus berisi minimal 1 item",
1266+
},
12381267
{
12391268
ns: "TestOtherTags.MaxString",
12401269
expected: "panjang maksimal MaxString adalah 5 karakter",
12411270
},
1271+
{
1272+
ns: "TestOtherTags.MaxSlice",
1273+
expected: "MaxSlice harus berisi maksimal 1 item",
1274+
},
12421275
{
12431276
ns: "TestOtherTags.IsDefault",
12441277
expected: "IsDefault harus berupa nilai default",
@@ -1263,6 +1296,14 @@ func TestOtherTagsTranslations(t *testing.T) {
12631296
ns: "TestOtherTags.RequiredWithAll",
12641297
expected: "RequiredWithAll wajib diisi jika Inner.RequiredWith telah diisi",
12651298
},
1299+
{
1300+
ns: "TestOtherTags.RequiredWithout",
1301+
expected: "RequiredWithout wajib diisi jika Inner.ExcludedWith tidak diisi",
1302+
},
1303+
{
1304+
ns: "TestOtherTags.RequiredWithoutAll",
1305+
expected: "RequiredWithoutAll wajib diisi jika Inner.ExcludedWith tidak diisi",
1306+
},
12661307
{
12671308
ns: "TestOtherTags.ExcludedIf",
12681309
expected: "ExcludedIf tidak boleh diisi jika Inner.RequiredWith value",
@@ -1273,19 +1314,19 @@ func TestOtherTagsTranslations(t *testing.T) {
12731314
},
12741315
{
12751316
ns: "TestOtherTags.ExcludedWith",
1276-
expected: "ExcludedWith tidak boleh diisi jika Inner.Field telah diisi",
1317+
expected: "ExcludedWith tidak boleh diisi jika Inner.RequiredWith telah diisi",
1318+
},
1319+
{
1320+
ns: "TestOtherTags.ExcludedWithAll",
1321+
expected: "ExcludedWithAll tidak boleh diisi jika semua Inner.RequiredWith telah diisi",
12771322
},
1278-
//{
1279-
// ns: "TestOtherTags.ExcludedWithAll",
1280-
// expected: "ExcludedWithAll tidak boleh diisi jika semua Inner.Field dan Inner.ExcludedWith telah diisi",
1281-
//},
12821323
{
12831324
ns: "TestOtherTags.ExcludedWithout",
12841325
expected: "ExcludedWithout tidak boleh diisi jika Inner.ExcludedWith tidak diisi",
12851326
},
12861327
{
12871328
ns: "TestOtherTags.ExcludedWithoutAll",
1288-
expected: "ExcludedWithoutAll tidak boleh diisi jika semua Inner.ExcludedWith tidak diisi",
1329+
expected: "ExcludedWithoutAll tidak boleh diisi jika Inner.ExcludedWith tidak diisi",
12891330
},
12901331
{
12911332
ns: "TestOtherTags.OneOf",

0 commit comments

Comments
 (0)