Skip to content

Commit

Permalink
feat(objectionary#404): add hello-world pack test
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Aug 21, 2024
1 parent 501251c commit 00f5648
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 56 deletions.
88 changes: 32 additions & 56 deletions src/test/java/it/AgentsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
package it;

import com.jcabi.xml.XMLDocument;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.cactoos.Scalar;
import org.cactoos.scalar.Sticky;
Expand Down Expand Up @@ -101,29 +103,18 @@ void verifiesAgents(final String yaml) {
private static final class InstructionPack {

/**
* Integer pattern.
* The pattern to match instructions.
* It matches:
* 1. Boolean values. Example: true, false.
* 2. Double values. Example: 10.12.
* 3. Long values. Example: 100L.
* 4. Integer values. Example: 100.
* 5. String values. Example: "Hello world!".
* 6. Instruction names. Example: LDC.
*/
private static final Pattern INTEGER = Pattern.compile("\\d+");

/**
* Boolean pattern.
*/
private static final Pattern BOOLEAN = Pattern.compile("true|false");

/**
* Double pattern.
*/
private static final Pattern DOUBLE = Pattern.compile("\\d+\\.\\d+");

/**
* Long pattern.
*/
private static final Pattern LONG = Pattern.compile("\\d+L");

/**
* String pattern.
*/
private static final Pattern STRING = Pattern.compile("\"*\"");
private static final Pattern INSTRUCTION = Pattern.compile(
"(\\bfalse|true\\b)|(\\d+\\.\\d+)|(\\d+L)|(\\d+)|\"([^\"]*)\"|(\\S+)"
);

/**
* Yaml pack.
Expand Down Expand Up @@ -181,41 +172,26 @@ Instruction[] instructions() {
* @return Instruction.
*/
private Instruction parse(final String instr) {
final String[] args = instr.split(" ");
final String opcode = args[0];
if (args.length > 1) {
return new OpcodeInstruction(
new OpcodeName(opcode).code(),
this.typed(Arrays.copyOfRange(args, 1, args.length))
);
} else {
return new OpcodeInstruction(new OpcodeName(opcode).code());
}
}

/**
* Typed arguments.
* @param args Arguments as strings.
* @return Typed arguments.
*/
private Object[] typed(final String... args) {
return Arrays.stream(args).map(arg -> {
if (InstructionPack.INTEGER.matcher(arg).matches()) {
return Integer.parseInt(arg);
} else if (InstructionPack.BOOLEAN.matcher(arg).matches()) {
return Boolean.parseBoolean(arg);
} else if (InstructionPack.DOUBLE.matcher(arg).matches()) {
return Double.parseDouble(arg);
} else if (InstructionPack.LONG.matcher(arg).matches()) {
return Long.parseLong(arg.substring(0, arg.length() - 1));
} else if (InstructionPack.STRING.matcher(arg).matches()) {
return arg.substring(1, arg.length() - 1);
final Matcher matcher = InstructionPack.INSTRUCTION.matcher(instr);
final AtomicInteger opcode = new AtomicInteger();
final List<Object> arguments = new ArrayList<>(0);
while (matcher.find()) {
if (matcher.group(1) != null) {
arguments.add(Boolean.parseBoolean(matcher.group(1)));
} else if (matcher.group(2) != null) {
arguments.add(Double.parseDouble(matcher.group(2)));
} else if (matcher.group(3) != null) {
arguments.add(Long.parseLong(matcher.group(3)));
} else if (matcher.group(4) != null) {
arguments.add(Integer.parseInt(matcher.group(4)));
} else if (matcher.group(5) != null) {
final String group = matcher.group(5);
arguments.add(group);
} else {
throw new IllegalArgumentException(
String.format("Unknown type of argument: %s", arg)
);
opcode.set(new OpcodeName(matcher.group(6)).code());
}
}).toArray();
}
return new OpcodeInstruction(opcode.get(), arguments.toArray());
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/test/resources/agents/hello_world.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
opcodes:
- GETSTATIC "java/lang/System" "out" "Ljava/io/PrintStream;"
- LDC "Hello world!"
- INVOKEVIRTUAL "java/io/PrintStream" "println" "(Ljava/lang/String;)V" false
agents:
- GetStaticAgent
- LdcAgent
- InvokevirtualAgent
eo: |
*
static-field
"descriptor=Ljava/io/PrintStream;|name=out|owner=java/lang/System"
.println
"descriptor=(Ljava/lang/String;)V|interfaced=false|name=println|owner=java/io/PrintStream|type=method"
load-constant
"Hello world!"

0 comments on commit 00f5648

Please sign in to comment.