Skip to content

Commit

Permalink
Widget flags: tests & documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
dseguin committed Jun 20, 2022
1 parent 8b553ec commit 8152f1a
Show file tree
Hide file tree
Showing 3 changed files with 290 additions and 0 deletions.
105 changes: 105 additions & 0 deletions data/mods/TEST_DATA/widgets.json
Original file line number Diff line number Diff line change
Expand Up @@ -1491,5 +1491,110 @@
"style": "layout",
"arrange": "columns",
"widgets": [ "test_layout_rows3", "test_layout_rows_in_columns" ]
},
{
"id": "test_bp_bleed_sym_arm_l",
"type": "widget",
"style": "symbol",
"label": "LA : ",
"bodypart": "arm_l",
"clauses": [
{
"id": "bleeding",
"text": "bleeding",
"sym": "",
"color": "c_light_red_white",
"condition": {
"and": [
{ "u_has_effect": "bleed", "intensity": 1 },
{ "compare_int": [ { "u_val": "effect_intensity", "effect": "bleed" }, "<", { "const": 11 } ] }
]
}
},
{
"id": "bleeding",
"text": "bleeding",
"sym": "",
"color": "c_red_white",
"condition": {
"and": [
{ "u_has_effect": "bleed", "intensity": 11 },
{ "compare_int": [ { "u_val": "effect_intensity", "effect": "bleed" }, "<", { "const": 21 } ] }
]
}
},
{
"id": "bleeding",
"text": "bleeding",
"sym": "",
"color": "c_red_red",
"condition": { "u_has_effect": "bleed", "intensity": 21 }
}
]
},
{
"id": "test_bp_broken_sym_arm_l",
"type": "widget",
"style": "symbol",
"width": 1,
"bodypart": "arm_l",
"clauses": [
{
"id": "broken",
"text": "broken",
"sym": "",
"color": "c_red_white",
"condition": { "compare_int": [ { "u_val": "hp" }, "==", { "const": 0 } ] }
},
{
"id": "intact",
"text": "intact",
"sym": "",
"color": "c_dark_gray_white",
"condition": { "compare_int": [ { "u_val": "hp" }, ">", { "const": 0 } ] }
}
]
},
{
"id": "test_bp_hpgraph_sym_arm_l",
"type": "widget",
"style": "graph",
"var": "bp_hp",
"bodypart": "arm_l",
"width": 5,
"symbols": " ▏▎▍▍▌▋▊▉█",
"fill": "bucket",
"colors": [ "c_red_white", "c_light_red_white", "c_yellow_white", "c_light_green_white", "c_green_white" ]
},
{
"id": "test_layout_nopad_nested",
"type": "widget",
"style": "layout",
"arrange": "columns",
"widgets": [ "test_bp_bleed_sym_arm_l", "test_bp_broken_sym_arm_l", "test_bp_hpgraph_sym_arm_l" ]
},
{
"id": "test_row_nopad",
"type": "widget",
"style": "layout",
"arrange": "rows",
"widgets": [ "test_layout_nopad_nested" ]
},
{
"//": "use sidebar width = 36",
"id": "test_layout_nopad",
"type": "widget",
"style": "layout",
"arrange": "columns",
"separator": "",
"widgets": [ "test_row_nopad", "test_row_nopad", "test_row_nopad" ],
"flags": [ "W_NO_PADDING" ]
},
{
"//": "use sidebar width = 36",
"id": "test_layout_nopad_noflag",
"type": "widget",
"copy-from": "test_layout_nopad",
"flags": [ ]
}
]
1 change: 1 addition & 0 deletions doc/WIDGETS.md
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ Here are some flags that can be included:
| `W_DISABLED_BY_DEFAULT` | Makes this widget disabled by default (only applies to top-level widgets/layouts)
| `W_DISABLED_WHEN_EMPTY` | Automatically hides this widget when the widget's text is empty
| `W_DYNAMIC_HEIGHT` | Allows certain multi-line widgets to dynamically adjust their height
| `W_NO_PADDING` | Removes extra padding added between columns for alignment (applies recursively to sub-widgets)


# Clauses and conditions
Expand Down
184 changes: 184 additions & 0 deletions tests/widget_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "weather_type.h"
#include "widget.h"

#include <clocale>

// Needed for screen scraping
#if !(defined(TILES) || defined(_WIN32))
namespace cata_curses_test
Expand Down Expand Up @@ -106,6 +108,8 @@ static const widget_id widget_test_hp_head_num( "test_hp_head_num" );
static const widget_id widget_test_hunger_clause( "test_hunger_clause" );
static const widget_id widget_test_int_color_num( "test_int_color_num" );
static const widget_id widget_test_layout_cols_in_cols( "test_layout_cols_in_cols" );
static const widget_id widget_test_layout_nopad( "test_layout_nopad" );
static const widget_id widget_test_layout_nopad_noflag( "test_layout_nopad_noflag" );
static const widget_id widget_test_layout_rows_in_columns( "test_layout_rows_in_columns" );
static const widget_id widget_test_lighting_clause( "test_lighting_clause" );
static const widget_id widget_test_mana_num( "test_mana_num" );
Expand Down Expand Up @@ -2362,3 +2366,183 @@ TEST_CASE( "widget rows in columns", "[widget]" )
CHECK( wgt.layout( ava, 68 ) == expected );
}
}

