1
0
mirror of https://github.com/pcvolkmer/osc-variant.git synced 2025-07-06 02:12:55 +00:00

feat: allow script code changes in profile form_fields segments

This commit is contained in:
2024-05-23 14:02:23 +02:00
parent 4b2ede0d80
commit 3ed38ca817
5 changed files with 182 additions and 10 deletions

View File

@ -696,6 +696,7 @@ mod tests {
use std::str::FromStr;
use crate::model::onkostar_editor::OnkostarEditor;
use crate::model::Script;
use crate::profile::Profile;
#[test]
@ -822,4 +823,76 @@ mod tests {
_ => panic!("Test failed: MenuCategory not found!"),
}
}
#[test]
fn should_change_dataform_entry_scripts_code_with_form_fields() {
let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc"));
assert!(onkostar_editor.is_ok());
let mut onkostar_editor = onkostar_editor.unwrap();
let profile = "forms:
- name: 'Hauptformular'
form_fields:
- name: Auswahl
scripts_code: |-
// Example code
console.log(42);
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[2].scripts,
None
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[2].scripts,
Some(Script {
code: "// Example code
console.log(42);".into(),
valid: true
})
);
}
#[test]
fn should_change_dataform_entry_scripts_code_with_form_references() {
let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc"));
assert!(onkostar_editor.is_ok());
let mut onkostar_editor = onkostar_editor.unwrap();
let profile = "forms:
- name: 'Hauptformular'
form_fields:
- name: Auswahl
scripts_code: |-
// Example code
console.log(42);
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[2].scripts,
None
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.data_form[0].entries.entry[2].scripts,
Some(Script {
code: "// Example code
console.log(42);".into(),
valid: true
})
);
}
}

View File

@ -30,7 +30,7 @@ use std::hash::{Hash, Hasher};
use serde::{Deserialize, Serialize};
use crate::model::requirements::Requires;
use crate::profile::{FormField, FormReference, Profile};
use crate::profile::{FormField, FormReference, Profile, WithScriptsCode};
pub mod data_catalogue;
pub mod data_form;
@ -40,7 +40,7 @@ pub mod property_catalogue;
pub mod requirements;
pub mod unterformular;
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct Script {
#[serde(rename = "Code")]
@ -337,12 +337,14 @@ where
if entry.get_name() == form_field.name && form_field.hide {
entry.hide()
}
if entry.get_name() == form_field.name {
if let Some(new_default_value) = &form_field.default_value {
entry.update_default_value(new_default_value.to_string())
}
}
if let Some(scripts_code) = &form_field.escaped_scripts_code() {
entry.update_scripts_code(scripts_code.clone());
}
}
pub trait FormEntryContainer {

View File

@ -692,6 +692,7 @@ mod tests {
use std::str::FromStr;
use crate::model::onkostar_editor::OnkostarEditor;
use crate::model::Script;
use crate::profile::Profile;
#[test]
@ -790,4 +791,76 @@ mod tests {
.menu_category
.is_none());
}
#[test]
fn should_change_dataform_entry_scripts_code_with_form_fields() {
let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc"));
assert!(onkostar_editor.is_ok());
let mut onkostar_editor = onkostar_editor.unwrap();
let profile = "forms:
- name: 'Unterformular'
form_fields:
- name: Termin
scripts_code: |-
// Example code
console.log(42);
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].scripts,
None
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].scripts,
Some(Script {
code: "// Example code
console.log(42);".into(),
valid: true
})
);
}
#[test]
fn should_change_dataform_entry_scripts_code_with_form_references() {
let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc"));
assert!(onkostar_editor.is_ok());
let mut onkostar_editor = onkostar_editor.unwrap();
let profile = "forms:
- name: 'Unterformular'
form_fields:
- name: Termin
scripts_code: |-
// Example code
console.log(42);
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].scripts,
None
);
onkostar_editor.apply_profile(&profile);
assert_eq!(
onkostar_editor.editor.unterformular[0].entries.entry[1].scripts,
Some(Script {
code: "// Example code
console.log(42);".into(),
valid: true
})
);
}
}