File: router_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 (123 lines) | stat: -rw-r--r-- 4,065 bytes parent folder | download | duplicates (6)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# frozen_string_literal: true
require File.dirname(__FILE__) + '/spec_helper'

class MyRouterSpecRouter < Router
  def docs_prefix; 'mydocs/foo' end
  def list_prefix; 'mylist/foo' end
  def search_prefix; 'mysearch/foo' end

  def check_static_cache; nil end
end

RSpec.describe YARD::Server::Router do
  before do
    @adapter = mock_adapter
    @projects = @adapter.libraries['project']
    @request = mock_request
  end

  describe "#parse_library_from_path" do
    def parse(*args)
      @request.path_info = '/' + args.join('/')
      @router = MyRouterSpecRouter.new(@adapter)
      @router.request = @request
      @router.parse_library_from_path(args.flatten)
    end

    it "parses library and version name out of path" do
      expect(parse('project', '1.0.0')).to eq [@projects[0], []]
      expect(@request.version_supplied).to be true
    end

    it "parses library and use latest version if version is not supplied" do
      expect(parse('project')).to eq [@projects[1], []]
      expect(@request.version_supplied).to be false
    end

    it "parses library and use latest version if next component is not a version" do
      expect(parse('project', 'notaversion')).to eq [@projects[1], ['notaversion']]
      expect(@request.version_supplied).to be false
    end

    it "returns nil library if no library is found" do
      expect(parse('notproject')).to eq [nil, ['notproject']]
    end

    it "does not parse library or version if single_library == true" do
      allow(@adapter).to receive(:options).and_return(:single_library => true)
      expect(parse('notproject')).to eq [@projects[0], ['notproject']]
    end
  end

  describe "#route" do
    def route_to(route, command, script_name = '')
      req = mock_request(route, script_name)
      router = MyRouterSpecRouter.new(@adapter)
      expect(command).to receive(:new) do |*args|
        @command = command.allocate
        @command.send(:initialize, *args)
        class << @command; define_method(:call) {|*| self } end
        @command
      end
      router.call(req)
    end

    it "routes /docs/OBJECT to object if single_library = true" do
      allow(@adapter).to receive(:options).and_return(:single_library => true)
      route_to('/mydocs/foo/FOO', DisplayObjectCommand)
    end

    it "routes /docs" do
      route_to('/mydocs/foo', LibraryIndexCommand)
    end

    it "routes /docs as index for library if single_library == true" do
      allow(@adapter).to receive(:options).and_return(:single_library => true)
      route_to('/mydocs/foo/', DisplayObjectCommand)
    end

    it "routes /docs/name/version" do
      route_to('/mydocs/foo/project/1.0.0', DisplayObjectCommand)
      expect(@command.library).to eq @projects[0]
    end

    it "routes /docs/name/ to latest version of library" do
      route_to('/mydocs/foo/project', DisplayObjectCommand)
      expect(@command.library).to eq @projects[1]
    end

    it "routes /list/name/version/class" do
      route_to('/mylist/foo/project/1.0.0/class', ListCommand)
      expect(@command.library).to eq @projects[0]
    end

    it "routes /list/name/version/methods" do
      route_to('/mylist/foo/project/1.0.0/methods', ListCommand)
      expect(@command.library).to eq @projects[0]
    end

    it "routes /list/name/version/files" do
      route_to('/mylist/foo/project/1.0.0/files', ListCommand)
      expect(@command.library).to eq @projects[0]
    end

    it "routes /list/name to latest version of library" do
      route_to('/mylist/foo/project/class', ListCommand)
      expect(@command.library).to eq @projects[1]
    end

    it "routes /search/name/version" do
      route_to('/mysearch/foo/project/1.0.0', SearchCommand)
      expect(@command.library).to eq @projects[0]
    end

    it "routes /search/name to latest version of library" do
      route_to('/mysearch/foo/project', SearchCommand)
      expect(@command.library).to eq @projects[1]
    end

    it "searches static files for non-existent library" do
      route_to('/mydocs/foo/notproject', RootRequestCommand)
    end
  end
end