From 8701e67fb4d97d8a182e34ce57b8db695d6cf912 Mon Sep 17 00:00:00 2001 From: Semyon Krestyaninov <krestyaninov@perx.ru> Date: Tue, 16 Jul 2024 12:54:27 +0000 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20`Equal`=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20`locales.Locale`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/locales/locale.go | 4 ++++ pkg/locales/locale_test.go | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/pkg/locales/locale.go b/pkg/locales/locale.go index 22a8905a..f45f1d85 100644 --- a/pkg/locales/locale.go +++ b/pkg/locales/locale.go @@ -27,6 +27,10 @@ func (locale *Locale) IsDefault() bool { return locale.ID == DefaultID } +func (locale *Locale) Equal(other *Locale) bool { + return locale == other || locale != nil && other != nil && *locale == *other +} + // Возвращает язык локали, например "en", "ru" func (locale *Locale) GetLanguage() string { lang, err := language.Parse(locale.Code) diff --git a/pkg/locales/locale_test.go b/pkg/locales/locale_test.go index 18a03060..5875107a 100644 --- a/pkg/locales/locale_test.go +++ b/pkg/locales/locale_test.go @@ -36,3 +36,46 @@ func TestLocale_GetLanguage(t *testing.T) { }) } } + +func TestLocale_Equal(t *testing.T) { + tests := []struct { + locale *Locale + other *Locale + want bool + }{ + { + locale: &Locale{Code: "ru-RU"}, + other: &Locale{Code: "ru-RU"}, + want: true, + }, + { + locale: &Locale{Code: "ru-RU"}, + other: &Locale{Code: "ru-RU", Fallback: "en-US"}, + want: false, + }, + { + locale: &Locale{Code: "ru-RU", Fallback: "en-US"}, + other: &Locale{Code: "ru-RU"}, + want: false, + }, + { + locale: &Locale{Code: "ru-RU"}, + other: nil, + want: false, + }, + { + locale: nil, + other: &Locale{Code: "ru-RU"}, + want: false, + }, + { + locale: nil, + other: nil, + want: true, + }, + } + for _, tt := range tests { + got := tt.locale.Equal(tt.other) + require.Equal(t, tt.want, got) + } +} -- GitLab