libgraphql-macros 0.0.10

Macros provided by the `libgraphql` crate at `libgraphql::macros`.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
//! Tests for `RustMacroGraphQLTokenSource`.
//!
//! These tests verify that the token source correctly translates Rust proc-macro
//! tokens into GraphQL tokens per the GraphQL specification.
//!
//! See: https://spec.graphql.org/September2025/#sec-Lexical-Tokens
//!
//! ## Note on `quote!` vs string-based tests
//!
//! Most tests use `quote! { ... }` which provides compile-time Rust syntax
//! checking. However, `quote!` generates synthetic spans that don't preserve
//! accurate position information (all tokens report column 0). For tests that
//! require accurate position tracking (block strings, spaced dots, etc.),
//! we use `TokenStream::from_str()` which preserves real positions.

use crate::rust_macro_graphql_token_source::RustMacroGraphQLTokenSource;
use libgraphql_parser::token::GraphQLToken;
use libgraphql_parser::token::GraphQLTokenKind;
use libgraphql_parser::token::GraphQLTriviaToken;
use proc_macro2::TokenStream;
use quote::quote;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::str::FromStr;

/// Helper function to tokenize a GraphQL-like token stream and return the token
/// kinds.
///
/// Uses `'static` lifetime since `RustMacroGraphQLTokenSource` produces owned
/// strings (not borrowed from source).
fn tokenize(input: TokenStream) -> Vec<GraphQLTokenKind<'static>> {
    let span_map = Rc::new(RefCell::new(HashMap::new()));
    let source =
        RustMacroGraphQLTokenSource::new(input, span_map);
    source.map(|t| t.kind).collect()
}

/// Helper function to tokenize and return the full tokens (for span/trivia
/// testing).
///
/// Uses `'static` lifetime since `RustMacroGraphQLTokenSource` produces owned
/// strings.
fn tokenize_full(input: TokenStream) -> Vec<GraphQLToken<'static>> {
    let span_map = Rc::new(RefCell::new(HashMap::new()));
    let source =
        RustMacroGraphQLTokenSource::new(input, span_map);
    source.collect()
}

/// Helper to tokenize from a string (preserves accurate positions).
///
/// Uses `'static` lifetime since `RustMacroGraphQLTokenSource` produces owned
/// strings.
fn tokenize_str(input: &str) -> Vec<GraphQLTokenKind<'static>> {
    let stream = TokenStream::from_str(input)
        .expect("Failed to parse as Rust tokens");
    let span_map = Rc::new(RefCell::new(HashMap::new()));
    let source =
        RustMacroGraphQLTokenSource::new(stream, span_map);
    source.map(|t| t.kind).collect()
}

/// Helper to tokenize from a string and return full tokens
/// along with the populated span map for reverse-lookup of
/// synthetic byte offsets → `proc_macro2::Span`.
///
/// Uses `'static` lifetime since `RustMacroGraphQLTokenSource`
/// produces owned strings.
fn tokenize_str_full_with_span_map(
    input: &str,
) -> (Vec<GraphQLToken<'static>>, HashMap<u32, proc_macro2::Span>) {
    let stream = TokenStream::from_str(input)
        .expect("Failed to parse as Rust tokens");
    let span_map = Rc::new(RefCell::new(HashMap::new()));
    let source =
        RustMacroGraphQLTokenSource::new(stream, span_map.clone());
    let tokens: Vec<_> = source.collect();
    let map = Rc::try_unwrap(span_map)
        .expect("span_map Rc should have one ref")
        .into_inner();
    (tokens, map)
}