static void test_widget_flag_nopad( const bodypart_id &bid, int bleed_int, avatar &ava,
widget &wgt, bool skip_pad )
{
const int width = 36;
const int max_hp = ava.get_part_hp_max( bid );

std::string bleed_txt;
if( bleed_int <= 0 ) {
bleed_txt.clear();
} else if( bleed_int < 11 ) {
bleed_txt = "<color_c_light_red_white>│</color>";
} else if( bleed_int < 21 ) {
bleed_txt = "<color_c_red_white>║</color>";
} else {
bleed_txt = "<color_c_red_red>║</color>";
}

std::string broken_txt = "<color_c_dark_gray_white>‖</color>";

GIVEN( "left arm HP full" ) {
REQUIRE( ava.get_part_hp_cur( bid ) == max_hp );
REQUIRE( !ava.is_limb_broken( bid ) );
if( skip_pad ) {
const std::string txt = "LA : " + bleed_txt + broken_txt +
"<color_c_green_white>█████</color>";
CHECK( wgt.layout( ava, width, 0, skip_pad ) == txt + txt + txt );
} else {
CHECK( wgt.layout( ava, width, 0, skip_pad ) ==
"LA… " + broken_txt + " <color_c_green_white>█…</color> "
"LA… " + broken_txt + " <color_c_green_white>█…</color> "
"L… " + broken_txt + " <color_c_green_white>█…</color>" );
}
}
GIVEN( "left arm HP 3/4" ) {
ava.set_part_hp_cur( bid, ( max_hp * 3 ) / 4 );
REQUIRE( ava.get_part_hp_cur( bid ) == ( max_hp * 3 ) / 4 );
REQUIRE( !ava.is_limb_broken( bid ) );
if( skip_pad ) {
const std::string txt = "LA : " + bleed_txt + broken_txt +
"<color_c_light_green_white>███▋ </color>";
CHECK( wgt.layout( ava, width, 0, skip_pad ) == txt + txt + txt );
} else {
CHECK( wgt.layout( ava, width, 0, skip_pad ) ==
"LA… " + broken_txt + " <color_c_light_green_white>█…</color> "
"LA… " + broken_txt + " <color_c_light_green_white>█…</color> "
"L… " + broken_txt + " <color_c_light_green_white>█…</color>" );
}
}
GIVEN( "left arm HP 1/2" ) {
ava.set_part_hp_cur( bid, max_hp / 2 );
REQUIRE( ava.get_part_hp_cur( bid ) == max_hp / 2 );
REQUIRE( !ava.is_limb_broken( bid ) );
if( skip_pad ) {
const std::string txt = "LA : " + bleed_txt + broken_txt +
"<color_c_yellow_white>██▍ </color>";
CHECK( wgt.layout( ava, width, 0, skip_pad ) == txt + txt + txt );
} else {
CHECK( wgt.layout( ava, width, 0, skip_pad ) ==
"LA… " + broken_txt + " <color_c_yellow_white>█…</color> "
"LA… " + broken_txt + " <color_c_yellow_white>█…</color> "
"L… " + broken_txt + " <color_c_yellow_white>█…</color>" );
}
}
GIVEN( "left arm HP 1/4" ) {
ava.set_part_hp_cur( bid, max_hp / 4 );
REQUIRE( ava.get_part_hp_cur( bid ) == max_hp / 4 );
REQUIRE( !ava.is_limb_broken( bid ) );
if( skip_pad ) {
const std::string txt = "LA : " + bleed_txt + broken_txt +
"<color_c_light_red_white>█▎ </color>";
CHECK( wgt.layout( ava, width, 0, skip_pad ) == txt + txt + txt );
} else {
CHECK( wgt.layout( ava, width, 0, skip_pad ) ==
"LA… " + broken_txt + " <color_c_light_red_white>█…</color> "
"LA… " + broken_txt + " <color_c_light_red_white>█…</color> "
"L… " + broken_txt + " <color_c_light_red_white>█…</color>" );
}
}
GIVEN( "left arm HP 0" ) {
broken_txt = "<color_c_red_white>ℵ</color>";
ava.set_part_hp_cur( bid, 0 );
REQUIRE( ava.get_part_hp_cur( bid ) == 0 );
REQUIRE( ava.is_limb_broken( bid ) );
if( skip_pad ) {
const std::string txt = "LA : " + bleed_txt + broken_txt + "<color_c_red_white> </color>";
CHECK( wgt.layout( ava, width, 0, skip_pad ) == txt + txt + txt );
} else {
CHECK( wgt.layout( ava, width, 0, skip_pad ) ==
"LA… " + broken_txt + " <color_c_red_white> …</color> "
"LA… " + broken_txt + " <color_c_red_white> …</color> "
"L… " + broken_txt + " <color_c_red_white> …</color>" );
}
}
}

