Skip to content

Commit

Permalink
Suggestions return Set instead of List + Use dumb terminal for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strogiyotec committed Sep 9, 2020
1 parent 4b9bc75 commit 24b6629
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>jfill</groupId>
<artifactId>jfill</artifactId>
<version>2.2.1</version>
<version>2.2.2</version>


<properties>
Expand Down
6 changes: 4 additions & 2 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
# upload this zipped folder as a release in github

mvn clean package
echo "Native image compiled"
cd target
jfill_version=$(./jfill -v | grep -oP "([0-9.])+")
release_name="jfill-${jfill_version}"
mkdir $release_name
echo "Start upx compression"
upx_target="${release_name}/jfill"
echo $upx_target
upx -o $upx_target jfill
echo "upx file ${upx_target} is ready"
zipped_file="${release_name}.zip"
zip -r $zipped_file $release_name
echo "${release_name} is ready"
echo "${zipped_file} release is ready"

6 changes: 3 additions & 3 deletions src/main/java/jfill/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ boolean addEntry(final String tag, final Map<String, String> entry) {
* @param tag Tag
* @return Cache for given group
*/
List<String> historyPerKey(final String tag, final String key) {
Set<String> historyPerKey(final String tag, final String key) {
if (this.inMemoryCache.has(tag)) {
var cache = this.inMemoryCache.at(tag).at("values").asJsonList();
return cache.stream()
.filter(json -> json.has(key))
.map(json -> json.at(key).asString())
.collect(Collectors.toList());
.collect(Collectors.toSet());
} else {
return Collections.emptyList();
return Collections.emptySet();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jfill/Defaults.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface Defaults {

String NO_TAG = "noTag";

String VERSION = "jfillin 2.2.1";
String VERSION = "jfillin 2.2.2";

String CACHE_PATH = Optional.ofNullable(System.getenv("XDG_CONFIG_HOME"))
.or(() -> Optional.of(System.getProperty("user.home")))
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/jfill/ResolveFromTerminal.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ResolvedValues resolve(final Arguments arguments) {
for (var arg : arguments) {
if (arg.hasTag()) {
resolvedValues.addTagIfAbsent(arg.getTag());
//if not in the storage
//if value is already resolved, could happend when command contains duplicates
if (!resolvedValues.tagHasKey(arg.getTag(), arg.getKey())) {
var group = new TagGroup(arguments, arg.getTag());
if (!group.getKeys().isEmpty()) {
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/jfill/Suggestions.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
package jfill;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

class Suggestions {

private final List<String> suggestions;
private final Set<String> suggestions;

Suggestions(final List<String> suggestions) {
Suggestions(final Set<String> suggestions) {
this.suggestions = suggestions;
}

Suggestions(final List<Map<String, String>> history, final String delimiter) {
this.suggestions = history.stream()
.map(map -> String.join(delimiter, map.values()))
.collect(Collectors.toList());
.collect(Collectors.toSet());
}

Suggestions(final List<Map<String, String>> history, Function<Map<String, String>, String> mapper) {
this.suggestions = history.stream()
.map(mapper)
.filter(Objects::nonNull)
.collect(Collectors.toList());
.collect(Collectors.toSet());
}

/**
* Get list of suggestions.
*
* @return Suggestions
*/
List<String> get() {
Set<String> get() {
return this.suggestions;
}

Expand Down
5 changes: 3 additions & 2 deletions src/test/java/jfill/CacheTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

final class CacheTestCase {

Expand Down Expand Up @@ -47,7 +48,7 @@ void testFileCreated(@TempDir final Path tempPath) throws IOException {
void testHistoryForWord() {
Assertions.assertEquals(
cache.historyPerKey("psql", "user"),
List.of("postgres", "admin")
Set.of("postgres", "admin")
);

}
Expand All @@ -57,7 +58,7 @@ void testHistoryForWord() {
void testHistoryIsEmpty() {
Assertions.assertEquals(
cache.historyPerKey("psql", "connections"),
Collections.emptyList()
Collections.emptySet()
);
}

Expand Down
22 changes: 22 additions & 0 deletions src/test/java/jfill/SuggestionsTestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package jfill;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

public final class SuggestionsTestCase {


@Test
void testDuplicates() {
var suggestions = new Suggestions(
List.of(
Map.of("key", "value"),
Map.of("key", "value")
),
","
);
System.out.println(suggestions.get());
}
}
9 changes: 5 additions & 4 deletions src/test/java/jfill/TerminalInputTestCase.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jfill;

import org.jline.terminal.TerminalBuilder;
import org.jline.terminal.impl.DumbTerminal;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand All @@ -13,7 +14,7 @@ final class TerminalInputTestCase {

@Test
void testListInput() throws IOException {
try (var terminal = TerminalBuilder.builder().build()) {
try (var terminal = new DumbTerminal(System.in, System.out)) {
var terminalInput = new TerminalInput(
new MockedLineReader(
terminal,
Expand All @@ -23,7 +24,7 @@ void testListInput() throws IOException {
Assertions.assertEquals(
terminalInput.getValue(
List.of("name"),
new Suggestions(Collections.emptyList())
new Suggestions(Collections.emptySet())
),
"Almas"
);
Expand All @@ -32,7 +33,7 @@ void testListInput() throws IOException {

@Test
void testSingleInput() throws IOException {
try (var terminal = TerminalBuilder.builder().build()) {
try (var terminal = new DumbTerminal(System.in, System.out)) {
var terminalInput = new TerminalInput(
new MockedLineReader(
terminal,
Expand All @@ -42,7 +43,7 @@ void testSingleInput() throws IOException {
Assertions.assertEquals(
terminalInput.getValue(
"name",
new Suggestions(Collections.emptyList())
new Suggestions(Collections.emptySet())
),
"Almas"
);
Expand Down

0 comments on commit 24b6629

Please sign in to comment.