excel_to_code 0.0.11 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -42,6 +42,14 @@ END
42
42
  opts.on('-m','--run-in-memory',"Instead of writing intermediate files to disk, uses memory. Requires a lot of memory") do
43
43
  command.run_in_memory = true
44
44
  end
45
+
46
+ opts.on('--sloppy-tests',"The generated tests treat blanks and zeros as equivalent and only require numbers to be approximately the same. This is the default.") do
47
+ command.sloppy_tests = true
48
+ end
49
+
50
+ opts.on('--precise-tests',"The generated tests treat blanks and zeros as different and requires numbers to be exactly the same.") do
51
+ command.sloppy_tests = false
52
+ end
45
53
 
46
54
  opts.on("-h", "--help", "Show this message") do
47
55
  puts opts
@@ -64,4 +72,4 @@ end
64
72
  command.excel_file = ARGV[0]
65
73
  command.output_directory = ARGV[1] if ARGV[1]
66
74
 
67
- command.go!
75
+ command.go!
@@ -35,9 +35,17 @@ END
35
35
  command.actually_run_tests = true
36
36
  end
37
37
 
38
- opts.on('-m','--run-in-memory',"Instead of writing intermediate files to disk, uses memory. Requires a lot of memory") do
38
+ opts.on('-m','--run-in-memory',"Instead of writing intermediate files to disk, uses memory. Requires a lot of memory.") do
39
39
  command.run_in_memory = true
40
40
  end
41
+
42
+ opts.on('--sloppy-tests',"The generated tests treat blanks and zeros as equivalent and only require numbers to be approximately the same. This is the default.") do
43
+ command.sloppy_tests = true
44
+ end
45
+
46
+ opts.on('--precise-tests',"The generated tests treat blanks and zeros as different and requires numbers to be exactly the same.") do
47
+ command.sloppy_tests = false
48
+ end
41
49
 
42
50
  opts.on("-h", "--help", "Show this message") do
43
51
  puts opts
@@ -242,7 +242,7 @@ END
242
242
  end
243
243
  if refs_to_test && !refs_to_test.empty?
244
244
  refs_to_test = refs_to_test.map(&:upcase)
245
- CompileToCUnitTest.rewrite(i, c_name, refs_to_test, o)
245
+ CompileToCUnitTest.rewrite(i, sloppy_tests, c_name, refs_to_test, o)
246
246
  end
247
247
  close(i)
248
248
  end
@@ -104,7 +104,7 @@ class ExcelToRuby < ExcelToX
104
104
  end
105
105
  if refs_to_test && !refs_to_test.empty?
106
106
  refs_to_test = refs_to_test.map(&:upcase)
107
- c.rewrite(i, c_name, refs_to_test, o)
107
+ c.rewrite(i, sloppy_tests, c_name, refs_to_test, o)
108
108
  end
109
109
  o.puts " # End of #{name}"
110
110
  o.puts ""
@@ -124,4 +124,4 @@ class ExcelToRuby < ExcelToX
124
124
  puts `cd #{File.join(output_directory)}; ruby "test_#{output_name.downcase}.rb"`
125
125
  end
126
126
 
127
- end
127
+ end
@@ -63,6 +63,11 @@ class ExcelToX
63
63
 
64
64
  # This is the log file, if set it needs to respond to the same methods as the standard logger library
65
65
  attr_accessor :log
66
+
67
+ # Optional attribute. Boolean. Default true.
68
+ # * true - empty cells and zeros are treated as being equivalent in tests. Numbers greater then 1 are only expected to match with assert_in_epsilon, numbers less than 1 are only expected to match with assert_in_delta
69
+ # * false - empty cells and zeros are treated as being different in tests. Numbers must match to full accuracy.
70
+ attr_accessor :sloppy_tests
66
71
 
67
72
  def set_defaults
68
73
  raise ExcelToCodeException.new("No excel file has been specified") unless excel_file
