@@ -89,3 +89,71 @@ impl Changes {
89
89
self . filter_by_cell_path ( |x| f ( x. extension ( ) ) )
90
90
}
91
91
}
92
+ #[ cfg( test) ]
93
+ mod tests {
94
+ use super :: * ;
95
+ use crate :: buck:: types:: CellPath ;
96
+ use crate :: buck:: types:: ProjectRelativePath ;
97
+ use crate :: sapling:: status:: Status ;
98
+
99
+ #[ test]
100
+ fn test_changes_empty ( ) {
101
+ let changes = Changes :: default ( ) ;
102
+ assert ! ( changes. is_empty( ) ) ;
103
+ }
104
+
105
+ #[ test]
106
+ fn test_changes_new ( ) {
107
+ let cell_info = CellInfo :: testing ( ) ;
108
+ let project_paths = vec ! [
109
+ Status :: Modified ( ProjectRelativePath :: new( "src/lib.rs" ) ) ,
110
+ Status :: Modified ( ProjectRelativePath :: new( "src/main.rs" ) ) ,
111
+ ] ;
112
+ let changes = Changes :: new ( & cell_info, project_paths) . unwrap ( ) ;
113
+ assert ! ( !changes. is_empty( ) ) ;
114
+ assert_eq ! ( changes. paths. len( ) , 2 ) ;
115
+ }
116
+
117
+ #[ test]
118
+ fn test_contains_cell_path ( ) {
119
+ let cell_path = CellPath :: new ( "cell1//" ) ;
120
+ let project_path = ProjectRelativePath :: new ( "src/lib.rs" ) ;
121
+ let paths = vec ! [ Status :: Added ( ( cell_path. clone( ) , project_path) ) ] ;
122
+ let changes = Changes :: from_paths ( paths) ;
123
+ assert ! ( changes. contains_cell_path( & cell_path) ) ;
124
+ }
125
+
126
+ #[ test]
127
+ fn test_filter_by_cell_path ( ) {
128
+ let cell_path1 = CellPath :: new ( "cell1//" ) ;
129
+ let cell_path2 = CellPath :: new ( "cell2//" ) ;
130
+ let project_path1 = ProjectRelativePath :: new ( "src/lib.rs" ) ;
131
+ let project_path2 = ProjectRelativePath :: new ( "src/main.rs" ) ;
132
+ let paths = vec ! [
133
+ Status :: Added ( ( cell_path1. clone( ) , project_path1) ) ,
134
+ Status :: Added ( ( cell_path2. clone( ) , project_path2) ) ,
135
+ ] ;
136
+ let changes = Changes :: from_paths ( paths) ;
137
+ let filtered_changes = changes. filter_by_cell_path ( |path| path == & cell_path1) ;
138
+ assert_eq ! ( filtered_changes. paths. len( ) , 1 ) ;
139
+ assert ! ( filtered_changes. contains_cell_path( & cell_path1) ) ;
140
+ assert ! ( !filtered_changes. contains_cell_path( & cell_path2) ) ;
141
+ }
142
+
143
+ #[ test]
144
+ fn test_filter_by_extension ( ) {
145
+ let cell_path1 = CellPath :: new ( "Cell1//foo/bar/cell1.rs" ) ;
146
+ let cell_path2 = CellPath :: new ( "Cell2//foo/baz/cell2.txt" ) ;
147
+ let project_path1 = ProjectRelativePath :: new ( "foo/bar/cell1.rs" ) ;
148
+ let project_path2 = ProjectRelativePath :: new ( "foo/baz/cell2.txt" ) ;
149
+ let paths = vec ! [
150
+ Status :: Added ( ( cell_path1. clone( ) , project_path1) ) ,
151
+ Status :: Added ( ( cell_path2. clone( ) , project_path2) ) ,
152
+ ] ;
153
+ let changes = Changes :: from_paths ( paths) ;
154
+ let filtered_changes = changes. filter_by_extension ( |ext| ext == Some ( "rs" ) ) ;
155
+ assert_eq ! ( filtered_changes. paths. len( ) , 1 ) ;
156
+ assert ! ( filtered_changes. contains_cell_path( & cell_path1) ) ;
157
+ assert ! ( !filtered_changes. contains_cell_path( & cell_path2) ) ;
158
+ }
159
+ }
0 commit comments