File: ant_spec_helper.rb

package info (click to toggle)
jruby 1.5.1-1%2Bdeb6u1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze-lts
  • size: 47,024 kB
  • ctags: 74,144
  • sloc: ruby: 398,155; java: 169,506; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (117 lines) | stat: -rw-r--r-- 3,745 bytes parent folder | download | duplicates (4)
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
require File.expand_path('../spec_helper', __FILE__)
require 'ant'
require 'tmpdir'

class Ant
  module Spec
    class AntExampleGroup < ::Spec::Example::ExampleGroup
      after :each do
        Ant.send(:instance_variable_set, "@ant", nil)
      end

      # Allows matching task structure with a nested hash as follows.
      #
      # { :_name => "jar", :destfile => "spec-test.jar", :compress => "true", :index => "true",
      #   :_children => [
      #     { :_name => "fileset", :dir => "build" }
      #   ]}
      #
      # would match the following:
      #
      # jar :destfile => "spec-test.jar", :compress => "true", :index => "true" do
      #   fileset :dir => "build"
      # end
      #
      class TaskStructureMatcher
        def initialize(hash, configure = false)
          @expected = hash
          @configure = configure
        end

        def description
          "have the specified #{(@configure ? 'configured' : 'element')} structure"
        end

        def matches?(actual)
          @actual = actual
          result = true
          tasks = actual.defined_tasks
          if Hash === @expected && tasks.length != 1 || tasks.length != @expected.length
            @message = "task list length different"
            return false
          end
          @expected.each_with_index do |h,i|
            tasks[i].maybe_configure if @configure
            result &&= match_wrapper(h, tasks[i].wrapper)
          end
          result
        end

        def match_wrapper(hash, wrapper, name = nil)
          hname = hash[:_name] ? hash[:_name] : '<?>'
          if name
            name = "#{name} => #{hname}"
          else
            name = hname
          end

          # name
          if hash[:_name] && hash[:_name] != wrapper.element_tag
            @message = "name different: expected #{hash[:_name].inspect} but was #{wrapper.element_tag.inspect}"
            return false
          end

          # proxy class name
          if hash[:_type] && (wrapper.proxy.nil? || hash[:_type] != wrapper.proxy.java_class.name)
            @message = "type different: expected #{hash[:_type]} but was #{wrapper.proxy.java_class.name}"
            return false
          end

          # attributes
          hash.keys.select {|k| k.to_s !~ /^_/}.each do |k|
            unless wrapper.attribute_map.containsKey(k.to_s)
              @message = "'#{name} => #{k}' attribute missing"
              return false
            end

            if hash[k] != wrapper.attribute_map[k.to_s]
              @message = "'#{name} => #{k}' attribute different: expected #{hash[k].inspect} but was #{wrapper.attribute_map[k.to_s].inspect}"
              return false
            end
          end

          # children, recursively
          children = wrapper.children.to_a
          (hash[:_children] || []).each_with_index do |h,i|
            return false unless match_wrapper(h, children[i], name)
          end
          true
        end

        def failure_message_for_should
          require 'pp'
          "expected #{@actual.name} to have structure:\n#{@expected.pretty_inspect}\n#{@message}"
        end

        def failure_message_for_should_not
          require 'pp'
          "expected #{@actual.name} to not have structure:\n#{@expected.pretty_inspect}\n#{@message}"
        end
      end

      def have_structure(hash)
        TaskStructureMatcher.new(hash)
      end

      def have_configured_structure(hash)
        TaskStructureMatcher.new(hash, true)
      end

      def example_ant(options = {}, &block)
        Ant.new({:basedir => Dir::tmpdir, :run => false, :output_level => 0}.merge(options),&block)
      end

      ::Spec::Example::ExampleGroupFactory.register(:ant, self)
    end
  end
end