summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_expand/src/tokenstream
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /compiler/rustc_expand/src/tokenstream
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--compiler/rustc_expand/src/tokenstream/tests.rs110
1 files changed, 110 insertions, 0 deletions
diff --git a/compiler/rustc_expand/src/tokenstream/tests.rs b/compiler/rustc_expand/src/tokenstream/tests.rs
new file mode 100644
index 000000000..eed696810
--- /dev/null
+++ b/compiler/rustc_expand/src/tokenstream/tests.rs
@@ -0,0 +1,110 @@
+use crate::tests::string_to_stream;
+
+use rustc_ast::token;
+use rustc_ast::tokenstream::{TokenStream, TokenStreamBuilder};
+use rustc_span::create_default_session_globals_then;
+use rustc_span::{BytePos, Span, Symbol};
+
+fn string_to_ts(string: &str) -> TokenStream {
+ string_to_stream(string.to_owned())
+}
+
+fn sp(a: u32, b: u32) -> Span {
+ Span::with_root_ctxt(BytePos(a), BytePos(b))
+}
+
+#[test]
+fn test_concat() {
+ create_default_session_globals_then(|| {
+ let test_res = string_to_ts("foo::bar::baz");
+ let test_fst = string_to_ts("foo::bar");
+ let test_snd = string_to_ts("::baz");
+ let mut builder = TokenStreamBuilder::new();
+ builder.push(test_fst);
+ builder.push(test_snd);
+ let eq_res = builder.build();
+ assert_eq!(test_res.trees().count(), 5);
+ assert_eq!(eq_res.trees().count(), 5);
+ assert_eq!(test_res.eq_unspanned(&eq_res), true);
+ })
+}
+
+#[test]
+fn test_to_from_bijection() {
+ create_default_session_globals_then(|| {
+ let test_start = string_to_ts("foo::bar(baz)");
+ let test_end = test_start.trees().cloned().collect();
+ assert_eq!(test_start, test_end)
+ })
+}
+
+#[test]
+fn test_eq_0() {
+ create_default_session_globals_then(|| {
+ let test_res = string_to_ts("foo");
+ let test_eqs = string_to_ts("foo");
+ assert_eq!(test_res, test_eqs)
+ })
+}
+
+#[test]
+fn test_eq_1() {
+ create_default_session_globals_then(|| {
+ let test_res = string_to_ts("::bar::baz");
+ let test_eqs = string_to_ts("::bar::baz");
+ assert_eq!(test_res, test_eqs)
+ })
+}
+
+#[test]
+fn test_eq_3() {
+ create_default_session_globals_then(|| {
+ let test_res = string_to_ts("");
+ let test_eqs = string_to_ts("");
+ assert_eq!(test_res, test_eqs)
+ })
+}
+
+#[test]
+fn test_diseq_0() {
+ create_default_session_globals_then(|| {
+ let test_res = string_to_ts("::bar::baz");
+ let test_eqs = string_to_ts("bar::baz");
+ assert_eq!(test_res == test_eqs, false)
+ })
+}
+
+#[test]
+fn test_diseq_1() {
+ create_default_session_globals_then(|| {
+ let test_res = string_to_ts("(bar,baz)");
+ let test_eqs = string_to_ts("bar,baz");
+ assert_eq!(test_res == test_eqs, false)
+ })
+}
+
+#[test]
+fn test_is_empty() {
+ create_default_session_globals_then(|| {
+ let test0 = TokenStream::default();
+ let test1 = TokenStream::token_alone(token::Ident(Symbol::intern("a"), false), sp(0, 1));
+ let test2 = string_to_ts("foo(bar::baz)");
+
+ assert_eq!(test0.is_empty(), true);
+ assert_eq!(test1.is_empty(), false);
+ assert_eq!(test2.is_empty(), false);
+ })
+}
+
+#[test]
+fn test_dotdotdot() {
+ create_default_session_globals_then(|| {
+ let mut builder = TokenStreamBuilder::new();
+ builder.push(TokenStream::token_joint(token::Dot, sp(0, 1)));
+ builder.push(TokenStream::token_joint(token::Dot, sp(1, 2)));
+ builder.push(TokenStream::token_alone(token::Dot, sp(2, 3)));
+ let stream = builder.build();
+ assert!(stream.eq_unspanned(&string_to_ts("...")));
+ assert_eq!(stream.trees().count(), 1);
+ })
+}