Class: Plist4r::Backend::Test::Harness

Defined in:
lib/plist4r/backend/test/harness.rb

Instance Methods

Methods inherited from Plist4r::Backend

#call, #generic_call

Constant Summary

ReferenceBackends =
{
  :from_xml => :libxml4r,
  :to_xml   => :haml,
  :from_binary => :c_f_property_list,
  :to_binary   => :c_f_property_list,
  # :from_binary => :ruby_cocoa,
  # :to_binary   => :ruby_cocoa,
  # :from_binary => :osx_plist,
  # :to_binary   => :osx_plist,
}

Constants inherited from Plist4r::Backend

ApiMethods, PlistCacheApiMethods, PrivateApiMethods

Constructor Details

- (Harness) initialize(*args, &blk)

A new instance of Harness



13
14
15
16
17
18
19
20
21
# File 'lib/plist4r/backend/test/harness.rb', line 13

def initialize *args, &blk

  @backends = Dir.glob(File.dirname(__FILE__) + "/../../backend/*.rb").collect {|b| File.basename(b,".rb") }
  @backends.flatten!
  @backends.uniq!
  @backends = @backends - ["example"]

  @data_types = DataTypes.new
end

Instance Method Details

- (Object) reference_backend_for(meth)



34
35
36
# File 'lib/plist4r/backend/test/harness.rb', line 34

def reference_backend_for meth
  ReferenceBackends[meth]
end

- (Object) results



38
39
40
# File 'lib/plist4r/backend/test/harness.rb', line 38

def results
  @results
end

- (Object) run_tests



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
# File 'lib/plist4r/backend/test/harness.rb', line 42

