File: token_list_spec.rb

package info (click to toggle)
yard 0.9.38-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,736 kB
  • sloc: ruby: 31,680; javascript: 7,658; makefile: 21
file content (79 lines) | stat: -rw-r--r-- 2,477 bytes parent folder | download | duplicates (5)
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
# frozen_string_literal: true

RSpec.describe YARD::Parser::Ruby::Legacy::TokenList do
  Legacy = YARD::Parser::Ruby::Legacy
  TokenList = Legacy::TokenList
  LT = Legacy::RubyToken

  describe "#initialize / #push" do
    it "accepts a tokenlist (via constructor or push)" do
      expect { TokenList.new(TokenList.new) }.not_to raise_error
      expect(TokenList.new.push(TokenList.new("x = 2")).size).to eq 6
    end

    it "accept a token (via constructor or push)" do
      expect { TokenList.new(LT::Token.new(0, 0)) }.not_to raise_error
      expect(TokenList.new.push(LT::Token.new(0, 0),
                                LT::Token.new(1, 1)).size).to eq 2
    end

    it "accepts a string and parse it as code (via constructor or push)" do
      expect { TokenList.new("x = 2") }.not_to raise_error
      x = TokenList.new
      x.push("x", "=", "2")
      expect(x.size).to eq 6
      expect(x.to_s).to eq "x\n=\n2\n"
    end

    it "does not accept any other input" do
      expect { TokenList.new(:notcode) }.to raise_error(ArgumentError)
    end

    it "does not interpolate string data" do
      x = TokenList.new('x = "hello #{world}"')
      expect(x.size).to eq 6
      expect(x[4].class).to eq LT::TkDSTRING
      expect(x.to_s).to eq 'x = "hello #{world}"' + "\n"
    end

    it "handles label syntax" do
      x = TokenList.new('a:1,b:2')
      expect(x[0].class).to eq LT::TkLABEL
      expect(x[0].text).to eq 'a:'
      expect(x[3].class).to eq LT::TkLABEL
      expect(x[3].text).to eq 'b:'
    end
  end

  describe "#to_s" do
    before do
      @t = TokenList.new
      @t << LT::TkDEF.new(1, 1, "def")
      @t << LT::TkSPACE.new(1, 1)
      @t << LT::TkIDENTIFIER.new(1, 1, "x")
      @t << LT::TkStatementEnd.new(1, 1)
      @t << LT::TkSEMICOLON.new(1, 1) << LT::TkSPACE.new(1, 1)
      @t << LT::TkBlockContents.new(1, 1)
      @t << LT::TkSPACE.new(1, 1) << LT::TkEND.new(1, 1, "end")
      @t[0].set_text "def"
      @t[1].set_text " "
      @t[2].set_text "x"
      @t[4].set_text ";"
      @t[5].set_text " "
      @t[7].set_text " "
      @t[8].set_text "end"
    end

    it "only shows the statement portion of the tokens by default" do
      expect(@t.to_s).to eq "def x"
    end

    it "shows ... for the block token if all of the tokens are shown" do
      expect(@t.to_s(true)).to eq "def x; ... end"
    end

    it "ignores ... if show_block = false" do
      expect(@t.to_s(true, false)).to eq "def x;  end"
    end
  end
end