Skip to content

Commit c07b9c6

Browse files
committed
Expand widget clauses: Unit tests
1 parent 87be5c3 commit c07b9c6

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

data/mods/TEST_DATA/widgets.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,65 @@
466466
"width": 3,
467467
"height": 3
468468
},
469+
{
470+
"id": "test_phrase_number",
471+
"type": "widget",
472+
"style": "number",
473+
"label": "Num Values",
474+
"default_phrase": { "value": 1, "color": "dark_gray" },
475+
"phrases": [
476+
{ "value": -20, "color": "red_red", "condition": { "not": "u_can_see" } },
477+
{ "value": -10, "color": "i_yellow", "condition": "u_is_deaf" },
478+
{ "value": 0, "color": "yellow", "condition": "is_day" },
479+
{ "value": 10, "color": "white_green", "condition": { "u_has_trait": "GOODHEARING" } },
480+
{ "value": 20, "color": "light_green", "condition": { "u_has_trait": "NIGHTVISION" } }
481+
]
482+
},
483+
{
484+
"id": "test_phrase_text",
485+
"type": "widget",
486+
"style": "text",
487+
"label": "Text Values",
488+
"default_phrase": { "text": "None", "color": "dark_gray" },
489+
"phrases": [
490+
{ "text": "blind", "color": "red_red", "condition": { "not": "u_can_see" } },
491+
{ "text": "deaf", "color": "i_yellow", "condition": "u_is_deaf" },
492+
{ "text": "daylight", "color": "yellow", "condition": "is_day" },
493+
{ "text": "good hearing", "color": "white_green", "condition": { "u_has_trait": "GOODHEARING" } },
494+
{ "text": "good vision", "color": "light_green", "condition": { "u_has_trait": "NIGHTVISION" } }
495+
]
496+
},
497+
{
498+
"id": "test_phrase_sym",
499+
"type": "widget",
500+
"style": "symbol",
501+
"label": "Symbol Values",
502+
"default_phrase": { "sym": ".", "color": "dark_gray" },
503+
"phrases": [
504+
{ "sym": "<", "color": "red_red", "condition": { "not": "u_can_see" } },
505+
{ "sym": "-", "color": "i_yellow", "condition": "u_is_deaf" },
506+
{ "sym": "=", "color": "yellow", "condition": "is_day" },
507+
{ "sym": "+", "color": "white_green", "condition": { "u_has_trait": "GOODHEARING" } },
508+
{ "sym": ">", "color": "light_green", "condition": { "u_has_trait": "NIGHTVISION" } }
509+
]
510+
},
511+
{
512+
"id": "test_phrase_legend",
513+
"type": "widget",
514+
"style": "legend",
515+
"label": "Legend Values",
516+
"height": 5,
517+
"width": 0,
518+
"default_phrase": { "text": "None", "sym": ".", "color": "dark_gray" },
519+
"phrases": [
520+
{ "text": "blind", "sym": "<", "color": "red_red", "condition": { "not": "u_can_see" } },
521+
{ "text": "deaf", "sym": "-", "color": "i_yellow", "condition": "u_is_deaf" },
522+
{ "text": "daylight", "sym": "=", "color": "yellow", "condition": "is_day" },
523+
{ "text": "good hearing", "sym": "+", "color": "white_green", "condition": { "u_has_trait": "GOODHEARING" } },
524+
{ "text": "good vision", "sym": ">", "color": "light_green", "condition": { "u_has_trait": "NIGHTVISION" } }
525+
],
526+
"flags": [ "W_LABEL_NONE", "W_DYNAMIC_HEIGHT" ]
527+
},
469528
{
470529
"id": "test_stat_panel",
471530
"type": "widget",

tests/widget_test.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ static const efftype_id effect_infected( "infected" );
4040

4141
static const flag_id json_flag_SPLINT( "SPLINT" );
4242

43+
static const itype_id itype_blindfold( "blindfold" );
44+
static const itype_id itype_ear_plugs( "ear_plugs" );
4345
static const itype_id itype_rad_badge( "rad_badge" );
4446

4547
static const move_mode_id move_mode_crouch( "crouch" );
4648
static const move_mode_id move_mode_prone( "prone" );
4749
static const move_mode_id move_mode_run( "run" );
4850
static const move_mode_id move_mode_walk( "walk" );
4951

52+
static const trait_id trait_GOODHEARING( "GOODHEARING" );
53+
static const trait_id trait_NIGHTVISION( "NIGHTVISION" );
54+
5055
static const weather_type_id weather_acid_rain( "acid_rain" );
5156
static const weather_type_id weather_cloudy( "cloudy" );
5257
static const weather_type_id weather_drizzle( "drizzle" );
@@ -85,6 +90,10 @@ static const widget_id widget_test_move_mode_text( "test_move_mode_text" );
8590
static const widget_id widget_test_move_num( "test_move_num" );
8691
static const widget_id widget_test_overmap_3x3_text( "test_overmap_3x3_text" );
8792
static const widget_id widget_test_per_color_num( "test_per_color_num" );
93+
static const widget_id widget_test_phrase_legend( "test_phrase_legend" );
94+
static const widget_id widget_test_phrase_number( "test_phrase_number" );
95+
static const widget_id widget_test_phrase_sym( "test_phrase_sym" );
96+
static const widget_id widget_test_phrase_text( "test_phrase_text" );
8897
static const widget_id widget_test_pool_graph( "test_pool_graph" );
8998
static const widget_id widget_test_rad_badge_text( "test_rad_badge_text" );
9099
static const widget_id widget_test_speed_num( "test_speed_num" );
@@ -1831,3 +1840,81 @@ TEST_CASE( "Widget alignment", "[widget]" )
18311840
" " + line3 + " " );
18321841
}
18331842
}
1843+
1844+
TEST_CASE( "Phrase conditions - pure JSON widgets", "[widget][phrase][condition]" )
1845+
{
1846+
const int sidebar_width = 20;
1847+
1848+
const time_point midnight = calendar::turn_zero + 0_hours;
1849+
const time_point midday = calendar::turn_zero + 12_hours;
1850+
1851+
const item blindfold( itype_blindfold );
1852+
const item earplugs( itype_ear_plugs );
1853+
1854+
widget w_num = widget_test_phrase_number.obj();
1855+
widget w_txt = widget_test_phrase_text.obj();
1856+
widget w_sym = widget_test_phrase_sym.obj();
1857+
widget w_lgd = widget_test_phrase_legend.obj();
1858+
1859+
avatar &ava = get_avatar();
1860+
clear_avatar();
1861+
set_time( midnight );
1862+
1863+
REQUIRE( !ava.has_trait( trait_GOODHEARING ) );
1864+
REQUIRE( !ava.has_trait( trait_NIGHTVISION ) );
1865+
REQUIRE( !is_day( calendar::turn ) );
1866+
REQUIRE( !ava.is_deaf() );
1867+
REQUIRE( !ava.is_blind() );
1868+
1869+
SECTION( "Default values" ) {
1870+
CHECK( w_num.layout( ava ) == "Num Values: <color_c_dark_gray>1</color>" );
1871+
CHECK( w_txt.layout( ava ) == "Text Values: <color_c_dark_gray>None</color>" );
1872+
CHECK( w_sym.layout( ava ) == "Symbol Values: <color_c_dark_gray>.</color>" );
1873+
CHECK( w_lgd.layout( ava, sidebar_width ) == "<color_c_dark_gray>. None</color> " );
1874+
}
1875+
1876+
SECTION( "GOODHEARING" ) {
1877+
ava.toggle_trait( trait_GOODHEARING );
1878+
CHECK( w_num.layout( ava ) == "Num Values: <color_c_white_green>10</color>" );
1879+
CHECK( w_txt.layout( ava ) == "Text Values: <color_c_white_green>good hearing</color>" );
1880+
CHECK( w_sym.layout( ava ) == "Symbol Values: <color_c_white_green>+</color>" );
1881+
CHECK( w_lgd.layout( ava, sidebar_width ) == "<color_c_white_green>+</color> good hearing\n" );
1882+
}
1883+
1884+
SECTION( "Daylight" ) {
1885+
set_time( midday );
1886+
CHECK( w_num.layout( ava ) == "Num Values: <color_c_yellow>0</color>" );
1887+
CHECK( w_txt.layout( ava ) == "Text Values: <color_c_yellow>daylight</color>" );
1888+
CHECK( w_sym.layout( ava ) == "Symbol Values: <color_c_yellow>=</color>" );
1889+
CHECK( w_lgd.layout( ava, sidebar_width ) == "<color_c_yellow>=</color> daylight\n" );
1890+
}
1891+
1892+
SECTION( "Daylight / Blind" ) {
1893+
set_time( midday );
1894+
ava.wear_item( blindfold, false );
1895+
CHECK( w_num.layout( ava ) ==
1896+
"Num Values: <color_c_red_red>-20</color>, <color_c_yellow>0</color>" );
1897+
CHECK( w_txt.layout( ava ) ==
1898+
"Text Values: <color_c_red_red>blind</color>, <color_c_yellow>daylight</color>" );
1899+
CHECK( w_sym.layout( ava ) ==
1900+
"Symbol Values: <color_c_red_red><</color><color_c_yellow>=</color>" );
1901+
CHECK( w_lgd.layout( ava, sidebar_width ) ==
1902+
"<color_c_red_red><</color> blind <color_c_yellow>=</color> daylight\n" );
1903+
}
1904+
1905+
SECTION( "Daylight / Blind / Deaf / GOODHEARING / NIGHTVISION" ) {
1906+
set_time( midday );
1907+
ava.wear_item( blindfold, false );
1908+
ava.wear_item( earplugs, false );
1909+
ava.toggle_trait( trait_GOODHEARING );
1910+
ava.toggle_trait( trait_NIGHTVISION );
1911+
CHECK( w_num.layout( ava ) ==
1912+
"Num Values: <color_c_red_red>-20</color>, <color_i_yellow>-10</color>, <color_c_yellow>0</color>, <color_c_white_green>10</color>, <color_c_light_green>20</color>" );
1913+
CHECK( w_txt.layout( ava ) ==
1914+
"Text Values: <color_c_red_red>blind</color>, <color_i_yellow>deaf</color>, <color_c_yellow>daylight</color>, <color_c_white_green>good hearing</color>, <color_c_light_green>good vision</color>" );
1915+
CHECK( w_sym.layout( ava ) ==
1916+
"Symbol Values: <color_c_red_red><</color><color_i_yellow>-</color><color_c_yellow>=</color><color_c_white_green>+</color><color_c_light_green>></color>" );
1917+
CHECK( w_lgd.layout( ava, sidebar_width ) ==
1918+
"<color_c_red_red><</color> blind <color_i_yellow>-</color> deaf\n<color_c_yellow>=</color> daylight\n<color_c_white_green>+</color> good hearing\n<color_c_light_green>></color> good vision\n" );
1919+
}
1920+
}

0 commit comments

Comments
 (0)