File: spec_helper.rb

package info (click to toggle)
ruby-rubyvis 0.6.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,808 kB
  • ctags: 679
  • sloc: ruby: 11,114; makefile: 2
file content (250 lines) | stat: -rw-r--r-- 7,227 bytes parent folder | download | duplicates (3)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
$:.unshift(File.dirname(__FILE__)+"/../lib")
begin
  require 'simplecov'
  SimpleCov.start do
    add_filter "/spec/"
    add_group "Libraries", "lib"
  end
rescue LoadError
end
require 'rspec'
require 'rspec/mocks'
#require 'spec/autorun'
require 'rubyvis'
require 'pp'
require 'nokogiri'


$PROTOVIS_DIR=File.dirname(__FILE__)+"/../vendor/protovis/src"
module Rubyvis
  module GeneralSpec
    def fixture_svg_read(filename)
      File.read(File.dirname(__FILE__)+"/fixtures/#{filename}")
    end
  end
  module LayoutSpec
    include GeneralSpec
    def waves(n, m) 
    Rubyvis.range(n).map {|i| 
      Rubyvis.range(m).map {|j| 
        x = 20 * j / m.to_f - i / 3.0
        x > 0 ? 2 * x * Math.exp(-0.5 * x) : 0
      }
    }
    end

    
    
    
    def net_nodes
      [
      OpenStruct.new({:node_value=>'A', :group=>1}),
      OpenStruct.new({:node_value=>'B', :group=>1}),
      OpenStruct.new({:node_value=>'C', :group=>2}),
      OpenStruct.new({:node_value=>'D',:group=>2}),
      OpenStruct.new({:node_value=>'E',:group=>3}),
      OpenStruct.new({:node_value=>'F',:group=>3})
      
      ]
    end
    def net_links
      [
      OpenStruct.new({:source=>0,:target=>1, :value=>1}),
      OpenStruct.new({:source=>1,:target=>2, :value=>1}),
      OpenStruct.new({:source=>2,:target=>3, :value=>1}),
      OpenStruct.new({:source=>3,:target=>4, :value=>1}),
      OpenStruct.new({:source=>4,:target=>5, :value=>1}),
      OpenStruct.new({:source=>1,:target=>0, :value=>1}),
      OpenStruct.new({:source=>2,:target=>1, :value=>1}),
      OpenStruct.new({:source=>3,:target=>2, :value=>1}),
      OpenStruct.new({:source=>4,:target=>3, :value=>1}),
      OpenStruct.new({:source=>5,:target=>4, :value=>1}),
      ]
    end
    def hier_nodes
      Rubyvis.dom({:a=>1,:b=>{:ba=>2,:bb=>{:bba=>3}, :bc=>4}, :c=>5}).root("test").nodes
    end
    def hier_nodes_big
      subtree={:a=>1,:b=>2,:c=>3,:d=>4}

      Rubyvis.dom({:a=>subtree,:b=>subtree, :c=>subtree, :d=>subtree,:e=>subtree,:f=>subtree}).root("test").nodes()

    end
  end
  class JohnsonLoader
    begin
      require 'johnson'
      def self.available?
        true
      end
    rescue LoadError
      def self.available?
        false
      end
    end
    attr_accessor :runtime
    def initialize(*files)
      files=["/pv.js","/pv-internals.js", "/data/Arrays.js","/data/Numbers.js", "/data/Scale.js", "/data/QuantitativeScale.js", "/data/LinearScale.js","/color/Color.js","/color/Colors.js","/text/Format.js", "/text/DateFormat.js","/text/NumberFormat.js","/text/TimeFormat.js"]+files
      files.uniq!
      files=files.map {|v| $PROTOVIS_DIR+v}
      @runtime = Johnson.load(*files)
    end
  end
end
# Spec matcher 
RSpec::Matchers.define :have_svg_attributes do |exp|
  match do |obs|
    exp.each {|k,v|
      obs.attributes[k].should be_truthy
      obs.attributes[k].value.should==v
    }
  end
  failure_message_for_should do |obs|
    "\n#{exp} attributes expected, but xml doesn't contains them \n#{obs.to_s}"
  end
end

