vb6parse 1.0.1

vb6parse is a library for parsing and analyzing VB6 code, from projects, to controls, to modules, and forms.
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
/// This module is used by the `project` module to build error messages when parsing a project file.
///
use crate::errors::{DiagnosticLabel, ParserContext, ProjectError};
use crate::io::SourceStream;

use std::fmt::Debug;
use std::marker::PhantomData;

use strum::{EnumMessage, IntoEnumIterator};

/// Formats all valid values for an enum type as a string.
///
/// Returns a comma-separated list of valid enum values in the format:
/// ```text
/// 'numeric value' "message"
/// ```
/// for each variant, with the final variant
/// being appended with:
/// ```text
/// ", and 'numeric value' "message"
/// ```
/// This makes it slightly nicer to read.
///
/// Long live the Oxford comma!
///
/// # Example
/// For an enum with values 0, 1, 2 this should return:
/// `'0' "No Compatibility", '1' "Project Compatibility", and '2' "Compatible Exe Mode"`
pub fn format_valid_enum_values<T>() -> String
where
    T: IntoEnumIterator + EnumMessage + Debug + Into<i16> + Copy,
{
    match T::iter()
        .map(|v| {
            let numeric: i16 = v.into();
            format!("'{:?}' {:#?}", numeric, v.get_message().unwrap_or(""))
        })
        .collect::<Vec<_>>()
        .split_last()
    {
        Some((last, elements)) => {
            format!("{}, and {}", elements.join(", "), last)
        } // we shoiuld never get a 'None' here since all
        // the enums should have multiple variants with values, but...
        None => String::new(),
    }
}

