|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'test_helper' |
| 4 | +require 'rubycritic/core/smell' |
| 5 | +require 'rubycritic/core/analysed_module' |
| 6 | + |
| 7 | +describe 'TypeCheck' do |
| 8 | + describe RubyCritic::Smell do |
| 9 | + it 'allows a value of the declared type' do |
| 10 | + smell = RubyCritic::Smell.new |
| 11 | + smell.cost = 42 |
| 12 | + |
| 13 | + _(smell.cost).must_equal 42 |
| 14 | + end |
| 15 | + |
| 16 | + it 'allows a subclass of the declared type (Integer for Numeric)' do |
| 17 | + smell = RubyCritic::Smell.new |
| 18 | + smell.score = 3 |
| 19 | + |
| 20 | + _(smell.score).must_equal 3 |
| 21 | + end |
| 22 | + |
| 23 | + it 'allows nil for any attribute' do |
| 24 | + smell = RubyCritic::Smell.new |
| 25 | + smell.message = nil |
| 26 | + |
| 27 | + _(smell.message).must_be_nil |
| 28 | + end |
| 29 | + |
| 30 | + it 'accepts either member of a union type (String or Symbol for type)' do |
| 31 | + smell = RubyCritic::Smell.new |
| 32 | + smell.type = 'DuplicateCode' |
| 33 | + |
| 34 | + _(smell.type).must_equal 'DuplicateCode' |
| 35 | + smell.type = :complexity |
| 36 | + |
| 37 | + _(smell.type).must_equal :complexity |
| 38 | + end |
| 39 | + |
| 40 | + it 'raises a TypeError when assigning the wrong type' do |
| 41 | + smell = RubyCritic::Smell.new |
| 42 | + error = _ { smell.cost = 'expensive' }.must_raise TypeError |
| 43 | + _(error.message).must_match(/Smell#cost= expected Numeric or nil, got String/) |
| 44 | + end |
| 45 | + |
| 46 | + it 'raises when assigning the wrong type to a union attribute' do |
| 47 | + smell = RubyCritic::Smell.new |
| 48 | + error = _ { smell.type = 123 }.must_raise TypeError |
| 49 | + _(error.message).must_match(/Smell#type= expected String \| Symbol or nil, got Integer/) |
| 50 | + end |
| 51 | + |
| 52 | + it 'enforces the type through the initializer' do |
| 53 | + _ { RubyCritic::Smell.new(locations: 'not-an-array') }.must_raise TypeError |
| 54 | + end |
| 55 | + |
| 56 | + it 'constructs with realistic arguments without raising' do |
| 57 | + smell = RubyCritic::Smell.new( |
| 58 | + context: '#bar', |
| 59 | + cost: 16, |
| 60 | + locations: [RubyCritic::Location.new('./foo', '42')], |
| 61 | + message: 'This smells', |
| 62 | + score: 0, |
| 63 | + type: 'DuplicateMethodCall', |
| 64 | + analyser: 'flog' |
| 65 | + ) |
| 66 | + |
| 67 | + _(smell.analyser).must_equal 'flog' |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + describe RubyCritic::AnalysedModule do |
| 72 | + it 'allows an Integer for a Numeric attribute (coverage)' do |
| 73 | + mod = RubyCritic::AnalysedModule.new |
| 74 | + mod.coverage = 0 |
| 75 | + |
| 76 | + _(mod.coverage).must_equal 0 |
| 77 | + end |
| 78 | + |
| 79 | + it 'allows a Float for a Numeric attribute (coverage)' do |
| 80 | + mod = RubyCritic::AnalysedModule.new |
| 81 | + mod.coverage = 87.5 |
| 82 | + |
| 83 | + _(mod.coverage).must_equal 87.5 |
| 84 | + end |
| 85 | + |
| 86 | + it 'preserves the Float::INFINITY default' do |
| 87 | + _(RubyCritic::AnalysedModule.new.complexity).must_equal Float::INFINITY |
| 88 | + end |
| 89 | + |
| 90 | + it 'accumulates duplication through the writer (+=)' do |
| 91 | + mod = RubyCritic::AnalysedModule.new |
| 92 | + mod.duplication += 18 |
| 93 | + |
| 94 | + _(mod.duplication).must_equal 18 |
| 95 | + end |
| 96 | + |
| 97 | + it 'raises a TypeError when assigning the wrong type to pathname' do |
| 98 | + mod = RubyCritic::AnalysedModule.new |
| 99 | + error = _ { mod.pathname = 'foo.rb' }.must_raise TypeError |
| 100 | + _(error.message).must_match(/AnalysedModule#pathname= expected Pathname \| FakeFS::Pathname or nil, got String/) |
| 101 | + end |
| 102 | + |
| 103 | + it 'raises when assigning a non-Array to smells' do |
| 104 | + _ { RubyCritic::AnalysedModule.new.smells = 5 }.must_raise TypeError |
| 105 | + end |
| 106 | + |
| 107 | + it 'constructs with realistic arguments without raising' do |
| 108 | + mod = RubyCritic::AnalysedModule.new( |
| 109 | + name: 'Foo', |
| 110 | + pathname: Pathname.new('foo.rb'), |
| 111 | + smells: [], |
| 112 | + churn: 3, |
| 113 | + complexity: 12.5, |
| 114 | + methods_count: 4 |
| 115 | + ) |
| 116 | + |
| 117 | + _(mod.name).must_equal 'Foo' |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + describe 'the TypeCheck error type' do |
| 122 | + it 'is a kind of the standard TypeError' do |
| 123 | + _(TypeCheckHelper::TypeError.ancestors).must_include TypeError |
| 124 | + end |
| 125 | + end |
| 126 | +end |
0 commit comments