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

Add test for atom order stability #290

Merged
merged 2 commits into from
Feb 19, 2025
Merged
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
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>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this could be annotated with #[cfg(test)]. I think it isn't useful otherwise and this can help to avoid breaking API in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I annotated it, but it caused breakage in the test. So I converted tests to unit test.

{
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);
}
Loading