util_spec.cr 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #
  2. # Copyright (c) 2023 supercell
  3. #
  4. # SPDX-License-Identifier: BSD-3-Clause
  5. #
  6. describe "String#to_lines" do
  7. it "works with a single line without an ending" do
  8. text = "Foo"
  9. lines = text.to_lines
  10. lines.map(&.to_hash).should eq [
  11. {
  12. "content" => "Foo",
  13. "is_blank_line" => false,
  14. },
  15. ]
  16. end
  17. it "works with a single line with a line ending" do
  18. text = "Foo\n"
  19. lines = text.to_lines
  20. lines.map(&.to_hash).should eq [
  21. {
  22. "content" => "Foo",
  23. "is_blank_line" => false,
  24. },
  25. ]
  26. end
  27. it "works with multiple lines with a blank line in between" do
  28. text = "Foo\r\n\nBar"
  29. lines = text.to_lines
  30. lines.map(&.to_hash).should eq [
  31. {
  32. "content" => "Foo",
  33. "is_blank_line" => false,
  34. },
  35. {
  36. "content" => "",
  37. "is_blank_line" => true,
  38. },
  39. {
  40. "content" => "Bar",
  41. "is_blank_line" => false,
  42. },
  43. ]
  44. end
  45. end
  46. module Luce
  47. describe Luce do
  48. describe "#string_indentation" do
  49. it "correctly counts with only spaces" do
  50. Luce.string_indentation(" ").should eq 3
  51. Luce.string_indentation(" ").should eq 4
  52. Luce.string_indentation(" ").should eq 5
  53. end
  54. it "correctly counts spaces and tabs" do
  55. Luce.string_indentation("\t ").should eq 6
  56. Luce.string_indentation(" \t ").should eq 5
  57. Luce.string_indentation(" \t").should eq 4
  58. Luce.string_indentation("\t\t ").should eq 10
  59. Luce.string_indentation(" \t\t ").should eq 9
  60. Luce.string_indentation(" \t\t").should eq 8
  61. end
  62. it "correctly counts spaces and tabs with non-whitespace characters" do
  63. Luce.string_indentation("\t foo").should eq 6
  64. Luce.string_indentation(" \t foo").should eq 5
  65. Luce.string_indentation(" \tfoo").should eq 4
  66. end
  67. end
  68. end
  69. end
  70. class Luce::Line
  71. def to_hash : Hash(String, Bool | String)
  72. {
  73. "content" => content,
  74. "is_blank_line" => is_blank_line?,
  75. }
  76. end
  77. end