Refactor translation API
This commit is contained in:
parent
c04b2bcf3a
commit
f84de7bfff
@ -9,22 +9,20 @@ import (
|
||||
|
||||
|
||||
type Translation struct {
|
||||
word string
|
||||
fromLang string
|
||||
toLang string
|
||||
Translator func(*Translation) (string, error)
|
||||
Word string
|
||||
Language string
|
||||
Translator func(string, string, string) (string, error)
|
||||
}
|
||||
|
||||
func New(word, fromLang, toLang string) Translation {
|
||||
func New(word string) Translation {
|
||||
return Translation{
|
||||
word: word,
|
||||
fromLang: fromLang,
|
||||
toLang: toLang,
|
||||
Translator: executeTransShell,
|
||||
Word: word,
|
||||
Language: "",
|
||||
Translator: executeTrans,
|
||||
}
|
||||
}
|
||||
|
||||
func executeTransShell(t *Translation) (string, error) {
|
||||
func executeTrans(word, fromLang, toLang string) (string, error) {
|
||||
config := []string{
|
||||
"-no-ansi",
|
||||
"-show-original", "n",
|
||||
@ -32,19 +30,23 @@ func executeTransShell(t *Translation) (string, error) {
|
||||
"-show-dictionary", "y",
|
||||
"-show-languages", "n",
|
||||
"-show-prompt-message", "n",
|
||||
fmt.Sprintf("%s:%s", t.fromLang, t.toLang),
|
||||
t.word,
|
||||
fmt.Sprintf("%s:%s", fromLang, toLang),
|
||||
word,
|
||||
}
|
||||
outBytes, err := exec.Command("trans", config...).Output()
|
||||
return string(outBytes), err
|
||||
}
|
||||
|
||||
func (t Translation) Translate() []string {
|
||||
output, err := t.Translator(&t)
|
||||
func (t Translation) Translate(toLang string) []string {
|
||||
if t.Language == "" {
|
||||
// TODO: Identify
|
||||
panic("noooooo")
|
||||
}
|
||||
output, err := t.Translator(t.Word, t.Language, toLang)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return uniqueSlice(parseTransOutput(output))
|
||||
return parseTransOutput(output)
|
||||
}
|
||||
|
||||
func parseTransOutput(out string) []string {
|
||||
@ -57,7 +59,7 @@ func parseTransOutput(out string) []string {
|
||||
results = append(results, strings.TrimSpace(word))
|
||||
}
|
||||
}
|
||||
return results
|
||||
return uniqueSlice(results)
|
||||
}
|
||||
|
||||
func uniqueSlice(items []string) []string {
|
||||
|
@ -9,7 +9,9 @@ import (
|
||||
|
||||
|
||||
type testData struct {
|
||||
job []string
|
||||
word string
|
||||
fromLang string
|
||||
toLang string
|
||||
transOutput string
|
||||
expectedResults []string
|
||||
}
|
||||
@ -17,7 +19,7 @@ type testData struct {
|
||||
func TestTranslation(t *testing.T) {
|
||||
cases := []testData{
|
||||
testData{
|
||||
[]string{"actuator", "en", "hu"},
|
||||
"actuator", "en", "hu",
|
||||
`működtető
|
||||
|
||||
noun
|
||||
@ -30,7 +32,7 @@ actuator
|
||||
[]string{"működtető", "indítókar", "beavatkozó", "hajtómű", "aktuátor", "hajtás"},
|
||||
},
|
||||
testData{
|
||||
[]string{"szaxofon", "hu", "en"},
|
||||
"szaxofon", "hu", "en",
|
||||
`saxophone
|
||||
|
||||
szaxofon
|
||||
@ -39,7 +41,7 @@ szaxofon
|
||||
[]string{"saxophone"},
|
||||
},
|
||||
testData{
|
||||
[]string{"cat", "en", "hu"},
|
||||
"cat", "en", "hu",
|
||||
`macska
|
||||
|
||||
noun
|
||||
@ -58,7 +60,7 @@ cat
|
||||
[]string{"macska", "pletykás nő", "felvon", "cica"},
|
||||
},
|
||||
testData{
|
||||
[]string{"méltányosság", "hu", "en"},
|
||||
"méltányosság", "hu", "en",
|
||||
`equity
|
||||
|
||||
noun
|
||||
@ -77,13 +79,14 @@ méltányosság
|
||||
}
|
||||
|
||||
for _, data := range cases {
|
||||
j := trans.New(data.job[0], data.job[1], data.job[2])
|
||||
tr := trans.New(data.word)
|
||||
tr.Language = data.fromLang
|
||||
if _, ok := os.LookupEnv("NOMOCK"); !ok {
|
||||
j.Translator = func(t *trans.Translation) (string, error) {
|
||||
tr.Translator = func(string, string, string) (string, error) {
|
||||
return data.transOutput, nil
|
||||
}
|
||||
}
|
||||
r := j.Translate()
|
||||
r := tr.Translate(data.toLang)
|
||||
if (!reflect.DeepEqual(r, data.expectedResults)) {
|
||||
t.Errorf("\nExpected: '%v'\nActual: '%v'", data.expectedResults, r)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user