Add visitor tests for strings, bytes, f-strings (#9009)

This PR adds tests for visitor implementation for string literals, bytes
literals and f-strings.
This commit is contained in:
Dhruv Manilawala 2023-12-06 10:52:19 -06:00 committed by GitHub
parent ee6548d7dd
commit bd443ebe91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 174 additions and 10 deletions

View file

@ -3,14 +3,15 @@ use std::fmt::{Debug, Write};
use insta::assert_snapshot;
use ruff_python_ast::visitor::preorder::{
walk_alias, walk_comprehension, walk_except_handler, walk_expr, walk_keyword, walk_match_case,
walk_module, walk_parameter, walk_parameters, walk_pattern, walk_stmt, walk_type_param,
walk_with_item, PreorderVisitor,
walk_alias, walk_bytes_literal, walk_comprehension, walk_except_handler, walk_expr,
walk_keyword, walk_match_case, walk_module, walk_parameter, walk_parameters, walk_pattern,
walk_stmt, walk_string_literal, walk_type_param, walk_with_item, PreorderVisitor,
};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{
Alias, BoolOp, CmpOp, Comprehension, ExceptHandler, Expr, Keyword, MatchCase, Mod, Operator,
Parameter, Parameters, Pattern, Singleton, Stmt, TypeParam, UnaryOp, WithItem,
Alias, BoolOp, BytesLiteral, CmpOp, Comprehension, ExceptHandler, Expr, Keyword, MatchCase,
Mod, Operator, Parameter, Parameters, Pattern, Singleton, Stmt, StringLiteral, TypeParam,
UnaryOp, WithItem,
};
use ruff_python_parser::lexer::lex;
use ruff_python_parser::{parse_tokens, Mode};
@ -128,6 +129,33 @@ fn function_type_parameters() {
assert_snapshot!(trace);
}
#[test]
fn string_literals() {
let source = r"'a' 'b' 'c'";
let trace = trace_preorder_visitation(source);
assert_snapshot!(trace);
}
#[test]
fn bytes_literals() {
let source = r"b'a' b'b' b'c'";
let trace = trace_preorder_visitation(source);
assert_snapshot!(trace);
}
#[test]
fn f_strings() {
let source = r"'pre' f'foo {bar:.{x}f} baz'";
let trace = trace_preorder_visitation(source);
assert_snapshot!(trace);
}
fn trace_preorder_visitation(source: &str) -> String {
let tokens = lex(source, Mode::Module);
let parsed = parse_tokens(tokens, source, Mode::Module, "test.py").unwrap();
@ -278,4 +306,16 @@ impl PreorderVisitor<'_> for RecordVisitor {
walk_type_param(self, type_param);
self.exit_node();
}
fn visit_string_literal(&mut self, string_literal: &StringLiteral) {
self.enter_node(string_literal);
walk_string_literal(self, string_literal);
self.exit_node();
}
fn visit_bytes_literal(&mut self, bytes_literal: &BytesLiteral) {
self.enter_node(bytes_literal);
walk_bytes_literal(self, bytes_literal);
self.exit_node();
}
}

View file

@ -0,0 +1,11 @@
---
source: crates/ruff_python_ast/tests/preorder.rs
expression: trace
---
- ModModule
- StmtExpr
- ExprBytesLiteral
- BytesLiteral
- BytesLiteral
- BytesLiteral

View file

@ -0,0 +1,22 @@
---
source: crates/ruff_python_ast/tests/preorder.rs
expression: trace
---
- ModModule
- StmtExpr
- ExprFString
- StringLiteral
- ExprStringLiteral
- StringLiteral
- ExprFormattedValue
- ExprName
- ExprFString
- ExprStringLiteral
- StringLiteral
- ExprFormattedValue
- ExprName
- ExprStringLiteral
- StringLiteral
- ExprStringLiteral
- StringLiteral

View file

@ -0,0 +1,11 @@
---
source: crates/ruff_python_ast/tests/preorder.rs
expression: trace
---
- ModModule
- StmtExpr
- ExprStringLiteral
- StringLiteral
- StringLiteral
- StringLiteral

View file

@ -0,0 +1,10 @@
---
source: crates/ruff_python_ast/tests/visitor.rs
expression: trace
---
- StmtExpr
- ExprBytesLiteral
- BytesLiteral
- BytesLiteral
- BytesLiteral

View file

@ -0,0 +1,21 @@
---
source: crates/ruff_python_ast/tests/visitor.rs
expression: trace
---
- StmtExpr
- ExprFString
- StringLiteral
- ExprStringLiteral
- StringLiteral
- ExprFormattedValue
- ExprName
- ExprFString
- ExprStringLiteral
- StringLiteral
- ExprFormattedValue
- ExprName
- ExprStringLiteral
- StringLiteral
- ExprStringLiteral
- StringLiteral

View file

@ -0,0 +1,10 @@
---
source: crates/ruff_python_ast/tests/visitor.rs
expression: trace
---
- StmtExpr
- ExprStringLiteral
- StringLiteral
- StringLiteral
- StringLiteral

View file

@ -6,14 +6,14 @@ use ruff_python_parser::lexer::lex;
use ruff_python_parser::{parse_tokens, Mode};
use ruff_python_ast::visitor::{
walk_alias, walk_comprehension, walk_except_handler, walk_expr, walk_keyword, walk_match_case,
walk_parameter, walk_parameters, walk_pattern, walk_stmt, walk_type_param, walk_with_item,
Visitor,
walk_alias, walk_bytes_literal, walk_comprehension, walk_except_handler, walk_expr,
walk_keyword, walk_match_case, walk_parameter, walk_parameters, walk_pattern, walk_stmt,
walk_string_literal, walk_type_param, walk_with_item, Visitor,
};
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{
Alias, BoolOp, CmpOp, Comprehension, ExceptHandler, Expr, Keyword, MatchCase, Operator,
Parameter, Parameters, Pattern, Stmt, TypeParam, UnaryOp, WithItem,
Alias, BoolOp, BytesLiteral, CmpOp, Comprehension, ExceptHandler, Expr, Keyword, MatchCase,
Operator, Parameter, Parameters, Pattern, Stmt, StringLiteral, TypeParam, UnaryOp, WithItem,
};
#[test]
@ -129,6 +129,33 @@ fn function_type_parameters() {
assert_snapshot!(trace);
}
#[test]
fn string_literals() {
let source = r"'a' 'b' 'c'";
let trace = trace_visitation(source);
assert_snapshot!(trace);
}
#[test]
fn bytes_literals() {
let source = r"b'a' b'b' b'c'";
let trace = trace_visitation(source);
assert_snapshot!(trace);
}
#[test]
fn f_strings() {
let source = r"'pre' f'foo {bar:.{x}f} baz'";
let trace = trace_visitation(source);
assert_snapshot!(trace);
}
fn trace_visitation(source: &str) -> String {
let tokens = lex(source, Mode::Module);
let parsed = parse_tokens(tokens, source, Mode::Module, "test.py").unwrap();
@ -281,4 +308,16 @@ impl Visitor<'_> for RecordVisitor {
walk_type_param(self, type_param);
self.exit_node();
}
fn visit_string_literal(&mut self, string_literal: &StringLiteral) {
self.enter_node(string_literal);
walk_string_literal(self, string_literal);
self.exit_node();
}
fn visit_bytes_literal(&mut self, bytes_literal: &BytesLiteral) {
self.enter_node(bytes_literal);
walk_bytes_literal(self, bytes_literal);
self.exit_node();
}
}