@@ -141,4 +141,79 @@ mod rustfluent {
141141 Ok ( value. to_string ( ) )
142142 }
143143 }
144+
145+ #[ cfg( test) ]
146+ mod tests {
147+
148+ use std:: collections:: HashMap ;
149+
150+ use pyo3:: { Python , types:: IntoPyDict } ;
151+
152+ use super :: rustfluent;
153+
154+ #[ test]
155+ fn bundle_new_success ( ) {
156+ vec ! [
157+ ( "en-US" , vec![ ] , false ) ,
158+ (
159+ "en-US" ,
160+ vec![ std:: path:: PathBuf :: from( "tests/data/en.ftl" ) ] ,
161+ false ,
162+ ) ,
163+ (
164+ "en-US" ,
165+ vec![
166+ std:: path:: PathBuf :: from( "tests/data/en.ftl" ) ,
167+ std:: path:: PathBuf :: from( "tests/data/en_hello.ftl" ) ,
168+ ] ,
169+ false ,
170+ ) ,
171+ ]
172+ . into_iter ( )
173+ . enumerate ( )
174+ . for_each ( |( case, ( language, ftl_filenames, strict) ) | {
175+ let result = rustfluent:: Bundle :: new ( language, ftl_filenames, strict) ;
176+ assert ! ( result. is_ok( ) , "case {case} failed" ) ;
177+ } ) ;
178+ }
179+
180+ #[ test]
181+ fn bundle_get_translation ( ) {
182+ let mut bundle = rustfluent:: Bundle :: new (
183+ "en-US" ,
184+ vec ! [ std:: path:: PathBuf :: from( "tests/data/en.ftl" ) ] ,
185+ false ,
186+ )
187+ . expect ( "valid fluent bundle" ) ;
188+
189+ let result = bundle. get_translation ( "hello-world" , None , false ) ;
190+
191+ assert ! ( result. is_ok( ) ) ;
192+ assert_eq ! ( result. unwrap( ) , "Hello World" ) ;
193+ }
194+
195+ #[ test]
196+ fn bundle_get_translation_with_ctx ( ) {
197+ let mut bundle = rustfluent:: Bundle :: new (
198+ "en-US" ,
199+ vec ! [ std:: path:: PathBuf :: from( "tests/data/en.ftl" ) ] ,
200+ false ,
201+ )
202+ . expect ( "valid fluent bundle" ) ;
203+
204+ Python :: with_gil ( |py| {
205+ let mut ctx = HashMap :: new ( ) ;
206+ ctx. insert ( "user" , "John" ) ;
207+
208+ let result = bundle. get_translation (
209+ "hello-user" ,
210+ Some ( & ctx. into_py_dict ( py) . unwrap ( ) ) ,
211+ false ,
212+ ) ;
213+
214+ assert ! ( result. is_ok( ) ) ;
215+ assert_eq ! ( result. unwrap( ) , "Hello, John" ) ;
216+ } ) ;
217+ }
218+ }
144219}
0 commit comments