Skip to content

Commit 42a87bd

Browse files
committed
Add test for atom order stability.
1 parent 14ae86a commit 42a87bd

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

string-cache-codegen/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ edition = "2018"
1212
name = "string_cache_codegen"
1313
path = "lib.rs"
1414

15+
[[test]]
16+
name = "reproducibility_test"
17+
harness = true
18+
1519
[dependencies]
1620
phf_generator = "0.11"
1721
phf_shared = "0.11"

string-cache-codegen/lib.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
#![recursion_limit = "128"]
7070

7171
use quote::quote;
72-
use std::collections::HashSet;
72+
use std::collections::BTreeSet;
7373
use std::fs::File;
7474
use std::io::{self, BufWriter, Write};
7575
use std::path::Path;
@@ -81,7 +81,7 @@ pub struct AtomType {
8181
static_set_doc: Option<String>,
8282
macro_name: String,
8383
macro_doc: Option<String>,
84-
atoms: HashSet<String>,
84+
atoms: BTreeSet<String>,
8585
}
8686

8787
impl AtomType {
@@ -114,7 +114,7 @@ impl AtomType {
114114
atom_doc: None,
115115
static_set_doc: None,
116116
macro_doc: None,
117-
atoms: HashSet::new(),
117+
atoms: BTreeSet::new(),
118118
}
119119
}
120120

@@ -181,6 +181,25 @@ impl AtomType {
181181
)
182182
}
183183

184+
/// Write generated code to destination [`Vec<u8>`] and return it as [`String`]
185+
///
186+
/// Used mostly for testing or displaying a value.
187+
pub fn write_to_string(&mut self, mut destination: Vec<u8>) -> io::Result<String>
188+
{
189+
destination.write_all(
190+
self.to_tokens()
191+
.to_string()
192+
// Insert some newlines to make the generated code slightly easier to read.
193+
.replace(" [ \"", "[\n\"")
194+
.replace("\" , ", "\",\n")
195+
.replace(" ( \"", "\n( \"")
196+
.replace("; ", ";\n")
197+
.as_bytes(),
198+
)?;
199+
let str = String::from_utf8(destination).unwrap();
200+
Ok(str)
201+
}
202+
184203
fn to_tokens(&mut self) -> proc_macro2::TokenStream {
185204
// `impl Default for Atom` requires the empty string to be in the static set.
186205
// This also makes sure the set in non-empty,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use string_cache_codegen;
2+
3+
#[test]
4+
fn test_iteration_order() {
5+
6+
let x1 = string_cache_codegen::AtomType::new("foo::Atom", "foo_atom!")
7+
.atoms(&["x", "xlink", "svg", "test"])
8+
.write_to_string(Vec::new()).unwrap();
9+
10+
let x2 = string_cache_codegen::AtomType::new("foo::Atom", "foo_atom!")
11+
.atoms(&["x", "xlink", "svg", "test"])
12+
.write_to_string(Vec::new()).unwrap();
13+
14+
assert_eq!(x1, x2);
15+
}

0 commit comments

Comments
 (0)