Skip to content

Commit

Permalink
Add test for atom order stability (#290)
Browse files Browse the repository at this point in the history
* Add test for atom order stability.

* Made test for iteration order a unit test

# Conflicts:
#	string-cache-codegen/tests/reproducibility_test.rs
  • Loading branch information
Ygg01 authored Feb 19, 2025
1 parent 14ae86a commit d9e888f
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions string-cache-codegen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
#![recursion_limit = "128"]

use quote::quote;
use std::collections::HashSet;
use std::collections::BTreeSet;
use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::path::Path;
Expand All @@ -81,7 +81,7 @@ pub struct AtomType {
static_set_doc: Option<String>,
macro_name: String,
macro_doc: Option<String>,
atoms: HashSet<String>,
atoms: BTreeSet<String>,
}

impl AtomType {
Expand Down Expand Up @@ -114,7 +114,7 @@ impl AtomType {
atom_doc: None,
static_set_doc: None,
macro_doc: None,
atoms: HashSet::new(),
atoms: BTreeSet::new(),
}
}

Expand Down Expand Up @@ -181,6 +181,26 @@ impl AtomType {
)
}

#[cfg(test)]
/// Write generated code to destination [`Vec<u8>`] and return it as [`String`]
///
/// Used mostly for testing or displaying a value.
pub fn write_to_string(&mut self, mut destination: Vec<u8>) -> io::Result<String>
{
destination.write_all(
self.to_tokens()
.to_string()
// Insert some newlines to make the generated code slightly easier to read.
.replace(" [ \"", "[\n\"")
.replace("\" , ", "\",\n")
.replace(" ( \"", "\n( \"")
.replace("; ", ";\n")
.as_bytes(),
)?;
let str = String::from_utf8(destination).unwrap();
Ok(str)
}

fn to_tokens(&mut self) -> proc_macro2::TokenStream {
// `impl Default for Atom` requires the empty string to be in the static set.
// This also makes sure the set in non-empty,
Expand Down Expand Up @@ -315,3 +335,16 @@ impl AtomType {
self.write_to(BufWriter::new(File::create(path)?))
}
}

#[test]
fn test_iteration_order() {
let x1 = crate::AtomType::new("foo::Atom", "foo_atom!")
.atoms(&["x", "xlink", "svg", "test"])
.write_to_string(Vec::new()).expect("write to string cache x1");

let x2 = crate::AtomType::new("foo::Atom", "foo_atom!")
.atoms(&["x", "xlink", "svg", "test"])
.write_to_string(Vec::new()).expect("write to string cache x2");

assert_eq!(x1, x2);
}

0 comments on commit d9e888f

Please sign in to comment.