Skip to content

Commit

Permalink
Refactoring with Cactoos
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-semenyuk committed Jul 21, 2017
1 parent 98daa0a commit 09cdec2
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public JavaInterface(final String name, final Collection<Method> methods) {

@Override
public String path() {
return String.format("%s.java", this.name);
return new UncheckedText(
new FormattedText("%s.java", this.name)
).asString();
}

@Override
Expand All @@ -77,7 +79,9 @@ public Input code() {
"\n ",
new MappedIterable<>(
this.methods,
mtd -> String.format("%s;", mtd.java())
mtd -> new UncheckedText(
new FormattedText("%s;", mtd.java())
).asString()
)
)
).asString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package org.eolang.compiler.syntax;

import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* Attribute as java constructor initializer.
*
Expand All @@ -34,6 +37,8 @@ public final class AttrCtorInitFormat implements AttributeFormat {

@Override
public String code(final String type, final String name) {
return String.format("this.%s = %s;", name, name);
return new UncheckedText(
new FormattedText("this.%s = %s;", name, name)
).asString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package org.eolang.compiler.syntax;

import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* Attribute as java class field.
*
Expand All @@ -34,6 +37,8 @@ public final class AttrFieldFormat implements AttributeFormat {

@Override
public String code(final String type, final String name) {
return String.format("private final %s %s;", type, name);
return new UncheckedText(
new FormattedText("private final %s %s;", type, name)
).asString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package org.eolang.compiler.syntax;

import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* Method implementation.
*
Expand Down Expand Up @@ -67,10 +70,12 @@ public MethodImpl(
* @return Java code.
*/
public String java() {
return String.format(
"public %s {\n return %s;\n}",
this.declaration.java(),
this.copying.java()
);
return new UncheckedText(
new FormattedText(
"public %s {\n return %s;\n}",
this.declaration.java(),
this.copying.java()
)
).asString();
}
}
30 changes: 23 additions & 7 deletions eo-compiler/src/test/java/org/eolang/compiler/ProgramTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import org.cactoos.io.BytesAsInput;
import org.cactoos.io.InputAsBytes;
import org.cactoos.io.PathAsInput;
import org.cactoos.io.ResourceAsInput;
import org.cactoos.list.IterableAsList;
import org.hamcrest.MatcherAssert;
Expand Down Expand Up @@ -58,7 +60,9 @@ public void processZeroExample() throws Exception {
program.compile();
MatcherAssert.assertThat(
new String(
Files.readAllBytes(dir.resolve(Paths.get("zero.java")))
new InputAsBytes(
new PathAsInput(dir.resolve(Paths.get("zero.java")))
).asBytes()
),
Matchers.stringContainsInOrder(
new IterableAsList<>(
Expand Down Expand Up @@ -96,7 +100,9 @@ public void parsesFibonacciExample() throws Exception {
program.compile();
MatcherAssert.assertThat(
new String(
Files.readAllBytes(dir.resolve(Paths.get("fibonacci.java")))
new InputAsBytes(
new PathAsInput(dir.resolve(Paths.get("fibonacchi.java")))
).asBytes()
),
Matchers.stringContainsInOrder(
new IterableAsList<>(
Expand Down Expand Up @@ -145,7 +151,9 @@ public void parsesSimpleType() throws Exception {
program.compile();
MatcherAssert.assertThat(
new String(
Files.readAllBytes(dir.resolve(Paths.get("Book.java")))
new InputAsBytes(
new PathAsInput(dir.resolve(Paths.get("Book.java")))
).asBytes()
),
Matchers.allOf(
Matchers.containsString("interface Book"),
Expand All @@ -168,7 +176,9 @@ public void parsesTypeWithParametrizedMethods() throws Exception {
program.compile();
MatcherAssert.assertThat(
new String(
Files.readAllBytes(dir.resolve(Paths.get("Pixel.java")))
new InputAsBytes(
new PathAsInput(dir.resolve(Paths.get("Pixel.java")))
).asBytes()
),
Matchers.allOf(
Matchers.containsString("interface Pixel"),
Expand All @@ -193,7 +203,9 @@ public void parsesBigType() throws Exception {
program.compile();
MatcherAssert.assertThat(
new String(
Files.readAllBytes(dir.resolve(Paths.get("Car.java")))
new InputAsBytes(
new PathAsInput(dir.resolve(Paths.get("Car.java")))
).asBytes()
),
Matchers.allOf(
Matchers.containsString("interface Car"),
Expand All @@ -219,7 +231,9 @@ public void parsesMultipleTypes() throws Exception {
program.compile();
MatcherAssert.assertThat(
new String(
Files.readAllBytes(dir.resolve(Paths.get("Number.java")))
new InputAsBytes(
new PathAsInput(dir.resolve(Paths.get("Number.java")))
).asBytes()
),
Matchers.allOf(
Matchers.containsString("interface Number"),
Expand All @@ -229,7 +243,9 @@ public void parsesMultipleTypes() throws Exception {
);
MatcherAssert.assertThat(
new String(
Files.readAllBytes(dir.resolve(Paths.get("Text.java")))
new InputAsBytes(
new PathAsInput(dir.resolve(Paths.get("Text.java")))
).asBytes()
),
Matchers.allOf(
Matchers.containsString("interface Text"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.eolang.compiler.java;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import org.cactoos.InputHasContent;
Expand Down Expand Up @@ -58,14 +57,14 @@ public void classCode() {
name,
Collections.singleton(type),
new ObjectBody(
Collections.emptyList(),
Collections.emptyList(),
Collections.emptyList()
new IterableAsList<>(),
new IterableAsList<>(),
new IterableAsList<>()
)
).code(),
new InputHasContent(
Matchers.stringContainsInOrder(
Arrays.asList(
new IterableAsList<>(
"public",
"final",
"class",
Expand All @@ -86,7 +85,7 @@ public void classCode() {
@Test
public void multiTypes() {
final String name = "pdf";
final Collection<String> types = Arrays.asList("Text", "Book");
final Collection<String> types = new IterableAsList<>("Text", "Book");
MatcherAssert.assertThat(
new JavaClass(
name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.Collections;
import org.cactoos.InputHasContent;
import org.cactoos.list.IterableAsList;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
Expand Down Expand Up @@ -76,10 +78,12 @@ public void interfacePath() {
Collections.emptyList()
).path().toString(),
Matchers.equalTo(
String.format(
"%s.java",
name
)
new UncheckedText(
new FormattedText(
"%s.java",
name
)
).asString()
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ public void javaCode() {
"Type",
"name"
).java(
(type, name) ->
new UncheckedText(
new FormattedText("%s:%s", type, name)
).asString()
(type, name) -> new UncheckedText(
new FormattedText("%s:%s", type, name)
).asString()
),
Matchers.equalTo(
"Type:name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.eolang.compiler.syntax;

import java.util.Collections;
import org.cactoos.list.IterableAsList;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
Expand All @@ -48,7 +47,7 @@ public void javaCode() {
new MethodImpl(
new Method(
"text",
Collections.singleton(
new IterableAsList<>(
new Parameter(
"locale",
"Locale"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.eolang.compiler.syntax;

import java.util.Collections;
import org.cactoos.list.IterableAsList;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -74,8 +73,8 @@ public void fieldsAndPrimaryCtorTest() {
public void secondaryConstructorTest() {
MatcherAssert.assertThat(
new ObjectBody(
Collections.emptyList(),
Collections.singleton(
new IterableAsList<>(),
new IterableAsList<>(
new Ctor(
new IterableAsList<>(
new Parameter("title", "Text")
Expand Down

0 comments on commit 09cdec2

Please sign in to comment.