# Include
# * rect
# * circle
# * text
# * path
# Using attributes and content
RSpec::Matchers.define :have_same_svg_elements do |exp|
  def equal_float(a,b)
    a||=0
    b||=0
    (a.to_f-b.to_f).abs<0.0001
  end
  def equal_string_nil(a,b)
    a||="none"
    b||="none"
    a.to_s==b.to_s
  end
  def equal_string(a,b)
    a.to_s==b.to_s
  end
  def path_scan(path)
    path.scan(/([MmCcZzLlHhVvSsQqTtAa, ])(\d+(?:\.\d+)?)/).map {|v|
      v[0]="," if v[0]==" "
      v[1]=v[1].to_f
      v
    }
  end
  
  def equal_path(a,b)
    path_a=path_scan(a)
    path_b=path_scan(b)
    correct=true
    path_a.each_with_index do |v,i|
      if (v[0]!=path_b[i][0]) or (v[1]-path_b[i][1]).abs>0.001
        @path_error=[v[0],path_b[i][0], v[1],path_b[i][1]]
        correct=false
        break
      end
      
    end
    correct
  end
  match do |obs|
    obs_xml=Nokogiri::XML(obs)
    exp_xml=Nokogiri::XML(exp)
    correct=true
    attrs={
      "circle"=>{'fill'=>:string_nil, 'fill-opacity'=>:float, 'cx'=>:float,'stroke'=>:string_nil, 'stroke-opacity'=>:string, 'cy'=>:float,'r'=>:float},
      "rect"=>{"x"=>:float,"y"=>:float,"width"=>:float,"height"=>:float, 'fill'=>:string_nil, 'fill-opacity'=>:float, 'stroke'=>:string_nil, 'stroke-opacity'=>:float},
      "text"=>{"x"=>:float,"dx"=>:float,"y"=>:float,"dy"=>:float},
      'path'=>{'d'=>:path, 'fill'=>:string_nil, 'fill-opacity'=>:float, 'stroke'=>:string_nil, 'stroke-opacity'=>:float, 'stroke-width'=>:float}
    }
    
    @error={:type=>"Undefined error"}
    attrs.each_pair do |key,attrs|
      exp_elements=exp_xml.xpath("//#{key}")
      obs_elements=obs_xml.xpath("//xmlns:#{key}")
      if exp_elements.size!=obs_elements.size
        @error={:type=>"Different number of #{key} elements",:exp=>exp_elements.size, :obs=>obs_elements.size}
        correct=false
        break
      end
      
      exp_elements.each_with_index {|exp_data,i|
        obs_data=obs_elements[i]  
        if obs_data.nil?
          @error={:type=>"Missing obs", :exp=>exp_data, :i=>i}
          correct=false
          break
        end
        if exp_data.content!=obs_data.content
          @error={:type=>"Content", :exp=>exp_data, :i=>i, :obs=>obs_data, :exp_attr=>exp_data.content, :obs_attr=>obs_data.content}
          correct=false
          break;
        end
        
        attrs.each do |attr,method|
          eq=send("equal_#{method}", obs_data[attr], exp_data[attr])
          if !eq
            ty=(method=='path' ? "!= path:#{@path_error.join(',')}" : "!=attr(#{method})" )
            #puts "Uneql attr: #{method}->#{attr}"
            @error={:type=>"!= attr", :exp=>exp_data, :obs=>obs_data, :attr=>attr, :exp_attr=>exp_data[attr], :obs_attr=>obs_data[attr],:i=>i}
            correct=false
            break
          end
        end
        if !correct
          break
        end
      }
    end
    correct
  end
  failure_message_for_should do |obs|
    "#{@error[:type]}: #{@error[:exp].to_s} expected, but #{@error[:obs]} retrieved, on #{@error[:attr]} -> #{@error[:i]} : '#{@error[:exp_attr]}' <> '#{@error[:obs_attr]}'"
  end
  
end
RSpec::Matchers.define :have_same_position do |exp|
  match do |obs|
    correct=true
    attrs={
      "circle"=>['cx','cy','r'],
      "rect"=>["x","y","width","height"],
      "text"=>["x","dx","y","dy","transform"]
    }
    attrs[exp.name].each do |attr|
      if (obs[attr].to_f -  exp[attr].to_f).abs>0.0001
        correct=false
        break
      end
    end
    correct
  end
end


RSpec::Matchers.define :have_path_data_close_to do |exp|
  def path_scan(path)
      path.scan(/([MmCcZzLlHhVvSsQqTtAa, ])(\d+(?:\.\d+)?)/).map {|v|
      v[0]="," if v[0]==" "
      v[1]=v[1].to_f
      v
      }
  end
  match do |obs|
    correct=true
    obs_array=path_scan(obs.attributes["d"].value)
    
    exp_array=path_scan(exp)
    obs_array.each_with_index {|v,i|
      if (v[0]!=exp_array[i][0]) or (v[1]-exp_array[i][1]).abs>0.001
        correct=false
        break
      end
    }
    correct
  end
  failure_message_for_should do |obs|
    obs_array=path_scan(obs.attributes["d"].value)
    exp_array=path_scan(exp)
    "\n#{obs_array} path should be equal to \n#{exp_array}"
  end
end