File: quickcheck.rs

package info (click to toggle)
node-source-map 0.7.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 960 kB
  • sloc: javascript: 7,200; python: 252; sh: 21; makefile: 14
file content (597 lines) | stat: -rw-r--r-- 19,314 bytes parent folder | download
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
#[macro_use]
extern crate quickcheck;
extern crate source_map_mappings;
extern crate vlq;

use quickcheck::{Arbitrary, Gen};
use source_map_mappings::{Bias, Error};
use std::cmp::Ordering;
use std::fmt;
use std::i64;
use std::iter;
use std::marker::PhantomData;

trait VlqRange: 'static + Send + Copy + Clone + fmt::Debug + fmt::Display {
    fn low() -> i64;
    fn high() -> i64;
}

#[derive(Copy, Clone, Debug)]
struct Vlq<R>(i64, PhantomData<R>);

impl<R> Arbitrary for Vlq<R>
where
    R: VlqRange,
{
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        Vlq(g.gen_range(R::low(), R::high()), PhantomData)
    }

    fn shrink(&self) -> Box<Iterator<Item = Self>> {
        Box::new(self.0.shrink().map(|x| Vlq(x, PhantomData)))
    }
}

impl<R> fmt::Display for Vlq<R> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut v = vec![];
        vlq::encode(self.0, &mut v).unwrap();
        write!(f, "{}", String::from_utf8_lossy(&v))
    }
}

#[derive(Clone, Debug)]
enum Mapping<R> {
    Generated {
        generated_column: Vlq<R>,
    },
    Original {
        generated_column: Vlq<R>,
        source: Vlq<R>,
        original_line: Vlq<R>,
        original_column: Vlq<R>,
    },
    OriginalWithName {
        generated_column: Vlq<R>,
        source: Vlq<R>,
        original_line: Vlq<R>,
        original_column: Vlq<R>,
        name: Vlq<R>,
    },
}

impl<R> Arbitrary for Mapping<R>
where
    R: VlqRange,
{
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        match g.gen_range(0, 3) {
            0 => Mapping::Generated {
                generated_column: Vlq::<R>::arbitrary(g),
            },
            1 => Mapping::Original {
                generated_column: Vlq::<R>::arbitrary(g),
                source: Vlq::<R>::arbitrary(g),
                original_line: Vlq::<R>::arbitrary(g),
                original_column: Vlq::<R>::arbitrary(g),
            },
            2 => Mapping::OriginalWithName {
                generated_column: Vlq::<R>::arbitrary(g),
                source: Vlq::<R>::arbitrary(g),
                original_line: Vlq::<R>::arbitrary(g),
                original_column: Vlq::<R>::arbitrary(g),
                name: Vlq::<R>::arbitrary(g),
            },
            _ => unreachable!(),
        }
    }

    fn shrink(&self) -> Box<Iterator<Item = Self>> {
        match *self {
            Mapping::Generated { generated_column } => Box::new(
                generated_column
                    .shrink()
                    .map(|generated_column| Mapping::Generated { generated_column }),
            ),
            Mapping::Original {
                generated_column,
                source,
                original_line,
                original_column,
            } => {
                let shrunkens = generated_column.shrink().zip(
                    source
                        .shrink()
                        .zip(original_line.shrink().zip(original_column.shrink())),
                );
                let shrunkens = shrunkens.map(
                    move |(generated_column, (source, (original_line, original_column)))| {
                        Mapping::Original {
                            generated_column,
                            source,
                            original_line,
                            original_column,
                        }
                    },
                );

                let generated = Mapping::Generated { generated_column };
                Box::new(iter::once(generated).chain(shrunkens))
            }
            Mapping::OriginalWithName {
                generated_column,
                source,
                original_line,
                original_column,
                name,
            } => {
                let shrunkens = generated_column.shrink().zip(
                    source.shrink().zip(
                        original_line
                            .shrink()
                            .zip(original_column.shrink().zip(name.shrink())),
                    ),
                );
                let shrunkens = shrunkens.map(
                    move |(
                        generated_column,
                        (source, (original_line, (original_column, name))),
                    )| {
                        Mapping::OriginalWithName {
                            generated_column,
                            source,
                            original_line,
                            original_column,
                            name,
                        }
                    },
                );

                let generated = Mapping::Generated { generated_column };
                let original = Mapping::Original {
                    generated_column,
                    source,
                    original_line,
                    original_column,
                };
                Box::new(
                    iter::once(generated)
                        .chain(iter::once(original))
                        .chain(shrunkens),
                )
            }
        }
    }
}

