dyi 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. data/COPYING +674 -0
  2. data/README +28 -0
  3. data/examples/class_diagram.rb +151 -0
  4. data/examples/data/03311056.xlsx +0 -0
  5. data/examples/data/currency.xlsx +0 -0
  6. data/examples/data/money.csv +12 -0
  7. data/examples/line_and_bar.rb +26 -0
  8. data/examples/line_chart.rb +30 -0
  9. data/examples/logo.rb +68 -0
  10. data/examples/pie_chart.rb +19 -0
  11. data/examples/simple_shapes.rb +15 -0
  12. data/lib/dyi.rb +49 -0
  13. data/lib/dyi/chart.rb +34 -0
  14. data/lib/dyi/chart/array_reader.rb +136 -0
  15. data/lib/dyi/chart/base.rb +580 -0
  16. data/lib/dyi/chart/csv_reader.rb +93 -0
  17. data/lib/dyi/chart/excel_reader.rb +100 -0
  18. data/lib/dyi/chart/line_chart.rb +468 -0
  19. data/lib/dyi/chart/pie_chart.rb +141 -0
  20. data/lib/dyi/chart/table.rb +201 -0
  21. data/lib/dyi/color.rb +218 -0
  22. data/lib/dyi/coordinate.rb +224 -0
  23. data/lib/dyi/drawing.rb +32 -0
  24. data/lib/dyi/drawing/canvas.rb +100 -0
  25. data/lib/dyi/drawing/clipping.rb +61 -0
  26. data/lib/dyi/drawing/color_effect.rb +118 -0
  27. data/lib/dyi/drawing/filter.rb +74 -0
  28. data/lib/dyi/drawing/pen.rb +231 -0
  29. data/lib/dyi/drawing/pen_3d.rb +270 -0
  30. data/lib/dyi/font.rb +132 -0
  31. data/lib/dyi/formatter.rb +36 -0
  32. data/lib/dyi/formatter/base.rb +245 -0
  33. data/lib/dyi/formatter/emf_formatter.rb +253 -0
  34. data/lib/dyi/formatter/eps_formatter.rb +397 -0
  35. data/lib/dyi/formatter/svg_formatter.rb +260 -0
  36. data/lib/dyi/formatter/svg_reader.rb +113 -0
  37. data/lib/dyi/formatter/xaml_formatter.rb +317 -0
  38. data/lib/dyi/length.rb +399 -0
  39. data/lib/dyi/matrix.rb +122 -0
  40. data/lib/dyi/painting.rb +177 -0
  41. data/lib/dyi/shape.rb +1332 -0
  42. data/lib/dyi/svg_element.rb +149 -0
  43. data/lib/dyi/type.rb +104 -0
  44. data/lib/ironruby.rb +326 -0
  45. data/lib/util.rb +231 -0
  46. data/test/path_command_test.rb +217 -0
  47. data/test/test_length.rb +91 -0
  48. metadata +114 -0
