File: block_string_spec.rb

package info (click to toggle)
ruby-graphql 2.5.19-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,868 kB
  • sloc: ruby: 80,420; ansic: 1,808; yacc: 845; javascript: 480; makefile: 6
file content (87 lines) | stat: -rw-r--r-- 2,237 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Language::BlockString do
  describe "trimming whitespace" do
    def trim_whitespace(str)
      GraphQL::Language::BlockString.trim_whitespace(str)
    end

    it "matches the examples in graphql-js" do
      # these are taken from:
      # https://github.com/graphql/graphql-js/blob/36ec0e9d34666362ff0e2b2b18edeb98e3c9abee/src/language/__tests__/blockStringValue-test.js#L12
      # A set of [before, after] pairs:
      examples = [
        [
          # Removes common whitespace:
          "
          Hello,
            World!

          Yours,
            GraphQL.
          ",
          "Hello,\n  World!\n\nYours,\n  GraphQL."
        ],
        [
          # Removes leading and trailing newlines:
          "

          Hello,
            World!

          Yours,
            GraphQL.

          ",
          "Hello,\n  World!\n\nYours,\n  GraphQL."
        ],
        [
          # Removes blank lines (with whitespace _and_ newlines:)
          "\n    \n
          Hello,
            World!

          Yours,
            GraphQL.

          \n     \n",
          "Hello,\n  World!\n\nYours,\n  GraphQL."
        ],
        [
          # Retains indentation from the first line
          "    Hello,\n      World!\n\n    Yours,\n      GraphQL.",
          "    Hello,\n  World!\n\nYours,\n  GraphQL.",
        ],
        [
          # Doesn't alter trailing spaces
          "\n    \n    Hello,     \n      World!   \n\n    Yours,     \n      GraphQL.  ",
          "Hello,     \n  World!   \n\nYours,     \n  GraphQL.  ",

        ],
        [
          # Doesn't crash when the string is only a newline
          "\n",
          ""
        ],
        [
          # Removes long blank lines
          "  \n                                     \n
          Hello,
            World!

          Yours,
            GraphQL.

          \n                                        \n",
          "Hello,\n  World!\n\nYours,\n  GraphQL."
        ]
      ]

      examples.each_with_index do |(before, after), idx|
        transformed_str = trim_whitespace(before)
        assert_equal(after, transformed_str, "Example ##{idx + 1}")
      end
    end
  end
end