impl<R: Copy> fmt::Display for Mapping<R> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Mapping::Generated { generated_column } => generated_column.fmt(f),
            Mapping::Original {
                generated_column,
                source,
                original_line,
                original_column,
            } => {
                generated_column.fmt(f)?;
                source.fmt(f)?;
                original_line.fmt(f)?;
                original_column.fmt(f)
            }
            Mapping::OriginalWithName {
                generated_column,
                source,
                original_line,
                original_column,
                name,
            } => {
                generated_column.fmt(f)?;
                source.fmt(f)?;
                original_line.fmt(f)?;
                original_column.fmt(f)?;
                name.fmt(f)
            }
        }
    }
}

#[derive(Clone, Debug)]
struct GeneratedLine<R>(Vec<Mapping<R>>);

impl<R> Arbitrary for GeneratedLine<R>
where
    R: VlqRange,
{
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        GeneratedLine(Vec::arbitrary(g))
    }

    fn shrink(&self) -> Box<Iterator<Item = Self>> {
        Box::new(self.0.shrink().map(|v| GeneratedLine(v)))
    }
}

impl<R: Copy> fmt::Display for GeneratedLine<R> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut needs_comma = false;
        for m in &self.0 {
            if needs_comma {
                write!(f, ",")?;
            }
            m.fmt(f)?;
            needs_comma = true;
        }
        Ok(())
    }
}

#[derive(Clone, Debug)]
struct Mappings<R>(Vec<GeneratedLine<R>>);

impl<R> Arbitrary for Mappings<R>
where
    R: VlqRange,
{
    fn arbitrary<G: Gen>(g: &mut G) -> Self {
        Mappings(Vec::arbitrary(g))
    }

    fn shrink(&self) -> Box<Iterator<Item = Self>> {
        Box::new(self.0.shrink().map(|v| Mappings(v)))
    }
}

impl<R: Copy> fmt::Display for Mappings<R> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut needs_semi = false;
        for line in &self.0 {
            if needs_semi {
                write!(f, ";")?;
            }
            line.fmt(f)?;
            needs_semi = true;
        }
        Ok(())
    }
}

#[derive(Copy, Clone, Debug)]
struct FullRange;

impl fmt::Display for FullRange {
    fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
        Ok(())
    }
}

impl VlqRange for FullRange {
    fn low() -> i64 {
        i64::MIN
    }
    fn high() -> i64 {
        i64::MAX
    }
}

#[derive(Copy, Clone, Debug)]
struct SmallPositives;

impl fmt::Display for SmallPositives {
    fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
        Ok(())
    }
}

impl VlqRange for SmallPositives {
    fn low() -> i64 {
        0
    }
    fn high() -> i64 {
        5
    }
}