/// Tests that a simple GraphQL type definition produces the expected token
/// sequence.
///
/// This verifies basic tokenization of Names, punctuators, and Eof.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_simple_type_definition() {
    let kinds = tokenize(quote! { type Query { name: String } });

    // Expected: type, Query, {, name, :, String, }, Eof
    assert_eq!(kinds.len(), 8, "Expected 8 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::Name(n) if n == "type"));
    assert!(matches!(&kinds[1], GraphQLTokenKind::Name(n) if n == "Query"));
    assert!(matches!(&kinds[2], GraphQLTokenKind::CurlyBraceOpen));
    assert!(matches!(&kinds[3], GraphQLTokenKind::Name(n) if n == "name"));
    assert!(matches!(&kinds[4], GraphQLTokenKind::Colon));
    assert!(matches!(&kinds[5], GraphQLTokenKind::Name(n) if n == "String"));
    assert!(matches!(&kinds[6], GraphQLTokenKind::CurlyBraceClose));
    assert!(matches!(&kinds[7], GraphQLTokenKind::Eof));
}

/// Tests that commas are accumulated as trivia (attached to subsequent tokens)
/// and not emitted as separate tokens.
///
/// Per GraphQL spec, commas are optional and act as trivia.
/// See: https://spec.graphql.org/September2025/#sec-Punctuators
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_commas_as_trivia() {
    let tokens = tokenize_full(quote! { a, b, c });

    // Expected: a, b, c, Eof (4 tokens - commas are trivia)
    assert_eq!(tokens.len(), 4, "Expected 4 tokens (commas are trivia)");

    // First token has no preceding trivia
    assert!(tokens[0].preceding_trivia.is_empty());

    // Second token should have comma trivia
    assert_eq!(
        tokens[1].preceding_trivia.len(),
        1,
        "Second token should have comma trivia"
    );
    assert!(matches!(
        &tokens[1].preceding_trivia[0],
        GraphQLTriviaToken::Comma { .. }
    ));

    // Third token should have comma trivia
    assert_eq!(
        tokens[2].preceding_trivia.len(),
        1,
        "Third token should have comma trivia"
    );
    assert!(matches!(
        &tokens[2].preceding_trivia[0],
        GraphQLTriviaToken::Comma { .. }
    ));
}