TEST_CASE( "W_NO_PADDING widget flag", "[widget]" )
{
widget wgt = widget_test_layout_nopad.obj();
avatar &ava = get_avatar();
clear_avatar();
clear_map();
ava.reset_all_missions();
ava.set_focus( 100 );
ava.movecounter = 0;

// mbstowcs can only process utf-8 chars when LC_CTYPE is set as UTF-8
const char *tmp_loc = std::setlocale( LC_CTYPE, nullptr );
std::setlocale( LC_CTYPE, "C.UTF-8" );

SECTION( "without flag" ) {
widget wgt = widget_test_layout_nopad_noflag.obj();
REQUIRE( !wgt.has_flag( "W_NO_PADDING" ) );

GIVEN( "left arm bleed intensity = 0" ) {
REQUIRE( !ava.has_effect( effect_bleed, body_part_arm_l ) );
test_widget_flag_nopad( body_part_arm_l, 0, ava, wgt, false );
}

GIVEN( "left arm bleed intensity = 1" ) {
ava.add_effect( effect_bleed, 10_turns, body_part_arm_l );
ava.get_effect( effect_bleed, body_part_arm_l ).set_intensity( 1 );
REQUIRE( ava.has_effect( effect_bleed, body_part_arm_l ) );
REQUIRE( ava.get_effect_int( effect_bleed, body_part_arm_l ) == 1 );
test_widget_flag_nopad( body_part_arm_l, 1, ava, wgt, false );
}

GIVEN( "left arm bleed intensity = 11" ) {
ava.add_effect( effect_bleed, 10_turns, body_part_arm_l );
ava.get_effect( effect_bleed, body_part_arm_l ).set_intensity( 11 );
REQUIRE( ava.has_effect( effect_bleed, body_part_arm_l ) );
REQUIRE( ava.get_effect_int( effect_bleed, body_part_arm_l ) == 11 );
test_widget_flag_nopad( body_part_arm_l, 11, ava, wgt, false );
}

GIVEN( "left arm bleed intensity = 21" ) {
ava.add_effect( effect_bleed, 10_turns, body_part_arm_l );
ava.get_effect( effect_bleed, body_part_arm_l ).set_intensity( 21 );
REQUIRE( ava.has_effect( effect_bleed, body_part_arm_l ) );
REQUIRE( ava.get_effect_int( effect_bleed, body_part_arm_l ) == 21 );
test_widget_flag_nopad( body_part_arm_l, 21, ava, wgt, false );
}
}

SECTION( "with flag" ) {
widget wgt = widget_test_layout_nopad.obj();
REQUIRE( wgt.has_flag( "W_NO_PADDING" ) );

GIVEN( "left arm bleed intensity = 0" ) {
REQUIRE( !ava.has_effect( effect_bleed, body_part_arm_l ) );
test_widget_flag_nopad( body_part_arm_l, 0, ava, wgt, true );
}

GIVEN( "left arm bleed intensity = 1" ) {
ava.add_effect( effect_bleed, 10_turns, body_part_arm_l );
ava.get_effect( effect_bleed, body_part_arm_l ).set_intensity( 1 );
REQUIRE( ava.has_effect( effect_bleed, body_part_arm_l ) );
REQUIRE( ava.get_effect_int( effect_bleed, body_part_arm_l ) == 1 );
test_widget_flag_nopad( body_part_arm_l, 1, ava, wgt, true );
}

GIVEN( "left arm bleed intensity = 11" ) {
ava.add_effect( effect_bleed, 10_turns, body_part_arm_l );
ava.get_effect( effect_bleed, body_part_arm_l ).set_intensity( 11 );
REQUIRE( ava.has_effect( effect_bleed, body_part_arm_l ) );
REQUIRE( ava.get_effect_int( effect_bleed, body_part_arm_l ) == 11 );
test_widget_flag_nopad( body_part_arm_l, 11, ava, wgt, true );
}

GIVEN( "left arm bleed intensity = 21" ) {
ava.add_effect( effect_bleed, 10_turns, body_part_arm_l );
ava.get_effect( effect_bleed, body_part_arm_l ).set_intensity( 21 );
REQUIRE( ava.has_effect( effect_bleed, body_part_arm_l ) );
REQUIRE( ava.get_effect_int( effect_bleed, body_part_arm_l ) == 21 );
test_widget_flag_nopad( body_part_arm_l, 21, ava, wgt, true );
}
}

std::setlocale( LC_CTYPE, tmp_loc );
}

0 comments on commit 8152f1a

Please sign in to comment.