Skip to content

Commit 99fda18

Browse files
fix: add coverage for switch statements (#43)
1 parent badb106 commit 99fda18

2 files changed

Lines changed: 103 additions & 4 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//@ts-nocheck
2+
3+
import { SomeThrow, someObjectLiteral } from "./something"
4+
5+
const someRandomThrow = () => {
6+
throw new Error('some random throw')
7+
}
8+
9+
const server = http.createServer(async (req, res) => {
10+
11+
switch (req.url) {
12+
case '/api/pong':
13+
console.log('pong!', INSTANCE_ID, PRIVATE_IP)
14+
throw new Error('')
15+
break
16+
case '/api/ping':
17+
console.log('ping!', INSTANCE_ID, PRIVATE_IP)
18+
const ips = await SomeThrow()
19+
someObjectLiteral.objectLiteralThrow()
20+
const others = ips.filter(ip => ip !== PRIVATE_IP)
21+
22+
others.forEach(ip => {
23+
http.get(`http://[${ip}]:8080/api/pong`)
24+
})
25+
break
26+
case '/api/throw':
27+
someRandomThrow()
28+
break
29+
}
30+
31+
res.end()
32+
})
33+
34+
const wss = new WebSocketServer({ noServer: true })

crates/does-it-throw/src/main.rs

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,13 +594,10 @@ mod integration_tests {
594594
.iter()
595595
.for_each(|f| assert!(function_names_contains(&imported_identifier_usages, f)));
596596

597-
println!("Import sources {:?}", result.import_sources);
598-
599597
let import_sources = result.import_sources.into_iter().collect::<Vec<String>>();
600598
fn import_sources_contains(import_sources: &Vec<String>, import_source: &str) -> bool {
601599
import_sources.iter().any(|f| f == import_source)
602600
}
603-
println!("Import sources {:?}", import_sources);
604601
[
605602
"./something3",
606603
"path",
@@ -651,9 +648,77 @@ mod integration_tests {
651648
fn calls_to_throws_contains(calls_to_throws: &Vec<String>, call_to_throw: &str) -> bool {
652649
calls_to_throws.iter().any(|c| c == call_to_throw)
653650
}
654-
println!("Calls to throws {:?}", calls_to_throws);
655651
["SomeClass-someCallToThrow", "SomeClass-someCallToThrow"]
656652
.iter()
657653
.for_each(|f| assert!(calls_to_throws_contains(&calls_to_throws, f)));
658654
}
655+
656+
#[test]
657+
fn test_switch_statement() {
658+
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
659+
let file_path = format!("{}/src/fixtures/switchStatement.ts", manifest_dir);
660+
// Read sample code from file
661+
let sample_code = fs::read_to_string(file_path).expect("Something went wrong reading the file");
662+
let cm: Lrc<SourceMap> = Default::default();
663+
664+
let (result, _cm) = analyze_code(&sample_code, cm);
665+
666+
// general result assertions
667+
assert_eq!(result.functions_with_throws.len(), 2);
668+
assert_eq!(result.calls_to_throws.len(), 2);
669+
assert_eq!(result.imported_identifier_usages.len(), 2);
670+
assert_eq!(result.import_sources.len(), 1);
671+
672+
// function names
673+
let function_names: Vec<String> = result
674+
.functions_with_throws
675+
.iter()
676+
.map(|f| f.function_or_method_name.clone())
677+
.collect();
678+
fn function_names_contains(function_names: &Vec<String>, function_name: &str) -> bool {
679+
function_names.iter().any(|f| f == function_name)
680+
}
681+
682+
["someRandomThrow", "createServer"]
683+
.iter()
684+
.for_each(|f| assert!(function_names_contains(&function_names, f)));
685+
686+
// calls to throws
687+
let calls_to_throws: Vec<String> = result
688+
.calls_to_throws
689+
.iter()
690+
.map(|c| c.id.clone())
691+
.collect();
692+
693+
fn calls_to_throws_contains(calls_to_throws: &Vec<String>, call_to_throw: &str) -> bool {
694+
calls_to_throws.iter().any(|c| c == call_to_throw)
695+
}
696+
697+
["NOT_SET-createServer", "http-<anonymous>"]
698+
.iter()
699+
.for_each(|f| assert!(calls_to_throws_contains(&calls_to_throws, f)));
700+
701+
let import_sources = result.import_sources.into_iter().collect::<Vec<String>>();
702+
fn import_sources_contains(import_sources: &Vec<String>, import_source: &str) -> bool {
703+
import_sources.iter().any(|f| f == import_source)
704+
}
705+
["./something"]
706+
.iter()
707+
.for_each(|f| assert!(import_sources_contains(&import_sources, f)));
708+
709+
let import_identifiers = result
710+
.imported_identifier_usages
711+
.into_iter()
712+
.map(|i| i.id)
713+
.collect::<Vec<String>>();
714+
fn import_identifiers_contains(
715+
import_identifiers: &Vec<String>,
716+
import_identifier: &str,
717+
) -> bool {
718+
import_identifiers.iter().any(|f| f == import_identifier)
719+
}
720+
["someObjectLiteral-objectLiteralThrow", "NOT_SET-SomeThrow"]
721+
.iter()
722+
.for_each(|f| assert!(import_identifiers_contains(&import_identifiers, f)));
723+
}
659724
}

0 commit comments

Comments
 (0)