Skip to content

Commit

Permalink
Adds new tests for sequence and scalar types
Browse files Browse the repository at this point in the history
  • Loading branch information
desaikd committed May 29, 2024
1 parent 706e04d commit c4b327f
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 8 deletions.
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");
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
}
8 changes: 4 additions & 4 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ fn test_write_all_values(#[case] number: i32, #[case] expected_output: &str) ->
}
}
"#,
&["nested_type: NestedType1"],
&["pub fn nested_type(&self) -> &NestedType1 {"]
&["nested_type: i64"],
&["pub fn nested_type(&self) -> &i64 {"]
)]
/// Calls ion-cli beta generate with different schema file. Pass the test if the return value contains the expected properties and accessors.
fn test_code_generation_in_rust(
Expand Down Expand Up @@ -391,8 +391,8 @@ fn test_code_generation_in_rust(
}
}
"#,
&["private NestedType1 nestedType;"],
&["public NestedType1 getNestedType() {"]
&["private int nestedType;"],
&["public int getNestedType() {"]
)]
/// Calls ion-cli beta generate with different schema file. Pass the test if the return value contains the expected properties and accessors.
fn test_code_generation_in_java(
Expand Down

0 comments on commit c4b327f

Please sign in to comment.