quickcheck! {
    fn parse_without_panicking(mappings: Mappings<FullRange>) -> () {
        let mappings_string = mappings.to_string();
        let _ = source_map_mappings::parse_mappings::<()>(mappings_string.as_bytes());
    }

    fn parse_valid_mappings(mappings: Mappings<SmallPositives>) -> Result<(), Error> {
        let mappings_string = mappings.to_string();
        source_map_mappings::parse_mappings::<()>(mappings_string.as_bytes())?;
        Ok(())
    }

    fn compute_column_spans(mappings: Mappings<SmallPositives>) -> Result<(), Error> {
        let mappings_string = mappings.to_string();
        let mut mappings = source_map_mappings::parse_mappings::<()>(mappings_string.as_bytes())?;

        // Can compute column spans without panicking.
        mappings.compute_column_spans();

        // And those column spans make sense.
        for window in mappings.by_generated_location().windows(2) {
            let this_mapping = &window[0];
            let next_mapping = &window[1];
            if this_mapping.generated_line == next_mapping.generated_line {
                assert_eq!(this_mapping.last_generated_column.unwrap(), next_mapping.generated_column);
            } else {
                assert!(this_mapping.last_generated_column.is_none());
            }
        }

        Ok(())
    }

    fn original_location_for(
        mappings: Mappings<SmallPositives>,
        line: u32,
        col: u32,
        lub: bool
    ) -> Result<(), Error> {
        let mappings_string = mappings.to_string();
        let mappings = source_map_mappings::parse_mappings::<()>(mappings_string.as_bytes())?;
        if mappings.by_generated_location().is_empty() {
            return Ok(());
        }

        // To make this more useful, wrap `line` and `col` around the maximum
        // line and column in the mappings respectively.
        let max_line = mappings.by_generated_location()
            .iter()
            .map(|m| m.generated_line)
            .max()
            .unwrap();
        let max_col = mappings.by_generated_location()
            .iter()
            .map(|m| m.generated_column)
            .max()
            .unwrap();
        let line = line % (max_line + 1);
        let col = col % (max_col + 1);

        let bias = if lub {
            Bias::LeastUpperBound
        } else {
            Bias::GreatestLowerBound
        };

        // If we find a mapping, then it should either be an exact match or it
        // should have the proper ordering relation to our query line/column
        // based on the given bias.
        if let Some(mapping) = mappings.original_location_for(line, col, bias) {
            let found_line = mapping.generated_line;
            let found_col = mapping.generated_column;
            match line.cmp(&found_line).then(col.cmp(&found_col)) {
                Ordering::Equal => {}
                Ordering::Greater if bias == Bias::GreatestLowerBound => {}
                Ordering::Less if bias == Bias::LeastUpperBound => {}
                _ => panic!(
                    "Found bad location {{ line = {}, col = {} }} when \
                     searching for {{ line = {}, col = {} }} with bias {:?}",
                    found_line,
                    found_col,
                    line,
                    col,
                    bias
                ),
            }
            return Ok(())
        }

        // If we didn't get any result, then every mapping should not match our
        // query, and should additionally be on the opposite side of ordering
        // from our requested bias.
        for m in mappings.by_generated_location().iter() {
            match m.generated_line.cmp(&line).then(m.generated_column.cmp(&col)) {
                Ordering::Equal => panic!("found matching mapping when we returned none"),
                Ordering::Less => {
                    assert_eq!(bias, Bias::LeastUpperBound);
                }
                Ordering::Greater => {
                    assert_eq!(bias, Bias::GreatestLowerBound);
                }
            }
        }

        Ok(())
    }

    fn original_mappings_have_original(
        mappings: Mappings<SmallPositives>
    ) -> Result<bool, Error> {
        let mappings_string = mappings.to_string();
        let mut mappings = source_map_mappings::parse_mappings::<()>(mappings_string.as_bytes())?;
        Ok(mappings.by_original_location().all(|m| m.original.as_ref().is_some()))
    }

    fn generated_location_for(
        mappings: Mappings<SmallPositives>,
        source: u32,
        line: u32,
        col: u32,
        lub: bool
    ) -> Result<(), Error> {
        let mappings_string = mappings.to_string();
        let mut mappings = source_map_mappings::parse_mappings::<()>(mappings_string.as_bytes())?;
        if !mappings.by_generated_location().iter().any(|m| m.original.is_some()) {
            return Ok(());
        }

        // To make this more useful, wrap `source`, `line`, and `col` around the
        // maximums.
        let max_source = mappings.by_original_location()
            .map(|m| m.original.as_ref().unwrap().source)
            .max()
            .unwrap();
        let max_line = mappings.by_original_location()
            .map(|m| m.original.as_ref().unwrap().original_line)
            .max()
            .unwrap();
        let max_col = mappings.by_original_location()
            .map(|m| m.original.as_ref().unwrap().original_column)
            .max()
            .unwrap();
        let source = source % (max_source + 1);
        let line = line % (max_line + 1);
        let col = col % (max_col + 1);

        let bias = if lub {
            Bias::LeastUpperBound
        } else {
            Bias::GreatestLowerBound
        };

        // If we find a mapping, then it should either be an exact match or it
        // should have the proper ordering relation to our query line/column
        // based on the given bias.
        if let Some(mapping) = mappings.generated_location_for(source, line, col, bias) {
            let found_source = mapping.original.as_ref().unwrap().source;
            let found_line = mapping.original.as_ref().unwrap().original_line;
            let found_col = mapping.original.as_ref().unwrap().original_column;

            let order = source.cmp(&found_source)
                .then(line.cmp(&found_line))
                .then(col.cmp(&found_col));

            match order {
                Ordering::Equal => {}
                Ordering::Greater if bias == Bias::GreatestLowerBound => {}
                Ordering::Less if bias == Bias::LeastUpperBound => {}
                _ => panic!(
                    "Found bad location {{ line = {}, col = {} }} when \
                     searching for {{ line = {}, col = {} }} with bias {:?}",
                    found_line,
                    found_col,
                    line,
                    col,
                    bias
                ),
            }
            return Ok(())
        }

        // If we didn't get any result, then every mapping should not match our
        // query, and should additionally be on the opposite side of ordering
        // from our requested bias.
        for m in mappings.by_original_location() {
            let m_orig = m.original.as_ref().unwrap();
            let m_source = m_orig.source;
            let m_line = m_orig.original_line;
            let m_col = m_orig.original_column;

            let order = m_source.cmp(&source)
                .then(m_line.cmp(&line))
                .then(m_col.cmp(&col));

            match order {
                Ordering::Equal => panic!("found matching mapping when we returned none"),
                Ordering::Less => {
                    assert_eq!(bias, Bias::LeastUpperBound);
                }
                Ordering::Greater => {
                    assert_eq!(bias, Bias::GreatestLowerBound);
                }
            }
        }

        Ok(())
    }

    fn all_generated_locations_for(
        mappings: Mappings<SmallPositives>,
        source: u32,
        line: u32,
        col: Option<u32>
    ) -> Result<(), Error> {
        let mappings_string = mappings.to_string();
        let mut mappings = source_map_mappings::parse_mappings::<()>(mappings_string.as_bytes())?;
        if !mappings.by_generated_location().iter().any(|m| m.original.is_some()) {
            return Ok(());
        }

        let max_source = mappings.by_original_location()
            .map(|m| m.original.as_ref().unwrap().source)
            .max()
            .unwrap();
        let max_line = mappings.by_original_location()
            .map(|m| m.original.as_ref().unwrap().original_line)
            .max()
            .unwrap();
        let max_col = mappings.by_original_location()
            .map(|m| m.original.as_ref().unwrap().original_column)
            .max()
            .unwrap();
        let source = source % (max_source + 1);
        let mut line = line % (max_line + 1);
        let mut col = if let Some(col) = col {
            Some(col % (max_col + 1))
        } else {
            None
        };

        let mut count = 0;
        {
            let locations = mappings.all_generated_locations_for(source, line, col);

            for (idx, m) in locations.into_iter().enumerate() {
                count += 1;

                let m_orig = m.original.as_ref().unwrap();

                // `all_generated_locations_for` does fuzzy searching: it will
                // slide down to the next original line if there are no mappings
                // on the queried line. Ditto for columns. This is to match
                // mozilla/source-map's behavior.
                if idx == 0 {
                    if m_orig.original_line != line {
                        assert!(m_orig.original_line > line);
                        line = m_orig.original_line;
                    }
                    if let Some(c) = col {
                        if c != m_orig.original_column {
                            col = Some(m_orig.original_column);
                        }
                    }
                }

                assert_eq!(
                    m_orig.original_line,
                    line,
                    "result location's line is our query's line",
                );

                if let Some(col) = col {
                    assert_eq!(
                        m_orig.original_column,
                        col,
                        "result location's column is our query's column",
                    );
                }
            }
        }

        assert_eq!(
            count,
            mappings.by_original_location()
                .filter(|m| {
                    let m_orig = m.original.as_ref().unwrap();
                    if m_orig.source != source || m_orig.original_line != line {
                        return false;
                    }
                    if let Some(col) = col {
                        if m_orig.original_column != col {
                            return false;
                        }
                    }
                    true
                })
                .count(),
            "the iterator should find the right number of results"
        );

        Ok(())
    }
}