/// Represents different kinds of parameter parsing errors.
pub enum ParameterErrorKind<'a, T> {
    /// Missing value at EOF for a required parameter
    MissingValueEof,
    /// Missing value at EOF for an optional parameter
    OptionalMissingValueEof,
    /// Missing value at EOF for a parameter with a default value
    MissingValueEofWithDefault(PhantomData<T>),
    /// Missing opening quote
    MissingOpeningQuote { value: &'a str },
    /// Missing closing quote
    MissingClosingQuote { value: &'a str },
    /// Missing both value and closing quote (only has opening quote)
    MissingValueAndClosingQuote {
        value: &'a str,
        _phantom: PhantomData<T>,
    },
    /// Missing both quotes with default
    MissingQuotesWithDefault {
        value: &'a str,
        _phantom: PhantomData<T>,
    },
    /// Invalid value for enum
    InvalidValue {
        value: &'a str,
        _phantom: PhantomData<T>,
    },
    /// Missing value and quotes
    MissingValueAndQuotes(PhantomData<T>),
    /// Empty parameter value
    EmptyValue,
    /// Missing both quotes (without default)
    MissingBothQuotes,
    /// Property name not found (no '=' delimiter)
    PropertyNameNotFound,
    /// Unterminated section header (missing closing ']')
    UnterminatedSectionHeader { value: &'a str },
}

/// Reports a parameter error based on the error kind.
///
/// This consolidated function replaces multiple similar error reporting functions
/// by using an enum to determine which specific error to report.
pub fn report_parameter_error<'a, T>(
    ctx: &mut ParserContext<'a>,
    input: &SourceStream<'a>,
    line_type: &'a str,
    parameter_start: usize,
    kind: &ParameterErrorKind<'a, T>,
) where
    T: TryFrom<&'a str, Error = String>
        + IntoEnumIterator
        + EnumMessage
        + Debug
        + Into<i16>
        + Default
        + Copy,
{
    match kind {
        ParameterErrorKind::MissingValueEof => {
            let value_span = input.span_range(parameter_start - 1, parameter_start);
            ctx.error(
                value_span,
                ProjectError::ParameterValueNotFound {
                    parameter_line_name: line_type.to_string(),
                },
            );
        }
        ParameterErrorKind::OptionalMissingValueEof => {
            let value_span = input.span_range(parameter_start - 1, parameter_start);
            let valid_value_message = "Text string values are valid here as well as !None!, (None), !(None)!, \"(None)\", \"!None!\", or \"!(None)!\" to indicate no value is selected.".to_string();
            ctx.error_with(
                value_span,
                ProjectError::ParameterWithDefaultValueNotFoundEOF {
                    parameter_line_name: line_type.to_string(),
                    valid_value_message,
                },
            )
            .with_label(DiagnosticLabel::new(
                value_span,
                format!("'{line_type}' must have a double quoted value and end with a newline."),
            ))
            .with_note(format!("{line_type}=\"!None!\""))
            .emit(ctx);
        }
        ParameterErrorKind::MissingValueEofWithDefault(_) => {
            let value_span = input.span_range(parameter_start - 1, parameter_start);
            let valid_value_message = format_valid_enum_values::<T>();
            ctx.error_with(
                value_span,
                ProjectError::ParameterWithDefaultValueNotFoundEOF {
                    parameter_line_name: line_type.to_string(),
                    valid_value_message,
                },
            )
            .with_label(DiagnosticLabel::new(
                value_span,
                format!("'{line_type}' must have a double qouted value and end with a newline."),
            ))
            .with_note(format!("{line_type}=\"{}\"", T::default().into()))
            .emit(ctx);
        }
        ParameterErrorKind::MissingOpeningQuote { value } => {
            let value_span = input.span_range(parameter_start, parameter_start + value.len());
            ctx.error_with(
                value_span,
                ProjectError::ParameterValueMissingOpeningQuote {
                    parameter_line_name: line_type.to_string(),
                },
            )
            .with_label(DiagnosticLabel::new(
                value_span,
                format!("'{line_type}' value must be surrounded by double quotes."),
            ))
            .with_note(format!("{line_type}=\"{value}"))
            .emit(ctx);
        }
        ParameterErrorKind::MissingClosingQuote { value } => {
            let value_span = input.span_range(parameter_start, parameter_start + value.len());
            ctx.error_with(
                value_span,
                ProjectError::ParameterValueMissingClosingQuote {
                    parameter_line_name: line_type.to_string(),
                },
            )
            .with_label(DiagnosticLabel::new(
                value_span,
                format!("'{line_type}' value must be surrounded by double quotes."),
            ))
            .with_note(format!("{line_type}={value}\""))
            .emit(ctx);
        }
        ParameterErrorKind::MissingValueAndClosingQuote { value, .. } => {
            let value_span = input.span_range(parameter_start, parameter_start + value.len());
            let valid_value_message = format_valid_enum_values::<T>();
            let default_value = T::default().into();
            let note_message = format!("{line_type}=\"{default_value}\"");

            ctx.error_with(
                value_span,
                ProjectError::ParameterValueMissingClosingQuoteAndValue {
                    parameter_line_name: line_type.to_string(),
                    valid_value_message,
                },
            )
            .with_label(DiagnosticLabel::new(
                value_span,
                format!("'{line_type}' value must be surrounded by double quotes."),
            ))
            .with_note(note_message)
            .emit(ctx);
        }
        ParameterErrorKind::MissingQuotesWithDefault { value, .. } => {
            let valid_value_message = format_valid_enum_values::<T>();
            let note_message = if T::try_from(value).is_ok() {
                format!("{line_type}=\"{value}\"")
            } else {
                let default_value = T::default().into();
                format!("{line_type}=\"{default_value}\"")
            };

            let value_span = input.span_at(parameter_start);
            ctx.error_with(
                value_span,
                ProjectError::ParameterValueMissingQuotes {
                    parameter_line_name: line_type.to_string(),
                    valid_value_message,
                },
            )
            .with_label(DiagnosticLabel::new(
                value_span,
                format!("'{line_type}' value must be contained within double qoutes."),
            ))
            .with_note(note_message)
            .emit(ctx);
        }
        ParameterErrorKind::InvalidValue { value, .. } => {
            let valid_value_message = format_valid_enum_values::<T>();
            let value_span = input.span_at(parameter_start + 1);
            ctx.error_with(
                value_span,
                ProjectError::ParameterValueInvalid {
                    parameter_line_name: line_type.to_string(),
                    invalid_value: value.to_string(),
                    valid_value_message,
                },
            )
            .with_label(DiagnosticLabel::new(value_span, "invalid value"))
            .with_note("Change the quoted value to one of the valid values.")
            .emit(ctx);
        }
        ParameterErrorKind::MissingValueAndQuotes(_) => {
            let valid_value_message = format_valid_enum_values::<T>();
            let default_value = T::default().into();
            let note_message = format!("{line_type}=\"{default_value}\"");

            let value_span = input.span_at(parameter_start);
            ctx.error_with(
                value_span,
                ProjectError::ParameterWithDefaultValueNotFound {
                    parameter_line_name: line_type.to_string(),
                    valid_value_message,
                },
            )
            .with_label(DiagnosticLabel::new(
                value_span,
                format!(
                    "'{line_type}' value must be one of the valid values contained within double qoutes."
                ),
            ))
            .with_note(note_message)
            .emit(ctx);
        }
        ParameterErrorKind::EmptyValue => {
            let value_span = input.span_at(parameter_start);
            ctx.error(
                value_span,
                ProjectError::ParameterValueNotFound {
                    parameter_line_name: line_type.to_string(),
                },
            );
        }
        ParameterErrorKind::MissingBothQuotes => {
            let value_span = input.span_at(parameter_start);
            ctx.error(
                value_span,
                ProjectError::ParameterWithoutDefaultValueMissingQuotes {
                    parameter_line_name: line_type.to_string(),
                },
            );
        }
        ParameterErrorKind::PropertyNameNotFound => {
            let value_span = input.span_at(parameter_start);
            let end_of_line = input.end_of_line();
            let end_span = input.span_range(parameter_start, end_of_line);
            ctx.error_with(value_span, ProjectError::PropertyNameNotFound)
                .with_label(DiagnosticLabel::new(
                    end_span,
                    "'=' and related value missing.",
                ))
                .emit(ctx);
        }
        ParameterErrorKind::UnterminatedSectionHeader { value } => {
            let value_span = input.span_at(parameter_start);
            let end_offset = parameter_start + value.len();
            let end_span = input.span_range(end_offset, end_offset + 1);

            ctx.error_with(value_span, ProjectError::UnterminatedSectionHeader)
                .with_label(DiagnosticLabel::new(
                    end_span,
                    "section header must be terminated with ']'",
                ))
                .with_note(format!("[{value}]"))
                .emit(ctx);
        }
    }
}

// Helper dummy type for non-generic error functions
#[derive(Debug, Copy, Clone)]
pub struct DummyEnumType;

impl Default for DummyEnumType {
    fn default() -> Self {
        DummyEnumType
    }
}

impl From<DummyEnumType> for i16 {
    fn from(_val: DummyEnumType) -> Self {
        0
    }
}

impl IntoEnumIterator for DummyEnumType {
    type Iterator = std::iter::Empty<Self>;
    fn iter() -> Self::Iterator {
        std::iter::empty()
    }
}

impl EnumMessage for DummyEnumType {
    fn get_message(&self) -> Option<&'static str> {
        None
    }
    fn get_detailed_message(&self) -> Option<&'static str> {
        None
    }
    fn get_documentation(&self) -> Option<&'static str> {
        None
    }
    fn get_serializations(&self) -> &'static [&'static str] {
        &[]
    }
}

