6969#![ recursion_limit = "128" ]
7070
7171use quote:: quote;
72- use std:: collections:: HashSet ;
72+ use std:: collections:: BTreeSet ;
7373use std:: fs:: File ;
7474use std:: io:: { self , BufWriter , Write } ;
7575use 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
8787impl 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,26 @@ impl AtomType {
181181 )
182182 }
183183
184+ #[ cfg( test) ]
185+ /// Write generated code to destination [`Vec<u8>`] and return it as [`String`]
186+ ///
187+ /// Used mostly for testing or displaying a value.
188+ pub fn write_to_string ( & mut self , mut destination : Vec < u8 > ) -> io:: Result < String >
189+ {
190+ destination. write_all (
191+ self . to_tokens ( )
192+ . to_string ( )
193+ // Insert some newlines to make the generated code slightly easier to read.
194+ . replace ( " [ \" " , "[\n \" " )
195+ . replace ( "\" , " , "\" ,\n " )
196+ . replace ( " ( \" " , "\n ( \" " )
197+ . replace ( "; " , ";\n " )
198+ . as_bytes ( ) ,
199+ ) ?;
200+ let str = String :: from_utf8 ( destination) . unwrap ( ) ;
201+ Ok ( str)
202+ }
203+
184204 fn to_tokens ( & mut self ) -> proc_macro2:: TokenStream {
185205 // `impl Default for Atom` requires the empty string to be in the static set.
186206 // This also makes sure the set in non-empty,
@@ -315,3 +335,16 @@ impl AtomType {
315335 self . write_to ( BufWriter :: new ( File :: create ( path) ?) )
316336 }
317337}
338+
339+ #[ test]
340+ fn test_iteration_order ( ) {
341+ let x1 = crate :: AtomType :: new ( "foo::Atom" , "foo_atom!" )
342+ . atoms ( & [ "x" , "xlink" , "svg" , "test" ] )
343+ . write_to_string ( Vec :: new ( ) ) . expect ( "write to string cache x1" ) ;
344+
345+ let x2 = crate :: AtomType :: new ( "foo::Atom" , "foo_atom!" )
346+ . atoms ( & [ "x" , "xlink" , "svg" , "test" ] )
347+ . write_to_string ( Vec :: new ( ) ) . expect ( "write to string cache x2" ) ;
348+
349+ assert_eq ! ( x1, x2) ;
350+ }
0 commit comments