yql_warning_ut.cpp 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include "yql_warning.h"
  3. using namespace NYql;
  4. Y_UNIT_TEST_SUITE(TWarningRuleTest) {
  5. Y_UNIT_TEST(AllValidActionsAndPatternsAreParsed) {
  6. TWarningRule rule;
  7. TString errorMessage;
  8. UNIT_ASSERT_VALUES_EQUAL(TWarningRule::ParseFrom("1234", "disable", rule, errorMessage), TWarningRule::EParseResult::PARSE_OK);
  9. UNIT_ASSERT_VALUES_EQUAL(TWarningRule::ParseFrom("*", "error", rule, errorMessage), TWarningRule::EParseResult::PARSE_OK);
  10. UNIT_ASSERT_VALUES_EQUAL(TWarningRule::ParseFrom("0", "default", rule, errorMessage), TWarningRule::EParseResult::PARSE_OK);
  11. }
  12. Y_UNIT_TEST(InvalidActionIsDetected) {
  13. TWarningRule rule;
  14. TString errorMessage;
  15. UNIT_ASSERT_VALUES_EQUAL(TWarningRule::ParseFrom("1234", "wrong", rule, errorMessage), TWarningRule::EParseResult::PARSE_ACTION_FAIL);
  16. UNIT_ASSERT_VALUES_EQUAL(TWarningRule::ParseFrom("1234", "", rule, errorMessage), TWarningRule::EParseResult::PARSE_ACTION_FAIL);
  17. }
  18. Y_UNIT_TEST(InvalidPatternIsDetected) {
  19. TWarningRule rule;
  20. TString errorMessage;
  21. UNIT_ASSERT_VALUES_EQUAL(TWarningRule::ParseFrom("", "default", rule, errorMessage), TWarningRule::EParseResult::PARSE_PATTERN_FAIL);
  22. UNIT_ASSERT_VALUES_EQUAL(TWarningRule::ParseFrom("-1", "default", rule, errorMessage), TWarningRule::EParseResult::PARSE_PATTERN_FAIL);
  23. UNIT_ASSERT_VALUES_EQUAL(TWarningRule::ParseFrom("*1", "default", rule, errorMessage), TWarningRule::EParseResult::PARSE_PATTERN_FAIL);
  24. UNIT_ASSERT_VALUES_EQUAL(TWarningRule::ParseFrom("default", "default", rule, errorMessage), TWarningRule::EParseResult::PARSE_PATTERN_FAIL);
  25. }
  26. }
  27. void AddRule(TWarningPolicy& policy, const TString& codePattern, const TString& action)
  28. {
  29. TWarningRule rule;
  30. TString errorMessage;
  31. UNIT_ASSERT_VALUES_EQUAL(TWarningRule::ParseFrom(codePattern, action, rule, errorMessage), TWarningRule::EParseResult::PARSE_OK);
  32. policy.AddRule(rule);
  33. }
  34. Y_UNIT_TEST_SUITE(TWarningPolicyTest) {
  35. Y_UNIT_TEST(BasicIntegerRules) {
  36. TWarningPolicy policy;
  37. AddRule(policy, "123", "error");
  38. AddRule(policy, "456", "error");
  39. AddRule(policy, "456", "disable");
  40. UNIT_ASSERT_VALUES_EQUAL(policy.GetRules().size(), 3);
  41. UNIT_ASSERT_VALUES_EQUAL(policy.GetAction(123), EWarningAction::ERROR);
  42. UNIT_ASSERT_VALUES_EQUAL(policy.GetAction(456), EWarningAction::DISABLE);
  43. UNIT_ASSERT_VALUES_EQUAL(policy.GetAction(111), EWarningAction::DEFAULT);
  44. }
  45. Y_UNIT_TEST(AsteriskRules) {
  46. TWarningPolicy policy;
  47. AddRule(policy, "*", "error");
  48. AddRule(policy, "456", "disable");
  49. UNIT_ASSERT_VALUES_EQUAL(policy.GetAction(999), EWarningAction::ERROR);
  50. UNIT_ASSERT_VALUES_EQUAL(policy.GetAction(456), EWarningAction::DISABLE);
  51. AddRule(policy, "*", "default");
  52. UNIT_ASSERT_VALUES_EQUAL(policy.GetRules().size(), 3);
  53. UNIT_ASSERT_VALUES_EQUAL(policy.GetAction(999), EWarningAction::DEFAULT);
  54. UNIT_ASSERT_VALUES_EQUAL(policy.GetAction(456), EWarningAction::DEFAULT);
  55. UNIT_ASSERT_VALUES_EQUAL(policy.GetAction(123), EWarningAction::DEFAULT);
  56. }
  57. Y_UNIT_TEST(ReactionOnPull) {
  58. TWarningPolicy policy;
  59. AddRule(policy, "*", "error");
  60. AddRule(policy, "456", "default");
  61. AddRule(policy, "999", "disable");
  62. UNIT_ASSERT_VALUES_EQUAL(policy.GetAction(999), EWarningAction::DISABLE);
  63. UNIT_ASSERT_VALUES_EQUAL(policy.GetAction(456), EWarningAction::DEFAULT);
  64. UNIT_ASSERT_VALUES_EQUAL(policy.GetAction(123), EWarningAction::ERROR);
  65. UNIT_ASSERT_VALUES_EQUAL(policy.GetRules().size(), 3);
  66. policy.Clear();
  67. UNIT_ASSERT_VALUES_EQUAL(policy.GetRules().size(), 0);
  68. UNIT_ASSERT_VALUES_EQUAL(policy.GetAction(999), EWarningAction::DEFAULT);
  69. UNIT_ASSERT_VALUES_EQUAL(policy.GetAction(456), EWarningAction::DEFAULT);
  70. UNIT_ASSERT_VALUES_EQUAL(policy.GetAction(123), EWarningAction::DEFAULT);
  71. }
  72. }