Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tests for new model changes #146

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// nested struct with mismatched sequence type
{
A: "hello",
B: 12,
C: {
D: false,
E: (1 2 3) // expected list
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
B: 12,
C: {
D: 1e0, // expected type: bool
E: [1, 2, 3]
}
}
1 change: 1 addition & 0 deletions code-gen-projects/input/bad/scalar/mismatched_type.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12 // expected string
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1, 2, 3] // expected list of strings
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
("foo" "bar" "baz") // expected list
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// struct with mismatched sequence element
{
A: "hello",
B: 12,
C: (1 2 3), // expected sexpression of strings
D: 10e2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// simple struct with type mismatched sequence type
{
A: "hello",
B: 12,
C: ["foo", "bar", "baz"], // expected sexp
D: 10e2
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
A: "hello",
B: false, // expected field type: int
C: ("foo" "bar" "baz"),
D: 10e2
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
C: {
D: false,
E: [],
},
A: "",
B: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
B: 12,
C: {
D: false,
E: [1, 2, 3]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
A: "hello",
C: {
D: false,
E: [1, 2, 3]
}
}

2 changes: 2 additions & 0 deletions code-gen-projects/input/good/scalar/empty_value.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// empty string
""
2 changes: 2 additions & 0 deletions code-gen-projects/input/good/scalar/valid_value.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// a scalar value of string type
"Hello World!"
1 change: 1 addition & 0 deletions code-gen-projects/input/good/sequence/empty_sequence.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
1 change: 1 addition & 0 deletions code-gen-projects/input/good/sequence/valid_elements.ion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["foo", "bar", "baz"]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// struct with empty list, empty string and zeros
{
C: (),
A: "",
B: 0,
D: 0e0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
{
A: "hello",
B: 12,
C: ("foo" "bar" "baz"),
D: 10e2
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

// struct with unordered fields
{
C: ("foo" "bar" "baz"),
A: "hello",
B: 12,
D: 10e2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.File;

class CodeGenTest {
Expand Down Expand Up @@ -46,95 +47,147 @@ class CodeGenTest {
@Test void getterAndSetterTestForNestedStruct() {
// getter tests for `NestedStruct`
NestedStruct n = new NestedStruct();
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(1);
a.add(2);
a.add(3);

// set all the fields of `NestedStruct`
n.setA("hello");
n.setB(12);
n.setC(false);
n.setC(false, a);

// getter tests for `NestedStruct`
assertEquals("hello", n.getA(), "n.getA() should return \"hello\"");
assertEquals(12, n.getB(), "n.getB() should return `12`");
assertEquals(false, n.getC().getD(), "n.getC().getD() should return `false`");
assertEquals(3, n.getC().getE().size(), "n.getC().getE().size() should return ArrayList fo size 3");

// setter tests for `NestedStruct`
n.setA("hi");
assertEquals("hi", n.getA(), "s.getA() should return \"hi\"");
n.setB(6);
assertEquals(6, n.getB(), "s.getB() should return `6`");
n.getC().setD(true);
assertEquals(true, n.getC().getD(), "s.getC().getD() should return `true`");
// setter tests for `NestedStruct`
n.setA("hi");
assertEquals("hi", n.getA(), "s.getA() should return \"hi\"");
n.setB(6);
assertEquals(6, n.getB(), "s.getB() should return `6`");
n.getC().setD(true);
assertEquals(true, n.getC().getD(), "s.getC().getD() should return `true`");
n.getC().setE(new ArrayList<Integer>());
assertEquals(0, n.getC().getE().size(), "s.getC().getE().size() should return ArrayList fo size 0");
}

@Test void roundtripGoodTestForStructWithFields() throws IOException {
File dir = new File(System.getenv("ION_INPUT") + "/good/struct_with_fields");
String[] fileNames = dir.list();
for (String fileName : fileNames) {
File f = new File(dir, fileName);
InputStream inputStream = new FileInputStream(f);
IonTextWriterBuilder b = IonTextWriterBuilder.standard();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonReaderBuilder readerBuilder = IonReaderBuilder.standard();
try (IonReader reader = readerBuilder.build(inputStream)) {
reader.next();
StructWithFields s = StructWithFields.readFrom(reader);
IonWriter writer = b.build(out);
s.writeTo(writer);
writer.close();
assertEquals(ionLoader.load(f), ionLoader.load(out.toByteArray()));
}
}
@Test void getterAndSetterTestForSequence() {
ArrayList<String> a = new ArrayList<String>();
a.add("foo");
a.add("bar");
a.add("baz");
Sequence s = new Sequence();

// set all the fields of `Sequence`
s.setValue(a);

// getter tests for `Sequence`
assertEquals(3, s.getValue().size(), "s.getValue().size() should return ArrayList fo size 3");

// setter tests for `Sequence`
s.setValue(new ArrayList<String>());
assertEquals(true, s.getValue().isEmpty(), "s.getValue().isEmpty() should return `true`");
}

@Test void getterAndSetterTestForScalar() {
Scalar s = new Scalar();

// set all the fields of `Scalar`
s.setValue("hello");

// getter tests for `Scalar`
assertEquals("hello", s.getValue(), "s.getValue() should return \"hello\"");

// setter tests for `Scalar`
s.setValue("hi");
assertEquals("hi", s.getValue(), "s.getValue() should return \"hi\"");
}

@FunctionalInterface
interface ReaderFunction<T> {
T read(IonReader reader) throws IOException;
}

@FunctionalInterface
interface WriterFunction<T> {
void write(T item, IonWriter writer) throws IOException;
}

@Test
void roundtripBadTestForScalar() throws IOException {
runRoundtripBadTest("/bad/scalar", Scalar::readFrom);
}

@Test
void roundtripBadTestForSequence() throws IOException {
runRoundtripBadTest("/bad/sequence", Sequence::readFrom);
}

@Test void roundtripBadTestForStructWithFields() throws IOException {
File dir = new File(System.getenv("ION_INPUT") + "/bad/struct_with_fields");
@Test
void roundtripBadTestForStructWithFields() throws IOException {
runRoundtripBadTest("/bad/struct_with_fields", StructWithFields::readFrom);
}

@Test
void roundtripBadTestForNestedStruct() throws IOException {
runRoundtripBadTest("/bad/nested_struct", NestedStruct::readFrom);
}

private <T> void runRoundtripBadTest(String path, ReaderFunction<T> readerFunction) throws IOException {
File dir = new File(System.getenv("ION_INPUT") + path);
String[] fileNames = dir.list();
for (String fileName : fileNames) {
File f = new File(dir, fileName);
InputStream inputStream = new FileInputStream(f);
IonTextWriterBuilder b = IonTextWriterBuilder.standard();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonReaderBuilder readerBuilder = IonReaderBuilder.standard();
try (IonReader reader = readerBuilder.build(inputStream)) {
try (InputStream inputStream = new FileInputStream(f);
BufferedInputStream bufferedStream = new BufferedInputStream(inputStream);
IonReader reader = IonReaderBuilder.standard().build(bufferedStream)) {
reader.next();
assertThrows(Throwable.class, () -> { StructWithFields s = StructWithFields.readFrom(reader); });
assertThrows(Throwable.class, () -> readerFunction.read(reader));
}
}
}

@Test void roundtripGoodTestForNestedStruct() throws IOException {
File dir = new File(System.getenv("ION_INPUT") + "/good/nested_struct");
@Test
void roundtripGoodTestForScalar() throws IOException {
runRoundtripGoodTest("/good/scalar", Scalar::readFrom, (item, writer) -> item.writeTo(writer));
}

@Test
void roundtripGoodTestForSequence() throws IOException {
runRoundtripGoodTest("/good/sequence", Sequence::readFrom, (item, writer) -> item.writeTo(writer));
}

@Test
void roundtripGoodTestForStructWithFields() throws IOException {
runRoundtripGoodTest("/good/struct_with_fields", StructWithFields::readFrom, (item, writer) -> item.writeTo(writer));
}

@Test
void roundtripGoodTestForNestedStruct() throws IOException {
runRoundtripGoodTest("/good/nested_struct", NestedStruct::readFrom, (item, writer) -> item.writeTo(writer));
}

private <T> void runRoundtripGoodTest(String path, ReaderFunction<T> readerFunction, WriterFunction<T> writerFunction) throws IOException {
File dir = new File(System.getenv("ION_INPUT") + path);
String[] fileNames = dir.list();
for (String fileName : fileNames) {
File f = new File(dir, fileName);
InputStream inputStream = new FileInputStream(f);
BufferedInputStream bufferedStream = new BufferedInputStream(inputStream);
IonTextWriterBuilder b = IonTextWriterBuilder.standard();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonReaderBuilder readerBuilder = IonReaderBuilder.standard();
try (IonReader reader = readerBuilder.build(inputStream)) {
try (IonReader reader = readerBuilder.build(bufferedStream)) {
reader.next();
NestedStruct n = NestedStruct.readFrom(reader);
IonWriter writer = b.build(out);
n.writeTo(writer);
T item = readerFunction.read(reader);
writerFunction.write(item, writer);
writer.close();
assertEquals(ionLoader.load(f), ionLoader.load(out.toByteArray()));
}
}
}

@Test void roundtripBadTestForNestedStruct() throws IOException {
File dir = new File(System.getenv("ION_INPUT") + "/bad/nested_struct");
String[] fileNames = dir.list();
for (String fileName : fileNames) {
File f = new File(dir, fileName);
InputStream inputStream = new FileInputStream(f);
IonTextWriterBuilder b = IonTextWriterBuilder.standard();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IonReaderBuilder readerBuilder = IonReaderBuilder.standard();
try (IonReader reader = readerBuilder.build(inputStream)) {
reader.next();
assertThrows(Throwable.class, () -> { NestedStruct n = NestedStruct.readFrom(reader); });
}
}
}
}
1 change: 1 addition & 0 deletions code-gen-projects/schema/nested_struct.isl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type::{
type: struct,
fields: {
D: bool,
E: { type: list, element: int }
}
}
}
Expand Down
1 change: 1 addition & 0 deletions code-gen-projects/schema/struct_with_fields.isl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type::{
fields: {
A: string,
B: int,
C: { element: string, type: sexp },
D: float,
}
}
Expand Down
Loading