From c986c605bb506791d3339c0768040f3ec35f571c Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Tue, 5 Sep 2023 09:34:04 +0530 Subject: [PATCH] Add some more f-string test cases --- crates/ruff_python_parser/src/parser.rs | 38 ++ ...ython_parser__parser__tests__fstrings.snap | 541 ++++++++++++++++++ ..._parser__tests__fstrings_with_unicode.snap | 220 +++++++ 3 files changed, 799 insertions(+) create mode 100644 crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings.snap create mode 100644 crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings_with_unicode.snap diff --git a/crates/ruff_python_parser/src/parser.rs b/crates/ruff_python_parser/src/parser.rs index 2303ed7636b8ae..1d55317ff5336d 100644 --- a/crates/ruff_python_parser/src/parser.rs +++ b/crates/ruff_python_parser/src/parser.rs @@ -1249,4 +1249,42 @@ a = 1 .to_string() ); } + + #[test] + fn test_fstrings() { + let parse_ast = parse_suite( + r#" +f"{" f"}" +f"{3,}" +f"{3!=4:}" +f'{3:{"}"}>10}' +f'{3:{"{"}>10}' +f"{ foo = }" +f"{ foo = :.3f }" +f"{ foo = !s }" +f"{ 1, 2 = }" +f'{f"{3.1415=:.1f}":*^20}' +"# + .trim(), + "", + ) + .unwrap(); + insta::assert_debug_snapshot!(parse_ast); + } + + #[test] + fn test_fstrings_with_unicode() { + let parse_ast = parse_suite( + r#" +u"foo" f"{bar}" "baz" " some" +"foo" f"{bar}" u"baz" " some" +"foo" f"{bar}" "baz" u" some" +u"foo" f"bar {baz} realy" u"bar" "no" +"# + .trim(), + "", + ) + .unwrap(); + insta::assert_debug_snapshot!(parse_ast); + } } diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings.snap new file mode 100644 index 00000000000000..2813b5f68c1733 --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings.snap @@ -0,0 +1,541 @@ +--- +source: crates/ruff_python_parser/src/parser.rs +expression: parse_ast +--- +[ + Expr( + StmtExpr { + range: 0..9, + value: FString( + ExprFString { + range: 0..9, + values: [ + FormattedValue( + ExprFormattedValue { + range: 2..8, + value: Constant( + ExprConstant { + range: 3..7, + value: Str( + StringConstant { + value: " f", + implicit_concatenated: false, + }, + ), + kind: None, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + implicit_concatenated: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 10..17, + value: FString( + ExprFString { + range: 10..17, + values: [ + FormattedValue( + ExprFormattedValue { + range: 12..16, + value: Tuple( + ExprTuple { + range: 13..15, + elts: [ + Constant( + ExprConstant { + range: 13..14, + value: Int( + 3, + ), + kind: None, + }, + ), + ], + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + ], + implicit_concatenated: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 18..28, + value: FString( + ExprFString { + range: 18..28, + values: [ + FormattedValue( + ExprFormattedValue { + range: 20..27, + value: Compare( + ExprCompare { + range: 21..25, + left: Constant( + ExprConstant { + range: 21..22, + value: Int( + 3, + ), + kind: None, + }, + ), + ops: [ + NotEq, + ], + comparators: [ + Constant( + ExprConstant { + range: 24..25, + value: Int( + 4, + ), + kind: None, + }, + ), + ], + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FString( + ExprFString { + range: 26..26, + values: [], + implicit_concatenated: false, + }, + ), + ), + }, + ), + ], + implicit_concatenated: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 29..44, + value: FString( + ExprFString { + range: 29..44, + values: [ + FormattedValue( + ExprFormattedValue { + range: 31..43, + value: Constant( + ExprConstant { + range: 32..33, + value: Int( + 3, + ), + kind: None, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FString( + ExprFString { + range: 34..42, + values: [ + FormattedValue( + ExprFormattedValue { + range: 34..39, + value: Constant( + ExprConstant { + range: 35..38, + value: Str( + StringConstant { + value: "}", + implicit_concatenated: false, + }, + ), + kind: None, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Constant( + ExprConstant { + range: 39..42, + value: Str( + StringConstant { + value: ">10", + implicit_concatenated: false, + }, + ), + kind: None, + }, + ), + ], + implicit_concatenated: false, + }, + ), + ), + }, + ), + ], + implicit_concatenated: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 45..60, + value: FString( + ExprFString { + range: 45..60, + values: [ + FormattedValue( + ExprFormattedValue { + range: 47..59, + value: Constant( + ExprConstant { + range: 48..49, + value: Int( + 3, + ), + kind: None, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FString( + ExprFString { + range: 50..58, + values: [ + FormattedValue( + ExprFormattedValue { + range: 50..55, + value: Constant( + ExprConstant { + range: 51..54, + value: Str( + StringConstant { + value: "{", + implicit_concatenated: false, + }, + ), + kind: None, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Constant( + ExprConstant { + range: 55..58, + value: Str( + StringConstant { + value: ">10", + implicit_concatenated: false, + }, + ), + kind: None, + }, + ), + ], + implicit_concatenated: false, + }, + ), + ), + }, + ), + ], + implicit_concatenated: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 61..75, + value: FString( + ExprFString { + range: 61..75, + values: [ + FormattedValue( + ExprFormattedValue { + range: 63..74, + value: Name( + ExprName { + range: 66..69, + id: "foo", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: " ", + trailing: " = ", + }, + ), + conversion: None, + format_spec: None, + }, + ), + ], + implicit_concatenated: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 76..96, + value: FString( + ExprFString { + range: 76..96, + values: [ + FormattedValue( + ExprFormattedValue { + range: 78..95, + value: Name( + ExprName { + range: 81..84, + id: "foo", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: " ", + trailing: " = ", + }, + ), + conversion: None, + format_spec: Some( + FString( + ExprFString { + range: 89..94, + values: [ + Constant( + ExprConstant { + range: 89..94, + value: Str( + StringConstant { + value: ".3f ", + implicit_concatenated: false, + }, + ), + kind: None, + }, + ), + ], + implicit_concatenated: false, + }, + ), + ), + }, + ), + ], + implicit_concatenated: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 97..115, + value: FString( + ExprFString { + range: 97..115, + values: [ + FormattedValue( + ExprFormattedValue { + range: 99..114, + value: Name( + ExprName { + range: 102..105, + id: "foo", + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: " ", + trailing: " = ", + }, + ), + conversion: Str, + format_spec: None, + }, + ), + ], + implicit_concatenated: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 116..132, + value: FString( + ExprFString { + range: 116..132, + values: [ + FormattedValue( + ExprFormattedValue { + range: 118..131, + value: Tuple( + ExprTuple { + range: 121..125, + elts: [ + Constant( + ExprConstant { + range: 121..122, + value: Int( + 1, + ), + kind: None, + }, + ), + Constant( + ExprConstant { + range: 124..125, + value: Int( + 2, + ), + kind: None, + }, + ), + ], + ctx: Load, + }, + ), + debug_text: Some( + DebugText { + leading: " ", + trailing: " = ", + }, + ), + conversion: None, + format_spec: None, + }, + ), + ], + implicit_concatenated: false, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 133..159, + value: FString( + ExprFString { + range: 133..159, + values: [ + FormattedValue( + ExprFormattedValue { + range: 135..158, + value: FString( + ExprFString { + range: 136..152, + values: [ + FormattedValue( + ExprFormattedValue { + range: 138..151, + value: Constant( + ExprConstant { + range: 139..145, + value: Float( + 3.1415, + ), + kind: None, + }, + ), + debug_text: Some( + DebugText { + leading: "", + trailing: "=", + }, + ), + conversion: None, + format_spec: Some( + FString( + ExprFString { + range: 147..150, + values: [ + Constant( + ExprConstant { + range: 147..150, + value: Str( + StringConstant { + value: ".1f", + implicit_concatenated: false, + }, + ), + kind: None, + }, + ), + ], + implicit_concatenated: false, + }, + ), + ), + }, + ), + ], + implicit_concatenated: false, + }, + ), + debug_text: None, + conversion: None, + format_spec: Some( + FString( + ExprFString { + range: 153..157, + values: [ + Constant( + ExprConstant { + range: 153..157, + value: Str( + StringConstant { + value: "*^20", + implicit_concatenated: false, + }, + ), + kind: None, + }, + ), + ], + implicit_concatenated: false, + }, + ), + ), + }, + ), + ], + implicit_concatenated: false, + }, + ), + }, + ), +] diff --git a/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings_with_unicode.snap b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings_with_unicode.snap new file mode 100644 index 00000000000000..878148a374c3ce --- /dev/null +++ b/crates/ruff_python_parser/src/snapshots/ruff_python_parser__parser__tests__fstrings_with_unicode.snap @@ -0,0 +1,220 @@ +--- +source: crates/ruff_python_parser/src/parser.rs +expression: parse_ast +--- +[ + Expr( + StmtExpr { + range: 0..29, + value: FString( + ExprFString { + range: 0..29, + values: [ + Constant( + ExprConstant { + range: 2..5, + value: Str( + StringConstant { + value: "foo", + implicit_concatenated: true, + }, + ), + kind: Some( + "u", + ), + }, + ), + FormattedValue( + ExprFormattedValue { + range: 9..14, + value: Name( + ExprName { + range: 10..13, + id: "bar", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Constant( + ExprConstant { + range: 17..28, + value: Str( + StringConstant { + value: "baz some", + implicit_concatenated: true, + }, + ), + kind: None, + }, + ), + ], + implicit_concatenated: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 30..59, + value: FString( + ExprFString { + range: 30..59, + values: [ + Constant( + ExprConstant { + range: 31..34, + value: Str( + StringConstant { + value: "foo", + implicit_concatenated: true, + }, + ), + kind: None, + }, + ), + FormattedValue( + ExprFormattedValue { + range: 38..43, + value: Name( + ExprName { + range: 39..42, + id: "bar", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Constant( + ExprConstant { + range: 47..58, + value: Str( + StringConstant { + value: "baz some", + implicit_concatenated: true, + }, + ), + kind: Some( + "u", + ), + }, + ), + ], + implicit_concatenated: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 60..89, + value: FString( + ExprFString { + range: 60..89, + values: [ + Constant( + ExprConstant { + range: 61..64, + value: Str( + StringConstant { + value: "foo", + implicit_concatenated: true, + }, + ), + kind: None, + }, + ), + FormattedValue( + ExprFormattedValue { + range: 68..73, + value: Name( + ExprName { + range: 69..72, + id: "bar", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Constant( + ExprConstant { + range: 76..88, + value: Str( + StringConstant { + value: "baz some", + implicit_concatenated: true, + }, + ), + kind: None, + }, + ), + ], + implicit_concatenated: true, + }, + ), + }, + ), + Expr( + StmtExpr { + range: 90..127, + value: FString( + ExprFString { + range: 90..127, + values: [ + Constant( + ExprConstant { + range: 92..103, + value: Str( + StringConstant { + value: "foobar ", + implicit_concatenated: true, + }, + ), + kind: Some( + "u", + ), + }, + ), + FormattedValue( + ExprFormattedValue { + range: 103..108, + value: Name( + ExprName { + range: 104..107, + id: "baz", + ctx: Load, + }, + ), + debug_text: None, + conversion: None, + format_spec: None, + }, + ), + Constant( + ExprConstant { + range: 108..126, + value: Str( + StringConstant { + value: " realybarno", + implicit_concatenated: true, + }, + ), + kind: None, + }, + ), + ], + implicit_concatenated: true, + }, + ), + }, + ), +]