expose and test set_stock_translation to Python

This commit is contained in:
holger krekel
2019-10-09 10:58:06 +02:00
parent bc86201b44
commit 45f011c63c
10 changed files with 122 additions and 32 deletions

View File

@@ -257,7 +257,7 @@ const DC_SHOW_EMAILS_ACCEPTED_CONTACTS: usize = 1;
const DC_SHOW_EMAILS_ALL: usize = 2;
// TODO: Strings need some doumentation about used placeholders.
// These constants are used to request strings using #DC_EVENT_GET_STRING.
// These constants are used to set stock translation strings
const DC_STR_NOMESSAGES: usize = 1;
const DC_STR_SELF: usize = 2;

View File

@@ -63,7 +63,7 @@ pub struct Context {
pub running_state: Arc<RwLock<RunningState>>,
/// Mutex to avoid generating the key for the user more than once.
pub generating_key_mutex: Mutex<()>,
pub translated_stockstrings: HashMap<usize, String>,
pub translated_stockstrings: Arc<RwLock<HashMap<usize, String>>>,
}
#[derive(Debug, PartialEq, Eq)]
@@ -146,7 +146,7 @@ impl Context {
probe_imap_network: Arc::new(RwLock::new(false)),
perform_inbox_jobs_needed: Arc::new(RwLock::new(false)),
generating_key_mutex: Mutex::new(()),
translated_stockstrings: HashMap::new(),
translated_stockstrings: Arc::new(RwLock::new(HashMap::new())),
};
ensure!(

View File

@@ -805,19 +805,12 @@ mod tests {
assert!(msg.contains("-----END PGP MESSAGE-----\n"));
}
fn ac_setup_msg_cb(ctx: &Context, evt: Event) -> libc::uintptr_t {
match evt {
Event::GetString {
id: StockMessage::AcSetupMsgBody,
..
} => unsafe { "hello\r\nthere".strdup() as usize },
_ => logging_cb(ctx, evt),
}
}
#[test]
fn otest_render_setup_file_newline_replace() {
let t = test_context(Some(Box::new(ac_setup_msg_cb)));
let t = dummy_context();
t.ctx
.set_stock_translation(StockMessage::AcSetupMsgBody, "hello\r\nthere".to_string())
.unwrap();
configure_alice_keypair(&t.ctx);
let msg = render_setup_file(&t.ctx, "pw").unwrap();
println!("{}", &msg);

View File

@@ -57,7 +57,7 @@ pub mod qr;
pub mod securejoin;
mod smtp;
pub mod sql;
mod stock;
pub mod stock;
mod token;
#[macro_use]
mod wrapmime;

View File

@@ -125,7 +125,7 @@ impl Context {
/// Set the stock string for the [StockMessage].
///
pub fn set_stock_translation(
&mut self,
&self,
id: StockMessage,
stockstring: String,
) -> Result<(), Error> {
@@ -143,7 +143,10 @@ impl Context {
id.fallback()
);
}
self.translated_stockstrings.insert(id as usize, stockstring);
self.translated_stockstrings
.write()
.unwrap()
.insert(id as usize, stockstring);
Ok(())
}
@@ -152,8 +155,13 @@ impl Context {
/// Return a translation (if it was set with set_stock_translation before)
/// or a default (English) string.
pub fn stock_str(&self, id: StockMessage) -> Cow<str> {
match self.translated_stockstrings.get(&(id as usize)) {
Some(x) => Cow::Borrowed(x),
match self
.translated_stockstrings
.read()
.unwrap()
.get(&(id as usize))
{
Some(ref x) => Cow::Owned(x.to_string()),
None => Cow::Borrowed(id.fallback()),
}
}
@@ -274,7 +282,7 @@ mod tests {
#[test]
fn test_set_stock_translation() {
let mut t = dummy_context();
let t = dummy_context();
t.ctx
.set_stock_translation(StockMessage::NoMessages, "xyz".to_string())
.unwrap();
@@ -283,7 +291,7 @@ mod tests {
#[test]
fn test_set_stock_translation_wrong_replacements() {
let mut t = dummy_context();
let t = dummy_context();
assert!(t
.ctx
.set_stock_translation(StockMessage::NoMessages, "xyz %1$s ".to_string())