You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.7 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
  1. require_relative '../../lib/formatters/wrapper'
  2. require 'minitest/autorun'
  3. class Wrapper2Test < Minitest::Test
  4. def test_wrap_score
  5. assert_wrap("\
  6. ********************\n\
  7. ********************\n\
  8. **********",
  9. ('*' * 50), 20)
  10. assert_wrap("\
  11. > ******************\n\
  12. > ******************\n\
  13. > **************",
  14. ('*' * 50), 20, '> ')
  15. end
  16. def test_wrap_sql
  17. assert_wrap("\
  18. \e[0;35mScore Load (0.6ms)\e[0m \e[0;36mS\e[0m\n\
  19. \e[0;36mELECT \"scores\".* FRO\e[0m\n\
  20. \e[0;36mM \"scores\" WHERE \"sc\e[0m\n\
  21. \e[0;36mores\".\"id\" = $1 LIMI\e[0m\n\
  22. \e[0;36mT $2\e[0m [[\"id\", 2], [\"L\n\
  23. IMIT\", 1]]", "\
  24. \e[0;35mScore Load (0.6ms)\e[0m \e[0;36mSELECT \"scores\".* FROM \"scores\" WHERE \"scores\".\"id\" = $1 LIMIT $2\e[0m [[\"id\", 2], [\"LIMIT\", 1]]",
  25. 20)
  26. end
  27. def test_carriage_return
  28. assert_wrap('text', "text\n\n", 10)
  29. assert_wrap('first ', "first \n", 10)
  30. assert_wrap('first ', "first \n", 10)
  31. assert_wrap('first ', "first \n", 10)
  32. assert_wrap("first \n second ", "first\n second\n", 10)
  33. assert_wrap("\e[0;35mhello\e[0m \n\e[0;35m\e[0m\n\e[0;35myop", "\e[0;35mhello\n\nyop", 10)
  34. end
  35. private
  36. def assert_wrap(expectation, text, length, prefix = '')
  37. show_with_style(text, length, prefix)
  38. assert_equal(expectation, prefix + Wrapper.wrap(text, prefix, length - prefix.length))
  39. end
  40. def show_with_style(text, length, prefix = '')
  41. expectation = Wrapper.wrap(text, prefix, length - prefix.length)
  42. puts "original_o is:\n#{prefix}[#{text}]\e[0m"
  43. puts "original_i is:\n#{prefix}[#{text.inspect}]\e[0m"
  44. puts
  45. puts "final_o is:\n#{prefix}[#{expectation}]\e[0m"
  46. puts "final_i is:\n#{prefix}[#{expectation.inspect}]\e[0m"
  47. puts
  48. end
  49. end