|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.translate.samples; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.junit.runners.JUnit4; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.io.ByteArrayOutputStream; |
| 28 | +import java.io.PrintStream; |
| 29 | + |
| 30 | +/** |
| 31 | + * Unit tests for {@link Analyze}. |
| 32 | + */ |
| 33 | +@RunWith(JUnit4.class) |
| 34 | +public class TranslateTextTest { |
| 35 | + |
| 36 | + @Test public void testSupportedLanguages() throws Exception { |
| 37 | + // Supported languages |
| 38 | + List<String> languages = Arrays.asList( |
| 39 | + "Afrikaans", "Albanian", "Amharic", "Arabic", "Armenian", "Azerbaijani", "Basque", |
| 40 | + "Belarusian", "Bengali", "Bosnian", "Bulgarian", "Catalan", "Cebuano", "Chichewa", |
| 41 | + "Chinese", "Chinese", "Corsican", "Croatian", "Czech", "Danish", "Dutch", "English", |
| 42 | + "Esperanto", "Estonian", "Filipino", "Finnish", "French", "Frisian", "Galician", |
| 43 | + "Georgian", "German", "Greek", "Gujarati", "Haitian", "Hausa", "Hawaiian", "Hebrew", |
| 44 | + "Hindi", "Hmong", "Hungarian", "Icelandic", "Igbo", "Indonesian", "Irish", "Italian", |
| 45 | + "Japanese", "Javanese", "Kannada", "Kazakh", "Khmer", "Korean", "Kurdish", "Kyrgyz", |
| 46 | + "Lao", "Latin", "Latvian", "Lithuanian", "Luxembourgish", "Macedonian", "Malagasy", |
| 47 | + "Malay", "Malayalam", "Maltese", "Maori", "Marathi", "Mongolian", "Myanmar", "Nepali", |
| 48 | + "Norwegian", "Pashto", "Persian", "Polish", "Portuguese", "Punjabi", "Romanian", |
| 49 | + "Russian", "Samoan", "Scots", "Serbian", "Sesotho", "Shona", "Sindhi", "Sinhala", |
| 50 | + "Slovak", "Slovenian", "Somali", "Spanish", "Sundanese", "Swahili", "Swedish", |
| 51 | + "Tajik", "Tamil", "Telugu", "Thai", "Turkish", "Ukrainian", "Urdu", "Uzbek", |
| 52 | + "Vietnamese", "Welsh", "Xhosa", "Yiddish", "Yoruba", "Zulu"); |
| 53 | + |
| 54 | + // Arrange |
| 55 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 56 | + PrintStream out = new PrintStream(bout); |
| 57 | + |
| 58 | + // Act |
| 59 | + TranslateText.displaySupportedLanguages(out); |
| 60 | + |
| 61 | + // Assert |
| 62 | + String got = bout.toString(); |
| 63 | + for(String language : languages) { |
| 64 | + assertThat(got).contains(language); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test public void testEnglishLangDetection() throws Exception { |
| 69 | + // Arrange |
| 70 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 71 | + PrintStream out = new PrintStream(bout); |
| 72 | + |
| 73 | + // Act |
| 74 | + TranslateText.detectLanguage("With power comes great responsibility.", out); |
| 75 | + |
| 76 | + // Assert |
| 77 | + String got = bout.toString(); |
| 78 | + assertThat(got).contains("language=en, confidence=0.79742646"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test public void testGermanLangDetection() throws Exception { |
| 82 | + // Arrange |
| 83 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 84 | + PrintStream out = new PrintStream(bout); |
| 85 | + |
| 86 | + // Act |
| 87 | + TranslateText.detectLanguage("Mit Macht kommt große Verantwortung.", out); |
| 88 | + |
| 89 | + // Assert |
| 90 | + String got = bout.toString(); |
| 91 | + assertThat(got).contains("language=de, confidence=0.9293963"); |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + @Test public void testDefaultIdentityTranslation() throws Exception { |
| 96 | + // Arrange |
| 97 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 98 | + PrintStream out = new PrintStream(bout); |
| 99 | + |
| 100 | + // Act |
| 101 | + String proverb = "What you do not wish for yourself, do not do to others."; |
| 102 | + TranslateText.translateText(proverb, out); |
| 103 | + |
| 104 | + // Assert |
| 105 | + String got = bout.toString(); |
| 106 | + assertThat(got).contains(proverb); |
| 107 | + } |
| 108 | + |
| 109 | + @Test public void testGermanToSpanishTranslation() throws Exception { |
| 110 | + // Arrange |
| 111 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 112 | + PrintStream out = new PrintStream(bout); |
| 113 | + |
| 114 | + // Act |
| 115 | + TranslateText.translateTextWithOptions("Mit Macht kommt große Verantwortung.", "de", "es", out); |
| 116 | + |
| 117 | + // Assert |
| 118 | + String got = bout.toString(); |
| 119 | + assertThat(got).contains("Con el poder viene una gran responsabilidad."); |
| 120 | + } |
| 121 | +} |
0 commit comments