def run_tests

  ref_strings = {}
  ref_string_1024 = {}

  cols = [:from, :to].size + [:xml, :binary].size
  rows = @backends.size

  t = Haml4r::Table.new :size => [0..cols-1, 0..rows-1]

  t.col_header.size [0..cols-1, 0]
  t.row_header.size [0, 0..rows-1]

  # t.css_style = "border-spacing: 5px;"
  c2 = "#F5F5FF"
  c1 = "#FAFAFA"
  cpv = 5
  cph = 15
  t.cells.css_style            = "padding: #{cpv}px; padding-left: #{cph}px; padding-right: #{cph}px; background-color: #{c1}; text-align: center"
  t.col_header.cells.css_style = "padding: #{cpv}px; padding-left: #{cph}px; padding-right: #{cph}px; background-color: #{c2}; text-align: center"
  t.row_header.cells.css_style = "padding: #{cpv}px; padding-left: #{cph}px; padding-right: #{cph}px; background-color: #{c2}; text-align: right"

  bhi = 0
  @backends.each do |b_sym|
    t.row_header.cell(0, bhi).content = "#{b_sym}"
    bhi += 1
  end

  chi = 0
  [:xml, :binary].each do |fmt|
    [:from, :to].each do |op|
      t.col_header.cell(chi, 0).content = ":#{op}_#{fmt}"
      chi += 1
    end
  end

  cgi = 0
  [:xml, :binary].each do |fmt|
    puts ""
    puts fmt.inspect
    puts ""
    # setup reference test data
    api_sym = "to_#{fmt}".to_sym
    ref_strings[fmt] = Plist4r::OrderedHash.new
    @data_types.plists.each_pair do |key,plist|
      plist.backends [reference_backend_for(api_sym)]
      ref_strings[fmt][key] = plist.send(api_sym)
    end

    @data_types.plist_1024.backends [reference_backend_for(api_sym)]
    ref_string_1024[fmt] = @data_types.plist_1024.send(api_sym)


    ri = 0
    @backends.each do |b_sym|
      ci = cgi

      backend = eval "::Plist4r::Backend::#{b_sym.to_s.camelcase}"
      if backend.respond_to? "from_#{fmt}"
        failures = []
        ref_strings[fmt].each_pair do |sym,string|
          begin
            plist_result = Plist4r.new :backends => [b_sym], :from_string => string                
            if @data_types.plists[sym].to_hash == plist_result.to_hash
              puts "match, #{b_sym}, #{sym}"
              # puts @data_types.plists[sym].hash.inspect
              # puts @data_types.plists[sym].to_hash["DataKeyW"].hash.inspect
              # puts plist_result.to_hash["DataKeyW"].hash.inspect
            else
              puts "fail, #{b_sym}, #{sym}"
              failures << sym

              if true
                puts "expected:"
                puts @data_types.plists[sym].hash.inspect
                puts @data_types.plists[sym].to_hash.inspect
                puts "got:"
                puts plist_result.to_hash.inspect
                # puts plist_result.to_xml if b_sym == "ruby_cocoa"
              end
            end
          rescue
            puts "fail, #{b_sym}, #{sym} - exception"
            failures << sym
          end
        end
        if failures.empty?
          time = Benchmark.measure { plist_result = Plist4r.new :backends => [b_sym], :from_string => ref_string_1024[fmt] }.real
          tms = (time*1000).round(1).to_s
          # puts "  :from_#{fmt} - time for 1024 keys = " + (time*1000).round(1).to_s + " ms"
          t.cell(ci, ri).content = "#{tms} ms"
        else
          t.cell(ci, ri).content = "Failed #{failures.inspect}"
        end
        
      else
        # puts "  not implemented, :from_#{fmt}"
        t.cell(ci, ri).content = "n/a"
      end
      ci += 1


      if backend.respond_to? "to_#{fmt}"
        failures = []
        @data_types.plists.each_pair do |sym,ref_plist|
            begin
              plist = Plist4r.new :backends => [b_sym]
              plist.instance_eval "@hash = ref_plist.to_hash"
              
              # puts "was: to_#{fmt}, #{b_sym} - #{sym.inspect}" if [:data, :bool].include?(sym)
              # puts plist.to_binary.inspect if [:data, :bool].include?(sym)
              # puts "should be:"
              # 
              # puts "sepr char: #{plist.to_binary[20].chr.inspect}, last int #{plist.to_binary[20].inspect}," if sym == :data
              # puts "last char: #{plist.to_binary[plist.to_binary.length-1].chr.inspect}, last int #{plist.to_binary[plist.to_binary.length-1].inspect}," if sym == :data

              plist_gen_from = Plist4r.new :from_string => plist.send("to_#{fmt}"),
                                :backends => [reference_backend_for("from_#{fmt}".to_sym)]

              if plist_gen_from.to_hash == plist.to_hash
                  puts "match, #{b_sym}, #{sym}"
                  # puts plist.hash.inspect
                  # puts plist_gen_from.hash.inspect
                  # if sym == :data
                    if b_sym == "ruby_cocoa"
                      # plist_gen_from.to_hash.keys.each do |key|
                      # puts "point a"
                      # plist.to_hash.keys.each do |key|
                      #   puts "key hash mismatch: #{key.inspect}" unless plist.to_hash[key].hash == plist_gen_from.to_hash[key].hash
                      # end
                      # puts "key hash mismatch: keys differ" unless plist.to_hash.keys == plist_gen_from.to_hash.keys
                      # puts "point b"
                      # puts plist.to_hash["DataKeyW"].hash.inspect
                      # puts plist_gen_from.to_hash["DataKeyW"].hash.inspect


                      # puts "expected:"
                      # puts plist.to_hash.inspect
                      # puts "got:"
                      # puts plist.send("to_#{fmt}")
                      # puts plist_gen_from.to_hash.inspect
                    end
                  # end
              else
                puts "fail, #{b_sym}, #{sym}"
                failures << sym
                if true
                  puts "expected:"
                  puts plist.to_hash.inspect
                  puts "got:"
                  puts plist_gen_from.to_hash.inspect
                  puts "got:"
                  puts plist.send("to_#{fmt}")
                end
              end
            rescue
              puts "fail, #{b_sym}, #{sym} - exception"
              # puts $!.inspect if b_sym == :ruby_cocoa
              raise
              begin
                # what went wrong?
                if fmt == :binary
                  puts "to_#{fmt}, #{b_sym} - for #{sym.inspect}. (1)was, (2)should be:"
                  plist = Plist4r.new :backends => [b_sym]
                  plist.instance_eval "@hash = ref_plist.to_hash"
                  puts plist.to_binary.inspect
                
                  plist = Plist4r.new :backends => [ReferenceBackends["to_#{fmt}".to_sym]]
                  plist.instance_eval "@hash = ref_plist.to_hash"
                  puts plist.to_binary.inspect
                end
              rescue
              end
              # raise if b_sym == :ruby_cocoa
              failures << sym
            end
          end
          if failures.empty?
            plist = Plist4r.new :backends => [b_sym]
            plist_1024 = @data_types.plist_1024
            plist.instance_eval "@hash = plist_1024.to_hash"
            time = Benchmark.measure { eval "plist.to_#{fmt}" }.real
            tms = (time*1000).round(1).to_s
            # puts "  :to_#{fmt}   - time for 1024 keys = " + (time*1000).round(1).to_s + " ms"
            t.cell(ci, ri).content = "#{tms} ms"
          else
            t.cell(ci, ri).content = "Failed #{failures.inspect}"
          end
      else
        # puts "  not implemented, :to_#{fmt}"
        t.cell(ci, ri).content = "n/a"
      end

      # puts ""
      ri += 1
    end

    cgi += 2
  end
  puts ""
  @results = t
end