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

Removes generating a separate class/struct for nested sequence and scalar type #111

Merged
merged 8 commits into from
Jun 10, 2024
Merged
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 string
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
("foo" "bar" "baz") // expected list
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
Expand Up @@ -37,7 +37,7 @@ class CodeGenTest {
// getter tests for `StructWithFields`
assertEquals("hello", s.getA(), "s.getA() should return \"hello\"");
assertEquals(12, s.getB(), "s.getB() should return `12`");
assertEquals(3, s.getC().getValue().size(), "s.getC().getValue() should return ArrayList fo size 3");
assertEquals(3, s.getC().size(), "s.getC() should return ArrayList fo size 3");
assertEquals(10e2, s.getD(), "s.getD() should return `10e2`");

// setter tests for `StructWithFields`
Expand All @@ -46,7 +46,7 @@ class CodeGenTest {
s.setB(6);
assertEquals(6, s.getB(), "s.getB() should return `6`");
s.setC(new ArrayList<String>());
assertEquals(true, s.getC().getValue().isEmpty(), "s.getC().isEmpty() should return `true`");
assertEquals(true, s.getC().isEmpty(), "s.getC().isEmpty() should return `true`");
s.setD(11e3);
assertEquals(11e3 ,s.getD(), "s.getD() should return `11e3`");
}
Expand All @@ -68,7 +68,7 @@ class CodeGenTest {
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().getValue().size(), "n.getC().getE().getValue().size() should return ArrayList fo size 3");
assertEquals(3, n.getC().getE().size(), "n.getC().getE().size() should return ArrayList fo size 3");

// setter tests for `NestedStruct`
n.setA("hi");
Expand All @@ -78,7 +78,39 @@ class CodeGenTest {
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().getValue().size(), "s.getC().getE().getValue().size() should return ArrayList fo size 0");
assertEquals(0, n.getC().getE().size(), "s.getC().getE().size() should return ArrayList fo size 0");
}

@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\"");
}

@Test void roundtripGoodTestForStructWithFields() throws IOException {
Expand Down Expand Up @@ -152,4 +184,76 @@ class CodeGenTest {
}
}
}

@Test void roundtripGoodTestForSequence() throws IOException {
File dir = new File("./../../input/good/sequence");
desaikd marked this conversation as resolved.
Show resolved Hide resolved
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();
Sequence s = Sequence.readFrom(reader);
IonWriter writer = b.build(out);
s.writeTo(writer);
writer.close();
assertEquals(ionLoader.load(f), ionLoader.load(out.toByteArray()));
}
}
}

@Test void roundtripBadTestForSequence() throws IOException {
File dir = new File("./../../input/bad/sequence");
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, () -> { Sequence s = Sequence.readFrom(reader); });
}
}
}

@Test void roundtripGoodTestForScalar() throws IOException {
File dir = new File("./../../input/good/scalar");
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();
Scalar s = Scalar.readFrom(reader);
IonWriter writer = b.build(out);
s.writeTo(writer);
writer.close();
assertEquals(ionLoader.load(f), ionLoader.load(out.toByteArray()));
}
}
}

@Test void roundtripBadTestForScalar() throws IOException {
File dir = new File("./../../input/bad/scalar");
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, () -> { Scalar s = Scalar.readFrom(reader); });
}
}
}
}
66 changes: 66 additions & 0 deletions code-gen-projects/rust/code-gen-demo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,70 @@ mod tests {

Ok(())
}

#[test_resources("../../input/good/scalar/**/*.ion")]
fn roundtrip_good_test_generated_code_scalar(file_name: &str) -> SerdeResult<()> {
let ion_string = fs::read_to_string(file_name).unwrap();
let mut reader = ReaderBuilder::new().build(ion_string.clone())?;
let mut buffer = Vec::new();
let mut text_writer = TextWriterBuilder::default().build(&mut buffer)?;
// read given Ion value using Ion reader
reader.next()?;
let scalar: Scalar = Scalar::read_from(&mut reader)?;
// write the generated abstract data type using Ion writer
scalar.write_to(&mut text_writer)?;
text_writer.flush()?;
// compare given Ion value with round tripped Ion value written using abstract data type's `write_to` API
assert_eq!(
Element::read_one(text_writer.output().as_slice())?,
(Element::read_one(&ion_string)?)
);

Ok(())
}

#[test_resources("../../input/bad/scalar/**/*.ion")]
fn roundtrip_bad_test_generated_code_scalar(file_name: &str) -> SerdeResult<()> {
let ion_string = fs::read_to_string(file_name).unwrap();
let mut reader = ReaderBuilder::new().build(ion_string.clone())?;
// read given Ion value using Ion reader
reader.next()?;
let result = Scalar::read_from(&mut reader);
assert!(result.is_err());

Ok(())
}

#[test_resources("../../input/good/sequence/**/*.ion")]
fn roundtrip_good_test_generated_code_sequence(file_name: &str) -> SerdeResult<()> {
let ion_string = fs::read_to_string(file_name).unwrap();
let mut reader = ReaderBuilder::new().build(ion_string.clone())?;
let mut buffer = Vec::new();
let mut text_writer = TextWriterBuilder::default().build(&mut buffer)?;
// read given Ion value using Ion reader
reader.next()?;
let sequence: Sequence = Sequence::read_from(&mut reader)?;
// write the generated abstract data type using Ion writer
sequence.write_to(&mut text_writer)?;
text_writer.flush()?;
// compare given Ion value with round tripped Ion value written using abstract data type's `write_to` API
assert_eq!(
Element::read_one(text_writer.output().as_slice())?,
(Element::read_one(&ion_string)?)
);

Ok(())
}

#[test_resources("../../input/bad/sequence/**/*.ion")]
fn roundtrip_bad_test_generated_code_sequence(file_name: &str) -> SerdeResult<()> {
let ion_string = fs::read_to_string(file_name).unwrap();
let mut reader = ReaderBuilder::new().build(ion_string.clone())?;
// read given Ion value using Ion reader
reader.next()?;
let result = Sequence::read_from(&mut reader);
assert!(result.is_err());

Ok(())
}
}
4 changes: 4 additions & 0 deletions code-gen-projects/schema/scalar.isl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type::{
name: scalar,
type: string
}
5 changes: 5 additions & 0 deletions code-gen-projects/schema/sequence.isl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type::{
name: sequence,
type: list,
element: string
}
Loading
Loading