savage 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/savage/core_extensions/string.rb +8 -0
- data/lib/savage/direction.rb +29 -0
- data/lib/savage/directions/arc_to.rb +24 -0
- data/lib/savage/directions/close_path.rb +14 -0
- data/lib/savage/directions/coordinate_target.rb +20 -0
- data/lib/savage/directions/cubic_curve_to.rb +25 -0
- data/lib/savage/directions/horizontal_to.rb +10 -0
- data/lib/savage/directions/line_to.rb +10 -0
- data/lib/savage/directions/move_to.rb +10 -0
- data/lib/savage/directions/point_target.rb +20 -0
- data/lib/savage/directions/quadratic_curve_to.rb +22 -0
- data/lib/savage/directions/vertical_to.rb +10 -0
- data/lib/savage/path.rb +16 -0
- data/lib/savage/sub_path.rb +47 -0
- data/lib/savage/utils.rb +7 -0
- data/lib/savage.rb +9 -0
- data/savage.gemspec +100 -0
- data/spec/savage/directions/arc_to_spec.rb +96 -0
- data/spec/savage/directions/close_path_spec.rb +29 -0
- data/spec/savage/directions/cubic_curve_to_spec.rb +117 -0
- data/spec/savage/directions/horizontal_to_spec.rb +10 -0
- data/spec/savage/directions/line_to_spec.rb +10 -0
- data/spec/savage/directions/move_to_spec.rb +10 -0
- data/spec/savage/directions/point_spec.rb +12 -0
- data/spec/savage/directions/quadratic_curve_spec.rb +80 -0
- data/spec/savage/directions/vertical_to_spec.rb +10 -0
- data/spec/savage/path_spec.rb +24 -0
- data/spec/savage/sub_path_spec.rb +112 -0
- data/spec/savage_spec.rb +5 -0
- data/spec/shared/command.rb +13 -0
- data/spec/shared/coordinate_target.rb +35 -0
- data/spec/shared/direction.rb +21 -0
- data/spec/shared/point_target.rb +44 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +36 -0
- metadata +132 -0
@@ -0,0 +1,117 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
include Savage::Directions
|
4
|
+
|
5
|
+
describe CubicCurveTo do
|
6
|
+
def dir_class; CubicCurveTo; end
|
7
|
+
def create_relative; CubicCurveTo.new(100,200,300,400,500,600,false); end
|
8
|
+
def command_code; 'c'; end
|
9
|
+
|
10
|
+
before :each do
|
11
|
+
@dir = dir_class.new(100,200,300,400,500,600)
|
12
|
+
end
|
13
|
+
|
14
|
+
include DirectionShared
|
15
|
+
|
16
|
+
it 'should have a target' do
|
17
|
+
@dir.respond_to?(:target).should == true
|
18
|
+
@dir.target.class.should == Point
|
19
|
+
end
|
20
|
+
it 'should have a first control point' do
|
21
|
+
@dir.respond_to?(:control_1).should == true
|
22
|
+
@dir.control_1.class.should == Point
|
23
|
+
end
|
24
|
+
it 'should have a first control point' do
|
25
|
+
@dir.respond_to?(:control_2).should == true
|
26
|
+
@dir.control_2.class.should == Point
|
27
|
+
end
|
28
|
+
it 'should have an accessible target x, based on the constructor argument' do
|
29
|
+
@dir.target.x.should == 500
|
30
|
+
end
|
31
|
+
it 'should have an accessible target y, based on the constructor argument' do
|
32
|
+
@dir.target.y.should == 600
|
33
|
+
end
|
34
|
+
it 'should have an accessible first control x, based on the constructor argument' do
|
35
|
+
@dir.control_1.x.should == 100
|
36
|
+
end
|
37
|
+
it 'should have an accessible first control y, based on the constructor argument' do
|
38
|
+
@dir.control_1.y.should == 200
|
39
|
+
end
|
40
|
+
it 'should have an accessible second control x, based on the constructor argument' do
|
41
|
+
@dir.control_2.x.should == 300
|
42
|
+
end
|
43
|
+
it 'should have an accessible second control y, based on the constructor argument' do
|
44
|
+
@dir.control_2.y.should == 400
|
45
|
+
end
|
46
|
+
it 'should be constructed with at least target x and y, a control 1 x and y, and a control 2 x and y parameters' do
|
47
|
+
lambda { dir_class.new }.should raise_error
|
48
|
+
lambda { dir_class.new 45 }.should raise_error
|
49
|
+
lambda { dir_class.new 45, 50 }.should raise_error
|
50
|
+
lambda { dir_class.new 45, 50, 60 }.should raise_error
|
51
|
+
lambda { dir_class.new 45, 50, 60, 70 }.should raise_error
|
52
|
+
lambda { dir_class.new 45, 50, 60, 70, 80 }.should raise_error
|
53
|
+
lambda { dir_class.new 45, 50, 60, 70, 80, 90 }.should_not raise_error
|
54
|
+
lambda { dir_class.new 45, 50, 60, 70, 80, 90, true }.should_not raise_error
|
55
|
+
end
|
56
|
+
it 'should be relative if constructed with a true seventh parameter' do
|
57
|
+
direction = dir_class.new 45, 50, 60, 70, 80, 90, false
|
58
|
+
direction.absolute?.should == false
|
59
|
+
end
|
60
|
+
it 'should be absolute if constructed with a false seventh parameter' do
|
61
|
+
direction = dir_class.new 45, 50, 60, 70, 80, 90, true
|
62
|
+
direction.absolute?.should == true
|
63
|
+
end
|
64
|
+
it 'should be absolute if constructed with only six parameters' do
|
65
|
+
direction = dir_class.new 45, 50, 60, 70, 80, 90
|
66
|
+
direction.absolute?.should == true
|
67
|
+
end
|
68
|
+
describe '#to_command' do
|
69
|
+
context 'when not asked to be continuous' do
|
70
|
+
it 'should have exactly 6 numerical parameters' do
|
71
|
+
extract_coordinates(@dir.to_command).length.should == 6
|
72
|
+
end
|
73
|
+
it 'should show the provided control 1 X value as the first parameter' do
|
74
|
+
extract_coordinates(@dir.to_command)[0].should == 100
|
75
|
+
end
|
76
|
+
it 'should show the provided control 1 Y value as the second parameter' do
|
77
|
+
extract_coordinates(@dir.to_command)[1].should == 200
|
78
|
+
end
|
79
|
+
it 'should show the provided control 2 X value as the third parameter' do
|
80
|
+
extract_coordinates(@dir.to_command)[2].should == 300
|
81
|
+
end
|
82
|
+
it 'should show the provided control 2 Y value as the fourth parameter' do
|
83
|
+
extract_coordinates(@dir.to_command)[3].should == 400
|
84
|
+
end
|
85
|
+
it 'should show the provided target X value as the fifth parameter' do
|
86
|
+
extract_coordinates(@dir.to_command)[4].should == 500
|
87
|
+
end
|
88
|
+
it 'should show the provided target Y value as the sixth parameter' do
|
89
|
+
extract_coordinates(@dir.to_command)[5].should == 600
|
90
|
+
end
|
91
|
+
end
|
92
|
+
context 'when asked to be continuous' do
|
93
|
+
it 'should start with a lower-case s when not absolute' do
|
94
|
+
rel_dir = create_relative
|
95
|
+
extract_command(rel_dir.to_command(true)).should == 's'
|
96
|
+
end
|
97
|
+
it 'should start with a capital S when absolute' do
|
98
|
+
extract_command(@dir.to_command(true)).should == 'S'
|
99
|
+
end
|
100
|
+
it 'should have exactly 4 numerical parameters' do
|
101
|
+
extract_coordinates(@dir.to_command(true)).length.should == 4
|
102
|
+
end
|
103
|
+
it 'should show the provided control 2 X value as the first parameter' do
|
104
|
+
extract_coordinates(@dir.to_command(true))[0].should == 300
|
105
|
+
end
|
106
|
+
it 'should show the provided control 2 Y value as the second parameter' do
|
107
|
+
extract_coordinates(@dir.to_command(true))[1].should == 400
|
108
|
+
end
|
109
|
+
it 'should show the provided target X value as the third parameter' do
|
110
|
+
extract_coordinates(@dir.to_command(true))[2].should == 500
|
111
|
+
end
|
112
|
+
it 'should show the provided target Y value as the fourth parameter' do
|
113
|
+
extract_coordinates(@dir.to_command(true))[3].should == 600
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
include Savage::Directions
|
4
|
+
|
5
|
+
describe HorizontalTo do
|
6
|
+
def dir_class; HorizontalTo; end
|
7
|
+
def create_relative; HorizontalTo.new(100,false); end
|
8
|
+
def command_code; 'h'; end
|
9
|
+
include CoordinateTargetShared
|
10
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
include Savage::Directions
|
4
|
+
|
5
|
+
describe LineTo do
|
6
|
+
def dir_class; LineTo; end
|
7
|
+
def create_relative; LineTo.new(100,200,false); end
|
8
|
+
def command_code; 'l'; end
|
9
|
+
include PointTargetShared
|
10
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
include Savage::Directions
|
4
|
+
|
5
|
+
describe MoveTo do
|
6
|
+
def dir_class; MoveTo; end
|
7
|
+
def create_relative; MoveTo.new(100,200,false); end
|
8
|
+
def command_code; 'm'; end
|
9
|
+
include PointTargetShared
|
10
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
include Savage::Directions
|
4
|
+
|
5
|
+
describe Point do
|
6
|
+
it 'should have an x' do
|
7
|
+
Point.new.respond_to?(:x).should == true;
|
8
|
+
end
|
9
|
+
it 'should have an y' do
|
10
|
+
Point.new.respond_to?(:y).should == true;
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
include Savage::Directions
|
4
|
+
|
5
|
+
describe QuadraticCurveTo do
|
6
|
+
def dir_class; QuadraticCurveTo; end
|
7
|
+
def create_relative; QuadraticCurveTo.new(100,200,300,400,false); end
|
8
|
+
def command_code; 'q'; end
|
9
|
+
|
10
|
+
before :each do
|
11
|
+
@dir = dir_class.new(100,200,300,400)
|
12
|
+
end
|
13
|
+
|
14
|
+
include DirectionShared
|
15
|
+
|
16
|
+
it 'should have a target' do
|
17
|
+
@dir.respond_to?(:target).should == true
|
18
|
+
@dir.target.class.should == Point
|
19
|
+
end
|
20
|
+
it 'should have a control point' do
|
21
|
+
@dir.respond_to?(:control).should == true
|
22
|
+
@dir.control.class.should == Point
|
23
|
+
end
|
24
|
+
it 'should have an accessible target x, based on the constructor argument' do
|
25
|
+
@dir.target.x.should == 300
|
26
|
+
end
|
27
|
+
it 'should have an accessible target y, based on the constructor argument' do
|
28
|
+
@dir.target.y.should == 400
|
29
|
+
end
|
30
|
+
it 'should have an accessible first control x, based on the constructor argument' do
|
31
|
+
@dir.control.x.should == 100
|
32
|
+
end
|
33
|
+
it 'should have an accessible first control y, based on the constructor argument' do
|
34
|
+
@dir.control.y.should == 200
|
35
|
+
end
|
36
|
+
it 'should be constructed with at least target x and y, a control 1 x and y, and a control 2 x and y parameters' do
|
37
|
+
lambda { dir_class.new }.should raise_error
|
38
|
+
lambda { dir_class.new 45 }.should raise_error
|
39
|
+
lambda { dir_class.new 45, 50 }.should raise_error
|
40
|
+
lambda { dir_class.new 45, 50, 60 }.should raise_error
|
41
|
+
lambda { dir_class.new 45, 50, 60, 70 }.should_not raise_error
|
42
|
+
lambda { dir_class.new 45, 50, 60, 70, true }.should_not raise_error
|
43
|
+
end
|
44
|
+
it 'should be relative if constructed with a false fifth parameter' do
|
45
|
+
direction = dir_class.new 45, 50, 60, 70, false
|
46
|
+
direction.absolute?.should == false
|
47
|
+
end
|
48
|
+
it 'should be absolute if constructed with a true fifth parameter' do
|
49
|
+
direction = dir_class.new 45, 50, 60, 70, true
|
50
|
+
direction.absolute?.should == true
|
51
|
+
end
|
52
|
+
it 'should be absolute if constructed with only four parameters' do
|
53
|
+
direction = dir_class.new 45, 50, 60, 70
|
54
|
+
direction.absolute?.should == true
|
55
|
+
end
|
56
|
+
describe '#to_command' do
|
57
|
+
it 'should start with a capital T when not absolute' do
|
58
|
+
rel_dir = create_relative
|
59
|
+
extract_command(rel_dir.to_command(true)).should == 't'
|
60
|
+
end
|
61
|
+
it 'should start with a lower-case t when absolute' do
|
62
|
+
extract_command(@dir.to_command(true)).should == 'T'
|
63
|
+
end
|
64
|
+
it 'should have exactly 4 numerical parameters' do
|
65
|
+
extract_coordinates(@dir.to_command).length.should == 4
|
66
|
+
end
|
67
|
+
it 'should show the provided control X value as the first parameter' do
|
68
|
+
extract_coordinates(@dir.to_command)[0].should == 100
|
69
|
+
end
|
70
|
+
it 'should show the provided control Y value as the second parameter' do
|
71
|
+
extract_coordinates(@dir.to_command)[1].should == 200
|
72
|
+
end
|
73
|
+
it 'should show the provided target X value as the third parameter' do
|
74
|
+
extract_coordinates(@dir.to_command)[2].should == 300
|
75
|
+
end
|
76
|
+
it 'should show the provided target Y value as the fourth parameter' do
|
77
|
+
extract_coordinates(@dir.to_command)[3].should == 400
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
include Savage::Directions
|
4
|
+
|
5
|
+
describe VerticalTo do
|
6
|
+
def dir_class; VerticalTo; end
|
7
|
+
def create_relative; VerticalTo.new(100,false); end
|
8
|
+
def command_code; 'v'; end
|
9
|
+
include CoordinateTargetShared
|
10
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
include Savage
|
4
|
+
|
5
|
+
describe Path do
|
6
|
+
it 'should accept no parameters in a constructor for a new, empty path' do
|
7
|
+
lambda{ Path.new }.should_not raise_error
|
8
|
+
end
|
9
|
+
it 'should accept a string parameter to build a path based on existing path data' do
|
10
|
+
lambda{ Path.new("M100 200") }.should_not raise_error
|
11
|
+
end
|
12
|
+
it 'should raise an error if anything besides a string is passed to the constructor' do
|
13
|
+
lambda{ Path.new([]) }.should raise_error
|
14
|
+
lambda{ Path.new({}) }.should raise_error
|
15
|
+
lambda{ Path.new(56) }.should raise_error
|
16
|
+
lambda{ Path.new(Object.new) }.should raise_error
|
17
|
+
end
|
18
|
+
it 'should have subpaths' do
|
19
|
+
Path.new.respond_to?(:subpaths).should == true
|
20
|
+
end
|
21
|
+
it 'should have a to_command method' do
|
22
|
+
Path.new.respond_to?(:to_command).should == true
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
include Savage
|
4
|
+
|
5
|
+
describe SubPath do
|
6
|
+
it 'should have a commands list' do
|
7
|
+
SubPath.new.respond_to?(:commands).should == true
|
8
|
+
end
|
9
|
+
it 'should have a move_to method' do
|
10
|
+
SubPath.new.respond_to?(:move_to).should == true
|
11
|
+
end
|
12
|
+
it 'should have a line_to method' do
|
13
|
+
SubPath.new.respond_to?(:line_to).should == true
|
14
|
+
end
|
15
|
+
it 'should have a horizontal_to method' do
|
16
|
+
SubPath.new.respond_to?(:horizontal_to).should == true
|
17
|
+
end
|
18
|
+
it 'should have a vertical_to method' do
|
19
|
+
SubPath.new.respond_to?(:vertical_to).should == true
|
20
|
+
end
|
21
|
+
it 'should have a quadratic_curve_to method' do
|
22
|
+
SubPath.new.respond_to?(:quadratic_curve_to).should == true
|
23
|
+
end
|
24
|
+
it 'should have a cubic_curve_to method' do
|
25
|
+
SubPath.new.respond_to?(:cubic_curve_to).should == true
|
26
|
+
end
|
27
|
+
it 'should have a arc_to method' do
|
28
|
+
SubPath.new.respond_to?(:arc_to).should == true
|
29
|
+
end
|
30
|
+
it 'should have a close_path method' do
|
31
|
+
SubPath.new.respond_to?(:close_path).should == true
|
32
|
+
end
|
33
|
+
it 'should have a closed? method' do
|
34
|
+
SubPath.new.respond_to?(:closed?).should == true
|
35
|
+
end
|
36
|
+
it 'should have a to_command method' do
|
37
|
+
SubPath.new.respond_to?(:to_command).should == true
|
38
|
+
end
|
39
|
+
describe '#closed?' do
|
40
|
+
it 'should be true if the last direction in the commands list is of type ClosePath' do
|
41
|
+
path = SubPath.new
|
42
|
+
path.move_to 100, 300
|
43
|
+
path.line_to 243, 21
|
44
|
+
path.close_path
|
45
|
+
path.closed?.should == true
|
46
|
+
end
|
47
|
+
it 'should be false if the last direction in the commands list is of any other type or absent' do
|
48
|
+
path = SubPath.new
|
49
|
+
path.move_to 100, 300
|
50
|
+
path.line_to 234, 21
|
51
|
+
path.closed?.should == false
|
52
|
+
path2 = SubPath.new
|
53
|
+
path2.closed?.should == false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
describe '#to_command' do
|
57
|
+
it 'should output the concatenation of all the subcommands if no two are the same in sequence' do
|
58
|
+
path = SubPath.new
|
59
|
+
path.move_to 100, 200
|
60
|
+
path.horizontal_to -200
|
61
|
+
path.quadratic_curve_to 342, -341.23, 405, 223
|
62
|
+
path.line_to -342.002, 231.42
|
63
|
+
path.close_path
|
64
|
+
path.to_command.should == 'M100 200H-200Q342-341.23 405 223L-342.002 231.42Z'
|
65
|
+
end
|
66
|
+
it 'should strip the command code if the previous code was the same as the present' do
|
67
|
+
path = SubPath.new
|
68
|
+
com1 = path.move_to 100, 200
|
69
|
+
com2 = path.horizontal_to -200
|
70
|
+
com4 = path.line_to -342.002, 231.42
|
71
|
+
com4 = path.line_to -234, 502
|
72
|
+
path.to_command.should == 'M100 200H-200L-342.002 231.42-234 502'
|
73
|
+
end
|
74
|
+
it 'should strip the command code if the previous code was a MoveTo and the current code is a LineTo' do
|
75
|
+
path = SubPath.new
|
76
|
+
com1 = path.move_to 100, 200
|
77
|
+
com4 = path.line_to -342.002, 231.42
|
78
|
+
com4 = path.line_to -234, 502
|
79
|
+
path.to_command.should == 'M100 200-342.002 231.42-234 502'
|
80
|
+
end
|
81
|
+
end
|
82
|
+
describe 'move_to' do
|
83
|
+
before :each do
|
84
|
+
@path = SubPath.new
|
85
|
+
end
|
86
|
+
context 'when the command list is empty' do
|
87
|
+
it 'should add a MoveTo command on to the commands list' do
|
88
|
+
this_move = @path.move_to(100,200)
|
89
|
+
@path.commands.should == [this_move]
|
90
|
+
end
|
91
|
+
it 'should return the newly created MoveTo command' do
|
92
|
+
@path.move_to(100,200).class.should == Directions::MoveTo
|
93
|
+
end
|
94
|
+
end
|
95
|
+
context 'when the command list is not empty' do
|
96
|
+
it 'does something' do
|
97
|
+
first_move = @path.move_to(200,400)
|
98
|
+
@path.move_to(100,200)
|
99
|
+
@path.commands.should == [first_move]
|
100
|
+
end
|
101
|
+
it 'should return nil' do
|
102
|
+
@path.move_to(200,400)
|
103
|
+
@path.move_to(100,200).nil?.should == true
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
describe '#commands' do
|
108
|
+
it 'should be able to access items via the bracket operator' do
|
109
|
+
SubPath.new.commands.respond_to?(:[]).should == true
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
data/spec/savage_spec.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module Command
|
2
|
+
def extract_coordinates(command_string)
|
3
|
+
coordinates = []
|
4
|
+
command_string.scan /-?\d+(\.\d+)?/ do |match_group|
|
5
|
+
coordinates << $&.to_f
|
6
|
+
end
|
7
|
+
coordinates
|
8
|
+
end
|
9
|
+
|
10
|
+
def extract_command(command_string)
|
11
|
+
command_string[0,1]
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
share_as :CoordinateTargetShared do
|
2
|
+
before :each do
|
3
|
+
@dir = dir_class.new(100)
|
4
|
+
end
|
5
|
+
include DirectionShared
|
6
|
+
it 'should have an accessible target, based on the constructor argument' do
|
7
|
+
@dir.respond_to?(:target).should == true
|
8
|
+
@dir.target.should == 100
|
9
|
+
end
|
10
|
+
it 'should be constructed with at least a target parameter' do
|
11
|
+
lambda { dir_class.new }.should raise_error
|
12
|
+
lambda { dir_class.new 45 }.should_not raise_error
|
13
|
+
lambda { dir_class.new 45, true }.should_not raise_error
|
14
|
+
end
|
15
|
+
it 'should be relative if constructed with a false third parameter' do
|
16
|
+
direction = dir_class.new(45, false)
|
17
|
+
direction.absolute?.should == false
|
18
|
+
end
|
19
|
+
it 'should be absolute if constructed with a true third parameter' do
|
20
|
+
direction = dir_class.new(45, true)
|
21
|
+
direction.absolute?.should == true
|
22
|
+
end
|
23
|
+
it 'should be absolute if constructed with only two parameters' do
|
24
|
+
direction = dir_class.new(45)
|
25
|
+
direction.absolute?.should == true
|
26
|
+
end
|
27
|
+
describe '#to_command' do
|
28
|
+
it 'should have exactly 1 numerical parameter' do
|
29
|
+
extract_coordinates(@dir.to_command).length.should == 1
|
30
|
+
end
|
31
|
+
it 'should show the provided X value as the next parameter' do
|
32
|
+
extract_coordinates(@dir.to_command)[0].should == 100
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
share_as :DirectionShared do
|
2
|
+
include Command
|
3
|
+
it 'should have a to_command method' do
|
4
|
+
@dir.respond_to?(:to_command).should == true
|
5
|
+
end
|
6
|
+
it 'should have an absolute? method' do
|
7
|
+
@dir.respond_to?(:absolute?).should == true
|
8
|
+
end
|
9
|
+
describe '#to_command' do
|
10
|
+
it 'should start with a lower-case letter when not absolute' do
|
11
|
+
rel_dir = create_relative
|
12
|
+
extract_command(rel_dir.to_command).should == command_code.downcase
|
13
|
+
end
|
14
|
+
it 'should start with a capital letter when absolute' do
|
15
|
+
extract_command(@dir.to_command).should == command_code.upcase
|
16
|
+
end
|
17
|
+
it 'should only have one alphabetical command code' do
|
18
|
+
@dir.to_command.match(/[A-Za-z]/).size.should == 1
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
share_as :PointTargetShared do
|
2
|
+
before :each do
|
3
|
+
@dir = dir_class.new(100,200)
|
4
|
+
end
|
5
|
+
include DirectionShared
|
6
|
+
it 'should have a target' do
|
7
|
+
@dir.respond_to?(:target).should == true
|
8
|
+
@dir.target.class.should == Point
|
9
|
+
end
|
10
|
+
it 'should have an accessible target x, based on the constructor argument' do
|
11
|
+
@dir.target.x.should == 100
|
12
|
+
end
|
13
|
+
it 'should have an accessible target y, based on the constructor argument' do
|
14
|
+
@dir.target.y.should == 200
|
15
|
+
end
|
16
|
+
it 'should be constructed with at least an x and y parameter' do
|
17
|
+
lambda { dir_class.new }.should raise_error
|
18
|
+
lambda { dir_class.new 45 }.should raise_error
|
19
|
+
lambda { dir_class.new 45, 50 }.should_not raise_error
|
20
|
+
end
|
21
|
+
it 'should be relative if constructed with a false third parameter' do
|
22
|
+
direction = dir_class.new(45, 50, false)
|
23
|
+
direction.absolute?.should == false
|
24
|
+
end
|
25
|
+
it 'should be absolute if constructed with a true third parameter' do
|
26
|
+
direction = dir_class.new(45, 50, true)
|
27
|
+
direction.absolute?.should == true
|
28
|
+
end
|
29
|
+
it 'should be absolute if constructed with only two parameters' do
|
30
|
+
direction = dir_class.new(45, 45)
|
31
|
+
direction.absolute?.should == true
|
32
|
+
end
|
33
|
+
describe '#to_command' do
|
34
|
+
it 'should have exactly 2 numerical parameters' do
|
35
|
+
extract_coordinates(@dir.to_command).length.should == 2
|
36
|
+
end
|
37
|
+
it 'should show the provided X value as the next parameter' do
|
38
|
+
extract_coordinates(@dir.to_command)[0].should == 100
|
39
|
+
end
|
40
|
+
it 'should show the provided Y value as the final parameter' do
|
41
|
+
extract_coordinates(@dir.to_command)[1].should == 200
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spork'
|
3
|
+
|
4
|
+
Spork.prefork do
|
5
|
+
# Loading more in this block will cause your tests to run faster. However,
|
6
|
+
# if you change any configuration or code from libraries loaded here, you'll
|
7
|
+
# need to restart spork for it take effect.
|
8
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
9
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
10
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'shared'))
|
11
|
+
|
12
|
+
require 'savage'
|
13
|
+
require 'spec'
|
14
|
+
require 'spec/autorun'
|
15
|
+
Spec::Runner.configure do |config|
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
Dir[File.join(File.dirname(__FILE__) << '/shared', "*.rb")].each {|file| require File.basename(file) }
|
20
|
+
end
|
21
|
+
|
22
|
+
Spork.each_run do
|
23
|
+
# This code will be run each time you run your specs.
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
# --- Instructions ---
|
28
|
+
# - Sort through your spec_helper file. Place as much environment loading
|
29
|
+
# code that you don't normally modify during development in the
|
30
|
+
# Spork.prefork block.
|
31
|
+
# - Place the rest under Spork.each_run block
|
32
|
+
# - Any code that is left outside of the blocks will be ran during preforking
|
33
|
+
# and during each_run!
|
34
|
+
# - These instructions should self-destruct in 10 seconds. If they don't,
|
35
|
+
# feel free to delete them.
|
36
|
+
#
|