@@ -95,6 +100,9 @@ class ExcelToX
95
100
 
96
101
  # Set up our log file
97
102
  self.log ||= Logger.new(STDOUT)
103
+
104
+ # By default, tests allow empty cells and zeros to be treated as equivalent, and numbers only have to match to a 0.001 epsilon (if expected>1) or 0.001 delta (if expected<1)
105
+ self.sloppy_tests ||= true
98
106
  end
99
107
 
100
108
  def go!
@@ -1,10 +1,18 @@
1
1
  class CompileToCUnitTest
2
+
3
+ attr_accessor :epsilon
4
+ attr_accessor :delta
5
+
6
+ def initialize
7
+ @epsilon = 0.001
8
+ @delta = 0.001
9
+ end
2
10
 
3
11
  def self.rewrite(*args)
4
12
  self.new.rewrite(*args)
5
13
  end
6
14
 
7
- def rewrite(input,c_name, refs_to_test, output)
15
+ def rewrite(input, sloppy, c_name, refs_to_test, output)
8
16
  input.lines do |line|
9
17
  begin
10
18
  ref, formula = line.split("\t")
@@ -14,8 +22,22 @@ class CompileToCUnitTest
14
22
  ast = eval(formula)
15
23
  case ast.first
16
24
  when :number, :percentage
17
- output.puts " assert_equal(:ExcelNumber,r[:type])"
18
- output.puts " assert_in_epsilon(#{ast.last.to_f.to_s},r[:number])"
25
+ unless sloppy
26
+ output.puts " assert_equal(:ExcelNumber,r[:type])"
27
+ output.puts " assert_equal(#{ast.last.to_f.to_s},r[:number])"
28
+ else
29
+ if ast.last.to_f == 0
30
+ output.puts " pass if r[:type] == :ExcelEmpty"
31
+ end
32
+
33
+ output.puts " assert_equal(:ExcelNumber,r[:type])"
34
+
35
+ if ast.last.to_f <= 1
36
+ output.puts " assert_in_delta(#{ast.last.to_f.to_s},r[:number],#{@delta})"
37
+ else
38
+ output.puts " assert_in_epsilon(#{ast.last.to_f.to_s},r[:number],#{@epsilon})"
39
+ end
40
+ end
19
41
  when :error
20
42
  output.puts " assert_equal(:ExcelError,r[:type])"
21
43
  when :string
@@ -27,8 +49,16 @@ class CompileToCUnitTest
27
49
  when :boolean_false
28
50
  output.puts " assert_equal(:ExcelBoolean,r[:type])"
29
51
  output.puts " assert_equal(0,r[:number])"
52
+ when :blank
53
+ unless sloppy
54
+ output.puts " assert_equal(:ExcelEmpty,r[:type])"
55
+ else
56
+ output.puts " pass if r[:type] == :ExcelEmpty"
57
+ output.puts " assert_equal(:ExcelNumber,r[:type])"
58
+ output.puts " assert_in_delta(0.0,r[:number],#{@delta})"
59
+ end
30
60
  else
31
- raise NotSupportedException.new("#{ast} type can't be settable")
61
+ raise NotSupportedException.new("#{ast} type can't be tested")
32
62
  end
33
63
  output.puts "end"
34
64
  output.puts
@@ -2,11 +2,20 @@ require_relative "map_values_to_ruby"
2
2
 
3
3
  class CompileToRubyUnitTest
4
4
 
5
+
6
+ attr_accessor :epsilon
7
+ attr_accessor :delta
8
+
9
+ def initialize
10
+ @epsilon = 0.001
11
+ @delta = 0.001
12
+ end
13
+
5
14
  def self.rewrite(*args)
6
15
  self.new.rewrite(*args)
7
16
  end
8
17
 
9
- def rewrite(input, c_name, refs_to_test, output)
18
+ def rewrite(input, sloppy, c_name, refs_to_test, o)
10
19
  mapper = MapValuesToRuby.new
