Skip to content

Commit

Permalink
#10 ListedLanguages finished and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Jul 2, 2021
1 parent b981ca9 commit b27a0e1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @todo #8:60min Finish implementing this class and write unit tests for it.
*/
final class ListedLanguages implements Languages {

Expand All @@ -27,7 +26,13 @@ final class ListedLanguages implements Languages {

@Override
public Language bestMatch() {
throw new UnsupportedOperationException("Not yet implemented.");
Language best = this.languages.get(0);
for(int i = 1; i < this.languages.size(); i++) {
if(this.languages.get(i).confidence() > best.confidence()) {
best = languages.get(i);
}
}
return best;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.imagineobjects.linguinai;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.List;

/**
* Unit tests for {@link ListedLanguages}.
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
*/
public final class ListedLanguagesTestCase {

/**
* ListedLanguages can be iterated.
*/
@Test
public void canBeIterated() {
final Languages languages = new ListedLanguages(
List.of(
Mockito.mock(Language.class),
Mockito.mock(Language.class),
Mockito.mock(Language.class)
)
);
MatcherAssert.assertThat(
languages,
Matchers.iterableWithSize(3)
);
}

/**
* ListedLanguages can return the best match.
*/
@Test
public void returnsBestMatch() {
final Language first = Mockito.mock(Language.class);
Mockito.when(first.confidence()).thenReturn(0.5);
final Language second = Mockito.mock(Language.class);
Mockito.when(second.confidence()).thenReturn(0.7);
final Language third = Mockito.mock(Language.class);
Mockito.when(third.confidence()).thenReturn(0.4);
final Languages languages = new ListedLanguages(
List.of(first, second, third)
);
MatcherAssert.assertThat(
languages.bestMatch(),
Matchers.is(second)
);
}

}

1 comment on commit b27a0e1

@zoeself
Copy link
Collaborator

@zoeself zoeself commented on b27a0e1 Jul 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amihaiemil I've closed the Issues [#10] since their to-dos disappeared from the code.

The to-dos may have been removed in an earlier commit, but I've found it just now.

Please sign in to comment.