mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-09 09:38:19 +00:00
Merge pull request #8874 from ChillerDragon/pr_test_tooltip_style
Test tooltip description style
This commit is contained in:
commit
a3fc627285
|
@ -2930,6 +2930,7 @@ if(GTEST_FOUND OR DOWNLOAD_GTEST)
|
|||
compression.cpp
|
||||
csv.cpp
|
||||
datafile.cpp
|
||||
editor.cpp
|
||||
fs.cpp
|
||||
git_revision.cpp
|
||||
hash.cpp
|
||||
|
|
|
@ -134,7 +134,7 @@ REGISTER_QUICK_ACTION(
|
|||
ALWAYS_FALSE,
|
||||
ALWAYS_FALSE,
|
||||
DEFAULT_BTN,
|
||||
"[HOME] Restore map focus.")
|
||||
"[Home] Restore map focus.")
|
||||
REGISTER_QUICK_ACTION(
|
||||
Proof,
|
||||
"Proof",
|
||||
|
|
103
src/test/editor.cpp
Normal file
103
src/test/editor.cpp
Normal file
|
@ -0,0 +1,103 @@
|
|||
#include <gtest/gtest.h>
|
||||
|
||||
#include <base/system.h>
|
||||
|
||||
bool is_letter(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
|
||||
|
||||
bool IsValidEditorTooltip(const char *pTooltip, char *pErrorMsg, int ErrorMsgSize)
|
||||
{
|
||||
pErrorMsg[0] = '\0';
|
||||
char aHotkey[512];
|
||||
aHotkey[0] = '\0';
|
||||
|
||||
if(pTooltip[0] == '[')
|
||||
{
|
||||
str_copy(aHotkey, pTooltip + 1);
|
||||
const char *pHotkeyEnd = str_find(aHotkey, "]");
|
||||
if(!pHotkeyEnd)
|
||||
{
|
||||
str_copy(pErrorMsg, "tooltip missing closing square bracket", ErrorMsgSize);
|
||||
return false;
|
||||
}
|
||||
aHotkey[pHotkeyEnd - aHotkey] = '\0';
|
||||
|
||||
for(int i = 0; aHotkey[i]; i++)
|
||||
{
|
||||
bool ExpectLowerCase = true;
|
||||
if(i == 0)
|
||||
{
|
||||
ExpectLowerCase = false;
|
||||
}
|
||||
else if(!is_letter(aHotkey[i - 1]))
|
||||
{
|
||||
// the first character of a word should be uppercase
|
||||
ExpectLowerCase = false;
|
||||
}
|
||||
|
||||
bool IsLower = aHotkey[i] >= 'a' && aHotkey[i] <= 'z';
|
||||
bool IsUpper = aHotkey[i] >= 'A' && aHotkey[i] <= 'Z';
|
||||
|
||||
if(ExpectLowerCase && IsUpper)
|
||||
{
|
||||
str_format(pErrorMsg, ErrorMsgSize, "expected character '%c' at index %d to be lower case", aHotkey[i], i + 1);
|
||||
return false;
|
||||
}
|
||||
if(!ExpectLowerCase && IsLower)
|
||||
{
|
||||
str_format(pErrorMsg, ErrorMsgSize, "expected character '%c' at index %d to be upper case", aHotkey[i], i + 1);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char *pParenthesis = str_find(pTooltip, "(");
|
||||
if(pParenthesis)
|
||||
{
|
||||
const char *pHotkey = str_find_nocase(pParenthesis, "ctrl");
|
||||
if(!pHotkey)
|
||||
pHotkey = str_find_nocase(pParenthesis, "shift");
|
||||
if(!pHotkey)
|
||||
pHotkey = str_find_nocase(pParenthesis, "home");
|
||||
|
||||
if(pHotkey)
|
||||
{
|
||||
int Offset = pHotkey - pTooltip;
|
||||
str_format(pErrorMsg, ErrorMsgSize, "found hotkey at offset %d. Hotkeys must be defined at the start.", Offset);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if(!str_endswith(pTooltip, "."))
|
||||
{
|
||||
str_copy(pErrorMsg, "tooltip has to end with a dot", ErrorMsgSize);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void AssertTooltip(const char *pTooltip)
|
||||
{
|
||||
char aError[512];
|
||||
bool IsValid = IsValidEditorTooltip(pTooltip, aError, sizeof(aError));
|
||||
if(!IsValid)
|
||||
{
|
||||
dbg_msg("test", "Invalid tooltip: %s", pTooltip);
|
||||
dbg_msg("test", "ERROR: %s", aError);
|
||||
}
|
||||
EXPECT_TRUE(IsValid);
|
||||
}
|
||||
|
||||
TEST(Editor, QuickActionNames)
|
||||
{
|
||||
char aError[512];
|
||||
EXPECT_TRUE(IsValidEditorTooltip("hello world.", aError, sizeof(aError)));
|
||||
EXPECT_TRUE(IsValidEditorTooltip("[Ctrl+H] hello world.", aError, sizeof(aError)));
|
||||
EXPECT_FALSE(IsValidEditorTooltip("[Ctrl+H hello world.", aError, sizeof(aError)));
|
||||
EXPECT_FALSE(IsValidEditorTooltip("[ctrl+h] hello world.", aError, sizeof(aError)));
|
||||
EXPECT_FALSE(IsValidEditorTooltip("hello world", aError, sizeof(aError)));
|
||||
EXPECT_FALSE(IsValidEditorTooltip("hello world (Ctrl+H).", aError, sizeof(aError)));
|
||||
|
||||
#define REGISTER_QUICK_ACTION(name, text, callback, disabled, active, button_color, description) AssertTooltip(description);
|
||||
#include <game/editor/quick_actions.h>
|
||||
#undef REGISTER_QUICK_ACTION
|
||||
}
|
Loading…
Reference in a new issue