@@ -1698,6 +1698,85 @@ if True:
1698
1698
)
1699
1699
}
1700
1700
1701
+ #[ test]
1702
+ fn test_jupyter_magic_assignment ( ) {
1703
+ let source = r"
1704
+ pwd = !pwd
1705
+ foo = %timeit a = b
1706
+ bar = %timeit a % 3
1707
+ baz = %matplotlib \
1708
+ inline"
1709
+ . trim ( ) ;
1710
+ let tokens = lex_jupyter_source ( source) ;
1711
+ assert_eq ! (
1712
+ tokens,
1713
+ vec![
1714
+ Tok :: Name {
1715
+ name: "pwd" . to_string( )
1716
+ } ,
1717
+ Tok :: Equal ,
1718
+ Tok :: MagicCommand {
1719
+ value: "pwd" . to_string( ) ,
1720
+ kind: MagicKind :: Shell ,
1721
+ } ,
1722
+ Tok :: Newline ,
1723
+ Tok :: Name {
1724
+ name: "foo" . to_string( )
1725
+ } ,
1726
+ Tok :: Equal ,
1727
+ Tok :: MagicCommand {
1728
+ value: "timeit a = b" . to_string( ) ,
1729
+ kind: MagicKind :: Magic ,
1730
+ } ,
1731
+ Tok :: Newline ,
1732
+ Tok :: Name {
1733
+ name: "bar" . to_string( )
1734
+ } ,
1735
+ Tok :: Equal ,
1736
+ Tok :: MagicCommand {
1737
+ value: "timeit a % 3" . to_string( ) ,
1738
+ kind: MagicKind :: Magic ,
1739
+ } ,
1740
+ Tok :: Newline ,
1741
+ Tok :: Name {
1742
+ name: "baz" . to_string( )
1743
+ } ,
1744
+ Tok :: Equal ,
1745
+ Tok :: MagicCommand {
1746
+ value: "matplotlib inline" . to_string( ) ,
1747
+ kind: MagicKind :: Magic ,
1748
+ } ,
1749
+ Tok :: Newline ,
1750
+ ]
1751
+ )
1752
+ }
1753
+
1754
+ fn assert_no_jupyter_magic ( tokens : & [ Tok ] ) {
1755
+ for tok in tokens {
1756
+ match tok {
1757
+ Tok :: MagicCommand { .. } => panic ! ( "Unexpected magic command token: {:?}" , tok) ,
1758
+ _ => { }
1759
+ }
1760
+ }
1761
+ }
1762
+
1763
+ #[ test]
1764
+ fn test_jupyter_magic_not_an_assignment ( ) {
1765
+ let source = r"
1766
+ # Other magic kinds are not valid here (can't test `foo = ?str` because '?' is not a valid token)
1767
+ foo = /func
1768
+ foo = ;func
1769
+ foo = ,func
1770
+
1771
+ (foo == %timeit a = b)
1772
+ (foo := %timeit a = b)
1773
+ def f(arg=%timeit a = b):
1774
+ pass"
1775
+ . trim ( ) ;
1776
+ let tokens = lex_jupyter_source ( source) ;
1777
+ assert_no_jupyter_magic ( & tokens) ;
1778
+ }
1779
+
1701
1780
#[ test]
1702
1781
fn test_numbers ( ) {
1703
1782
let source = "0x2f 0o12 0b1101 0 123 123_45_67_890 0.2 1e+2 2.1e3 2j 2.2j" ;
0 commit comments