impl<'a> TryFrom<&'a str> for DummyEnumType {
    type Error = String;
    fn try_from(_: &'a str) -> Result<Self, Self::Error> {
        Ok(DummyEnumType)
    }
}

#[cfg(test)]
mod tests {
    use crate::errors::{ErrorKind, ParserContext, ProjectError, Severity};
    use crate::files::project::properties::*;
    use crate::io::{Comparator, SourceStream};
    use assert_matches::assert_matches;

    #[test]
    fn no_optional_value_eof() {
        use crate::files::project::parse_optional_quoted_value;
        use crate::io::{Comparator, SourceStream};

        let mut input = SourceStream::new("", "Startup=");

        let parameter_name = input
            .take("Startup", Comparator::CaseSensitive)
            .expect("Expected to find 'Startup' parameter");
        let _ = input
            .take("=", Comparator::CaseSensitive)
            .expect("Expected to find '=' after 'Startup'");

        let mut ctx = ParserContext::new(input.file_name(), input.contents);

        let result = parse_optional_quoted_value(&mut ctx, &mut input, parameter_name);

        let errors = ctx.errors();

        assert_eq!(errors.len(), 1);
        assert_eq!(result, None);
        assert_eq!(errors[0].line_start, 0);
        assert_eq!(errors[0].line_end, 8);
        assert_eq!(errors[0].error_offset, 7);
        assert_eq!(errors[0].labels.len(), 1);
        assert_eq!(
            errors[0].labels[0].message,
            "'Startup' must have a double quoted value and end with a newline."
        );
        assert_eq!(errors[0].labels[0].span.line_start, 0);
        assert_eq!(errors[0].labels[0].span.line_end, 8);
        assert_eq!(errors[0].labels[0].span.length, 1);
        assert_eq!(errors[0].labels[0].span.offset, 7);
        assert_eq!(errors[0].notes.len(), 1);
        assert_eq!(errors[0].notes[0], "Startup=\"!None!\"");
    }