/// Tests tokenization of integer literals.
///
/// GraphQL integers are signed 32-bit values.
/// See: https://spec.graphql.org/September2025/#sec-Int-Value
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_integer_literals() {
    let kinds = tokenize(quote! { 0 42 1000000 });

    assert_eq!(kinds.len(), 4, "Expected 4 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::IntValue(v) if v == "0"));
    assert!(matches!(&kinds[1], GraphQLTokenKind::IntValue(v) if v == "42"));
    assert!(matches!(&kinds[2], GraphQLTokenKind::IntValue(v) if v == "1000000"));
    assert!(matches!(&kinds[3], GraphQLTokenKind::Eof));
}

/// Tests tokenization of negative integer literals.
///
/// In GraphQL, `-17` is a valid IntValue. Rust tokenizes this as two tokens
/// (`-` and `17`), so we combine them into a single negative IntValue.
///
/// See: https://spec.graphql.org/September2025/#sec-Int-Value
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_negative_integer_literals() {
    let kinds = tokenize(quote! { 42 -17 -1 });

    assert_eq!(kinds.len(), 4, "Expected 4 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::IntValue(v) if v == "42"));
    assert!(matches!(&kinds[1], GraphQLTokenKind::IntValue(v) if v == "-17"));
    assert!(matches!(&kinds[2], GraphQLTokenKind::IntValue(v) if v == "-1"));
    assert!(matches!(&kinds[3], GraphQLTokenKind::Eof));
}

/// Tests tokenization of float literals.
///
/// GraphQL floats follow the JSON number format with optional exponents.
/// See: https://spec.graphql.org/September2025/#sec-Float-Value
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_float_literals() {
    let kinds = tokenize(quote! { 3.14 0.5 1e10 2.5e-3 });

    assert_eq!(kinds.len(), 5, "Expected 5 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::FloatValue(v) if v == "3.14"));
    assert!(matches!(&kinds[1], GraphQLTokenKind::FloatValue(v) if v == "0.5"));
    assert!(matches!(&kinds[2], GraphQLTokenKind::FloatValue(v) if v == "1e10"));
    assert!(matches!(&kinds[3], GraphQLTokenKind::FloatValue(v) if v == "2.5e-3"));
    assert!(matches!(&kinds[4], GraphQLTokenKind::Eof));
}

/// Tests tokenization of negative float literals.
///
/// Similar to integers, negative floats like `-3.14` are valid FloatValues.
///
/// See: https://spec.graphql.org/September2025/#sec-Float-Value
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_negative_float_literals() {
    let kinds = tokenize(quote! { 3.14 -2.718 -1e5 });

    assert_eq!(kinds.len(), 4, "Expected 4 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::FloatValue(v) if v == "3.14"));
    assert!(matches!(&kinds[1], GraphQLTokenKind::FloatValue(v) if v == "-2.718"));
    assert!(matches!(&kinds[2], GraphQLTokenKind::FloatValue(v) if v == "-1e5"));
    assert!(matches!(&kinds[3], GraphQLTokenKind::Eof));
}

/// Tests tokenization of string literals.
///
/// String values are stored as raw source text including quotes and escape
/// sequences. Processing (unescaping) happens via `cook_string_value()`.
///
/// See: https://spec.graphql.org/September2025/#sec-String-Value
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_string_literals() {
    let kinds = tokenize(quote! { "hello" "with\nescape" });

    assert_eq!(kinds.len(), 3, "Expected 3 tokens including Eof");

    // String literals stored as raw source text
    assert!(matches!(&kinds[0], GraphQLTokenKind::StringValue(v) if v == "\"hello\""));
    // Note: The backslash-n is stored literally as two characters (\ and n),
    // not as a newline character. The cook_string_value() method processes it.
    assert!(matches!(&kinds[1], GraphQLTokenKind::StringValue(v) if v == "\"with\\nescape\""));
    assert!(matches!(&kinds[2], GraphQLTokenKind::Eof));
}

/// Tests tokenization of boolean literals.
///
/// `true` and `false` are special keywords in GraphQL.
/// See: https://spec.graphql.org/September2025/#sec-Boolean-Value
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_boolean_literals() {
    let kinds = tokenize(quote! { true false });

    assert_eq!(kinds.len(), 3, "Expected 3 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::True));
    assert!(matches!(&kinds[1], GraphQLTokenKind::False));
    assert!(matches!(&kinds[2], GraphQLTokenKind::Eof));
}

/// Tests tokenization of the null literal.
///
/// See: https://spec.graphql.org/September2025/#sec-Null-Value
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_null_literal() {
    let kinds = tokenize(quote! { null });

    assert_eq!(kinds.len(), 2, "Expected 2 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::Null));
    assert!(matches!(&kinds[1], GraphQLTokenKind::Eof));
}

/// Tests tokenization of all GraphQL punctuators.
///
/// See: https://spec.graphql.org/September2025/#sec-Punctuators
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_punctuators() {
    let kinds = tokenize(quote! { ! $ & ( ) : = @ [ ] { | } });

    assert_eq!(kinds.len(), 14, "Expected 14 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::Bang));
    assert!(matches!(&kinds[1], GraphQLTokenKind::Dollar));
    assert!(matches!(&kinds[2], GraphQLTokenKind::Ampersand));
    assert!(matches!(&kinds[3], GraphQLTokenKind::ParenOpen));
    assert!(matches!(&kinds[4], GraphQLTokenKind::ParenClose));
    assert!(matches!(&kinds[5], GraphQLTokenKind::Colon));
    assert!(matches!(&kinds[6], GraphQLTokenKind::Equals));
    assert!(matches!(&kinds[7], GraphQLTokenKind::At));
    assert!(matches!(&kinds[8], GraphQLTokenKind::SquareBracketOpen));
    assert!(matches!(&kinds[9], GraphQLTokenKind::SquareBracketClose));
    assert!(matches!(&kinds[10], GraphQLTokenKind::CurlyBraceOpen));
    assert!(matches!(&kinds[11], GraphQLTokenKind::Pipe));
    assert!(matches!(&kinds[12], GraphQLTokenKind::CurlyBraceClose));
    assert!(matches!(&kinds[13], GraphQLTokenKind::Eof));
}

/// Tests tokenization of the ellipsis (`...`) spread operator.
///
/// The spread operator is a single punctuator, but Rust tokenizes it as three
/// separate `.` tokens. We combine adjacent `.` tokens into `...`.
///
/// See: https://spec.graphql.org/September2025/#sec-Punctuators
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_ellipsis() {
    let kinds = tokenize(quote! { ...Fragment });

    assert_eq!(kinds.len(), 3, "Expected 3 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::Ellipsis));
    assert!(matches!(&kinds[1], GraphQLTokenKind::Name(n) if n == "Fragment"));
    assert!(matches!(&kinds[2], GraphQLTokenKind::Eof));
}

/// Tests that two non-adjacent dots on the same line produce a terminal error
/// with a helpful message about the spread operator.
///
/// When the user writes `. .` with spaces on the same line, we emit a single
/// error covering both dots (since they can't become `...`), with a note
/// suggesting to remove the spacing.
///
/// Note: Uses `tokenize_str` because `quote!` doesn't preserve position info
/// needed to detect "same line but not adjacent" dots.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_spaced_dots_produce_error() {
    // Using string-based tokenization to preserve position information
    let kinds = tokenize_str(". .");

    // Should be: error (for `. .`), Eof
    assert_eq!(kinds.len(), 2, "Expected 2 tokens including Eof");

    assert!(matches!(
        &kinds[0],
        GraphQLTokenKind::Error(err)
            if err.message.contains("`. .`")
    ));
    assert!(matches!(&kinds[1], GraphQLTokenKind::Eof));
}

/// Tests that a single `.` produces an error.
///
/// GraphQL does not have a single dot punctuator - only `...`.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_single_dot_produces_error() {
    let kinds = tokenize(quote! { name . field });

    // Should be: name, error (for `.`), field, Eof
    assert_eq!(kinds.len(), 4, "Expected 4 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::Name(n) if n == "name"));
    assert!(matches!(
        &kinds[1],
        GraphQLTokenKind::Error(err)
            if err.message.contains("`.`")
    ));
    assert!(matches!(&kinds[2], GraphQLTokenKind::Name(n) if n == "field"));
    assert!(matches!(&kinds[3], GraphQLTokenKind::Eof));
}

/// Tests that two adjacent dots (`..`) produce an error.
///
/// Two dots are not valid GraphQL syntax - only `...` is valid.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_double_dot_produces_error() {
    let kinds = tokenize(quote! { name..field });

    // Should be: name, error (for `..`), field, Eof
    assert_eq!(kinds.len(), 4, "Expected 4 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::Name(n) if n == "name"));
    assert!(matches!(
        &kinds[1],
        GraphQLTokenKind::Error(err)
            if err.message.contains("`..`")
    ));
    assert!(matches!(&kinds[2], GraphQLTokenKind::Name(n) if n == "field"));
    assert!(matches!(&kinds[3], GraphQLTokenKind::Eof));
}

/// Tests that Rust raw strings produce an error with a helpful suggestion.
///
/// Raw strings (`r"..."`, `r#"..."#`) are Rust-specific syntax. The error
/// includes a suggestion for equivalent GraphQL string syntax.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_raw_string_produces_error() {
    let kinds = tokenize(quote! { r"raw content" });

    assert_eq!(kinds.len(), 2, "Expected 2 tokens including Eof");

    assert!(matches!(
        &kinds[0],
        GraphQLTokenKind::Error(err)
            if err.message.contains("raw string")
            && err.error_notes.len() == 1
            && err.error_notes[0].message.contains("Consider using:")
    ));
    assert!(matches!(&kinds[1], GraphQLTokenKind::Eof));
}

/// Tests that an unexpected minus sign (not followed by a number) produces an
/// error.
///
/// The `-` character is only valid when immediately followed by a number.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_standalone_minus_produces_error() {
    let kinds = tokenize(quote! { a - b });

    // Should be: a, error (for `-`), b, Eof
    assert_eq!(kinds.len(), 4, "Expected 4 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::Name(n) if n == "a"));
    assert!(matches!(
        &kinds[1],
        GraphQLTokenKind::Error(err)
            if err.message.contains("`-`")
    ));
    assert!(matches!(&kinds[2], GraphQLTokenKind::Name(n) if n == "b"));
    assert!(matches!(&kinds[3], GraphQLTokenKind::Eof));
}

/// Tests that an unexpected punctuation character produces an error.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_unexpected_punct_produces_error() {
    let kinds = tokenize(quote! { a % b });

    // Should be: a, error (for `%`), b, Eof
    assert_eq!(kinds.len(), 4, "Expected 4 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::Name(n) if n == "a"));
    assert!(matches!(
        &kinds[1],
        GraphQLTokenKind::Error(err)
            if err.message.contains("`%`")
    ));
    assert!(matches!(&kinds[2], GraphQLTokenKind::Name(n) if n == "b"));
    assert!(matches!(&kinds[3], GraphQLTokenKind::Eof));
}

/// Tests tokenization of a complete GraphQL query with variables.
///
/// This is an integration test to verify multiple token types work together.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_complete_query() {
    let kinds = tokenize(quote! { query GetUser($id: ID!) { user(id: $id) { name } } });

    // Count expected tokens:
    // query, GetUser, (, $, id, :, ID, !, ), {, user, (, id, :, $, id, ), {,
    // name, }, }, Eof
    assert_eq!(kinds.len(), 22, "Expected 22 tokens including Eof");

    // Verify key tokens
    assert!(matches!(&kinds[0], GraphQLTokenKind::Name(n) if n == "query"));
    assert!(matches!(&kinds[1], GraphQLTokenKind::Name(n) if n == "GetUser"));
    assert!(matches!(&kinds[2], GraphQLTokenKind::ParenOpen));
    assert!(matches!(&kinds[3], GraphQLTokenKind::Dollar));
    assert!(matches!(&kinds[4], GraphQLTokenKind::Name(n) if n == "id"));
    assert!(matches!(&kinds[6], GraphQLTokenKind::Name(n) if n == "ID"));
    assert!(matches!(&kinds[7], GraphQLTokenKind::Bang));
    assert!(matches!(&kinds[21], GraphQLTokenKind::Eof));
}

/// Tests that block strings are correctly detected and combined.
///
/// Rust tokenizes `"""content"""` as three separate string literals. We detect
/// when they are adjacent and combine them.
///
/// Note: Uses `tokenize_str` because `quote!` produces non-adjacent spans for
/// the three string literals, preventing block string detection.
///
/// See: https://spec.graphql.org/September2025/#sec-String-Value
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_block_string() {
    // Using string-based tokenization to preserve adjacent spans
    let kinds = tokenize_str(r#""""block content""""#);

    // Should be: StringValue (block), Eof
    assert_eq!(kinds.len(), 2, "Expected 2 tokens including Eof");

    assert!(matches!(
        &kinds[0],
        GraphQLTokenKind::StringValue(v) if v == "\"\"\"block content\"\"\""
    ));
    assert!(matches!(&kinds[1], GraphQLTokenKind::Eof));
}

/// Tests that non-adjacent empty strings are NOT combined into a block string.
///
/// `"" "content" ""` with spaces should remain as three separate strings.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_spaced_quotes_not_block_string() {
    let kinds = tokenize(quote! { "" "content" "" });

    // Should be: "", "content", "", Eof (4 tokens)
    assert_eq!(kinds.len(), 4, "Expected 4 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::StringValue(v) if v == "\"\""));
    assert!(matches!(&kinds[1], GraphQLTokenKind::StringValue(v) if v == "\"content\""));
    assert!(matches!(&kinds[2], GraphQLTokenKind::StringValue(v) if v == "\"\""));
    assert!(matches!(&kinds[3], GraphQLTokenKind::Eof));
}

/// Tests that the Eof token receives any trailing trivia (commas).
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_trailing_comma_attached_to_eof() {
    let tokens = tokenize_full(quote! { a, });

    // Should be: a, Eof (2 tokens)
    assert_eq!(tokens.len(), 2, "Expected 2 tokens including Eof");

    // The Eof token should have the trailing comma as trivia
    assert!(matches!(&tokens[1].kind, GraphQLTokenKind::Eof));
    assert_eq!(
        tokens[1].preceding_trivia.len(),
        1,
        "Eof should have trailing comma as trivia"
    );
    assert!(matches!(
        &tokens[1].preceding_trivia[0],
        GraphQLTriviaToken::Comma { .. }
    ));
}

/// Tests that multiple consecutive commas are all accumulated
/// as trivia on the next non-trivia token.
///
/// Per GraphQL spec, commas are "ignored tokens" equivalent to
/// whitespace, so `a,,, b` is identical to `a b`. All three
/// commas should appear as preceding trivia on the `b` token.
///
/// See: https://spec.graphql.org/September2025/#sec-Insignificant-Commas
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_multiple_consecutive_commas_as_trivia() {
    let tokens = tokenize_full(quote! { a,,, b });

    // Expected: a, b, Eof (3 tokens — commas are trivia)
    assert_eq!(
        tokens.len(),
        3,
        "Expected 3 tokens (commas are trivia)",
    );

    assert!(matches!(
        &tokens[0].kind,
        GraphQLTokenKind::Name(n) if n == "a",
    ));
    assert!(tokens[0].preceding_trivia.is_empty());

    assert!(matches!(
        &tokens[1].kind,
        GraphQLTokenKind::Name(n) if n == "b",
    ));
    assert_eq!(
        tokens[1].preceding_trivia.len(),
        3,
        "Second token should have 3 comma trivia items",
    );
    for trivia in &tokens[1].preceding_trivia {
        assert!(
            matches!(trivia, GraphQLTriviaToken::Comma { .. }),
            "All trivia should be Comma, got: {trivia:?}",
        );
    }

    assert!(matches!(&tokens[2].kind, GraphQLTokenKind::Eof));
    assert!(tokens[2].preceding_trivia.is_empty());
}

/// Tests that multiple trailing commas at end of input are all
/// accumulated as trivia on the Eof token.
///
/// See: https://spec.graphql.org/September2025/#sec-Insignificant-Commas
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_multiple_trailing_commas_on_eof() {
    let tokens = tokenize_full(quote! { a,,, });

    // Expected: a, Eof (2 tokens)
    assert_eq!(
        tokens.len(),
        2,
        "Expected 2 tokens (commas are trivia)",
    );

    assert!(matches!(
        &tokens[0].kind,
        GraphQLTokenKind::Name(n) if n == "a",
    ));
    assert!(tokens[0].preceding_trivia.is_empty());

    assert!(matches!(&tokens[1].kind, GraphQLTokenKind::Eof));
    assert_eq!(
        tokens[1].preceding_trivia.len(),
        3,
        "Eof should have 3 trailing commas as trivia",
    );
    for trivia in &tokens[1].preceding_trivia {
        assert!(
            matches!(trivia, GraphQLTriviaToken::Comma { .. }),
            "All trivia should be Comma, got: {trivia:?}",
        );
    }
}

/// Tests that comma trivia tokens carry accurate span positions.
///
/// Uses `tokenize_str_full_with_span_map` to get real position
/// info by reverse-looking-up synthetic byte offsets through
/// the SpanMap → `proc_macro2::Span` → `LineColumn`. In `a, b`,
/// the comma at column 1 should map back to column 1.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_comma_trivia_span_positions() {
    let (tokens, span_map) =
        tokenize_str_full_with_span_map("a, b");

    assert_eq!(
        tokens.len(),
        3,
        "Expected 3 tokens (a, b, Eof)",
    );

    // `a` at column 0
    assert!(matches!(
        &tokens[0].kind,
        GraphQLTokenKind::Name(n) if n == "a",
    ));
    let a_span = span_map.get(&tokens[0].span.start)
        .expect("span_map should have entry for `a`");
    assert_eq!(a_span.start().column, 0);

    // `b` at column 3, with 1 comma trivia
    assert!(matches!(
        &tokens[1].kind,
        GraphQLTokenKind::Name(n) if n == "b",
    ));
    let b_span = span_map.get(&tokens[1].span.start)
        .expect("span_map should have entry for `b`");
    assert_eq!(b_span.start().column, 3);
    assert_eq!(tokens[1].preceding_trivia.len(), 1);

    if let GraphQLTriviaToken::Comma { span } =
        &tokens[1].preceding_trivia[0]
    {
        // Comma is at column 1
        let comma_span = span_map.get(&span.start)
            .expect("span_map should have entry for comma");
        assert_eq!(
            comma_span.start().column,
            1,
            "Comma trivia should be at column 1",
        );
    } else {
        panic!("Expected Comma trivia");
    }
}

/// Tests that commas between items inside different delimiter
/// groups (parentheses, brackets, braces) are all captured as
/// trivia regardless of nesting.
///
/// This verifies that the trivia accumulation works correctly
/// as `RustMacroGraphQLTokenSource` descends into Rust `Group`
/// tokens.
///
/// See: https://spec.graphql.org/September2025/#sec-Insignificant-Commas
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_comma_trivia_across_delimiter_groups() {
    let tokens = tokenize_full(quote! {
        query {
            users(limit: 10, offset: 0) {
                name,
                email,
            }
        }
    });

    // Collect just Name tokens and check their trivia
    let name_tokens: Vec<_> = tokens
        .iter()
        .filter(|t| matches!(&t.kind, GraphQLTokenKind::Name(_)))
        .collect();

    // "offset" follows a comma after "10"
    let offset_token = name_tokens
        .iter()
        .find(|t| matches!(
            &t.kind,
            GraphQLTokenKind::Name(n) if n == "offset",
        ))
        .expect("Should have 'offset' token");
    assert_eq!(
        offset_token.preceding_trivia.len(),
        1,
        "'offset' should have 1 comma trivia",
    );
    assert!(matches!(
        &offset_token.preceding_trivia[0],
        GraphQLTriviaToken::Comma { .. },
    ));

    // "email" follows a comma after "name"
    let email_token = name_tokens
        .iter()
        .find(|t| matches!(
            &t.kind,
            GraphQLTokenKind::Name(n) if n == "email",
        ))
        .expect("Should have 'email' token");
    assert_eq!(
        email_token.preceding_trivia.len(),
        1,
        "'email' should have 1 comma trivia",
    );
    assert!(matches!(
        &email_token.preceding_trivia[0],
        GraphQLTriviaToken::Comma { .. },
    ));
}

/// Tests an empty input produces only an Eof token.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_empty_input() {
    let kinds = tokenize(quote! {});

    assert_eq!(kinds.len(), 1, "Expected 1 token (Eof)");
    assert!(matches!(&kinds[0], GraphQLTokenKind::Eof));
}

/// Tests position tracking across tokens by reverse-looking-up
/// synthetic byte offsets through the SpanMap to recover the
/// original `proc_macro2::Span` positions.
///
/// Uses `tokenize_str_full_with_span_map` because `quote!`
/// reports all tokens at column 0, preventing accurate position
/// verification.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_position_tracking() {
    let (tokens, span_map) =
        tokenize_str_full_with_span_map("type Query");

    assert_eq!(tokens.len(), 3, "Expected 3 tokens including Eof");

    let type_token = &tokens[0];
    let query_token = &tokens[1];

    let type_span = span_map.get(&type_token.span.start)
        .expect("span_map should have entry for `type`");
    let query_span = span_map.get(&query_token.span.start)
        .expect("span_map should have entry for `Query`");

    // proc_macro2 uses 1-based lines
    assert_eq!(type_span.start().line, 1);
    assert_eq!(query_span.start().line, 1);

    // "type" starts at column 0, "Query" starts at column 5
    assert_eq!(type_span.start().column, 0);
    assert_eq!(query_span.start().column, 5);
}

/// Tests that directive usage with arguments tokenizes correctly.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_directive_with_arguments() {
    let kinds = tokenize(quote! { @deprecated(reason: "old") });

    // @, deprecated, (, reason, :, "old", ), Eof
    assert_eq!(kinds.len(), 8, "Expected 8 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::At));
    assert!(matches!(&kinds[1], GraphQLTokenKind::Name(n) if n == "deprecated"));
    assert!(matches!(&kinds[2], GraphQLTokenKind::ParenOpen));
    assert!(matches!(&kinds[3], GraphQLTokenKind::Name(n) if n == "reason"));
    assert!(matches!(&kinds[4], GraphQLTokenKind::Colon));
    assert!(matches!(&kinds[5], GraphQLTokenKind::StringValue(v) if v == "\"old\""));
    assert!(matches!(&kinds[6], GraphQLTokenKind::ParenClose));
    assert!(matches!(&kinds[7], GraphQLTokenKind::Eof));
}

/// Tests array syntax with various value types.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_array_values() {
    let kinds = tokenize(quote! { [1, 2, 3] });

    // [, 1, 2, 3, ], Eof (commas are trivia)
    assert_eq!(kinds.len(), 6, "Expected 6 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::SquareBracketOpen));
    assert!(matches!(&kinds[1], GraphQLTokenKind::IntValue(v) if v == "1"));
    assert!(matches!(&kinds[2], GraphQLTokenKind::IntValue(v) if v == "2"));
    assert!(matches!(&kinds[3], GraphQLTokenKind::IntValue(v) if v == "3"));
    assert!(matches!(&kinds[4], GraphQLTokenKind::SquareBracketClose));
    assert!(matches!(&kinds[5], GraphQLTokenKind::Eof));
}

/// Tests union type syntax with pipes.
///
/// Written by Claude Code, reviewed by a human.
#[test]
fn test_union_type() {
    let kinds = tokenize(quote! { union SearchResult = User | Post | Comment });

    // union, SearchResult, =, User, |, Post, |, Comment, Eof
    assert_eq!(kinds.len(), 9, "Expected 9 tokens including Eof");

    assert!(matches!(&kinds[0], GraphQLTokenKind::Name(n) if n == "union"));
    assert!(matches!(&kinds[1], GraphQLTokenKind::Name(n) if n == "SearchResult"));
    assert!(matches!(&kinds[2], GraphQLTokenKind::Equals));
    assert!(matches!(&kinds[3], GraphQLTokenKind::Name(n) if n == "User"));
    assert!(matches!(&kinds[4], GraphQLTokenKind::Pipe));
    assert!(matches!(&kinds[5], GraphQLTokenKind::Name(n) if n == "Post"));
    assert!(matches!(&kinds[6], GraphQLTokenKind::Pipe));
    assert!(matches!(&kinds[7], GraphQLTokenKind::Name(n) if n == "Comment"));
    assert!(matches!(&kinds[8], GraphQLTokenKind::Eof));
}