polybar-dwm/tests/unit_tests/components/config_parser.cpp

315 lines
9.2 KiB
C++
Raw Normal View History

config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
#include "components/config_parser.hpp"
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
#include "common/test.hpp"
#include "components/logger.hpp"
using namespace polybar;
using namespace std;
/**
* \brief Testing-only subclass of config_parser to change access level
*/
class TestableConfigParser : public config_parser {
using config_parser::config_parser;
public:
TestableConfigParser(const logger& logger, string&& file)
: config_parser(logger, move(file)) {
m_files.push_back("test_config");
}
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
public:
using config_parser::get_line_type;
public:
using config_parser::parse_key;
public:
using config_parser::parse_header;
public:
using config_parser::parse_line;
Add initial support for an escape character (#2361) Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Also: - Add documentation about the escape character to polybar(5) manpage - Add info about the escape character to changelog - Add testcases for ParseLineKeyTest - Add new test ParseEscapedValueTest Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add escape character documentation to manpages Add information about the escape character to the polybar(5) manpage. Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Fix parse_escaped_value Add more test cases for ParseLineKeyTest Update CHANGELOG.md Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com> Adress review - Adjust documentation - Small code changes Improve parse_escaped_value Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Add more test cases for ParseLineKeyTest Adress review - Adjust documentation - Small code changes Remove duplicate testcase from ParseLineKeyTest Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Remove duplicate testcase from ParseLineKeyTest
2021-01-26 18:16:29 +00:00
public:
using config_parser::parse_escaped_value;
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
public:
using config_parser::m_files;
};
/**
* \brief Fixture class
*/
class ConfigParser : public ::testing::Test {
protected:
const logger l = logger(loglevel::NONE);
unique_ptr<TestableConfigParser> parser = make_unique<TestableConfigParser>(l, "/dev/zero");
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
};
// ParseLineTest {{{
class ParseLineInValidTest : public ConfigParser, public ::testing::WithParamInterface<string> {};
class ParseLineHeaderTest : public ConfigParser, public ::testing::WithParamInterface<pair<string, string>> {};
class ParseLineKeyTest : public ConfigParser,
public ::testing::WithParamInterface<pair<pair<string, string>, string>> {};
vector<string> parse_line_invalid_list = {
" # comment",
"; comment",
"\t#",
"",
" ",
"\t ",
};
vector<pair<string, string>> parse_line_header_list = {
{"section", "\t[section]"},
{"section", "\t[section] "},
{"bar/name", "\t[bar/name] "},
};
vector<pair<pair<string, string>, string>> parse_line_key_list = {
{{"key", "value"}, " key = value"},
{{"key", ""}, " key\t = \"\""},
{{"key", "\""}, " key\t = \"\"\""},
Add initial support for an escape character (#2361) Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Also: - Add documentation about the escape character to polybar(5) manpage - Add info about the escape character to changelog - Add testcases for ParseLineKeyTest - Add new test ParseEscapedValueTest Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add escape character documentation to manpages Add information about the escape character to the polybar(5) manpage. Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Fix parse_escaped_value Add more test cases for ParseLineKeyTest Update CHANGELOG.md Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com> Adress review - Adjust documentation - Small code changes Improve parse_escaped_value Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Add more test cases for ParseLineKeyTest Adress review - Adjust documentation - Small code changes Remove duplicate testcase from ParseLineKeyTest Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Remove duplicate testcase from ParseLineKeyTest
2021-01-26 18:16:29 +00:00
{{"key", "\\"}, " key = \\"},
{{"key", "\\"}, " key = \\\\"},
{{"key", "\\val\\ue\\"}, " key = \\val\\ue\\"},
{{"key", "\\val\\ue\\"}, " key = \\\\val\\\\ue\\\\"},
{{"key", "\\val\\ue\\"}, " key = \"\\val\\ue\\\""},
{{"key", "\\val\\ue\\"}, " key = \"\\\\val\\\\ue\\\\\""},
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
};
INSTANTIATE_TEST_SUITE_P(Inst, ParseLineInValidTest, ::testing::ValuesIn(parse_line_invalid_list));
TEST_P(ParseLineInValidTest, correctness) {
line_t line;
line.file_index = 0;
line.line_no = 0;
parser->parse_line(line, GetParam());
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
EXPECT_FALSE(line.useful);
}
INSTANTIATE_TEST_SUITE_P(Inst, ParseLineHeaderTest, ::testing::ValuesIn(parse_line_header_list));
TEST_P(ParseLineHeaderTest, correctness) {
line_t line;
line.file_index = 0;
line.line_no = 0;
parser->parse_line(line, GetParam().second);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
EXPECT_TRUE(line.useful);
EXPECT_TRUE(line.is_header);
EXPECT_EQ(GetParam().first, line.header);
}
INSTANTIATE_TEST_SUITE_P(Inst, ParseLineKeyTest, ::testing::ValuesIn(parse_line_key_list));
TEST_P(ParseLineKeyTest, correctness) {
line_t line;
line.file_index = 0;
line.line_no = 0;
parser->parse_line(line, GetParam().second);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
EXPECT_TRUE(line.useful);
EXPECT_FALSE(line.is_header);
EXPECT_EQ(GetParam().first.first, line.key);
EXPECT_EQ(GetParam().first.second, line.value);
}
TEST_F(ParseLineInValidTest, throwsSyntaxError) {
line_t line;
line.file_index = 0;
line.line_no = 0;
EXPECT_THROW(parser->parse_line(line, "unknown"), syntax_error);
EXPECT_THROW(parser->parse_line(line, "\ufeff"), syntax_error);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
// }}}
// GetLineTypeTest {{{
/**
* \brief Class for parameterized tests on get_line_type
*
* Parameters are pairs of the expected line type and a string that should be
* detected as that line type
*/
class GetLineTypeTest : public ConfigParser, public ::testing::WithParamInterface<pair<line_type, string>> {};
/**
* \brief Helper function generate GetLineTypeTest parameter values
*/
vector<pair<line_type, string>> line_type_transform(vector<string>&& in, line_type type) {
vector<pair<line_type, string>> out;
out.reserve(in.size());
for (const auto& i : in) {
out.emplace_back(type, i);
}
return out;
}
/**
* \brief Parameter values for GetLineTypeTest
*/
auto line_type_key = line_type_transform({"a = b", " a =b", " a\t =\t \t b", "a = "}, line_type::KEY);
auto line_type_header = line_type_transform({"[section]", "[section]", "[section/sub]"}, line_type::HEADER);
auto line_type_comment = line_type_transform({";abc", "#abc", ";", "#"}, line_type::COMMENT);
auto line_type_empty = line_type_transform({""}, line_type::EMPTY);
auto line_type_unknown = line_type_transform({"|a", " |a", "a"}, line_type::UNKNOWN);
/**
* Instantiate GetLineTypeTest for the different line types
*/
INSTANTIATE_TEST_SUITE_P(LineTypeKey, GetLineTypeTest, ::testing::ValuesIn(line_type_key));
INSTANTIATE_TEST_SUITE_P(LineTypeHeader, GetLineTypeTest, ::testing::ValuesIn(line_type_header));
INSTANTIATE_TEST_SUITE_P(LineTypeComment, GetLineTypeTest, ::testing::ValuesIn(line_type_comment));
INSTANTIATE_TEST_SUITE_P(LineTypeEmpty, GetLineTypeTest, ::testing::ValuesIn(line_type_empty));
INSTANTIATE_TEST_SUITE_P(LineTypeUnknown, GetLineTypeTest, ::testing::ValuesIn(line_type_unknown));
/**
* \brief Parameterized test for get_line_type
*/
TEST_P(GetLineTypeTest, correctness) {
EXPECT_EQ(GetParam().first, parser->get_line_type(GetParam().second));
}
// }}}
// ParseKeyTest {{{
/**
* \brief Class for parameterized tests on parse_key
*
* The first element of the pair is the expected key-value pair and the second
* element is the string to be parsed, has to be trimmed and valid.
*/
class ParseKeyTest : public ConfigParser, public ::testing::WithParamInterface<pair<pair<string, string>, string>> {};
vector<pair<pair<string, string>, string>> parse_key_list = {
{{"key", "value"}, "key = value"},
{{"key", "value"}, "key=value"},
{{"key", "value"}, "key =\"value\""},
{{"key", "value"}, "key\t=\t \"value\""},
{{"key", "\"value"}, "key = \"value"},
{{"key", "value\""}, "key = value\""},
{{"key", "= value"}, "key == value"},
{{"key", ""}, "key ="},
{{"key", ""}, R"(key ="")"},
{{"key", "\"\""}, R"(key ="""")"},
};
INSTANTIATE_TEST_SUITE_P(Inst, ParseKeyTest, ::testing::ValuesIn(parse_key_list));
/**
* Parameterized test for parse_key with valid line
*/
TEST_P(ParseKeyTest, correctness) {
line_t line;
line.file_index = 0;
line.line_no = 0;
EXPECT_EQ(GetParam().first, parser->parse_key(line, GetParam().second));
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
/**
* Tests if exception is thrown for invalid key line
*/
TEST_F(ParseKeyTest, throwsSyntaxError) {
line_t line;
line.file_index = 0;
line.line_no = 0;
EXPECT_THROW(parser->parse_key(line, "= empty name"), syntax_error);
EXPECT_THROW(parser->parse_key(line, "forbidden char = value"), syntax_error);
EXPECT_THROW(parser->parse_key(line, "forbidden\tchar = value"), syntax_error);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
// }}}
// ParseHeaderTest {{{
/**
* \brief Class for parameterized tests on parse_key
*
* The first element of the pair is the expected key-value pair and the second
* element is the string to be parsed, has to be trimmed and valid
*/
class ParseHeaderTest : public ConfigParser, public ::testing::WithParamInterface<pair<string, string>> {};
vector<pair<string, string>> parse_header_list = {
{"section", "[section]"},
{"bar/name", "[bar/name]"},
{"with_underscore", "[with_underscore]"},
};
INSTANTIATE_TEST_SUITE_P(Inst, ParseHeaderTest, ::testing::ValuesIn(parse_header_list));
/**
* Parameterized test for parse_header with valid line
*/
TEST_P(ParseHeaderTest, correctness) {
line_t line;
line.file_index = 0;
line.line_no = 0;
EXPECT_EQ(GetParam().first, parser->parse_header(line, GetParam().second));
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
/**
* Tests if exception is thrown for invalid header line
*/
TEST_F(ParseHeaderTest, throwsSyntaxError) {
line_t line;
line.file_index = 0;
line.line_no = 0;
EXPECT_THROW(parser->parse_header(line, "[]"), syntax_error);
EXPECT_THROW(parser->parse_header(line, "[no_end"), syntax_error);
EXPECT_THROW(parser->parse_header(line, "[forbidden char]"), syntax_error);
EXPECT_THROW(parser->parse_header(line, "[forbidden\tchar]"), syntax_error);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
// Reserved names
EXPECT_THROW(parser->parse_header(line, "[self]"), syntax_error);
EXPECT_THROW(parser->parse_header(line, "[BAR]"), syntax_error);
EXPECT_THROW(parser->parse_header(line, "[root]"), syntax_error);
config_parser: Introduce stricter syntax conventions (#1377) This is the next step to merge #1237 in stages. Currently there are barely any restrictions on how the config can be written. This causes things like config files with DOS line endings to not be parsed properly (#1366) because polybar splits by `\n` and when parsing section headers, it can't deal with the `\r` at the end of the line and thus doesn't recognize any section headers. With this PR we introduce some rules as to what characters are allowed in section names and keys. Note: When talking about spaces I refer to any character for which `isspace()` returns `true`. The rules are as follows: * A section name or a key name cannot contain any spaces as well as any of there characters:`"'=;#[](){}:.$\%` * Spaces at the beginning and end of lines are always ignored when parsing * Comment lines start with `;` or `#` and last for the whole line. The whole line will be ignored by the parser. You cannot start a comment at the end of a line. * Section headers have the following form `[HEADER_NAME]` * Key-value lines look like this: `KEY_NAME{SPACES}={SPACES}VALUE_STRING` where `{SPACES}` represents any number of spaces. `VALUE_STRING` can contain any characters. If it is *surrounded* with double quotes (`"`), those quotes will be removed, this can be used to add spaces to the beginning or end of the value * Empty lines are lines with only spaces in them * If the line has any other form, it is a syntax error This will introduce the following breaking changes because of how underdefined the config syntax was before: * `key = ""` will get treated as an empty string instead of the literal * string `""` * Any section or key name with forbidden characters will now be syntax errors. * Certain strings will be forbidden as section names: `self`, `root`, * `BAR`. Because they have a special meaning inside references and so a * section `[root]` can never be referenced. This replaces the current parser implementation with a new more robust one that will later be expanded to also check for dependency cycles and allow for values that contain references mixed with other strings. This PR also now expands the config paths given over the command line so that `--config=~/.config/polybar/config` resolves properly. Closes #1032 Closes #1694 * config_parser: Add skeleton with tests First step in the config_parser develoment. Only tests functions that are easily testable without many outside dependencies. Integration tests will follow. * config_parser: Implement parse_header * config_parser: Implement get_line_type * feat(string): Add trim functions with predicate Not only trimming based on single character matching but based on a freely specifiable predicate. Will be used to trim all spaces (based on isspace) * config_parser: Implement parse_key * config_parser: Implement parse_line for valid lines * config_parser: Throw exception on invalid lines * config_parser: Remove line_no and file_index from parse_line Cleaner to let the caller catch and fill in the line number and file path * string: Clear up misleading description of trim Before, trim would remove all characters that *didn't* match the predicate and thus the predicate isspace wouldn't work correctly. But because we used the inverse (isnospace_pred) it all worked out, but if the function was used with any other function, it wouldn't have given the desired output * config_parser: Implement parse_file * config_parser: Switch operation to config_parser This changes the way the config is invoked. Now main.cpp creates a config_parser object which then returns the singleton config object from the parse method. Subsequent calls to config::make will return the already created config object as before The config_parser does not yet have all the functionality of the old parser: `inherit` directives are not yet resolved. Other than that all the old functionality is implemented (creating sectionmap and applying include-file) Any sort of dependency detection (except for include-file) are still missing * config: Move xrm initialization to constructor config_parser handles the detection of xrdb references and passes that info to the config object. This finally allows us to delete the config::parse_file function because everything in it has been implemented (except for xrdb detection and file error handling) * refactor(config_parser): Cleanup * config_parser: Set config data after initialization Looks much cleaner this way * config_parser: Expand include-file paths * config_parser: Init xrm if the config uses %{xrdb references * config_parser: Use same type of maps as in old impl Polybar has some weird, not yet fixed, inheriting behaviour and it changes depending on the order in which the config stores its data. Using the same type of maps ensures that the behaviour stays the same. * refactor(config_parser): Clearer invalid name error message * config_parser: Don't allow reserved section names Sections with the names 'self', 'BAR', 'root' could never be referenced because those strings have a special meaning inside references * config_parser: Handle inherit directives This uses the old copy_inherited function, so this still suffers from crashes if there are cyclic dependencies. This also fixes the behaviour where any key that starts with 'inherit' would be treated as an inherit directive * config_parser: Clearer dependency cycle error message * refactor(config_parser): Handle file errors when parsing This removes the need to check if the file exists separately * fix(config): expand config file path Now paths using ~ and environment variables can be used as the config path * fix(config): Properly recognize xrdb references * config_parser: Make messages more informative * doc(config): Improve commenting Comments now describe what the config_parser actually does instead of what it will do. We also now follow the rule that single line comments inside functions should use `//` comments * refactor: Move else on same line as curly braces * fix(config_parser): Don't duplicate paths in `files` * refactor(config_parser): Use else if for clarity * fix(config): Undefined behavior in syntax_error Before the custom what() method produced undefined behavior because the returned string became invalid once the function returned. * refactor(config): descriptive name for useless lines is_valid could easily be confused as meaning syntactically invalid without it being clarified in a comment * refactor(config): Use separate strings instead of key_value Takes just as much space and is much better to read * fix(config_parser): TestCase -> TestSuite and fix macro call Ref: #1644 * config_parser: use const string& in method args * config_parser: Improve comments * config_parser: Incorporate review comments
2019-08-06 17:41:31 +00:00
}
// }}}
Add initial support for an escape character (#2361) Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Also: - Add documentation about the escape character to polybar(5) manpage - Add info about the escape character to changelog - Add testcases for ParseLineKeyTest - Add new test ParseEscapedValueTest Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add escape character documentation to manpages Add information about the escape character to the polybar(5) manpage. Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Fix parse_escaped_value Add more test cases for ParseLineKeyTest Update CHANGELOG.md Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com> Adress review - Adjust documentation - Small code changes Improve parse_escaped_value Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Add more test cases for ParseLineKeyTest Adress review - Adjust documentation - Small code changes Remove duplicate testcase from ParseLineKeyTest Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Remove duplicate testcase from ParseLineKeyTest
2021-01-26 18:16:29 +00:00
// ParseEscapedValueTest {{{
/**
* \brief Class for parameterized tests on parse_escaped_value
*
* The first element of the pair is the expected value and the second
* element is the escaped string to be parsed.
*/
class ParseEscapedValueTest : public ConfigParser, public ::testing::WithParamInterface<pair<string, string>> {};
vector<pair<string, string>> parse_escaped_value_list = {
{"\\", "\\"},
{"\\", "\\\\"},
{"\\val\\ue\\", "\\val\\ue\\"},
{"\\val\\ue\\", "\\\\val\\\\ue\\\\"},
{"\"\\val\\ue\\\"", "\"\\val\\ue\\\""},
{"\"\\val\\ue\\\"", "\"\\\\val\\\\ue\\\\\""},
};
INSTANTIATE_TEST_SUITE_P(Inst, ParseEscapedValueTest, ::testing::ValuesIn(parse_escaped_value_list));
/**
* Parameterized test for parse_escaped_value with valid line
*/
TEST_P(ParseEscapedValueTest, correctness) {
line_t line;
line.file_index = 0;
line.line_no = 0;
Add initial support for an escape character (#2361) Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Also: - Add documentation about the escape character to polybar(5) manpage - Add info about the escape character to changelog - Add testcases for ParseLineKeyTest - Add new test ParseEscapedValueTest Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add escape character documentation to manpages Add information about the escape character to the polybar(5) manpage. Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Fix parse_escaped_value Add more test cases for ParseLineKeyTest Update CHANGELOG.md Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com> Adress review - Adjust documentation - Small code changes Improve parse_escaped_value Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Add more test cases for ParseLineKeyTest Adress review - Adjust documentation - Small code changes Remove duplicate testcase from ParseLineKeyTest Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Remove duplicate testcase from ParseLineKeyTest
2021-01-26 18:16:29 +00:00
string value = GetParam().second;
value = parser->parse_escaped_value(line, move(value), "key");
Add initial support for an escape character (#2361) Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Also: - Add documentation about the escape character to polybar(5) manpage - Add info about the escape character to changelog - Add testcases for ParseLineKeyTest - Add new test ParseEscapedValueTest Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add escape character documentation to manpages Add information about the escape character to the polybar(5) manpage. Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Fix parse_escaped_value Add more test cases for ParseLineKeyTest Update CHANGELOG.md Co-authored-by: Patrick Ziegler <p.ziegler96@gmail.com> Adress review - Adjust documentation - Small code changes Improve parse_escaped_value Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Add info about the esacape character to changelog Add test cases for ParseLineKeyTest Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Add tests for escaped_value_parser method Also remove unsued include statement Simplify parse_escaped_value in config_parser Remove unnecessary escaped value generation, so we do not have to keep track of index differences. Fix ParseEscapedValueTest test cases Add more test cases for ParseLineKeyTest Adress review - Adjust documentation - Small code changes Remove duplicate testcase from ParseLineKeyTest Add initial support for an escape character Add a config parser method which, for now, deals only with escaping the literal backslash character and logs an error message to inform the user of the coming change. The error message includes a properly escaped value for the user. As a result of introducing an escape character('\'): - Warn the user of any unescaped backslashes, as they will not be treated as a literal character in the future - For now, still treat a single backslash as a literal character - Treat two consecutive backslashes as a single properly escaped literal backslash Resolves: First step in #2354 Improve value parsing - Take value arg in as an rvalue reference and move parsed value back - Remove unnecessary if statement - Rename function - Improve error message - Improve function description - Format Fix ParseLineKeyTest test cases Also make config parser method parse_escaped_value private. Remove duplicate testcase from ParseLineKeyTest
2021-01-26 18:16:29 +00:00
EXPECT_EQ(GetParam().first, value);
}
// }}}