    #[test]
    fn compatibility_mode_eof_after_equal() {
        use crate::files::project::parse_quoted_converted_value;

        let mut input = SourceStream::new("", "CompatibleMode=");

        let parameter_name = input
            .take("CompatibleMode", Comparator::CaseSensitive)
            .expect("Expected to find 'CompatibleMode' parameter");
        let _ = input
            .take("=", Comparator::CaseSensitive)
            .expect("Expected to find '=' after 'CompatibleMode'");

        let mut ctx = ParserContext::new(input.file_name(), input.contents);

        let _compatibility_mode: Option<CompatibilityMode> =
            parse_quoted_converted_value(&mut ctx, &mut input, parameter_name);

        let errors = ctx.errors();

        assert_eq!(errors.len(), 1);
        assert_matches!(
            *errors[0].kind,
            ErrorKind::Project(ProjectError::ParameterWithDefaultValueNotFoundEOF { .. })
        );
        assert_eq!(errors[0].severity, Severity::Error);
        assert_eq!(errors[0].labels.len(), 1);
        assert_eq!(errors[0].labels[0].span.line_start, 0);

        assert_eq!(errors[0].labels[0].span.line_end, 15);
        assert_eq!(errors[0].labels[0].span.offset, 14);
        assert_eq!(errors[0].labels[0].span.length, 1);
        assert_eq!(
            errors[0].labels[0].message,
            "'CompatibleMode' must have a double qouted value and end with a newline."
        );
        assert_eq!(errors[0].notes[0], "CompatibleMode=\"1\"");
    }

    #[test]
    fn compatibility_mode_with_only_start_quote() {
        use crate::files::project::parse_quoted_converted_value;

        let mut input = SourceStream::new("", "CompatibleMode=\"\n");

        let parameter_name = input
            .take("CompatibleMode", Comparator::CaseSensitive)
            .expect("Expected to find 'CompatibleMode' parameter");
        let _ = input
            .take("=", Comparator::CaseSensitive)
            .expect("Expected to find '=' after 'CompatibleMode'");

        let mut ctx = ParserContext::new(input.file_name(), input.contents);

        let _compatibility_mode: Option<CompatibilityMode> =
            parse_quoted_converted_value(&mut ctx, &mut input, parameter_name);

        let errors = ctx.errors();

        assert_eq!(errors.len(), 1);
        assert_matches!(
            *errors[0].kind,
            ErrorKind::Project(ProjectError::ParameterValueMissingClosingQuoteAndValue { .. })
        );
        assert_eq!(errors[0].severity, Severity::Error);
        assert_eq!(errors[0].labels.len(), 1);
        assert_eq!(errors[0].labels[0].span.line_start, 0);
        assert_eq!(errors[0].labels[0].span.line_end, 16);
        assert_eq!(errors[0].labels[0].span.offset, 15);
        assert_eq!(errors[0].labels[0].span.length, 1);
        assert_eq!(
            errors[0].labels[0].message,
            "'CompatibleMode' value must be surrounded by double quotes."
        );
        assert_eq!(errors[0].notes[0], "CompatibleMode=\"1\"");
    }

