From 4b13100c4549a106ca4c4da5e9397313b8a0c9d8 Mon Sep 17 00:00:00 2001 From: Taka Date: Sat, 1 Sep 2018 04:35:42 +0000 Subject: [PATCH] Handle ETO/PTO-only capabilities. --- vasl_templates/webapp/static/snippets.js | 18 ++++-- vasl_templates/webapp/templates/index.html | 2 +- .../fixtures/data/ordnance/american.json | 17 ++++++ .../vo-reports/ordnance/american/1940.txt | 2 +- .../vo-reports/ordnance/american/1941.txt | 2 +- .../vo-reports/ordnance/american/1942.txt | 2 +- .../vo-reports/ordnance/american/1943.txt | 2 +- .../vo-reports/ordnance/american/1944.txt | 4 +- .../vo-reports/ordnance/american/1945.txt | 4 +- .../webapp/tests/test_capabilities.py | 55 +++++++++++++++++++ vasl_templates/webapp/tests/utils.py | 35 ++++++++++-- 11 files changed, 124 insertions(+), 19 deletions(-) create mode 100644 vasl_templates/webapp/tests/fixtures/data/ordnance/american.json create mode 100644 vasl_templates/webapp/tests/test_capabilities.py diff --git a/vasl_templates/webapp/static/snippets.js b/vasl_templates/webapp/static/snippets.js index 60c16e8..de8c1d1 100644 --- a/vasl_templates/webapp/static/snippets.js +++ b/vasl_templates/webapp/static/snippets.js @@ -300,7 +300,7 @@ function make_capabilities( entry, scenario_theater, scenario_year, scenario_mon capabilities.push( make_raw_capability( key, entry.capabilities2[key] ) ) ; } else { - var cap = select_capability_by_date( entry.capabilities2[key], scenario_year, scenario_month ) ; + var cap = select_capability_by_date( entry.capabilities2[key], scenario_theater, scenario_year, scenario_month ) ; if ( ! cap ) continue ; if ( cap == "" ) { @@ -358,7 +358,7 @@ function make_raw_capability( name, capability ) return buf.join( "" ) ; } -function select_capability_by_date( capabilities, scenario_year, scenario_month ) +function select_capability_by_date( capabilities, scenario_theater, scenario_year, scenario_month ) { var MONTH_NAMES = { F:2, J:6, A:8 } ; @@ -368,10 +368,16 @@ function select_capability_by_date( capabilities, scenario_year, scenario_month // check for a ETO/PTO-only flag var cap = capabilities[i][1].toString() ; - if ( cap.substring( cap.length-1 ) === "E" ) - cap = cap.substring( 0, cap.length-1 ) ; // FIXME! handle this properly - if ( cap.substring( cap.length-1 ) === "P" ) - cap = cap.substring( 0, cap.length-1 ) ; // FIXME! handle this properly + if ( cap.substring( cap.length-1 ) === "E" ) { + if ( scenario_theater != "ETO" ) + return null ; + cap = cap.substring( 0, cap.length-1 ) ; + } + if ( cap.substring( cap.length-1 ) === "P" ) { + if ( scenario_theater != "PTO" ) + return null ; + cap = cap.substring( 0, cap.length-1 ) ; + } // remove any trailing "+" (FIXME! What does it even mean? Doesn't make sense :-/) if ( cap.substring( cap.length-1 ) == "+" ) cap = cap.substring( 0, cap.length-1 ) ; diff --git a/vasl_templates/webapp/templates/index.html b/vasl_templates/webapp/templates/index.html index 4aaad6a..069bbd5 100644 --- a/vasl_templates/webapp/templates/index.html +++ b/vasl_templates/webapp/templates/index.html @@ -62,7 +62,7 @@
diff --git a/vasl_templates/webapp/tests/fixtures/data/ordnance/american.json b/vasl_templates/webapp/tests/fixtures/data/ordnance/american.json new file mode 100644 index 0000000..a76fc12 --- /dev/null +++ b/vasl_templates/webapp/tests/fixtures/data/ordnance/american.json @@ -0,0 +1,17 @@ +[ + +{ "name": "M2A1 105mm Howitzer", + "capabilities": [ "NT" ], + "capabilities2": { "H": 6, "s": 7, "WP": 8, "C": [ [7,"4+P"], "\u20201" ] }, + "note_number": "14\u2020", + "notes": [ "C\u20201", "N" ] +}, + +{ "name": "M3 105mm Howitzer", + "capabilities": [ "NT" ], + "capabilities2": { "H": 7, "s": 7, "WP": 8, "C": [ [7,"P"], "\u20201" ] }, + "note_number": "15\u2020", + "notes": [ "C\u20201" ] +} + +] diff --git a/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1940.txt b/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1940.txt index 1262bb3..4f8d44c 100644 --- a/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1940.txt +++ b/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1940.txt @@ -16,7 +16,7 @@ M20 75mm Recoilless Rifle 5PP crewed†[1] H7 WP7 5PP crew M1A1 75mm Pack Howitzer NT QSU C4[4+]†[1] H7 WP8 NT QSU H7 WP8 12† C†1 F M1897A2 75mm Gun NT QSU WP8 NT QSU WP8 13 P M2A1 105mm Howitzer NT C7[4+P]†[1] H6 WP8 s7 NT H6 WP8 s7 14† C†1 N -M3 105mm Howitzer NT C7[P]†[1] H7 WP8 s7 NT C7†[1] H7 WP8 s7 15† C†1 +M3 105mm Howitzer NT C7[P]†[1] H7 WP8 s7 NT H7 WP8 s7 15† C†1 M1 4.5-in. Gun NT NT 16† P M1918 155mm Howitzer NT WP8 s7[3+] NT WP8 17† N M1 155mm Howitzer NT WP8 s7 NT WP8 s7 18† N diff --git a/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1941.txt b/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1941.txt index 91f0bf0..82efcd9 100644 --- a/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1941.txt +++ b/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1941.txt @@ -16,7 +16,7 @@ M20 75mm Recoilless Rifle 5PP crewed†[1] H7 WP7 5PP crew M1A1 75mm Pack Howitzer NT QSU C4[4+]†[1] H7 WP8 NT QSU H7 WP8 12† C†1 F M1897A2 75mm Gun NT QSU WP8 NT QSU WP8 13 P M2A1 105mm Howitzer NT C7[4+P]†[1] H6 WP8 s7 NT H6 WP8 s7 14† C†1 N -M3 105mm Howitzer NT C7[P]†[1] H7 WP8 s7 NT C7†[1] H7 WP8 s7 15† C†1 +M3 105mm Howitzer NT C7[P]†[1] H7 WP8 s7 NT H7 WP8 s7 15† C†1 M1 4.5-in. Gun NT NT 16† P M1918 155mm Howitzer NT WP8 s7[3+] NT WP8 17† N M1 155mm Howitzer NT WP8 s7 NT WP8 s7 18† N diff --git a/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1942.txt b/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1942.txt index 20b9809..36ea0f9 100644 --- a/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1942.txt +++ b/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1942.txt @@ -16,7 +16,7 @@ M20 75mm Recoilless Rifle 5PP crewed†[1] H7 WP7 5PP crew M1A1 75mm Pack Howitzer NT QSU C4[4+]†[1] H7 WP8 NT QSU H7 WP8 12† C†1 F M1897A2 75mm Gun NT QSU WP8 NT QSU WP8 13 P M2A1 105mm Howitzer NT C7[4+P]†[1] H6 WP8 s7 NT H6 WP8 s7 14† C†1 N -M3 105mm Howitzer NT C7[P]†[1] H7 WP8 s7 NT C7†[1] H7 WP8 s7 15† C†1 +M3 105mm Howitzer NT C7[P]†[1] H7 WP8 s7 NT H7 WP8 s7 15† C†1 M1 4.5-in. Gun NT NT 16† P M1918 155mm Howitzer NT WP8 s7[3+] NT WP8 17† N M1 155mm Howitzer NT WP8 s7 NT WP8 s7 18† N diff --git a/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1943.txt b/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1943.txt index 2339ad4..c4c21e5 100644 --- a/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1943.txt +++ b/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1943.txt @@ -16,7 +16,7 @@ M20 75mm Recoilless Rifle 5PP crewed†[1] H7 WP7 5PP crew M1A1 75mm Pack Howitzer NT QSU C4[4+]†[1] H7 WP8 NT QSU H7 WP8 12† C†1 F M1897A2 75mm Gun NT QSU WP8 NT QSU WP8 13 P M2A1 105mm Howitzer NT C7[4+P]†[1] H6 WP8 s7 NT H6 WP8 s7 14† C†1 N -M3 105mm Howitzer NT C7[P]†[1] H7 WP8 s7 NT C7†[1] H7 WP8 s7 15† C†1 +M3 105mm Howitzer NT C7[P]†[1] H7 WP8 s7 NT H7 WP8 s7 15† C†1 M1 4.5-in. Gun NT NT 16† P M1918 155mm Howitzer NT WP8 s7[3+] NT WP8 s7 17† N M1 155mm Howitzer NT WP8 s7 NT WP8 s7 18† N diff --git a/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1944.txt b/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1944.txt index 1a1c778..4b82b2b 100644 --- a/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1944.txt +++ b/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1944.txt @@ -15,8 +15,8 @@ M18 57mm Recoilless Rifle 3PP crewed†[1] H7 WP6 3PP crew M20 75mm Recoilless Rifle 5PP crewed†[1] H7 WP7 5PP crewed†[1] H7 WP7 11 D†1 E† M1A1 75mm Pack Howitzer NT QSU C4[4+]†[1] H7 WP8 NT QSU C4†[1] H7 WP8 12† C†1 F M1897A2 75mm Gun NT QSU WP8 NT QSU WP8 13 P -M2A1 105mm Howitzer NT C7[4+P]†[1] H6 WP8 s7 NT C7†[1] H6 WP8 s7 14† C†1 N -M3 105mm Howitzer NT C7[P]†[1] H7 WP8 s7 NT C7†[1] H7 WP8 s7 15† C†1 +M2A1 105mm Howitzer NT C7[4+P]†[1] H6 WP8 s7 NT H6 WP8 s7 14† C†1 N +M3 105mm Howitzer NT C7[P]†[1] H7 WP8 s7 NT H7 WP8 s7 15† C†1 M1 4.5-in. Gun NT NT 16† P M1918 155mm Howitzer NT WP8 s7[3+] NT WP8 s7 17† N M1 155mm Howitzer NT WP8 s7 NT WP8 s7 18† N diff --git a/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1945.txt b/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1945.txt index e55a5e0..6a3eee3 100644 --- a/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1945.txt +++ b/vasl_templates/webapp/tests/fixtures/vo-reports/ordnance/american/1945.txt @@ -15,8 +15,8 @@ M18 57mm Recoilless Rifle 3PP crewed†[1] H7 WP6 3PP crew M20 75mm Recoilless Rifle 5PP crewed†[1] H7 WP7 5PP crewed†[1] H7 WP7 11 D†1 E† M1A1 75mm Pack Howitzer NT QSU C4[4+]†[1] H7 WP8 NT QSU C4†[1] H7 WP8 12† C†1 F M1897A2 75mm Gun NT QSU WP8 NT QSU WP8 13 P -M2A1 105mm Howitzer NT C7[4+P]†[1] H6 WP8 s7 NT C7†[1] H6 WP8 s7 14† C†1 N -M3 105mm Howitzer NT C7[P]†[1] H7 WP8 s7 NT C7†[1] H7 WP8 s7 15† C†1 +M2A1 105mm Howitzer NT C7[4+P]†[1] H6 WP8 s7 NT H6 WP8 s7 14† C†1 N +M3 105mm Howitzer NT C7[P]†[1] H7 WP8 s7 NT H7 WP8 s7 15† C†1 M1 4.5-in. Gun NT NT 16† P M1918 155mm Howitzer NT WP8 s7[3+] NT WP8 s7 17† N M1 155mm Howitzer NT WP8 s7 NT WP8 s7 18† N diff --git a/vasl_templates/webapp/tests/test_capabilities.py b/vasl_templates/webapp/tests/test_capabilities.py new file mode 100644 index 0000000..1dfdb65 --- /dev/null +++ b/vasl_templates/webapp/tests/test_capabilities.py @@ -0,0 +1,55 @@ +""" Test snippet generation for capabilities. """ + +from vasl_templates.webapp.tests.utils import \ + init_webapp, select_tab, new_scenario, load_scenario_params, \ + find_child, wait_for_clipboard + +# --------------------------------------------------------------------- + +def test_scenario_theater( webapp, webdriver ): + """Test ETO/PTO-only capabilities.""" + + # initialize + init_webapp( webapp, webdriver ) + + def do_test( scenario_theater, scenario_date, expected ): + """Test snippet generation.""" + load_scenario_params( { + "scenario": { + "SCENARIO_THEATER": scenario_theater, + "SCENARIO_DATE": scenario_date, + } + } ) + select_tab( "ob1" ) + btn = find_child( "button.generate[data-id='ob_ordnance_1']" ) + btn.click() + wait_for_clipboard( 2, "capabilities: {}".format(expected), contains=True ) + + # M2A1 105mm Howitzer: C7(4+P)†1 + load_scenario_params( { + "scenario": { + "PLAYER_1": "american", + }, + "ob1": { + "OB_ORDNANCE_1": [ "M2A1 105mm Howitzer" ], + } + } ) + do_test( "ETO", "01/01/1940", '"NT" "H6" "WP8" "s7"') + do_test( "ETO", "01/01/1944", '"NT" "H6" "WP8" "s7"') + do_test( "ETO", "01/01/1945", '"NT" "H6" "WP8" "s7"') + do_test( "PTO", "01/01/1940", '"NT" "H6" "WP8" "s7"') + do_test( "PTO", "01/01/1944", '"NT" "C7" "H6" "WP8" "s7"') + do_test( "PTO", "01/01/1945", '"NT" "C7" "H6" "WP8" "s7"') + + # M3 105mm Howitzer: C7(P)†1 + new_scenario() + load_scenario_params( { + "scenario": { + "PLAYER_1": "american", + }, + "ob1": { + "OB_ORDNANCE_1": [ "M3 105mm Howitzer" ], + } + } ) + do_test( "ETO", "01/01/1940", '"NT" "H7" "WP8" "s7"') + do_test( "PTO", "01/01/1940", '"NT" "C7" "H7" "WP8" "s7"') diff --git a/vasl_templates/webapp/tests/utils.py b/vasl_templates/webapp/tests/utils.py index 2c6cec9..01edea6 100644 --- a/vasl_templates/webapp/tests/utils.py +++ b/vasl_templates/webapp/tests/utils.py @@ -110,6 +110,14 @@ def select_menu_option( menu_id ): # FUDGE! Work-around weird "is not clickable" errors because the PopMenu is still around :-/ time.sleep( 0.25 ) +def new_scenario(): + """Reset the scenario.""" + select_menu_option( "new_scenario" ) + # check if the webapp is asking for confirmation + if find_child( "#ask" ): + # yup - make it so + click_dialog_button( "OK" ) + # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - def load_scenario_params( params ): @@ -275,6 +283,8 @@ def set_stored_msg( msg_type, val ): def set_stored_msg_marker( msg_type ): """Store marker text in the message buffer (so we can tell if the front-end changes it).""" + # NOTE: Care should taken when using this function with "_clipboard_", + # since the tests might be using the real clipboard! marker = "marker:{}:{}".format( msg_type, uuid.uuid4() ) set_stored_msg( msg_type, marker ) return marker @@ -316,10 +326,13 @@ def _do_select_droplist( sel, val ): elem = find_child( "#{}-button .ui-selectmenu-icon".format( sel_id ) ) elem.click() - # select the requested option (nb: clicking on the child option doesn't work :shrug:) - elem = find_child( "#{}-button".format( sel_id ) ) - elem.send_keys( val ) - elem.send_keys( Keys.RETURN ) + # select the requested option + elems = [ + e for e in find_children( "#{}-menu.ui-menu .ui-menu-item-wrapper".format( sel_id ) ) + if e.text == val + ] + assert len(elems) == 1 + ActionChains(_webdriver).click( elems[0] ).perform() def get_droplist_vals_index( sel ): """Get the value/text for each option in a droplist.""" @@ -392,6 +405,20 @@ def wait_for_elem( timeout, elem_id, parent=None ): wait_for( timeout, check_elem ) return args["elem"] +def wait_for_clipboard( timeout, expected, contains=False ): + """Wait for the clipboard to hold an expected value.""" + args = { "last-clipboard": "" } + def check_clipboard(): #pylint: disable=missing-docstring + args["last-clipboard"] = get_clipboard() + return expected in args["last-clipboard"] if contains else expected == args["last-clipboard"] + try: + wait_for( timeout, check_clipboard ) + except AssertionError: + print( "Timed out waiting for the clipboard:" ) + print( "- Expecting:", expected ) + print( "- Got:", args["last-clipboard"] ) + raise + # --------------------------------------------------------------------- _IE_HTML_TAGS = [ "" ]