1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
mod hook; mod node; mod builder; pub use hook::*; pub use builder::*; pub use node::*; #[cfg(test)] mod test { use std::path::Path; use indoc::indoc; use crate::*; #[test] fn main_only() { let main = indoc!(" int main() { return RET_CODE; } "); let hook = MemHook::builder() .add_file(&Path::new("main.c"), main.to_string()).unwrap() .build(); let node = build(&hook, Path::new("main.c")).unwrap(); assert_eq!(node.collect().0, main); } #[test] fn single_header() { let main = indoc!(" #include <header.h> #include <header.h> // Main function int main() { return RET_CODE; } "); let header = indoc!(" #pragma once // Return code static const int RET_CODE = 0; "); let result = indoc!(" // Return code static const int RET_CODE = 0; // Main function int main() { return RET_CODE; } "); let hook = MemHook::builder() .add_file(&Path::new("main.c"), main.to_string()).unwrap() .add_file(&Path::new("header.h"), header.to_string()).unwrap() .build(); let node = build(&hook, Path::new("main.c")).unwrap(); assert_eq!(node.collect().0, result); } #[test] #[should_panic] fn recursion() { let first = indoc!(" #include <second.h> "); let second = indoc!(" #include <first.h> "); let hook = MemHook::builder() .add_file(&Path::new("first.h"), first.to_string()).unwrap() .add_file(&Path::new("second.h"), second.to_string()).unwrap() .build(); build(&hook, Path::new("first.h")).unwrap(); } #[test] fn recursion_prevented() { let first = indoc!(" #pragma once #include <second.h> "); let second = indoc!(" #pragma once #include <first.h> "); let hook = MemHook::builder() .add_file(&Path::new("first.h"), first.to_string()).unwrap() .add_file(&Path::new("second.h"), second.to_string()).unwrap() .build(); let node = build(&hook, Path::new("first.h")).unwrap(); assert_eq!(node.collect().0, "\n\n\n\n"); } #[test] fn multiple_headers() { let main = indoc!(" #include <h02.h> #include <h01.h> "); let h01 = indoc!(" #pragma once #include <h02.h> h01 "); let h02 = indoc!(" #pragma once #include <h01.h> h02 "); let hook = MemHook::builder() .add_file(&Path::new("main.c"), main.to_string()).unwrap() .add_file(&Path::new("h01.h"), h01.to_string()).unwrap() .add_file(&Path::new("h02.h"), h02.to_string()).unwrap() .build(); let node = build(&hook, Path::new("main.c")).unwrap(); assert_eq!(node.collect().0, "\n\n\n\n\nh01\nh02\n\n"); } #[test] fn line_numbers() { let main = indoc!(" 0 1 2 #include <h01.h> 9 10 #include <h03.h> 15 16 "); let h01 = indoc!(" 4 #include <h02.h> 8 "); let h02 = indoc!(" 6 7 "); let h03 = indoc!(" 12 13 14 "); let hook = MemHook::builder() .add_file(&Path::new("main.c"), main.to_string()).unwrap() .add_file(&Path::new("h01.h"), h01.to_string()).unwrap() .add_file(&Path::new("h02.h"), h02.to_string()).unwrap() .add_file(&Path::new("h03.h"), h03.to_string()).unwrap() .build(); let node = build(&hook, Path::new("main.c")).unwrap(); let source = node.collect().0; for (pos, line) in source.lines().enumerate() { let tline = line.trim_end(); if tline.len() > 0 { assert_eq!(tline.parse::<usize>().unwrap(), pos); } } } #[test] fn indexing() { let main = indoc!(" 00 01 02 #include <h01.h> 04 05 #include <h03.h> 07 08 "); let h01 = indoc!(" 10 #include <h02.h> 12 "); let h02 = indoc!(" 20 21 "); let h03 = indoc!(" 30 31 32 "); let hook = MemHook::builder() .add_file(&Path::new("main.c"), main.to_string()).unwrap() .add_file(&Path::new("h01.h"), h01.to_string()).unwrap() .add_file(&Path::new("h02.h"), h02.to_string()).unwrap() .add_file(&Path::new("h03.h"), h03.to_string()).unwrap() .build(); let node = build(&hook, Path::new("main.c")).unwrap(); let (source, index) = node.collect(); for (pos, line) in source.lines().enumerate() { let (name, lpos) = index.search(pos).unwrap(); let tline = line.trim_end(); if tline.len() > 0 { let n = tline.parse::<usize>().unwrap(); let (f, l) = (n/10, n%10); assert_eq!(match f { 0 => "main.c".to_string(), x => format!("h0{}.h", x), }, name.to_string_lossy()); assert_eq!(l, lpos); } } } }