File: class_condition_handler_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 (87 lines) | stat: -rw-r--r-- 2,453 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
80
81
82
83
84
85
86
87
# frozen_string_literal: true
require File.dirname(__FILE__) + '/spec_helper'

RSpec.describe "YARD::Handlers::Ruby::#{LEGACY_PARSER ? "Legacy::" : ""}ClassConditionHandler" do
  before(:all) { parse_file :class_condition_handler_001, __FILE__ }

  def verify_method(*names)
    names.each {|name| expect(Registry.at("A##{name}")).not_to be nil }
    names.each {|name| expect(Registry.at("A##{name}not")).to be nil }
  end

  def no_undoc_error(code)
    expect { StubbedSourceParser.parse_string(code) }.not_to raise_error
  end

  it "parses all unless blocks for complex conditions" do
    verify_method :g
  end

  it "does not parse conditionals inside methods" do
    verify_method :h
  end

  it "only parses then block if condition is literal value `true`" do
    verify_method :p
  end

  it "only parses then block if condition is literal integer != 0" do
    verify_method :o
  end

  it "inverts block to parse for literal condition if it's an unless block" do
    verify_method :e
  end

  it "handles conditions such as 'defined? VALUE'" do
    verify_method :j, :k
  end

  it "parses all if/elsif blocks for complex conditions" do
    verify_method :a, :b, :c, :d
  end

  it "parses else block if condition is literal value `false`" do
    verify_method :q
  end

  it "only parses else block if condition is literal integer == 0" do
    verify_method :n
  end

  it "maintains visibility and scope state inside condition" do
    expect(Registry.at('A#m').visibility).to eq :private
    expect(Registry.at('A#mnot').visibility).to eq :private
  end

  it "does not fail on complex conditions" do
    expect(log).not_to receive(:warn)
    expect(log).not_to receive(:error)
    no_undoc_error "if defined?(A) && defined?(B); puts 'hi' end"
    no_undoc_error(<<-eof)
      (<<-TEST) unless defined?(ABCD_MODEL_TEST)
        'String'
      TEST
    eof
    no_undoc_error "if caller.none? { |l| l =~ %r{lib/rails/generators\\.rb:(\\d+):in `lookup!'$} }; end"
  end

  it "only parses identifiers or namespaces from defined? expressions" do
    module A
      @@value = nil
      def self.b(value) @@value = value end
      def self.value; @@value end
    end

    YARD.parse_string <<-eof
      if defined? A.b(42).to_i
        class Foo; end
      else
        class Bar; end
      end
    eof
    expect(A.value).to be_nil
    expect(YARD::Registry.at('Foo')).not_to be_nil
    expect(YARD::Registry.at('Bar')).not_to be_nil
  end
end