Refactor translation API

This commit is contained in:
Kristóf Tóth 2019-10-13 17:05:27 +02:00
parent c04b2bcf3a
commit f84de7bfff
2 changed files with 29 additions and 24 deletions

View File

@ -9,22 +9,20 @@ import (
type Translation struct { type Translation struct {
word string Word string
fromLang string Language string
toLang string Translator func(string, string, string) (string, error)
Translator func(*Translation) (string, error)
} }
func New(word, fromLang, toLang string) Translation { func New(word string) Translation {
return Translation{ return Translation{
word: word, Word: word,
fromLang: fromLang, Language: "",
toLang: toLang, Translator: executeTrans,
Translator: executeTransShell,
} }
} }
func executeTransShell(t *Translation) (string, error) { func executeTrans(word, fromLang, toLang string) (string, error) {
config := []string{ config := []string{
"-no-ansi", "-no-ansi",
"-show-original", "n", "-show-original", "n",
@ -32,19 +30,23 @@ func executeTransShell(t *Translation) (string, error) {
"-show-dictionary", "y", "-show-dictionary", "y",
"-show-languages", "n", "-show-languages", "n",
"-show-prompt-message", "n", "-show-prompt-message", "n",
fmt.Sprintf("%s:%s", t.fromLang, t.toLang), fmt.Sprintf("%s:%s", fromLang, toLang),
t.word, word,
} }
outBytes, err := exec.Command("trans", config...).Output() outBytes, err := exec.Command("trans", config...).Output()
return string(outBytes), err return string(outBytes), err
} }
func (t Translation) Translate() []string { func (t Translation) Translate(toLang string) []string {
output, err := t.Translator(&t) if t.Language == "" {
// TODO: Identify
panic("noooooo")
}
output, err := t.Translator(t.Word, t.Language, toLang)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return uniqueSlice(parseTransOutput(output)) return parseTransOutput(output)
} }
func parseTransOutput(out string) []string { func parseTransOutput(out string) []string {
@ -57,7 +59,7 @@ func parseTransOutput(out string) []string {
results = append(results, strings.TrimSpace(word)) results = append(results, strings.TrimSpace(word))
} }
} }
return results return uniqueSlice(results)
} }
func uniqueSlice(items []string) []string { func uniqueSlice(items []string) []string {

View File

@ -9,7 +9,9 @@ import (
type testData struct { type testData struct {
job []string word string
fromLang string
toLang string
transOutput string transOutput string
expectedResults []string expectedResults []string
} }
@ -17,7 +19,7 @@ type testData struct {
func TestTranslation(t *testing.T) { func TestTranslation(t *testing.T) {
cases := []testData{ cases := []testData{
testData{ testData{
[]string{"actuator", "en", "hu"}, "actuator", "en", "hu",
`működtető `működtető
noun noun
@ -30,7 +32,7 @@ actuator
[]string{"működtető", "indítókar", "beavatkozó", "hajtómű", "aktuátor", "hajtás"}, []string{"működtető", "indítókar", "beavatkozó", "hajtómű", "aktuátor", "hajtás"},
}, },
testData{ testData{
[]string{"szaxofon", "hu", "en"}, "szaxofon", "hu", "en",
`saxophone `saxophone
szaxofon szaxofon
@ -39,7 +41,7 @@ szaxofon
[]string{"saxophone"}, []string{"saxophone"},
}, },
testData{ testData{
[]string{"cat", "en", "hu"}, "cat", "en", "hu",
`macska `macska
noun noun
@ -58,7 +60,7 @@ cat
[]string{"macska", "pletykás nő", "felvon", "cica"}, []string{"macska", "pletykás nő", "felvon", "cica"},
}, },
testData{ testData{
[]string{"méltányosság", "hu", "en"}, "méltányosság", "hu", "en",
`equity `equity
noun noun
@ -77,13 +79,14 @@ méltányosság
} }
for _, data := range cases { 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 { 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 return data.transOutput, nil
} }
} }
r := j.Translate() r := tr.Translate(data.toLang)
if (!reflect.DeepEqual(r, data.expectedResults)) { if (!reflect.DeepEqual(r, data.expectedResults)) {
t.Errorf("\nExpected: '%v'\nActual: '%v'", data.expectedResults, r) t.Errorf("\nExpected: '%v'\nActual: '%v'", data.expectedResults, r)
} }