Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions string-cache-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ edition = "2018"
name = "string_cache_codegen"
path = "lib.rs"

[[test]]
name = "reproducibility_test"
harness = true

[dependencies]
phf_generator = "0.11"
phf_shared = "0.11"
Expand Down
25 changes: 22 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,25 @@ impl AtomType {
)
}

/// 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
15 changes: 15 additions & 0 deletions string-cache-codegen/tests/reproducibility_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use string_cache_codegen;

#[test]
fn test_iteration_order() {

let x1 = string_cache_codegen::AtomType::new("foo::Atom", "foo_atom!")
.atoms(&["x", "xlink", "svg", "test"])
.write_to_string(Vec::new()).unwrap();

let x2 = string_cache_codegen::AtomType::new("foo::Atom", "foo_atom!")
.atoms(&["x", "xlink", "svg", "test"])
.write_to_string(Vec::new()).unwrap();

assert_eq!(x1, x2);
}
Loading