From 5fd46115f182b4c3a1a245adda001787a528a17b Mon Sep 17 00:00:00 2001 From: Gerald Sihotang Date: Sat, 22 Jan 2022 15:01:38 +0700 Subject: [PATCH 1/2] Support for Emoticons --- example_with_tags_lang_test.go | 2 ++ faker.go | 6 +++++- faker_test.go | 18 +++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/example_with_tags_lang_test.go b/example_with_tags_lang_test.go index 6e4c4e4..2d345f3 100644 --- a/example_with_tags_lang_test.go +++ b/example_with_tags_lang_test.go @@ -14,6 +14,7 @@ func Example_withTagsLang() { StringCHI string `faker:"lang=chi"` StringRUS string `faker:"lang=rus"` StringJPN string `faker:"lang=jpn"` + StringEMJ string `faker:"lang=emj"` } a := SomeStruct{} @@ -27,6 +28,7 @@ func Example_withTagsLang() { StringCHI: 随机字符串 StringRUS:ваЩфз StringJPN:びゃほぱヒてふ + StringEMJ:🐅😄🕢🍪🐡 } */ } diff --git a/faker.go b/faker.go index b28caa9..3aebbf8 100644 --- a/faker.go +++ b/faker.go @@ -64,6 +64,8 @@ var ( LangRUS = langRuneBoundary{1025, 1105, nil} // LangJPN is for japanese Hiragana Katakana language LangJPN = langRuneBoundary{12353, 12534, []rune{12436, 12437, 12438, 12439, 12440, 12441, 12442, 12443, 12444, 12445, 12446, 12447, 12448}} + // EmotEMJ is for emoticons + EmotEMJ = langRuneBoundary{126976, 129535, nil} ) // Supported tags @@ -310,7 +312,7 @@ func SetRandomStringLength(size int) error { return nil } -// SetStringLang sets language of random string generation (LangENG, LangCHI, LangRUS, LangJPN) +// SetStringLang sets language of random string generation (LangENG, LangCHI, LangRUS, LangJPN, EmotEMJ) func SetStringLang(l langRuneBoundary) { lang = l } @@ -916,6 +918,8 @@ func extractLangFromTag(tag string) (*langRuneBoundary, error) { return &LangCHI, nil case "jpn": return &LangJPN, nil + case "emj": + return &EmotEMJ, nil default: return &LangENG, nil } diff --git a/faker_test.go b/faker_test.go index ad2d186..2d0ca33 100644 --- a/faker_test.go +++ b/faker_test.go @@ -19,10 +19,11 @@ const ( someStructWithLenAndLangCHI = 10 someStructWithLenAndLangRUS = 15 someStructWithLenAndLangJPN = 20 + someStructWithLenAndEmotEMJ = 50 ) var ( - langCorrectTagsMap = map[string]langRuneBoundary{"lang=eng": LangENG, "lang=chi": LangCHI, "lang=rus": LangRUS, "lang=jpn": LangJPN} + langCorrectTagsMap = map[string]langRuneBoundary{"lang=eng": LangENG, "lang=chi": LangCHI, "lang=rus": LangRUS, "lang=jpn": LangJPN, "lang=emj": EmotEMJ} langUncorrectTags = [3]string{"lang=", "lang", "lng=eng"} lenCorrectTags = [3]string{"len=4", "len=5", "len=10"} @@ -112,6 +113,7 @@ type SomeStructWithLang struct { ValueCHI string `faker:"lang=chi"` ValueRUS string `faker:"lang=rus"` ValueJPN string `faker:"lang=jpn"` + ValueEMJ string `faker:"lang=emj"` ValueWithUndefinedLang string `faker:"lang=und"` } @@ -121,6 +123,7 @@ type SomeStructWithLenAndLang struct { ValueCHI string ` faker:"len=10, lang=chi"` ValueRUS string ` faker:"len=15, lang=rus"` ValueJPN string ` faker:"len=20, lang=jpn"` + ValueEMJ string ` faker:"len=50, lang=emj"` } func (s SomeStruct) String() string { @@ -595,6 +598,10 @@ func TestLang(t *testing.T) { if err != nil { t.Error(err.Error()) } + err = isStringLangCorrect(someStruct.ValueEMJ, EmotEMJ) + if err != nil { + t.Error(err.Error()) + } err = isStringLangCorrect(someStruct.ValueWithUndefinedLang, LangENG) if err != nil { @@ -737,6 +744,15 @@ func TestLangWithLen(t *testing.T) { if jpnLen != someStructWithLenAndLangJPN { t.Errorf("Got %d, but expected to be %d as a string len", jpnLen, someStructWithLenAndLangJPN) } + + err = isStringLangCorrect(someStruct.ValueEMJ, EmotEMJ) + if err != nil { + t.Error(err.Error()) + } + emjLen := utfLen(someStruct.ValueEMJ) + if emjLen != someStructWithLenAndEmotEMJ { + t.Errorf("Got %d, but expected to be %d as a string len", emjLen, someStructWithLenAndEmotEMJ) + } } func isStringLangCorrect(value string, lang langRuneBoundary) error { From 16e747d064d2ad71c4ca567224049bb0cf07f85a Mon Sep 17 00:00:00 2001 From: Gerald Sihotang Date: Mon, 31 Jan 2022 19:44:36 +0700 Subject: [PATCH 2/2] rollback example --- example_with_tags_lang_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example_with_tags_lang_test.go b/example_with_tags_lang_test.go index a29de69..be6b097 100644 --- a/example_with_tags_lang_test.go +++ b/example_with_tags_lang_test.go @@ -29,7 +29,7 @@ func Example_withTagsLang() { StringCHI: 随机字符串 StringRUS:ваЩфз StringJPN:びゃほぱヒてふ - StringKOR:텻밚쨋큊몉\ + StringKOR:텻밚쨋큊몉 StringEMJ:🐅😄🕢🍪🐡 } */