Skip to content
This repository was archived by the owner on Jul 27, 2023. It is now read-only.

Commit 71c2aa9

Browse files
committed
Test Magic command token in assignment value
1 parent 1f98fe2 commit 71c2aa9

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

parser/src/lexer.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,85 @@ if True:
16981698
)
16991699
}
17001700

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+
17011780
#[test]
17021781
fn test_numbers() {
17031782
let source = "0x2f 0o12 0b1101 0 123 123_45_67_890 0.2 1e+2 2.1e3 2j 2.2j";

0 commit comments

Comments
 (0)