@@ -0,0 +1,231 @@
1
+ # -*- encoding: UTF-8 -*-
2
+
3
+ # Copyright (c) 2009-2011 Sound-F Co., Ltd. All rights reserved.
4
+ #
5
+ # Author:: Mamoru Yuo
6
+ #
7
+ # This file is part of DYI.
8
+ #
9
+ # DYI is free software: you can redistribute it and/or modify it
10
+ # under the terms of the GNU General Public License as published by
11
+ # the Free Software Foundation, either version 3 of the License, or
12
+ # (at your option) any later version.
13
+ #
14
+ # DYI is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # along with DYI. If not, see <http://www.gnu.org/licenses/>.
21
+
22
+ class Numeric
23
+
24
+ def strfnum(format)
25
+ decimal_separator = (defined? ::Numeric::DECIMAL_SEPARATOR) ? ::Numeric::DECIMAL_SEPARATOR : '.'
26
+ group_separator = (defined? ::Numeric::GROUP_SEPARATOR) ? ::Numeric::GROUP_SEPARATOR : ','
27
+ group_sizes = (defined? ::Numeric::GROUP_SIZES) ? ::Numeric::GROUP_SIZES : 3
28
+ percent_symbol = (defined? ::Numeric::PERCENT_SYMBOL) ? ::Numeric::PERCENT_SYMBOL : '%'
29
+
30
+ # lexical analysis
31
+ sec = {:int => [], :dec => [], :int_digit => 0, :dec_digit => 0, :place => 0}
32
+ sections = [sec]
33
+ escaped = false
34
+ forcibly = false
35
+ format.split(//).each do |char|
36
+ if escaped
37
+ if sec[:decimal]
38
+ sec[:dec].push(char)
39
+ else
40
+ sec[:int].push(char)
41
+ end
42
+ escaped = false
43
+ next
44
+ end
45
+
46
+ case char
47
+ when '0'
48
+ if sec[:decimal]
49
+ sec[:dec_digit] += 1
50
+ sec[:dec].push(:zero)
51
+ else
52
+ forcibly = true
53
+ sec[:int_digit] += 1
54
+ sec[:int].push(:zero)
55
+ if sec[:place] != 0
56
+ sec[:use_separater] = true
57
+ sec[:place] = 0
58
+ end
59
+ end
60
+ when '#'
61
+ if sec[:decimal]
62
+ sec[:dec_digit] += 1
63
+ sec[:dec].push(:sharp)
64
+ else
65
+ sec[:int_digit] += 1
66
+ sec[:int].push(forcibly ? :zero : :sharp)
67
+ if sec[:place] != 0
68
+ sec[:use_separater] = true
69
+ sec[:place] = 0
70
+ end
71
+ end
72
+ when '.'
73
+ sec[:decimal] = true
74
+ when ','
75
+ sec[:place] += group_sizes unless sec[:decimal]
76
+ when ';'
77
+ forcibly = false
78
+ sec = {:int => [], :dec => [], :int_digit => 0, :dec_digit => 0, :place => 0}
79
+ sections.push(sec)
80
+ when '%'
81
+ sec[:percent] = true
82
+ if sec[:decimal]
83
+ sec[:dec].push(percent_symbol)
84
+ else
85
+ sec[:int].push(percent_symbol)
86
+ end
87
+ when '\\'
88
+ escaped = true
89
+ else
90
+ if sec[:decimal]
91
+ sec[:dec].push(char)
92
+ else
93
+ sec[:int].push(char)
94
+ end
95
+ end
96
+ end
97
+
98
+ # choosing of the format (fmt)
99
+ need_minus_sign = false
100
+ case sections.size
101
+ when 1
102
+ need_minus_sign = (self < 0)
103
+ frm = sections[0]
104
+ when 2
105
+ if self >= 0
106
+ frm = sections[0]
107
+ else
108
+ frm = sections[1]
109
+ end
110
+ else
111
+ if self > 0
112
+ frm = sections[0]
113
+ elsif self < 0
114
+ frm = sections[1]
115
+ else
116
+ frm = sections[2]
117
+ end
118
+ end
119
+
120
+ value = self
121
+
122
+ value *= 100 if frm[:percent]
123
+ value = value.quo(10 ** frm[:place]) if frm[:place] != 0
124
+
125
+ int_part, dec_part = ("%.#{frm[:dec_digit]}f" % value.abs).split('.')
126
+
127
+ if self.nonzero? && int_part == '0' && !(dec_part =~ /[^0]/)
128
+ if sections[2]
129
+ frm = sections[2]
130
+ value = 0
131
+ int_part, dec_part = ("%.#{frm[:dec_digit]}f" % value.abs).split('.')
132
+ elsif self < 0 && sections[1]
133
+ frm = sections[0]
134
+ value = 0
135
+ int_part, dec_part = ("%.#{frm[:dec_digit]}f" % value.abs).split('.')
136
+ end
137
+ end
138
+
139
+ # formatting of integer part
140
+ first = 0
141
+ last = int_part.size - frm[:int_digit]
142
+ int_index = frm[:int_digit]
143
+ last_num = nil
144
+ frm[:int].each_with_index do |place_holder, index|
145
+ case place_holder
146
+ when :zero
147
+ if frm[:use_separater]
148
+ if first == last || last < 0
149
+ num = last < 0 ? '0' : int_part[first, 1]
150
+ if last_num && int_index % group_sizes == 0
151
+ frm[:int][index] = group_separator + num
152
+ elsif last_num.nil? && need_minus_sign
153
+ frm[:int][index] = '-' + num
154
+ else
155
+ frm[:int][index] = num
156
+ end
157
+ else
158
+ num = int_part[first..last]
159
+ i = (int_part.size - 1) % 3 + 1
160
+ while i < num.size
161
+ num.insert(i, group_separator)
162
+ i += 4
163
+ end
164
+ frm[:int][index] = last_num.nil? && need_minus_sign ? '-' + num : num
165
+ end
166
+ else
167
+ num = last < 0 ? '0' : int_part[first..last]
168
+ num = '-' + num if last_num.nil? && need_minus_sign
169
+ frm[:int][index] = num
170
+ end
171
+ first = (last += 1)
172
+ int_index -= 1
173
+ last_num = frm[:int][index]
174
+ when :sharp
175
+ if last < 0
176
+ frm[:int][index] = nil
177
+ elsif frm[:use_separater]
178
+ if first == last
179
+ if last_num && int_index % group_sizes == 0
180
+ frm[:int][index] = group_separator + int_part[first, 1]
181
+ elsif last_num.nil? && need_minus_sign
182
+ frm[:int][index] = '-' + int_part[first, 1]
183
+ else
184
+ frm[:int][index] = int_part[first, 1]
185
+ end
186
+ else
187
+ num = int_part[first..last]
188
+ i = (int_part.size - 1) % 3 + 1
189
+ while i < num.size
190
+ num.insert(i, group_separator)
191
+ i += 4
192
+ end
193
+ frm[:int][index] = last_num.nil? && need_minus_sign ? '-' + num : num
194
+ end
195
+ else
196
+ num = int_part[first..last]
197
+ num = '-' + num if last_num.nil? && need_minus_sign
198
+ frm[:int][index] = num
199
+ end
200
+ first = (last += 1)
201
+ int_index -= 1
202
+ last_num = frm[:int][index]
203
+ end
204
+ end
205
+
206
+ # formatting of decimal part
207
+ needs_replaceing = false
208
+ needs_decimal_separator = nil
209
+ dec_index = frm[:dec_digit]
210
+ (frm[:dec].size - 1).downto(0) do |index|
211
+ case frm[:dec][index]
212
+ when :zero
213
+ dec_index -= 1
214
+ frm[:dec][index] = dec_part[dec_index, 1]
215
+ needs_replaceing = true
216
+ needs_decimal_separator = decimal_separator
217
+ when :sharp
218
+ dec_index -= 1
219
+ if needs_replaceing || dec_part[dec_index, 1] != '0'
220
+ frm[:dec][index] = dec_part[dec_index, 1]
221
+ needs_replaceing = true
222
+ needs_decimal_separator = decimal_separator
223
+ else
224
+ frm[:dec][index] = nil
225
+ end
226
+ end
227
+ end
228
+
229
+ (frm[:int] + [needs_decimal_separator] + frm[:dec]).join
230
+ end
231
+ end
@@ -0,0 +1,217 @@
1
+ # -*- encoding: UTF-8 -*-
2
+
3
+ require 'test/unit'
4
+ require File.join(File.dirname(__FILE__), '../lib/dyi')
5
+ require 'pp'
6
+
7
+ class PathCommandTest < Test::Unit::TestCase
8
+ def setup
9
+ @start_point = DYI::Coordinate.new([100,200])
10
+ @start_command = DYI::Shape::Path::MoveCommand.new(false, nil, @start_point)
11
+ @absolute_points = [[110,220],[80,180],[130,120],[60,200],[-30,100],[-85,165]].map{|pt| DYI::Coordinate.new(pt)}
12
+ @relative_points = [[10,20],[-30,-40],[50,-60],[-70,80],[-90,-100],[-55,65]].map{|pt| DYI::Coordinate.new(pt)}
13
+
14
+ @absolute_points_2 = [[70,160],[0,240],[-55,305]].map{|pt| DYI::Coordinate.new(pt)}
15
+ @absolute_points_3 = [[150,140],[95,205]].map{|pt| DYI::Coordinate.new(pt)}
16
+ @absolute_control_points_2 = [[110,220],[120,100],[-90,140]].map{|pt| DYI::Coordinate.new(pt)}
17
+ @absolute_control_points_3 = [[110,220],[70,160],[80,220],[60,40]].map{|pt| DYI::Coordinate.new(pt)}
18
+ end
19
+
20
+ def test_absolute_move_command
21
+ cmds = DYI::Shape::Path::MoveCommand.absolute_commands(nil, *([@start_point] + @absolute_points))
22
+ assert_equal(7, cmds.size)
23
+ cmds.each_with_index do |c, i|
24
+ assert_kind_of(i == 0 ? DYI::Shape::Path::MoveCommand : DYI::Shape::Path::LineCommand, c)
25
+ assert(c.absolute?)
26
+ assert_equal(@start_point, c.start_point)
27
+ assert_equal(i == 0 ? @start_point : @absolute_points[i-1], c.point)
28
+ assert_equal(i == 0 ? @start_point : @absolute_points[i-1], c.last_point)
29
+ assert_equal(case i
30
+ when 0 then nil
31
+ when 1 then @start_point
32
+ else @absolute_points[i-2]
33
+ end, c.preceding_point)
34
+ end
35
+ end
36
+
37
+ def test_relative_move_command
38
+ cmds = DYI::Shape::Path::MoveCommand.relative_commands(nil, *([@start_point] + @relative_points))
39
+ assert_equal(7, cmds.size)
40
+ cmds.each_with_index do |c, i|
41
+ assert_kind_of(i == 0 ? DYI::Shape::Path::MoveCommand : DYI::Shape::Path::LineCommand, c)
42
+ assert(i == 0 ? c.absolute? : c.relative?)
43
+ assert_equal(i == 0 ? @start_point : @relative_points[i-1], c.point)
44
+ assert_equal(@start_point, c.start_point)
45
+ assert_equal(i == 0 ? @start_point : @absolute_points[i-1], c.last_point)
46
+ assert_equal(case i
47
+ when 0 then nil
48
+ when 1 then @start_point
49
+ else @absolute_points[i-2]
50
+ end, c.preceding_point)
51
+ end
52
+ end
53
+
54
+ def test_absolute_line_command
55
+ cmds = DYI::Shape::Path::LineCommand.absolute_commands(@start_command, *@absolute_points)
56
+ assert_equal(6, cmds.size)
57
+ cmds.each_with_index do |c, i|
58
+ assert_kind_of(DYI::Shape::Path::LineCommand, c)
59
+ assert(c.absolute?)
60
+ assert_equal(@absolute_points[i], c.point)
61
+ assert_equal(@start_point, c.start_point)
62
+ assert_equal(@absolute_points[i], c.last_point)
63
+ assert_equal(i == 0 ? @start_point : @absolute_points[i-1], c.preceding_point)
64
+ end
65
+ end
66
+
67
+ def test_relative_line_command
68
+ cmds = DYI::Shape::Path::LineCommand.relative_commands(@start_command, *@relative_points)
69
+ assert_equal(6, cmds.size)
70
+ cmds.each_with_index do |c, i|
71
+ assert_kind_of(DYI::Shape::Path::LineCommand, c)
72
+ assert(c.relative?)
73
+ assert_equal(@relative_points[i], c.point)
74
+ assert_equal(@start_point, c.start_point)
75
+ assert_equal(@absolute_points[i], c.last_point)
76
+ assert_equal(i == 0 ? @start_point : @absolute_points[i-1], c.preceding_point)
77
+ end
78
+ end
79
+
80
+ def test_close_command
81
+ pre_cmd = DYI::Shape::Path::LineCommand.absolute_commands(@start_command, *@absolute_points).last
82
+ assert_raise(NoMethodError){DYI::Shape::Path::CloseCommand.absolute_commands(pre_cmd)}
83
+ assert_raise(NoMethodError){DYI::Shape::Path::CloseCommand.relative_commands(pre_cmd)}
84
+
85
+ cmds = DYI::Shape::Path::CloseCommand.commands(pre_cmd)
86
+ assert_equal(1, cmds.size)
87
+ cmd = cmds.first
88
+ assert_nil(cmd.point)
89
+ assert_equal(@start_point, cmd.start_point)
90
+ assert_equal(@start_point, cmd.last_point)
91
+ assert_equal(@absolute_points.last, cmd.preceding_point)
92
+ end
93
+
94
+ def test_absolute_curve_command
95
+ cmds = DYI::Shape::Path::CurveCommand.absolute_commands(@start_command, *@absolute_points)
96
+ assert_equal(2, cmds.size)
97
+ cmds.each_with_index do |c, i|
98
+ assert_kind_of(DYI::Shape::Path::CurveCommand, c)
99
+ assert(c.absolute?)
100
+ assert_equal(@absolute_points[i*3+2], c.point)
101
+ assert_equal(@absolute_points[i*3,2], [c.control_point1,c.control_point2])
102
+ assert_equal(@start_point, c.start_point)
103
+ assert_equal(@absolute_points[i*3+2], c.last_point)
104
+ assert_equal(i == 0 ? @start_point : @absolute_points[i*3-1], c.preceding_point)
105
+ end
106
+ end
107
+
108
+ def test_relative_curve_command
109
+ cmds = DYI::Shape::Path::CurveCommand.relative_commands(@start_command, *@relative_points)
110
+ assert_equal(2, cmds.size)
111
+ cmds.each_with_index do |c, i|
112
+ assert_kind_of(DYI::Shape::Path::CurveCommand, c)
113
+ assert(c.relative?)
114
+ assert_equal(@relative_points[i*3+2], c.point)
115
+ assert_equal(@relative_points[i*3,2], [c.control_point1,c.control_point2])
116
+ assert_equal(@start_point, c.start_point)
117
+ assert_equal(@absolute_points_3[i], c.last_point)
118
+ assert_equal(i == 0 ? @start_point : @absolute_points_3[i-1], c.preceding_point)
119
+ end
120
+ end
121
+
122
+ def test_absolute_shorthand_curve_command
123
+ # [100,200],[110,220],[80,180],[130,120], [60,200],[-30,100],[-85,165]
124
+ ctrl_pts = [[100,200],[110,220],[50,140],[130,120],[-10,280],[-30,100]].map{|pt| DYI::Coordinate.new(pt)}
125
+
126
+ cmds = DYI::Shape::Path::ShorthandCurveCommand.absolute_commands(@start_command, *@absolute_points)
127
+ assert_equal(3, cmds.size)
128
+ cmds.each_with_index do |c, i|
129
+ assert_kind_of(DYI::Shape::Path::ShorthandCurveCommand, c)
130
+ assert(c.absolute?)
131
+ assert_equal(@absolute_points[i*2+1], c.point)
132
+ assert_equal(ctrl_pts[i*2,2], [c.control_point1,c.control_point2])
133
+ assert_equal(@start_point, c.start_point)
134
+ assert_equal(@absolute_points[i*2+1], c.last_point)
135
+ assert_equal(i == 0 ? @start_point : @absolute_points[i*2-1], c.preceding_point)
136
+ end
137
+ end
138
+
139
+ def test_relative_shorthand_curve_command
140
+ # [100,200],[10,20],[-30,-40],[50,-60], [-70,80],[-90,-100],[-55,65]
141
+ ctrl_pts = [[0,0],[10,20],[-40,-60],[50,-60],[-120,140],[-90,-100]].map{|pt| DYI::Coordinate.new(pt)}
142
+
143
+ cmds = DYI::Shape::Path::ShorthandCurveCommand.relative_commands(@start_command, *@relative_points)
144
+ assert_equal(3, cmds.size)
145
+ cmds.each_with_index do |c, i|
146
+ assert_kind_of(DYI::Shape::Path::ShorthandCurveCommand, c)
147
+ assert(c.relative?)
148
+ assert_equal(@relative_points[i*2+1], c.point)
149
+ assert_equal(ctrl_pts[i*2,2], [c.control_point1,c.control_point2])
150
+ assert_equal(@start_point, c.start_point)
151
+ assert_equal(@absolute_points_2[i], c.last_point)
152
+ assert_equal(i == 0 ? @start_point : @absolute_points_2[i-1], c.preceding_point)
153
+ end
154
+ end
155
+
156
+ def test_absolute_quadratic_curve_command
157
+ cmds = DYI::Shape::Path::QuadraticCurveCommand.absolute_commands(@start_command, *@absolute_points)
158
+ assert_equal(3, cmds.size)
159
+ cmds.each_with_index do |c, i|
160
+ assert_kind_of(DYI::Shape::Path::QuadraticCurveCommand, c)
161
+ assert(c.absolute?)
162
+ assert_equal(@absolute_points[i*2+1], c.point)
163
+ assert_equal(@absolute_points[i*2], c.control_point)
164
+ assert_equal(@start_point, c.start_point)
165
+ assert_equal(@absolute_points[i*2+1], c.last_point)
166
+ assert_equal(i == 0 ? @start_point : @absolute_points[i*2-1], c.preceding_point)
167
+ end
168
+ end
169
+
170
+ def test_relative_quadratic_curve_command
171
+ cmds = DYI::Shape::Path::QuadraticCurveCommand.relative_commands(@start_command, *@relative_points)
172
+ assert_equal(3, cmds.size)
173
+ cmds.each_with_index do |c, i|
174
+ assert_kind_of(DYI::Shape::Path::QuadraticCurveCommand, c)
175
+ assert(c.relative?)
176
+ assert_equal(@relative_points[i*2+1], c.point)
177
+ assert_equal(@relative_points[i*2], c.control_point)
178
+ assert_equal(@start_point, c.start_point)
179
+ assert_equal(@absolute_points_2[i], c.last_point)
180
+ assert_equal(i == 0 ? @start_point : @absolute_points_2[i-1], c.preceding_point)
181
+ end
182
+ end
183
+
184
+ def test_absolute_shorthand_quadratic_curve_command
185
+ # [100,200],[110,220],[80,180],[130,120], [60,200],[-30,100],[-85,165]
186
+ ctrl_pts = [[100,200],[120,240],[40,120],[220,120],[-100,280],[40,-80]].map{|pt| DYI::Coordinate.new(pt)}
187
+
188
+ cmds = DYI::Shape::Path::ShorthandQuadraticCurveCommand.absolute_commands(@start_command, *@absolute_points)
189
+ assert_equal(6, cmds.size)
190
+ cmds.each_with_index do |c, i|
191
+ assert_kind_of(DYI::Shape::Path::ShorthandQuadraticCurveCommand, c)
192
+ assert(c.absolute?)
193
+ assert_equal(@absolute_points[i], c.point)
194
+ assert_equal(ctrl_pts[i], c.control_point)
195
+ assert_equal(@start_point, c.start_point)
196
+ assert_equal(@absolute_points[i], c.last_point)
197
+ assert_equal(i == 0 ? @start_point : @absolute_points[i-1], c.preceding_point)
198
+ end
199
+ end
200
+
201
+ def test_relative_shorthand_quadratic_curve_command
202
+ # [100,200],[10,20],[-30,-40],[50,-60], [-70,80],[-90,-100],[-55,65]
203
+ ctrl_pts = [[0,0],[10,20],[-40,-60], [90,0],[-160,80],[70,-180]].map{|pt| DYI::Coordinate.new(pt)}
204
+
205
+ cmds = DYI::Shape::Path::ShorthandQuadraticCurveCommand.relative_commands(@start_command, *@relative_points)
206
+ assert_equal(6, cmds.size)
207
+ cmds.each_with_index do |c, i|
208
+ assert_kind_of(DYI::Shape::Path::ShorthandQuadraticCurveCommand, c)
209
+ assert(c.relative?)
210
+ assert_equal(@relative_points[i], c.point)
211
+ assert_equal(ctrl_pts[i], c.control_point)
212
+ assert_equal(@start_point, c.start_point)
213
+ assert_equal(@absolute_points[i], c.last_point)
214
+ assert_equal(i == 0 ? @start_point : @absolute_points[i-1], c.preceding_point)
215
+ end
216
+ end
217
+ end
@@ -0,0 +1,91 @@
1
+ require 'test/unit'
2
+ require '../lib/dyi/length'
3
+
4
+ class TC_Length < Test::Unit::TestCase
5
+ include DYI
6
+
7
+ def setup
8
+ @one_px = Length.new(1)
9
+ end
10
+
11
+ def test_initialize
12
+ assert_not_nil @one_px
13
+ assert Length.new(3)
14
+ assert_instance_of(Length, @one_px)
15
+
16
+ assert equal_f?(1, @one_px)
17
+ assert equal_f?(@one_px, Length.new(@one_px))
18
+ assert equal_f?(Length.new(0), Length::ZERO)
19
+
20
+ assert_same(@one_px, Length.new(@one_px))
21
+ end
22
+
23
+ def test_unit_conversion
24
+ assert equal_f?(Length.new('4px'), Length.new(4))
25
+
26
+ assert equal_f?(1, Length.new('1px').to_f)
27
+ assert equal_f?(1.25, Length.new('1pt').to_f)
28
+ assert equal_f?(35.43307, Length.new('1cm').to_f)
29
+ assert equal_f?(3.543307, Length.new('1mm').to_f)
30
+ assert equal_f?(90.0, Length.new('1in').to_f)
31
+ assert equal_f?(15.0, Length.new('1pc').to_f)
32
+
33
+ assert equal_f?(Length.new('1px') * 1.25, Length.new('1pt'))
34
+ assert equal_f?(Length.new('1px') * 35.43307, Length.new('1cm'))
35
+ assert equal_f?(Length.new('1px') * 3.543307, Length.new('1mm'))
36
+ assert equal_f?(Length.new('1px') * 90.0, Length.new('1in'))
37
+ assert equal_f?(Length.new('1px') * 15.0, Length.new('1pc'))
38
+
39
+ assert equal_f?(1.25, Length.new('1px') * 1.25)
40
+ assert equal_f?(1.25, Length.new('1cm') * 1.25 / 35.43307)
41
+ assert equal_f?(1.25, Length.new('1mm') * 1.25 / 3.543307)
42
+ assert equal_f?(1.25, Length.new('1in') * 1.25 / 90.0)
43
+ assert equal_f?(1.25, Length.new('1pc') * 1.25 / 15.0)
44
+ end
45
+
46
+ def test_unary
47
+ assert equal_f?(@one_px, +@one_px)
48
+ assert equal_f?(Length.new(-2), -Length.new(2))
49
+ end
50
+
51
+ def test_binary_operator
52
+ assert equal_f?(3+5, Length.new(3) + Length.new(5))
53
+ assert equal_f?(4.2+5.3, Length.new(4.2) + Length.new(5.3))
54
+ assert equal_f?(Length.new(5 + 2), Length.new(5) + 2)
55
+
56
+ assert equal_f?(12 - 5, Length.new(12) - Length.new(5))
57
+ assert equal_f?(4.2 - 5.3, Length.new(4.2) - Length.new(5.3))
58
+ assert equal_f?(Length.new(7 - 4), Length.new(7) - Length.new(4))
59
+ assert equal_f?(Length.new(7 - 4), Length.new(7) - 4)
60
+
61
+ assert equal_f?(4 * 7, Length.new(4) * 7)
62
+ assert equal_f?(3.82 * 1.2, Length.new(3.82) * 1.2)
63
+ assert_raise(TypeError){
64
+ Length.new(4) * Length.new(7)
65
+ }
66
+
67
+ assert equal_f?(5 ** 3, Length.new(5) ** 3)
68
+ assert_raise(TypeError){
69
+ Length.new(5) ** Length.new(3)
70
+ }
71
+
72
+ assert equal_f?(6 / 5, Length.new(6).div(Length.new(5)))
73
+ assert_raise(TypeError){
74
+ Length.new(6).div(2)
75
+ }
76
+
77
+ assert equal_f?(7 % 5, Length.new(7) % Length.new(5))
78
+ assert_raise(TypeError){
79
+ Length.new(2) % 3
80
+ }
81
+
82
+ assert equal_f?(3.82 / 1.2, Length.new(3.82) / 1.2)
83
+ assert equal_f?(4 / 2, Length.new(4) / Length.new(2))
84
+ end
85
+
86
+ private
87
+
88
+ def equal_f?(left, right)
89
+ (left.to_f - right.to_f).abs <= 0.0000001
90
+ end
91
+ end