diff --git a/trans/trans.go b/trans/trans.go index ac48a3d..a36e9e0 100644 --- a/trans/trans.go +++ b/trans/trans.go @@ -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 { diff --git a/trans/trans_test.go b/trans/trans_test.go index c2397ae..2da1fa0 100644 --- a/trans/trans_test.go +++ b/trans/trans_test.go @@ -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()