11
20
  input.lines do |line|
12
21
  ref, formula = line.split("\t")
@@ -14,16 +23,32 @@ class CompileToRubyUnitTest
14
23
  ast = eval(formula)
15
24
  value = mapper.map(ast)
16
25
  full_reference = "worksheet.#{c_name}_#{ref.downcase}"
17
- if ast.first == :number
18
- if value == "0" # Need to do a slightly different test, because needs to pass if nil returned, as well as zero
19
- output.puts " def test_#{c_name}_#{ref.downcase}; assert_in_epsilon(#{value},#{full_reference} || 0); end"
26
+ test_name = "test_#{c_name}_#{ref.downcase}"
27
+ case ast.first
28
+ when :blank
29
+ if sloppy
30
+ o.puts " def #{test_name}; assert_includes([nil, 0], #{full_reference}); end"
31
+ else
32
+ o.puts " def #{test_name}; assert_equal(#{value}, #{full_reference}); end"
33
+ end
34
+ when :number
35
+ if sloppy
36
+ if value.to_f.abs <= 1
37
+ if value == "0"
38
+ o.puts " def #{test_name}; assert_in_delta(#{value}, (#{full_reference}||0), #{delta}); end"
39
+ else
40
+ o.puts " def #{test_name}; assert_in_delta(#{value}, #{full_reference}, #{delta}); end"
41
+ end
42
+ else
43
+ o.puts " def #{test_name}; assert_in_epsilon(#{value}, #{full_reference}, #{epsilon}); end"
44
+ end
20
45
  else
21
- output.puts " def test_#{c_name}_#{ref.downcase}; assert_in_epsilon(#{value},#{full_reference}); end"
46
+ o.puts " def #{test_name}; assert_equal(#{value}, #{full_reference}); end"
22
47
  end
23
48
  else
24
- output.puts " def test_#{c_name}_#{ref.downcase}; assert_equal(#{value},#{full_reference}); end"
49
+ o.puts " def #{test_name}; assert_equal(#{value}, #{full_reference}); end"
25
50
  end
26
51
  end
27
52
  end
28
53
 
29
- end
54
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: excel_to_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-17 00:00:00.000000000 Z
12
+ date: 2012-07-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rubypeg
16
- requirement: &70307966168940 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70307966168940
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: nokogiri
27
- requirement: &70307966167440 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: 1.5.0
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70307966167440
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.5.0
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: rspec
38
- requirement: &70307966165300 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: 2.7.0
44
54
  type: :runtime
45
55
  prerelease: false
46
- version_requirements: *70307966165300
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.7.0
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: ffi
49
- requirement: &70307966164020 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,7 +69,12 @@ dependencies:
54
69
  version: 1.0.11
55
70
  type: :runtime
56
71
  prerelease: false
57
- version_requirements: *70307966164020
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.0.11
58
78
  description: ! "# excel_to_code\n\nConverts some excel spreadsheets (.xlsx, not .xls)
59
79
  into some other programming languages (currently ruby or c).\nThis allows the excel
60
80
  spreadsheets to be run programatically, without excel.\n\nIts cannonical source
@@ -202,7 +222,7 @@ files:
202
222
  - src/util.rb
203
223
  - bin/excel_to_c
204
224
  - bin/excel_to_ruby
205
- homepage: http://github.com/tamc/excel2code
225
+ homepage: http://github.com/tamc/excel_to_code
206
226
  licenses: []
207
227
  post_install_message:
208
228
  rdoc_options: []
@@ -222,7 +242,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
222
242
  version: '0'
223
243
  requirements: []
224
244
  rubyforge_project:
225
- rubygems_version: 1.8.10
245
+ rubygems_version: 1.8.24
226
246
  signing_key:
227
247
  specification_version: 3
228
248
  summary: Converts .xlxs files into pure ruby 1.9 code or pure C code so that they