The main logic of my function is a switch case, with the default
case being the abnormal case.
function main(input) {
let result = process(input)
switch (result): {
//normal cases
case A:
processA();
break;
case B:
processB();
break;
...
default:
handle_abnormal()
}
//further process input
process2(input)
}
As the above pseudo codes show the main login of my function are 3 steps: 1. process input 2. switch case the processed result 3. further processing the result.
To test main()
, at some point the switch case block added a test case. The test logic is to send the input with some test data, then switch case handle that test case.
function main(input) {
let result = process(input)
switch (result): {
//This is a test case
case T:
processT();
break;
//normal cases
case A:
processA();
break;
case B:
processB();
break;
default:
handle_abnormal()
}
//further process input
process2(input)
}
There were two reasons to add that back then, 1. it is easier to generate the test data (than the normal cases) 2 with that test case we can easily verify the whole processing of main()
run as expected.
But now that test codes exist in the production codes! Although I have to say because of that test codes I am indeed feel more confident when refactor the main() function.
I have been trying to remove the test case from the production code, but to no avail.
-
typescript does not support preprocessor, e.g. https://stackoverflow.com/questions/32906097/preprocessor-defines-in-typescript so I don’t have a preprocessor to remove it.
-
Although terser has “conditional-compilation” to remove dead codes, but I find it won’t work in my case. Through my test, terse’s conditional-compilation is quite limited.
-
I have tried to write some standalone test code to test my
main
function so I can move that test case in to the test code. But because it is inside that switch case block I can’t find an easy way to move it out (at some point I just feel it does not worth the effort)
How do I remove those test codes out ? Or it is just not worth the effort ?