    #[test]
    fn compatibility_mode_is_invalid() {
        use crate::files::project::parse_quoted_converted_value;

        let mut input = SourceStream::new("", "CompatibleMode=\"5\"\n");

        let parameter_name = input
            .take("CompatibleMode", Comparator::CaseSensitive)
            .expect("Expected to find 'CompatibleMode' parameter");
        let _ = input
            .take("=", Comparator::CaseSensitive)
            .expect("Expected to find '=' after 'CompatibleMode'");

        let mut ctx = ParserContext::new(input.file_name(), input.contents);

        let _compatibility_mode: Option<CompatibilityMode> =
            parse_quoted_converted_value(&mut ctx, &mut input, parameter_name);

        let errors = ctx.errors();

        assert_eq!(errors.len(), 1);
        assert_matches!(
            *errors[0].kind,
            ErrorKind::Project(ProjectError::ParameterValueInvalid { .. })
        );
        assert_eq!(errors[0].severity, Severity::Error);
        assert_eq!(errors[0].labels.len(), 1);
        assert_eq!(errors[0].labels[0].span.line_start, 0);

        assert_eq!(errors[0].labels[0].span.line_end, 18);
        assert_eq!(errors[0].labels[0].span.offset, 16);
        assert_eq!(errors[0].labels[0].span.length, 1);
        assert_eq!(errors[0].labels[0].message, "invalid value");
        assert_eq!(
            errors[0].notes[0],
            "Change the quoted value to one of the valid values."
        );
    }

    #[test]
    fn compatibility_mode_without_quotes() {
        use crate::files::project::parse_quoted_converted_value;

        let mut input = SourceStream::new("", "CompatibleMode=0\n");

        let parameter_name = input
            .take("CompatibleMode", Comparator::CaseSensitive)
            .expect("Expected to find 'CompatibleMode' parameter");
        let _ = input
            .take("=", Comparator::CaseSensitive)
            .expect("Expected to find '=' after 'CompatibleMode'");

        let mut ctx = ParserContext::new(input.file_name(), input.contents);

        let _compatibility_mode: Option<CompatibilityMode> =
            parse_quoted_converted_value(&mut ctx, &mut input, parameter_name);

        let errors = ctx.errors();

        assert_eq!(errors.len(), 1);
        assert_matches!(
            *errors[0].kind,
            ErrorKind::Project(ProjectError::ParameterValueMissingQuotes { .. })
        );
        assert_eq!(errors[0].severity, Severity::Error);
        assert_eq!(errors[0].labels.len(), 1);
        assert_eq!(errors[0].labels[0].span.line_start, 0);

        assert_eq!(errors[0].labels[0].span.line_end, 16);
        assert_eq!(errors[0].labels[0].span.offset, 15);
        assert_eq!(errors[0].labels[0].span.length, 1);
        assert_eq!(
            errors[0].labels[0].message,
            "'CompatibleMode' value must be contained within double qoutes."
        );
        assert_eq!(errors[0].notes[0], "CompatibleMode=\"0\"");
    }

