Skip to content

Commit

Permalink
fix(i18n): change use to return error (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Mar 26, 2023
1 parent 81ebd29 commit b417d1b
Show file tree
Hide file tree
Showing 16 changed files with 138 additions and 68 deletions.
19 changes: 13 additions & 6 deletions i18n/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@ var _ = Describe("Text", Ordered, func() {

Context("native", func() {
BeforeEach(func() {
Use(func(o *UseOptions) {
if err := Use(func(o *UseOptions) {
o.Tag = DefaultLanguage.Get()
o.From.Sources = testTranslationFile
})
}); err != nil {
Fail(err.Error())
}
})

Context("given: ThirdPartyError", func() {
It("🧪 should: contain the third party error text", func() {
Use(func(o *UseOptions) {
if err := Use(func(o *UseOptions) {
o.Tag = language.BritishEnglish
})
}); err != nil {
Fail(err.Error())
}

err := NewThirdPartyErr(errors.New("computer says no"))
Expect(err.Error()).To(ContainSubstring("computer says no"))
})
Expand All @@ -64,11 +69,13 @@ var _ = Describe("Text", Ordered, func() {

Context("foreign", func() {
BeforeEach(func() {
Use(func(o *UseOptions) {
if err := Use(func(o *UseOptions) {
o.Tag = language.AmericanEnglish
o.From.Path = l10nPath
o.From.Sources = testTranslationFile
})
}); err != nil {
Fail(err.Error())
}
})

Context("Text", func() {
Expand Down
7 changes: 7 additions & 0 deletions i18n/translate-defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ type UseOptions struct {
//
From LoadFrom

// DefaultIsAcceptable controls whether an error is returned if the
// request language is not available. By default DefaultIsAcceptable
// is true so that the application continues in the default language
// even if the requested language is not available.
//
DefaultIsAcceptable bool

// Custom set-able by the client for what ever purpose is required.
//
Custom any
Expand Down
35 changes: 23 additions & 12 deletions i18n/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ type localizerContainer map[string]*i18n.Localizer
// can even be called without specifying the Tag and in this case
// the default language will be used. The client MUST call Use
// before using any functionality in this package.
func Use(options ...UseOptionFn) {
func Use(options ...UseOptionFn) error {
var err error
o := &UseOptions{}
o.DefaultIsAcceptable = true
o.Tag = DefaultLanguage.Get()

for _, fo := range options {
Expand All @@ -39,9 +41,22 @@ func Use(options ...UseOptionFn) {
lang := NewLanguageInfo(o)

if !ContainsLanguage(lang.Supported, o.Tag) {
panic(NewLanguageNotAvailableNativeError(o.Tag))
if o.DefaultIsAcceptable {
o.Tag = DefaultLanguage.Get()
lang.Tag = o.Tag
} else {
err = NewFailedToCreateTranslatorNativeError(o.Tag)
}
}

if err == nil {
applyLanguage(lang)
}

return err
}

func applyLanguage(lang *LanguageInfo) {
// since extendio is not trying to provide foreign translations for any
// of its dependencies, we only need create a localizer for this module
// (extendio). If we do need to provide these additional translations,
Expand All @@ -52,15 +67,11 @@ func Use(options ...UseOptionFn) {

tx = factory.New(lang)
TxRef = utils.NewRoProp(tx)

if TxRef.IsNone() {
panic(NewFailedToCreateTranslatorNativeError(o.Tag))
}
}

// ResetTx, do not use, required for unit testing only and is not considered
// part of the public api and may be removed without corresponding version
// number change.
// Deprecated: ResetTx, do not use, required for unit testing only and is
// not considered part of the public api and may be removed without
// corresponding version number change.
func ResetTx() {
// having to do this smells a bit, but required so unit tests can
// remain isolated (this is why package globals are bad, but sometimes
Expand All @@ -73,9 +84,9 @@ func ResetTx() {
TxRef = utils.NewRoProp(tx)
}

// UseTx, do not use, required for unit testing only and is not considered
// part of the public api and may be removed without corresponding version
// number change.
// Deprecated: UseTx, do not use, required for unit testing only and is
// not considered part of the public api and may be removed without
// corresponding version number change.
func UseTx(with Translator, setters ...UseOptionFn) error {
o := &UseOptions{}
for _, fo := range setters {
Expand Down
72 changes: 47 additions & 25 deletions i18n/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package i18n_test

import (
"fmt"
"strings"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -47,40 +46,63 @@ var _ = Describe("Translator", Ordered, func() {
Context("Use", func() {
When("requested language is available", func() {
It("🧪 should: create Translator", func() {
Use(func(o *UseOptions) {
if err := Use(func(o *UseOptions) {
o.Tag = language.AmericanEnglish
o.From.Path = l10nPath
o.From.Sources = testTranslationFile
})
}); err != nil {
Fail(err.Error())
}
Expect(TxRef.IsNone()).To(BeFalse())
Expect(TxRef.Get().LanguageInfoRef().Get().Tag).To(Equal(language.AmericanEnglish))
})
})

When("requested language is the default", func() {
It("🧪 should: create Translator", func() {
Use(func(o *UseOptions) {
if err := Use(func(o *UseOptions) {
o.Tag = language.BritishEnglish
})
}); err != nil {
Fail(err.Error())
}
Expect(TxRef.IsNone()).To(BeFalse())
Expect(TxRef.Get().LanguageInfoRef().Get().Tag).To(Equal(language.BritishEnglish))
})
})

When("requested language is NOT available", func() {
It("🧪 should: return error", func() {
defer func() {
pe := recover()
if err, ok := pe.(error); !ok || !strings.Contains(err.Error(), "not available") {
Fail("FAILED")
Context("DefaultIsAcceptable is true", func() {
When("requested language is NOT available", func() {
It("🧪 should: return NOT error", func() {
requested := language.Spanish
if err := Use(func(o *UseOptions) {
o.Tag = requested
}); err != nil {
Fail(
fmt.Sprintf(
"❌ request language: '%v' not supported, but default should be acceptable",
requested,
),
)
}
}()
})
})
})

requested := language.Spanish
Use(func(o *UseOptions) {
o.Tag = requested
Context("DefaultIsAcceptable is false", func() {
When("requested language is NOT available", func() {
It("🧪 should: return error", func() {
requested := language.Spanish
if err := Use(func(o *UseOptions) {
o.DefaultIsAcceptable = false
o.Tag = requested
}); err == nil {
Fail(
fmt.Sprintf("❌ expected error due to request language: '%v' not being supported",
requested,
),
)
}
})
Fail(fmt.Sprintf("❌ expected panic due to request language: '%v' not being supported", requested))
})
})
})
Expand All @@ -89,7 +111,7 @@ var _ = Describe("Translator", Ordered, func() {
Context("given: FailedToReadDirectoryContentsError", func() {
It("🧪 should: be identifiable via query function", func() {
reason := fmt.Errorf("file missing")
var err error = NewFailedToReadDirectoryContentsError("/foo/bar/", reason)
err := NewFailedToReadDirectoryContentsError("/foo/bar/", reason)
result := QueryFailedToReadDirectoryContentsError(err)
Expect(result).To(BeTrue())
})
Expand All @@ -98,39 +120,39 @@ var _ = Describe("Translator", Ordered, func() {
Context("given: NewFailedToResumeFromFileError", func() {
It("🧪 should: be identifiable via query function", func() {
reason := fmt.Errorf("file missing")
var err error = NewFailedToResumeFromFileError("/foo/bar/resume.json", reason)
err := NewFailedToResumeFromFileError("/foo/bar/resume.json", reason)
result := QueryFailedToResumeFromFileError(err)
Expect(result).To(BeTrue())
})
})

Context("given: InvalidConfigEntryError", func() {
It("🧪 should: be identifiable via query function", func() {
var err error = NewInvalidConfigEntryError("foo", "Store/Logging/Path")
err := NewInvalidConfigEntryError("foo", "Store/Logging/Path")
result := QueryInvalidConfigEntryError(err)
Expect(result).To(BeTrue())
})
})

Context("given: InvalidResumeStrategyError", func() {
It("🧪 should: be identifiable via query function", func() {
var err error = NewInvalidResumeStrategyError("foo")
err := NewInvalidResumeStrategyError("foo")
result := QueryInvalidResumeStrategyError(err)
Expect(result).To(BeTrue())
})
})

Context("given: MissingCallbackError", func() {
It("🧪 should: be identifiable via query function", func() {
var err error = NewMissingCallbackError()
err := NewMissingCallbackError()
result := QueryMissingCallbackError(err)
Expect(result).To(BeTrue())
})
})

Context("given: MissingCustomFilterDefinitionError", func() {
It("🧪 should: be identifiable via query function", func() {
var err error = NewMissingCustomFilterDefinitionError(
err := NewMissingCustomFilterDefinitionError(
"Options/Store/FilterDefs/Node/Custom",
)
result := QueryMissingCustomFilterDefinitionError(err)
Expand All @@ -140,23 +162,23 @@ var _ = Describe("Translator", Ordered, func() {

Context("given: NotADirectoryError", func() {
It("🧪 should: be identifiable via query function", func() {
var err error = NewNotADirectoryError("/foo/bar")
err := NewNotADirectoryError("/foo/bar")
result := QueryNotADirectoryError(err)
Expect(result).To(BeTrue())
})
})

Context("given: SortFnFailedError", func() {
It("🧪 should: be identifiable via query function", func() {
var err error = NewSortFnFailedError()
err := NewSortFnFailedError()
result := QuerySortFnFailedError(err)
Expect(result).To(BeTrue())
})
})

Context("given: UnknownMarshalFormatError", func() {
It("🧪 should: be identifiable via query function", func() {
var err error = NewUnknownMarshalFormatError(
err := NewUnknownMarshalFormatError(
"Options/Persist/Format", "jpg",
)
result := QueryUnknownMarshalFormatError(err)
Expand Down
6 changes: 4 additions & 2 deletions xfs/nav/filter-glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ var _ = Describe("FilterGlob", Ordered, func() {
})

BeforeEach(func() {
Use(func(o *UseOptions) {
if err := Use(func(o *UseOptions) {
o.Tag = DefaultLanguage.Get()
})
}); err != nil {
Fail(err.Error())
}
})

DescribeTable("GlobFilter",
Expand Down
6 changes: 4 additions & 2 deletions xfs/nav/filter-regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ var _ = Describe("FilterRegex", Ordered, func() {
})

BeforeEach(func() {
Use(func(o *UseOptions) {
if err := Use(func(o *UseOptions) {
o.Tag = DefaultLanguage.Get()
})
}); err != nil {
Fail(err.Error())
}
})

DescribeTable("RegexFilter",
Expand Down
6 changes: 4 additions & 2 deletions xfs/nav/marshal-state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ var _ = Describe("MarshalOptions", Ordered, func() {

BeforeEach(func() {
ResetTx()
Use(func(o *UseOptions) {
if err := Use(func(o *UseOptions) {
o.Tag = DefaultLanguage.Get()
})
}); err != nil {
Fail(err.Error())
}
})

Context("Marshal", func() {
Expand Down
7 changes: 4 additions & 3 deletions xfs/nav/navigation-listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ var _ = Describe("Listener", Ordered, func() {
var root string

BeforeAll(func() {
Use(func(o *UseOptions) {
if err := Use(func(o *UseOptions) {
o.Tag = DefaultLanguage.Get()
})

}); err != nil {
Fail(err.Error())
}
root = musico()
})

Expand Down
6 changes: 4 additions & 2 deletions xfs/nav/resume-strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,11 @@ var _ = Describe("Resume", Ordered, func() {
})

BeforeEach(func() {
Use(func(o *UseOptions) {
if err := Use(func(o *UseOptions) {
o.Tag = DefaultLanguage.Get()
})
}); err != nil {
Fail(err.Error())
}
})

DescribeTable("resume",
Expand Down
6 changes: 4 additions & 2 deletions xfs/nav/traverse-navigator-error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ var _ = Describe("TraverseNavigator errors", Ordered, func() {
})

BeforeEach(func() {
Use(func(o *UseOptions) {
if err := Use(func(o *UseOptions) {
o.Tag = DefaultLanguage.Get()
})
}); err != nil {
Fail(err.Error())
}
})

Context("new-navigator", func() {
Expand Down
6 changes: 4 additions & 2 deletions xfs/nav/traverse-navigator-scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ var _ = Describe("TraverseNavigatorScope", Ordered, func() {
})

BeforeEach(func() {
Use(func(o *UseOptions) {
if err := Use(func(o *UseOptions) {
o.Tag = DefaultLanguage.Get()
})
}); err != nil {
Fail(err.Error())
}
})

DescribeTable("scope",
Expand Down
Loading

0 comments on commit b417d1b

Please sign in to comment.