@@ -7,12 +7,25 @@ module Spoom
77 module Sorbet
88 class LocationTest < Minitest ::Test
99 def test_from_string
10- location = Location . from_string ( "foo.rb:1:2-3:4" )
11- assert_equal ( "foo.rb" , location . file )
12- assert_equal ( 1 , location . start_line )
13- assert_equal ( 2 , location . start_column )
14- assert_equal ( 3 , location . end_line )
15- assert_equal ( 4 , location . end_column )
10+ location1 = Location . from_string ( "foo.rb:1:2-3:4" )
11+ assert_equal ( "foo.rb" , location1 . file )
12+ assert_equal ( 1 , location1 . start_line )
13+ assert_equal ( 2 , location1 . start_column )
14+ assert_equal ( 3 , location1 . end_line )
15+ assert_equal ( 4 , location1 . end_column )
16+
17+ location2 = Location . from_string ( "foo.rb:1-3" )
18+ assert_equal ( 1 , location2 . start_line )
19+ assert_equal ( 3 , location2 . end_line )
20+ assert_nil ( location2 . start_column )
21+ assert_nil ( location2 . end_column )
22+
23+ location3 = Location . from_string ( "foo.rb" )
24+ assert_equal ( "foo.rb" , location3 . file )
25+ assert_nil ( location3 . start_line )
26+ assert_nil ( location3 . start_column )
27+ assert_nil ( location3 . end_line )
28+ assert_nil ( location3 . end_column )
1629 end
1730
1831 def test_raises_if_location_string_has_missing_components
@@ -27,65 +40,112 @@ def test_raises_if_location_string_has_missing_components
2740 assert_raises ( Location ::LocationError ) do
2841 Location . from_string ( "foo.rb:1" )
2942 end
43+ end
44+
45+ def test_raises_if_initialize_has_missing_attributes
46+ assert_raises ( Location ::LocationError ) do
47+ Location . new ( "foo.rb" , start_line : 1 , start_column : 2 , end_line : 3 )
48+ end
49+
50+ assert_raises ( Location ::LocationError ) do
51+ Location . new ( "foo.rb" , start_line : 1 , start_column : 2 , end_column : 3 )
52+ end
53+
54+ assert_raises ( Location ::LocationError ) do
55+ Location . new ( "foo.rb" , start_line : 1 , end_column : 2 , end_line : 3 )
56+ end
3057
3158 assert_raises ( Location ::LocationError ) do
32- Location . from_string ( "foo.rb" )
59+ Location . new ( "foo.rb" , start_column : 1 , end_column : 2 , end_line : 3 )
60+ end
61+
62+ assert_raises ( Location ::LocationError ) do
63+ Location . new ( "foo.rb" , start_line : 1 , start_column : 2 )
64+ end
65+
66+ assert_raises ( Location ::LocationError ) do
67+ Location . new ( "foo.rb" , start_line : 1 )
3368 end
3469 end
3570
3671 def test_include
37- location1 = Location . new ( "foo.rb" , 1 , 2 , 3 , 4 )
38- location2 = Location . new ( "foo.rb" , 1 , 2 , 3 , 4 )
72+ location1 = Location . new ( "foo.rb" , start_line : 1 , start_column : 2 , end_line : 3 , end_column : 4 )
73+ location2 = Location . new ( "foo.rb" , start_line : 1 , start_column : 2 , end_line : 3 , end_column : 4 )
3974 assert ( location1 . include? ( location2 ) )
4075
41- location3 = Location . new ( "foo.rb" , 1 , 2 , 3 , 5 )
76+ location3 = Location . new ( "foo.rb" , start_line : 1 , start_column : 2 , end_line : 3 , end_column : 5 )
4277 refute ( location1 . include? ( location3 ) )
4378 assert ( location3 . include? ( location1 ) )
4479
45- location4 = Location . new ( "foo.rb" , 1 , 2 , 4 , 4 )
80+ location4 = Location . new ( "foo.rb" , start_line : 1 , start_column : 2 , end_line : 4 , end_column : 4 )
4681 refute ( location1 . include? ( location4 ) )
4782 assert ( location4 . include? ( location1 ) )
4883
49- location5 = Location . new ( "foo.rb" , 1 , 3 , 3 , 4 )
84+ location5 = Location . new ( "foo.rb" , start_line : 1 , start_column : 3 , end_line : 3 , end_column : 4 )
5085 assert ( location1 . include? ( location5 ) )
5186 refute ( location5 . include? ( location1 ) )
5287
53- location6 = Location . new ( "foo.rb" , 2 , 2 , 3 , 4 )
88+ location6 = Location . new ( "foo.rb" , start_line : 2 , start_column : 2 , end_line : 3 , end_column : 4 )
5489 assert ( location1 . include? ( location6 ) )
5590 refute ( location6 . include? ( location1 ) )
5691
57- location7 = Location . new ( "bar.rb" , 1 , 2 , 3 , 4 )
92+ location7 = Location . new ( "bar.rb" , start_line : 1 , start_column : 2 , end_line : 3 , end_column : 4 )
5893 refute ( location1 . include? ( location7 ) )
5994 refute ( location7 . include? ( location1 ) )
95+
96+ location8 = Location . new ( "foo.rb" )
97+ location9 = Location . new ( "foo.rb" )
98+ assert ( location8 . include? ( location9 ) )
99+ assert ( location9 . include? ( location8 ) )
100+
101+ assert ( location8 . include? ( location1 ) )
102+ refute ( location1 . include? ( location8 ) )
103+
104+ location10 = Location . new ( "foo.rb" , start_line : 1 , end_line : 3 )
105+ assert ( location10 . include? ( location1 ) )
106+ refute ( location1 . include? ( location10 ) )
60107 end
61108
62109 def test_comparison
63- location1 = Location . new ( "foo.rb" , 1 , 2 , 3 , 4 )
64- location2 = Location . new ( "foo.rb" , 1 , 2 , 3 , 4 )
110+ location1 = Location . new ( "foo.rb" , start_line : 1 , start_column : 2 , end_line : 3 , end_column : 4 )
111+ location2 = Location . new ( "foo.rb" , start_line : 1 , start_column : 2 , end_line : 3 , end_column : 4 )
65112 assert_equal ( 0 , location1 <=> location2 )
66113
67- location3 = Location . new ( "foo.rb" , 1 , 2 , 3 , 5 )
114+ location3 = Location . new ( "foo.rb" , start_line : 1 , start_column : 2 , end_line : 3 , end_column : 5 )
68115 assert_equal ( -1 , location1 <=> location3 )
69116
70- location4 = Location . new ( "foo.rb" , 1 , 2 , 4 , 4 )
117+ location4 = Location . new ( "foo.rb" , start_line : 1 , start_column : 2 , end_line : 4 , end_column : 4 )
71118 assert_equal ( -1 , location1 <=> location4 )
72119
73- location5 = Location . new ( "foo.rb" , 1 , 3 , 3 , 4 )
120+ location5 = Location . new ( "foo.rb" , start_line : 1 , start_column : 3 , end_line : 3 , end_column : 4 )
74121 assert_equal ( -1 , location1 <=> location5 )
75122
76- location6 = Location . new ( "foo.rb" , 11 , 2 , 3 , 4 )
123+ location6 = Location . new ( "foo.rb" , start_line : 11 , start_column : 2 , end_line : 3 , end_column : 4 )
77124 assert_equal ( -1 , location1 <=> location6 )
78125
79- location7 = Location . new ( "bar.rb" , 1 , 2 , 3 , 4 )
126+ location7 = Location . new ( "bar.rb" , start_line : 1 , start_column : 2 , end_line : 3 , end_column : 4 )
80127 assert_equal ( 1 , location1 <=> location7 )
81128
82129 not_a_location = 42
83130 assert_nil ( location1 <=> not_a_location )
131+
132+ location8 = Location . new ( "foo.rb" )
133+ location9 = Location . new ( "foo.rb" )
134+ assert_equal ( 0 , location8 <=> location9 )
135+
136+ location10 = Location . new ( "foo.rb" , start_line : 1 , end_line : 3 )
137+ assert_equal ( -1 , location8 <=> location10 )
84138 end
85139
86140 def test_to_s
87- location = Location . new ( "foo.rb" , 1 , 2 , 3 , 4 )
88- assert_equal ( "foo.rb:1:2-3:4" , location . to_s )
141+ location1 = Location . new ( "foo.rb" , start_line : 1 , start_column : 2 , end_line : 3 , end_column : 4 )
142+ assert_equal ( "foo.rb:1:2-3:4" , location1 . to_s )
143+
144+ location2 = Location . new ( "foo.rb" , start_line : 1 , end_line : 3 )
145+ assert_equal ( "foo.rb:1-3" , location2 . to_s )
146+
147+ location3 = Location . new ( "foo.rb" )
148+ assert_equal ( "foo.rb" , location3 . to_s )
89149 end
90150 end
91151 end
0 commit comments