    #[test]
    fn compatibility_mode_invalid_without_quotes() {
        use crate::files::project::parse_quoted_converted_value;

        let mut input = SourceStream::new("", "CompatibleMode=5\n");

        let parameter_name = input
            .take("CompatibleMode", Comparator::CaseSensitive)
            .expect("Expected to find 'CompatibleMode' parameter");
        let _ = input
            .take("=", Comparator::CaseSensitive)
            .expect("Expected to find '=' after 'CompatibleMode'");

        let mut ctx = ParserContext::new(input.file_name(), input.contents);

        let _compatibility_mode: Option<CompatibilityMode> =
            parse_quoted_converted_value(&mut ctx, &mut input, parameter_name);

        let errors = ctx.errors();

        assert_eq!(errors.len(), 1);
        assert_matches!(
            *errors[0].kind,
            ErrorKind::Project(ProjectError::ParameterValueMissingQuotes { .. })
        );
        assert_eq!(errors[0].severity, Severity::Error);
        assert_eq!(errors[0].labels.len(), 1);
        assert_eq!(errors[0].labels[0].span.line_start, 0);

        assert_eq!(errors[0].labels[0].span.line_end, 16);
        assert_eq!(errors[0].labels[0].span.offset, 15);
        assert_eq!(errors[0].labels[0].span.length, 1);
        assert_eq!(
            errors[0].labels[0].message,
            "'CompatibleMode' value must be contained within double qoutes."
        ); // Since the unqouted value is invalid, we should show a note with the default for 'CompatibleMode'
        assert_eq!(errors[0].notes[0], "CompatibleMode=\"1\"");
    }

    #[test]
    fn compatibility_mode_without_value() {
        use crate::files::project::parse_quoted_converted_value;

        let mut input = SourceStream::new("", "CompatibleMode=\n");

        let parameter_name = input
            .take("CompatibleMode", Comparator::CaseSensitive)
            .expect("Expected to find 'CompatibleMode' parameter");
        let _ = input
            .take("=", Comparator::CaseSensitive)
            .expect("Expected to find '=' after 'CompatibleMode'");

        let mut ctx = ParserContext::new(input.file_name(), input.contents);

        let _compatibility_mode: Option<CompatibilityMode> =
            parse_quoted_converted_value(&mut ctx, &mut input, parameter_name);

        let errors = ctx.errors();

        assert_eq!(errors.len(), 1);
        assert_matches!(
            *errors[0].kind,
            ErrorKind::Project(ProjectError::ParameterWithDefaultValueNotFound { .. })
        );
        assert_eq!(errors[0].severity, Severity::Error);
        assert_eq!(errors[0].labels.len(), 1);
        assert_eq!(errors[0].labels[0].span.line_start, 0);

        assert_eq!(errors[0].labels[0].span.line_end, 15);
        assert_eq!(errors[0].labels[0].span.offset, 15);
        assert_eq!(errors[0].labels[0].span.length, 1);
        assert_eq!(
            errors[0].labels[0].message,
            "'CompatibleMode' value must be one of the valid values contained within double qoutes."
        );
        assert_eq!(errors[0].notes[0], "CompatibleMode=\"1\"");
    }

    #[test]
    fn compatibility_mode_without_end_quote() {
        use crate::files::project::parse_quoted_converted_value;

        let mut input = SourceStream::new("", "CompatibleMode=\"1\n");

        let parameter_name = input
            .take("CompatibleMode", Comparator::CaseSensitive)
            .expect("Expected to find 'CompatibleMode' parameter");
        let _ = input
            .take("=", Comparator::CaseSensitive)
            .expect("Expected to find '=' after 'CompatibleMode'");

        let mut ctx = ParserContext::new(input.file_name(), input.contents);

        let _compatibility_mode: Option<CompatibilityMode> =
            parse_quoted_converted_value(&mut ctx, &mut input, parameter_name);

        let errors = ctx.errors();

        assert_eq!(errors.len(), 1);
        assert_matches!(
            *errors[0].kind,
            ErrorKind::Project(ProjectError::ParameterValueMissingClosingQuote { .. })
        );
        assert_eq!(errors[0].severity, Severity::Error);
        assert_eq!(errors[0].labels.len(), 1);
        assert_eq!(errors[0].labels[0].span.line_start, 0);
        assert_eq!(errors[0].labels[0].span.line_end, 17);
        assert_eq!(errors[0].labels[0].span.offset, 15);
        assert_eq!(errors[0].labels[0].span.length, 2);
        assert_eq!(
            errors[0].labels[0].message,
            "'CompatibleMode' value must be surrounded by double quotes."
        );
        assert_eq!(errors[0].notes[0], "CompatibleMode=\"1\"");
    }

