How to test a visitor that uses visit_mut_script?

Let's say you have a visitor, something like this
struct Visitor;

impl VisitMut for Visitor {
fn visit_mut_script(&mut self, script: &mut Script) {
// Do something with script
}
}
struct Visitor;

impl VisitMut for Visitor {
fn visit_mut_script(&mut self, script: &mut Script) {
// Do something with script
}
}
Then you create a unit test, something like
#[test]
fn test_visitor() {
let input = "";
let expected = "";

test_transform(Syntax::default(), |_| as_folder(&mut Visitor), input, expected, true);

// Optionally do some assertions with assert, assert_eq etc on the visitor
}
#[test]
fn test_visitor() {
let input = "";
let expected = "";

test_transform(Syntax::default(), |_| as_folder(&mut Visitor), input, expected, true);

// Optionally do some assertions with assert, assert_eq etc on the visitor
}
The test function won't work because the internal code for test_transform parses the input code as a module and not a script, so visit_mut_script is never called. Is there a different function to use instead of test_transform or is there a way to make test_transform use parse_script instead of parse_module?
3 Replies
Levi
LeviOP11mo ago
So after a bit of digging it seems like visit_mut_module, visit_mut_script and visit_mut_program can be called if you use parse_program (I think?) but not parse_module which is what test_transform seems to use. Should test_transform maybe use parse_program instead? I think this should probably be the case as a test case could be either a module or a script but right now it's assuming it's always a module which I think is bad, parse_program doc states: Returns Module if it's a module and returns Script if it's not a module. so therefor test_transform should probably use parse_program correct?
kdy1
kdy111mo ago
You are correct I guess
Levi
LeviOP11mo ago
I’ve made an issue for now, we can perhaps discuss this further and see if changing it would break anything if you are interested

Did you find this page helpful?