Refactor and improve API
This commit is contained in:
parent
a6f5cd6268
commit
3ad5e57d84
@ -12,7 +12,7 @@ type Translation struct {
|
||||
word string
|
||||
fromLang string
|
||||
toLang string
|
||||
Executor func(*Translation) ([]byte, error)
|
||||
Translator func(*Translation) (string, error)
|
||||
}
|
||||
|
||||
func New(word, fromLang, toLang string) Translation {
|
||||
@ -20,11 +20,11 @@ func New(word, fromLang, toLang string) Translation {
|
||||
word: word,
|
||||
fromLang: fromLang,
|
||||
toLang: toLang,
|
||||
Executor: executeTransShell,
|
||||
Translator: executeTransShell,
|
||||
}
|
||||
}
|
||||
|
||||
func executeTransShell(t *Translation) ([]byte, error) {
|
||||
func executeTransShell(t *Translation) (string, error) {
|
||||
config := []string{
|
||||
"-no-ansi",
|
||||
"-show-original", "n",
|
||||
@ -35,15 +35,16 @@ func executeTransShell(t *Translation) ([]byte, error) {
|
||||
fmt.Sprintf("%s:%s", t.fromLang, t.toLang),
|
||||
t.word,
|
||||
}
|
||||
return exec.Command("trans", config...).Output()
|
||||
outBytes, err := exec.Command("trans", config...).Output()
|
||||
return string(outBytes), err
|
||||
}
|
||||
|
||||
func (t Translation) Translate() []string {
|
||||
outBytes, err := t.Executor(&t)
|
||||
output, err := t.Translator(&t)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return uniqueSlice(parseTransOutput(string(outBytes)))
|
||||
return uniqueSlice(parseTransOutput(output))
|
||||
}
|
||||
|
||||
func parseTransOutput(out string) []string {
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
|
||||
type testData struct {
|
||||
job []string
|
||||
transOutput []byte
|
||||
transOutput string
|
||||
expectedResults []string
|
||||
}
|
||||
|
||||
@ -17,7 +17,7 @@ func TestTranslation(t *testing.T) {
|
||||
cases := []testData{
|
||||
testData{
|
||||
[]string{"actuator", "en", "hu"},
|
||||
[]byte(`működtető
|
||||
`működtető
|
||||
|
||||
noun
|
||||
indítókar
|
||||
@ -25,21 +25,21 @@ noun
|
||||
|
||||
actuator
|
||||
működtető, beavatkozó, hajtómű, aktuátor, hajtás
|
||||
`),
|
||||
`,
|
||||
[]string{"működtető", "indítókar", "beavatkozó", "hajtómű", "aktuátor", "hajtás"},
|
||||
},
|
||||
testData{
|
||||
[]string{"szaxofon", "hu", "en"},
|
||||
[]byte(`saxophone
|
||||
`saxophone
|
||||
|
||||
szaxofon
|
||||
saxophone
|
||||
`),
|
||||
`,
|
||||
[]string{"saxophone"},
|
||||
},
|
||||
testData{
|
||||
[]string{"cat", "en", "hu"},
|
||||
[]byte(`macska
|
||||
`macska
|
||||
|
||||
noun
|
||||
macska
|
||||
@ -53,12 +53,12 @@ verb
|
||||
|
||||
cat
|
||||
macska, cica
|
||||
`),
|
||||
`,
|
||||
[]string{"macska", "pletykás nő", "felvon", "cica"},
|
||||
},
|
||||
testData{
|
||||
[]string{"méltányosság", "hu", "en"},
|
||||
[]byte(`equity
|
||||
`equity
|
||||
|
||||
noun
|
||||
equity
|
||||
@ -70,14 +70,14 @@ noun
|
||||
|
||||
méltányosság
|
||||
equity, fairness
|
||||
`),
|
||||
`,
|
||||
[]string{"equity", "fairness", "justness"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, data := range cases {
|
||||
j := trans.New(data.job[0], data.job[1], data.job[2])
|
||||
j.Executor = func(t *trans.Translation) ([]byte, error) {
|
||||
j.Translator = func(t *trans.Translation) (string, error) {
|
||||
return data.transOutput, nil
|
||||
}
|
||||
r := j.Translate()
|
||||
|
Loading…
Reference in New Issue
Block a user