    #[test]
    fn compatibility_mode_without_start_quote() {
        use crate::files::project::parse_quoted_converted_value;

        let mut input = SourceStream::new("", "CompatibleMode=2\"\n");

        let parameter_name = input
            .take("CompatibleMode", Comparator::CaseSensitive)
            .expect("Expected to find 'CompatibleMode' parameter");
        let _ = input
            .take("=", Comparator::CaseSensitive)
            .expect("Expected to find '=' after 'CompatibleMode'");

        let mut ctx = ParserContext::new(input.file_name(), input.contents);

        let _compatibility_mode: Option<CompatibilityMode> =
            parse_quoted_converted_value(&mut ctx, &mut input, parameter_name);

        let errors = ctx.errors();

        assert_eq!(errors.len(), 1);
        assert_matches!(
            *errors[0].kind,
            ErrorKind::Project(ProjectError::ParameterValueMissingOpeningQuote { .. })
        );
        assert_eq!(errors[0].severity, Severity::Error);
        assert_eq!(errors[0].labels.len(), 1);
        assert_eq!(errors[0].labels[0].span.line_start, 0);
        assert_eq!(errors[0].labels[0].span.line_end, 17);
        assert_eq!(errors[0].labels[0].span.offset, 15);
        assert_eq!(errors[0].labels[0].span.length, 2);
        assert_eq!(
            errors[0].labels[0].message,
            "'CompatibleMode' value must be surrounded by double quotes."
        );
        assert_eq!(errors[0].notes[0], "CompatibleMode=\"2\"");
    }

    #[test]
    fn property_name_not_found() {
        use crate::files::project::parse_property_name;

        let mut input = SourceStream::new("", "SomePropertyWithoutEquals\n");

        let mut ctx = ParserContext::new(input.file_name(), input.contents);

        let result = parse_property_name(&mut ctx, &mut input);

        let errors = ctx.errors();

        errors[0].print();

        assert_eq!(errors.len(), 1);
        assert_matches!(
            *errors[0].kind,
            ErrorKind::Project(ProjectError::PropertyNameNotFound)
        );
        assert_eq!(errors[0].severity, Severity::Error);
        assert_eq!(result, None);
        assert_eq!(errors[0].line_start, 0);
        assert_eq!(errors[0].line_end, 25);
        assert_eq!(errors[0].error_offset, 0);
        assert_eq!(errors[0].labels.len(), 1);
        assert_eq!(errors[0].labels[0].span.line_start, 0);
        assert_eq!(errors[0].labels[0].span.length, 25);
        assert_eq!(errors[0].labels[0].span.line_end, 25);
        assert_eq!(errors[0].labels[0].span.offset, 0);
        assert_eq!(
            errors[0].labels[0].message,
            "'=' and related value missing."
        );
        assert_eq!(errors[0].notes.len(), 0);
    }

    #[test]
    fn unterminated_section_header() {
        use crate::files::project::parse_section_header_line;

        let mut input = SourceStream::new("", "[MS Transaction Server\n");

        let mut ctx = ParserContext::new(input.file_name(), input.contents);

        let result = parse_section_header_line(&mut ctx, &mut input);

        let errors = ctx.errors();

        errors[0].print();

        assert_eq!(errors.len(), 1);
        assert_matches!(
            *errors[0].kind,
            ErrorKind::Project(ProjectError::UnterminatedSectionHeader)
        );
        assert_eq!(errors[0].severity, Severity::Error);
        // Should return MalformedHeader
        assert!(matches!(
            result,
            Some(crate::files::project::SectionHeaderDetection::MalformedHeader)
        ));
        assert_eq!(errors[0].line_start, 0);
        assert_eq!(errors[0].line_end, 22);
        assert_eq!(errors[0].error_offset, 1);
        // Should have one label pointing to where ']' should be
        assert_eq!(errors[0].labels.len(), 1);
        assert_eq!(
            errors[0].labels[0].message,
            "section header must be terminated with ']'"
        );
        // Should have one note showing the corrected line
        assert_eq!(errors[0].notes.len(), 1);
        assert_eq!(errors[0].notes[0], "[MS Transaction Server]");
    }
}