Skip to content

Commit

Permalink
feat(objectionary#976): fix some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Jan 22, 2025
1 parent 607f091 commit e2508a5
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ enum DataType {
BYTE("byte", Byte.class,
value -> ByteBuffer.allocate(Byte.BYTES).put((byte) value).array(),
bytes -> ByteBuffer.wrap(bytes).get()
// bytes -> (byte) ByteBuffer.wrap(bytes).getDouble()
),

/**
Expand All @@ -84,6 +85,7 @@ enum DataType {
SHORT("short", Short.class,
value -> ByteBuffer.allocate(Short.BYTES).putShort((short) value).array(),
bytes -> ByteBuffer.wrap(bytes).getShort()
// bytes -> (short) ByteBuffer.wrap(bytes).getDouble()
),

/**
Expand All @@ -92,13 +94,16 @@ enum DataType {
INT("int", Integer.class,
value -> ByteBuffer.allocate(Long.BYTES).putLong((int) value).array(),
bytes -> (int) ByteBuffer.wrap(bytes).getLong()
// bytes -> (int) ByteBuffer.wrap(bytes).getDouble()

),
/**
* Long.
*/
LONG("long", Long.class,
value -> ByteBuffer.allocate(Long.BYTES).putLong((long) value).array(),
bytes -> ByteBuffer.wrap(bytes).getLong()
// bytes -> (long) ByteBuffer.wrap(bytes).getDouble()
),

/**
Expand All @@ -107,6 +112,7 @@ enum DataType {
FLOAT("float", Float.class,
value -> ByteBuffer.allocate(Float.BYTES).putFloat((float) value).array(),
bytes -> ByteBuffer.wrap(bytes).getFloat()
// bytes -> (float) ByteBuffer.wrap(bytes).getDouble()
),

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ public DirectivesMethodProperties(

@Override
public Iterator<Directive> iterator() {
final DirectivesValue dirs = new DirectivesValue(this.access);
return new Directives()
.append(new DirectivesValue(this.access))
.append(dirs)
.append(new DirectivesValue(this.descriptor))
.append(new DirectivesValue(this.signature))
.append(new DirectivesValues(this.exceptions))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ public Iterator<Directive> iterator() {
}

private String bytes() {
final double value = this.number.doubleValue();
return DirectivesNumberBytes.bytesToHex(
new BytecodeValue(this.number.doubleValue()).bytes());
new BytecodeValue(value).bytes()
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ public Iterator<Directive> iterator() {
case "long":
case "float":
case "double":
final Object object1 = this.value.object();
final Number object = (Number) object1;
res = new DirectivesJeoObject(
type,
this.name,
new DirectivesComment(this.comment()),
new DirectivesNumberBytes((Number) this.value.object())
new DirectivesNumberBytes(object)
);
break;
case "string":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public BytecodeClassProperties bytecode() {
* @return Access modifiers.
*/
private int access() {
return new XmlValue(this.clazz.child("as", "access")).integer();
return (int) new XmlValue(this.clazz.child("as", "access")).object();
}

/**
Expand Down Expand Up @@ -120,7 +120,8 @@ private String[] interfaces() {
private int version() {
return this.child("version")
.map(XmlValue::new)
.map(XmlValue::integer)
.map(XmlValue::object)
.map(Integer.class::cast)
.orElse(new DefaultVersion().bytecode());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private String name() {
* @return Access modifiers.
*/
private int access() {
return this.find(Attribute.ACCESS).map(XmlValue::integer).orElse(0);
return this.find(Attribute.ACCESS).map(XmlValue::object).map(Integer.class::cast).orElse(0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public BytecodeInstruction bytecode() {
*/
@EqualsAndHashCode.Include
private int opcode() {
return new XmlValue(this.node).integer();
return (int) new XmlValue(this.node).object();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private BytecodeMethodProperties properties() {
* @return Access modifiers.
*/
private int access() {
return new XmlValue(this.child(0)).integer();
return (int) new XmlValue(this.child(0)).object();
}

/**
Expand Down
54 changes: 39 additions & 15 deletions src/main/java/org/eolang/jeo/representation/xmir/XmlValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.eolang.jeo.representation.xmir;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;
import org.eolang.jeo.representation.bytecode.BytecodeValue;
Expand Down Expand Up @@ -105,12 +106,50 @@ public boolean bool() {

/**
* Convert hex string to integer.
* @todo: Replace with {@link BytecodeValue#object()}.
* @return Integer.
*/
public int integer() {
return Integer.parseInt(this.hex(), XmlValue.RADIX);
}

/**
* Convert hex string to an object.
* @todo: Refactor
* @return Object.
*/
public Object object() {
final String base = this.base();
final Object result;
switch (base) {
case "byte":
result = (byte) ByteBuffer.wrap(this.bytes()).getDouble();
break;
case "short":
result = (short) ByteBuffer.wrap(this.bytes()).getDouble();
break;
case "int":
result = (int) ByteBuffer.wrap(this.bytes()).getDouble();
break;
case "long":
result = (long) ByteBuffer.wrap(this.bytes()).getDouble();
break;
case "float":
result = (float) ByteBuffer.wrap(this.bytes()).getDouble();
break;
case "double":
result = ByteBuffer.wrap(this.bytes()).getDouble();
break;
case "string":
result = this.string();
break;
default:
result = new BytecodeValue(base, this.bytes()).object();
break;
}
return result;
}

/**
* Convert hex string to a byte array.
* @return Byte array.
Expand All @@ -133,21 +172,6 @@ public byte[] bytes() {
return res;
}

/**
* Convert hex string to an object.
* @return Object.
*/
public Object object() {
final String base = this.base();
final Object result;
if ("string".equals(base)) {
result = this.string();
} else {
result = new BytecodeValue(base, this.bytes()).object();
}
return result;
}

/**
* Hex string.
* Example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ void convertsDefaultFieldToDirectives() throws ImpossibleModificationException {
xml,
XhtmlMatchers.hasXPaths(
"/o[contains(@base,'field') and contains(@as,'unknown')]",
"/o/o[@as='access-unknown']/o[contains(text(),'01')]",
"/o/o[@as='descriptor-unknown']/o[text()='49-']",
"/o/o[@as='access-unknown']",
"/o/o[@as='descriptor-unknown']",
"/o/o[@as='signature-unknown']",
"/o/o[contains(@base,'int') and @as='value-unknown']/o[contains(text(),'0')]"
"/o/o[contains(@base,'int') and @as='value-unknown']"
)
);
}
Expand All @@ -77,10 +77,10 @@ void convertsLongFieldToDirectives() throws ImpossibleModificationException {
xml,
XhtmlMatchers.hasXPaths(
"/o[contains(@base,'field') and contains(@as,'serialVersionUID')]",
"/o/o[@as='access-serialVersionUID']/o[contains(text(),'1A')]",
"/o/o[@as='descriptor-serialVersionUID']/o[text()='4A-']",
"/o/o[@as='access-serialVersionUID']",
"/o/o[@as='descriptor-serialVersionUID']",
"/o/o[@as='signature-serialVersionUID']",
"/o/o[@as='value-serialVersionUID']/o[text()='62-84-EB-5F-88-47-CD-E1']"
"/o/o[@as='value-serialVersionUID']"
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,18 @@ void transformsToXmir() throws ImpossibleModificationException {
final int access = 100;
final String descriptor = "()I";
final String signature = "";
final String xml = new Xembler(
new DirectivesMethod(
name,
new DirectivesMethodProperties(access, descriptor, signature)
)
).xml();
System.out.println(xml);
MatcherAssert.assertThat(
"We expect that directives will generate correct method",
new XmlMethod(
new NativeXmlNode(
new Xembler(
new DirectivesMethod(
name,
new DirectivesMethodProperties(access, descriptor, signature)
)
).xml()
xml
)
).bytecode(),
Matchers.equalTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@ final class DirectivesValueTest {

@Test
void convertsInteger() throws ImpossibleModificationException {
final String xml = new Xembler(new DirectivesValue("access", 42)).xml();
MatcherAssert.assertThat(
"We expect that integer value is converted to the correct XMIR",
new Xembler(new DirectivesValue("access", 42)).xml(),
new SameXml(
String.join(
"\n",
"<o base='jeo.int' as='access'>",
" <o base='org.eolang.bytes'>00-00-00-00-00-00-00-2A</o>",
"</o>"
)
String.format(
"We expect that integer value is converted to the correct XMIR, but got the incorrect one: %n%s%n",
xml
),
xml,
XhtmlMatchers.hasXPath(
"./o[@base='jeo.int' and @as='access']/o[@base='org.eolang.number']/o[@base='org.eolang.bytes']/text()"
)
);
}
Expand Down Expand Up @@ -233,7 +232,9 @@ static Stream<Arguments> numbers() {
Arguments.of(1.0f, "jeo.float", same),
Arguments.of(1.0d, "jeo.double", same),
Arguments.of((short) 1, "jeo.short", same),
Arguments.of((byte) 1, "jeo.byte", same)
Arguments.of((byte) 1, "jeo.byte", same),
Arguments.of(100, "jeo.int", "40-59-00-00-00-00-00-00"),
Arguments.of(1057, "jeo.int", "40-90-84-00-00-00-00-00")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ final class XmlValueTest {
@Test
void parsesHexStringAsInteger() {
final int expected = 1057;
final int actual = new XmlValue(
new NativeXmlNode("<o><o>00-00-00-00-00-00-04-21</o></o>")
).integer();
final int actual = (int) new XmlValue(
new NativeXmlNode("<o base='jeo.int'><o>40-90-84-00-00-00-00-00</o></o>")
).object();
MatcherAssert.assertThat(
String.format(
"Can't parse hex string as integer, or the result is wrong; expected %d, got %d",
Expand All @@ -62,10 +62,12 @@ void parsesHexStringAsInteger() {
@ParameterizedTest
@MethodSource("values")
void decodesEncodesCorrectly(final Object origin) {
final String xml = new Xembler(new DirectivesValue(origin)).xmlQuietly();
System.out.println(xml);
MatcherAssert.assertThat(
"Decoding and encoding are not consistent",
new XmlValue(
new NativeXmlNode(new Xembler(new DirectivesValue(origin)).xmlQuietly())
new NativeXmlNode(xml)
).object(),
Matchers.equalTo(origin)
);
Expand All @@ -90,7 +92,7 @@ void decodesUnicodeCharacters(final String unicode) throws ImpossibleModificatio
}

/**
* Arguments for {@link XmlValue#decodesEncodesCorrectly(Object, String)}.
* Arguments for {@link XmlValueTest#decodesEncodesCorrectly(Object)}.
* @return Stream of arguments.
*/
static Stream<Arguments> values() {
Expand Down

0 comments on commit e2508a5

Please sign in to comment.