summaryrefslogtreecommitdiffstats
path: root/spec/lib/spec/dsl/description.rb
blob: fe8c9b0c98d219dc93a04886bf8fc5e43d8bdd63 (plain)
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
module Spec
  module DSL
    class Description
      module ClassMethods
        def generate_description(*args)
          description = args.shift.to_s
          unless args.empty?
            suffix = args.shift.to_s
            description << " " unless suffix =~ /^\s|\.|#/
            description << suffix
          end
          description
        end
      end
      extend ClassMethods

      attr_reader :description, :described_type
      
      def initialize(*args)
        args, @options = args_and_options(*args)
        init_behaviour_type(@options)
        init_spec_path(@options)
        init_described_type(args)
        init_description(*args)
      end
  
      def [](key)
        @options[key]
      end
      
      def []=(key, value)
        @options[key] = value
      end
      
      def to_s; @description; end
      
      def ==(value)
        case value
        when Description
          @description == value.description
        else
          @description == value
        end
      end
      
    private
      def init_behaviour_type(options)
        # NOTE - BE CAREFUL IF CHANGING THIS NEXT LINE:
        #   this line is as it is to satisfy JRuby - the original version
        #   read, simply: "if options[:behaviour_class]", which passed against ruby, but failed against jruby
        if options[:behaviour_class] && options[:behaviour_class].ancestors.include?(Behaviour)
          options[:behaviour_type] = parse_behaviour_type(@options[:behaviour_class])
        end
      end
      
      def init_spec_path(options)
        if options.has_key?(:spec_path)
          options[:spec_path] = File.expand_path(@options[:spec_path])
        end
      end
      
      def init_description(*args)
        @description = self.class.generate_description(*args)
      end
      
      def init_described_type(args)
        @described_type = args.first unless args.first.is_a?(String)
      end
    
      def parse_behaviour_type(behaviour_class)
        behaviour_class.to_s.split("::").reverse[0].gsub!('Behaviour', '').downcase.to_sym
      end

    end
  end
end