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
857
858
859
860
861
862
use std::cmp::Ordering;
use std::collections::{HashMap, HashSet};
use std::collections::hash_map::RandomState;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use std::iter::FromIterator;

use itertools::Itertools;

use crate::check::context::{clss, Context};
use crate::check::context::clss::Class;
use crate::check::ident::Identifier;
use crate::check::name::string_name::StringName;
use crate::check::name::true_name::{IsTemp, MatchTempName, TrueName};
use crate::check::result::{TypeErr, TypeResult};
use crate::common::delimit::comma_delm;
use crate::common::position::Position;

pub mod string_name;
pub mod true_name;

pub mod generic;
pub mod python;

pub const TEMP: char = '@';

pub type NameMap = HashMap<Name, Name>;

pub trait Union<T> {
    fn union(&self, value: &T) -> Self;
}

pub trait IsSuperSet<T> {
    fn is_superset_of(&self, other: &T, ctx: &Context, pos: Position) -> TypeResult<bool>;
}

pub trait Empty {
    fn is_empty(&self) -> bool;
    fn empty() -> Self;
}

pub trait Any {
    fn any() -> Self;
}

pub trait Nullable {
    fn is_nullable(&self) -> bool;
    fn is_null(&self) -> bool;
    fn as_nullable(&self) -> Self;
}

pub trait Mutable {
    fn as_mutable(&self) -> Self;
}

pub trait Substitute {
    fn substitute(&self, generics: &NameMap, pos: Position) -> TypeResult<Self> where Self: Sized;
}

pub trait ColType {
    fn col_type(&self, ctx: &Context, pos: Position) -> TypeResult<Option<Name>>;
}

pub trait ContainsTemp {
    fn contains_temp(&self) -> bool;
}

pub trait TupleCallable<T1, T2, T3> {
    fn tuple(names: &[Name]) -> Self;
    fn callable(args: &[Name], ret_ty: &Name) -> Self;

    fn is_tuple(&self) -> T1;
    fn is_callable(&self) -> T1;

    fn elements(&self, pos: Position) -> TypeResult<T2>;
    fn args(&self, pos: Position) -> TypeResult<T2>;
    fn ret_ty(&self, pos: Position) -> TypeResult<T3>;
}

#[derive(Debug, Clone, Eq, Default)]
pub struct Name {
    pub names: HashSet<TrueName>,
    pub is_interchangeable: bool,
}

impl Any for Name {
    fn any() -> Self {
        Name::from(clss::ANY)
    }
}

pub fn match_name(
    identifier: &Identifier,
    name: &Name,
    pos: Position,
) -> TypeResult<HashMap<String, (bool, Name)>> {
    let unions: Vec<HashMap<String, (bool, Name)>> =
        name.names.iter().map(|ty| match_type_direct(identifier, ty, pos)).collect::<Result<_, _>>()?;

    let mut final_union: HashMap<String, (bool, Name)> = HashMap::new();
    for union in unions {
        for (id, (mutable, name)) in union {
            if let Some((current_mutable, current_name)) =
                final_union.insert(id.clone(), (mutable, name.clone()))
            {
                final_union
                    .insert(id.clone(), (mutable && current_mutable, current_name.union(&name)));
            }
        }
    }

    Ok(final_union)
}

pub fn match_type_direct(
    identifier: &Identifier,
    name: &TrueName,
    pos: Position,
) -> TypeResult<HashMap<String, (bool, Name)>> {
    if let Ok(elements) = name.elements(pos) {
        match identifier {
            Identifier::Single(mutable, id) => {
                let mut mapping = HashMap::with_capacity(1);
                mapping.insert(id.clone().object(pos)?, (*mutable, Name::from(name)));
                Ok(mapping)
            }
            Identifier::Multi(fields) if elements.len() == fields.len() => {
                let sets: Vec<HashMap<_, _>> = fields
                    .iter()
                    .zip(elements)
                    .map(|(identifier, ty)| match_name(identifier, &ty, pos))
                    .collect::<Result<_, _>>()?;
                Ok(sets.into_iter().flatten().collect())
            }
            Identifier::Multi(idens) => {
                let msg = format!("Expected tuple of {}, was {}", elements.len(), idens.len());
                Err(vec![TypeErr::new(pos, &msg)])
            }
        }
    } else if let Identifier::Single(mutable, id) = &identifier {
        let mut mapping = HashMap::with_capacity(1);
        mapping.insert(id.clone().object(pos)?, (*mutable, Name::from(name)));
        Ok(mapping)
    } else {
        let msg = format!("Cannot match {identifier} with a '{name}'");
        Err(vec![TypeErr::new(pos, &msg)])
    }
}

impl From<&HashSet<Class>> for Name {
    fn from(classes: &HashSet<Class>) -> Self {
        Name { names: classes.iter().map(|c| TrueName::from(&c.name)).collect(), is_interchangeable: false }
    }
}

impl PartialOrd<Self> for Name {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        let self_vec = self.names.iter().sorted();
        let other_vec = other.names.iter().sorted();
        self_vec.partial_cmp(other_vec)
    }
}

impl Ord for Name {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other).unwrap_or(Ordering::Equal)
    }
}

impl Mutable for Name {
    fn as_mutable(&self) -> Self {
        Name { names: self.names.iter().map(|n| n.as_mutable()).collect(), ..self.clone() }
    }
}

impl Union<Name> for Name {
    fn union(&self, name: &Name) -> Self {
        let names: HashSet<TrueName> = self.names.union(&name.names).cloned().collect();
        Name {
            names: if names.iter().any(TrueName::is_null) && names.len() > 1 {
                names.iter()
                    .filter(|n| !n.is_null())
                    .map(TrueName::as_nullable)
                    .collect()
            } else {
                names
            },
            is_interchangeable: self.is_interchangeable || name.is_interchangeable,
        }
    }
}

impl ColType for Name {
    fn col_type(&self, ctx: &Context, pos: Position) -> TypeResult<Option<Name>> {
        let names: Vec<Option<Name>> =
            self.names.iter().map(|n| n.col_type(ctx, pos)).collect::<Result<_, _>>()?;
        let mut union = Name::empty();
        for name in names {
            if let Some(name) = name {
                union = union.union(&name)
            } else {
                return Ok(None);
            }
        }
        Ok(Some(union))
    }
}

impl From<&HashSet<&str>> for Name {
    fn from(names: &HashSet<&str, RandomState>) -> Self {
        let names: HashSet<Name> = names.iter().map(|name| Name::from(*name)).collect();
        Name::from(&names)
    }
}

impl From<&HashSet<Name>> for Name {
    fn from(names: &HashSet<Name, RandomState>) -> Self {
        let mut final_name = Name::empty();
        for name in names {
            final_name = final_name.union(name);
        }
        final_name
    }
}

impl PartialEq for Name {
    fn eq(&self, other: &Self) -> bool {
        self.names.eq(&other.names) // order doesn't matter for partialeq
    }
}

impl Hash for Name {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.names.len().hash(state);
        self.names.iter()
            .sorted_by_key(|name| &name.variant)
            .for_each(|n| n.hash(state))
    }
}

impl Display for Name {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        if let Some(first) = &self.names.iter().last() {
            if self.names.len() > 1 {
                write!(f, "{{{}}}", comma_delm(&self.names))
            } else {
                write!(f, "{first}")
            }
        } else {
            write!(f, "()")
        }
    }
}

impl From<&TrueName> for Name {
    fn from(name: &TrueName) -> Self {
        let names: HashSet<TrueName> = HashSet::from_iter(vec![name.clone()]);
        Name { names, is_interchangeable: false }
    }
}

impl From<&str> for Name {
    fn from(name: &str) -> Self {
        Name::from(&TrueName::from(name))
    }
}

impl IsSuperSet<Name> for Name {
    fn is_superset_of(&self, other: &Name, ctx: &Context, pos: Position) -> TypeResult<bool> {
        if !self.is_empty() && other.is_empty() {
            return Ok(false);
        }

        let mut self_is_super_of = false;
        for name in &other.names {
            let is_superset = |s_name: &TrueName| s_name.is_superset_of(name, ctx, pos);
            let any_superset: Vec<_> = self.names.iter().map(is_superset).collect::<Result<_, _>>()?;

            if !other.is_interchangeable && any_superset.clone().iter().all(|b| !*b) {
                return Ok(false); // not a single of self was super
            }

            self_is_super_of |= any_superset.iter().any(|b| *b);
        }

        Ok(if other.is_interchangeable { self_is_super_of } else { true })
    }
}

impl Nullable for Name {
    fn is_nullable(&self) -> bool {
        self.names.iter().all(|n| n.is_nullable())
    }

    fn is_null(&self) -> bool {
        self.names.iter().all(|name| name.is_null())
    }

    fn as_nullable(&self) -> Self {
        Name { names: self.names.iter().map(|n| n.as_nullable()).collect(), ..self.clone() }
    }
}

impl Empty for Name {
    fn is_empty(&self) -> bool {
        self == &Name::empty() || self.names.iter().all(TrueName::is_empty)
    }

    fn empty() -> Name {
        Name { names: HashSet::new(), is_interchangeable: false }
    }
}

impl Substitute for Name {
    fn substitute(&self, generics: &HashMap<Name, Name>, pos: Position) -> TypeResult<Name> {
        let names =
            self.names.iter().map(|n| n.substitute(generics, pos)).collect::<Result<_, _>>()?;
        Ok(Name { names, ..self.clone() })
    }
}

impl ContainsTemp for Name {
    fn contains_temp(&self) -> bool {
        self.names.iter().any(TrueName::contains_temp)
    }
}

impl TupleCallable<HashSet<bool>, HashSet<Vec<Name>>, HashSet<Name>> for Name {
    fn tuple(names: &[Name]) -> Self {
        Self { names: HashSet::from([TrueName::tuple(names)]), ..Default::default() }
    }

    fn callable(args: &[Name], ret_ty: &Name) -> Self {
        Self { names: HashSet::from([TrueName::callable(args, ret_ty)]), ..Default::default() }
    }

    fn is_tuple(&self) -> HashSet<bool> {
        self.names.iter().map(|n| n.is_tuple()).collect()
    }

    fn is_callable(&self) -> HashSet<bool> {
        self.names.iter().map(|n| n.is_callable()).collect()
    }

    fn elements(&self, pos: Position) -> TypeResult<HashSet<Vec<Name>>> {
        self.names.iter().map(|n| n.elements(pos)).collect()
    }

    fn args(&self, pos: Position) -> TypeResult<HashSet<Vec<Name>>> {
        self.names.iter().map(|n| n.args(pos)).collect()
    }

    fn ret_ty(&self, pos: Position) -> TypeResult<HashSet<Name>> {
        self.names.iter().map(|n| n.ret_ty(pos)).collect()
    }
}

impl Name {
    pub fn trim_any(&self) -> Self {
        let names = self.names.iter().filter(|n| **n != TrueName::any()).cloned().collect();
        Name { names, ..self.clone() }
    }

    pub fn trim(&self, ty: &str) -> Self {
        let names = self.names.iter().flat_map(|n| n.trim(ty)).collect();
        Name { names, ..self.clone() }
    }

    /// Trim Name by removing any in the set which are superset of other members in the set.
    pub fn trim_super(&self, ctx: &Context) -> Self {
        let names = if self.names.len() > 1 {
            self.names
                .iter()
                .filter(|n| self.names.iter().any(|o_n| {
                    !o_n.is_superset_of(n, ctx, Position::invisible()).unwrap_or(true)
                }))
                .cloned()
                .collect()
        } else {
            self.names.clone()
        };
        Self { names, ..self.clone() }
    }

    pub fn as_direct(&self) -> HashSet<StringName> {
        self.names.iter().map(StringName::from).collect()
    }

    /// Any means that if one check if another [is_superset_of] self, then it will be true if it is
    /// just a superset of one.
    pub fn is_interchangeable(&self, is_interchangeable: bool) -> Self {
        Name { is_interchangeable, ..self.clone() }
    }

    pub fn contains(&self, item: &TrueName) -> bool {
        self.names.contains(item)
    }

    /// True if this was a temporary name, which is a name which starts with '@'.
    pub fn is_temporary(&self) -> bool {
        if let Some(name) = Vec::from_iter(&self.names).first() {
            return name.variant.is_temp();
        }
        false
    }

    pub fn as_name(&self, true_name: &TrueName, pos: Position) -> TypeResult<Name> {
        self.names.get(true_name).map(Name::from).ok_or_else(|| {
            let msg = format!("{self} does not define {true_name}");
            vec![TypeErr::new(pos, &msg)]
        })
    }

    pub fn temp_map(&self, other: &Name, pos: Position) -> TypeResult<HashMap<Name, Name>> {
        self.temp_map_with_mapping(other, HashMap::new(), pos)
    }

    pub(crate) fn temp_map_with_mapping(&self, other: &Name, mapping: NameMap, pos: Position) -> TypeResult<NameMap> {
        self.names.iter().fold(Ok(mapping), |acc, s_n| {
            other.names.iter().fold(acc, |acc, o_n| if let Ok(acc) = acc {
                s_n.temp_map(&o_n.variant, acc, pos)
            } else { acc })
        })
    }

    pub(crate) fn match_name_helper(&self, other: &Name, mapping: &mut NameMap, pos: Position) -> TypeResult<()> {
        for name in &self.names {
            for other_name in &other.names {
                name.variant.match_name_helper(&other_name.variant, mapping, pos)?;
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use crate::check::context::{clss, Context, LookupClass};
    use crate::check::context::clss::{BOOL, COLLECTION, FLOAT, HasParent, INT, RANGE, SET, STRING, TUPLE};
    use crate::check::ident::Identifier;
    use crate::check::name::{Any, ColType, Empty, IsSuperSet, match_name, Name, Nullable, TupleCallable, Union};
    use crate::check::name::string_name::StringName;
    use crate::check::name::true_name::TrueName;
    use crate::check::result::TypeResult;
    use crate::common::position::Position;

    #[test]
    fn trim_super_nullable() {
        let name_1 = Name::from("Str");
        let name_2 = Name::from(&TrueName::from("Str").as_nullable());

        let union = name_1.union(&name_2);
        assert_eq!(union, Name::from(&HashSet::from([name_1, name_2.clone()])));

        let ctx = Context::default().into_with_primitives().unwrap().into_with_std_lib().unwrap();
        let union = union.trim_super(&ctx);
        assert_eq!(union, Name::from(&HashSet::from([name_2])));
    }

    #[test]
    fn trim_super_float_int_is_float() {
        let name_1 = Name::from("Float");
        let name_2 = Name::from("Int");

        let union = name_1.union(&name_2);
        assert_eq!(union, Name::from(&HashSet::from([name_1.clone(), name_2])));

        let ctx = Context::default().into_with_primitives().unwrap().into_with_std_lib().unwrap();
        let union = union.trim_super(&ctx);
        assert_eq!(union, Name::from(&HashSet::from([name_1])));
    }

    #[test]
    fn trim_super_of_self() {
        let name_1 = Name::from("Float");
        let ctx = Context::default().into_with_primitives().unwrap().into_with_std_lib().unwrap();
        let union = name_1.trim_super(&ctx);
        assert_eq!(union, Name::from(&HashSet::from([name_1])));
    }

    #[test]
    fn union_none_nullable_str_is_nullable_str() {
        let name_1 = Name::from("None");
        let name_2 = Name::from(&TrueName::from("Str").as_nullable());

        let union = name_1.union(&name_2);
        assert_eq!(union.names.len(), 1);
        assert_eq!(*union.names.iter().next().unwrap(), TrueName::from("Str").as_nullable());
    }

    #[test]
    fn union_none_str_is_nullable_str() {
        let name_1 = Name::from("None");
        let name_2 = Name::from(&TrueName::from("Str"));

        let union = name_1.union(&name_2);
        assert_eq!(union.names.len(), 1);
        assert_eq!(*union.names.iter().next().unwrap(), TrueName::from("Str").as_nullable());
    }

    #[test]
    fn collect_any_superset_of_set_int() {
        let union_1 = Name::from(&StringName::new(COLLECTION, &[Name::any()]));
        let union_2 = Name::from(&StringName::new(SET, &[Name::from(INT)]));

        let ctx = Context::default().into_with_primitives().unwrap();
        assert!(union_1.is_superset_of(&union_2, &ctx, Position::invisible()).unwrap())
    }

    #[test]
    fn test_is_superset_numbers() {
        let names = vec![
            TrueName::from(BOOL),
            TrueName::from(STRING),
            TrueName::from(INT),
            TrueName::from(FLOAT),
        ];
        let union_1 = Name::from(&names);
        let union_2 = Name::from(INT);

        let ctx = Context::default().into_with_primitives().unwrap();
        assert!(union_1.is_superset_of(&union_2, &ctx, Position::invisible()).unwrap())
    }

    #[test]
    fn test_is_superset_slice_int() {
        let name1 = Name::from(&HashSet::from([INT, clss::SLICE]));
        let name2 = Name::from(&HashSet::from([INT]));

        let ctx = Context::default().into_with_primitives().unwrap();
        assert!(name1.is_superset_of(&name2, &ctx, Position::invisible()).unwrap());
        assert!(!name2.is_superset_of(&name1, &ctx, Position::invisible()).unwrap());
    }

    #[test]
    fn is_superset_does_not_contain() {
        let union_1 = Name::from(&vec![TrueName::from(BOOL), TrueName::from(STRING)]);
        let union_2 = Name::from(INT);

        let ctx = Context::default().into_with_primitives().unwrap();
        assert!(!union_1.is_superset_of(&union_2, &ctx, Position::invisible()).unwrap())
    }

    #[test]
    fn is_superset_contains() {
        let union_1 = Name::from(&vec![TrueName::from(BOOL), TrueName::from(INT)]);
        let union_2 = Name::from(INT);

        let ctx = Context::default().into_with_primitives().unwrap();
        assert!(union_1.is_superset_of(&union_2, &ctx, Position::invisible()).unwrap())
    }

    #[test]
    fn is_superset_any_only_one() {
        let union_1 = Name::from(&vec![TrueName::from(BOOL), TrueName::from(INT)]).is_interchangeable(true);
        let union_2 = Name::from(BOOL);

        let ctx = Context::default().into_with_primitives().unwrap();
        assert!(union_2.is_superset_of(&union_1, &ctx, Position::invisible()).unwrap())
    }

    #[test]
    fn is_superset_any_no_one() {
        let union_1 = Name::from(&vec![TrueName::from(BOOL), TrueName::from(INT)]).is_interchangeable(true);
        let union_2 = Name::from(RANGE);

        let ctx = Context::default().into_with_primitives().unwrap();
        assert!(!union_2.is_superset_of(&union_1, &ctx, Position::invisible()).unwrap())
    }

    #[test]
    fn is_superset_any_no_one_2() {
        let union_1 = Name::from(&vec![TrueName::from(INT)]).is_interchangeable(true);
        let union_2 = Name::from(STRING);

        let ctx = Context::default().into_with_primitives().unwrap();
        assert!(!union_2.is_superset_of(&union_1, &ctx, Position::invisible()).unwrap())
    }

    #[test]
    fn is_superset_any_nullable() {
        let union_1 = Name::from(&vec![TrueName::from(BOOL).as_nullable(), TrueName::from(INT)]).is_interchangeable(true);
        let union_2 = Name::from(BOOL);

        let ctx = Context::default().into_with_primitives().unwrap();
        assert!(!union_2.is_superset_of(&union_1, &ctx, Position::invisible()).unwrap())
    }

    #[test]
    fn nullabel_int_superset_int() {
        let true_name = TrueName::from(INT).as_nullable();
        let union_1 = Name::from(&true_name);
        let union_2 = Name::from(INT);

        let ctx = Context::default().into_with_primitives().unwrap();
        assert!(union_1.is_superset_of(&union_2, &ctx, Position::invisible()).unwrap())
    }

    #[test]
    fn int_not_superset_nullable_int() {
        let true_name = TrueName::from(INT).as_nullable();
        let union_1 = Name::from(INT);
        let union_2 = Name::from(&true_name);

        let ctx = Context::default().into_with_primitives().unwrap();
        assert!(!union_1.is_superset_of(&union_2, &ctx, Position::invisible()).unwrap())
    }

    #[test]
    fn is_superset_self() {
        let (name_1, name_2) = (Name::from(TUPLE), Name::from(TUPLE));
        let ctx = Context::default().into_with_primitives().unwrap();
        assert!(name_1.is_superset_of(&name_2, &ctx, Position::invisible()).unwrap())
    }

    #[test]
    fn int_not_superset_string() {
        let (name_1, name_2) = (Name::from(INT), Name::from(STRING));
        let ctx = Context::default().into_with_primitives().unwrap();
        assert!(!name_1.is_superset_of(&name_2, &ctx, Position::invisible()).unwrap())
    }

    #[test]
    fn test_temp_name() {
        let name1 = Name::from("@something");
        let name2 = Name::from(INT);

        assert!(name1.is_temporary());
        assert!(!name2.is_temporary())
    }

    #[test]
    fn test_superset_wrong_way() {
        let names = vec![
            TrueName::from(BOOL),
            TrueName::from(STRING),
            TrueName::from(INT),
            TrueName::from(FLOAT),
        ];
        let union_1 = Name::from(&names);
        let union_2 = Name::from(INT);

        let ctx = Context::default().into_with_primitives().unwrap();
        assert!(!union_2.is_superset_of(&union_1, &ctx, Position::invisible()).unwrap())
    }

    #[test]
    fn test_name_equal() {
        let name1 = Name::from("MyType");
        let name2 = Name::from("MyType");
        assert_eq!(name1, name2);
    }

    #[test]
    fn test_name_not_equal() {
        let name1 = Name::from("MyType");
        let name2 = Name::from("MyType2");
        assert_ne!(name1, name2);
    }

    #[test]
    fn test_name_is_nullable() {
        let name1 = Name::from("MyType").as_nullable();
        assert!(name1.is_nullable());
    }

    #[test]
    fn test_name_is_nullable_is_not_null() {
        let name1 = Name::from("MyType").as_nullable();
        assert!(!name1.is_null());
    }

    #[test]
    fn test_name_none_is_null() {
        let name1 = Name::from(clss::NONE);
        assert!(name1.is_null());
    }

    #[test]
    fn test_name_none_is_not_nullable() {
        let name1 = Name::from(clss::NONE);
        assert!(!name1.is_nullable());
    }

    #[test]
    fn test_name_nullable_not_equal() {
        let name1 = Name::from("MyType").as_nullable();
        let name2 = Name::from("MyType");
        assert_ne!(name1, name2);
    }

    #[test]
    fn test_tuple_equal() {
        let name1 = Name::tuple(&[Name::from("A1"), Name::from("A2")]);
        let name2 = Name::tuple(&[Name::from("A1"), Name::from("A2")]);
        assert_eq!(name1, name2)
    }

    #[test]
    fn test_tuple_not_equal_size() {
        let name1 = Name::tuple(&[Name::from("A1"), Name::from("A2")]);
        let name2 = Name::tuple(&[
            Name::from("A1"),
            Name::from("A2"),
            Name::from("A2"),
        ]);
        assert_ne!(name1, name2)
    }

    #[test]
    fn test_tuple_not_equal() {
        let name1 = Name::tuple(&[Name::from("A1"), Name::from("A2")]);
        let name2 = Name::tuple(&[Name::from("A2"), Name::from("A1")]);
        assert_ne!(name1, name2)
    }

    #[test]
    fn test_match_name() -> TypeResult<()> {
        let iden = Identifier::from((false, "abc"));
        let name = Name::from("MyType3");
        let matchings = match_name(&iden, &name, Position::invisible())?;

        assert_eq!(matchings.len(), 1);
        let (mutable, matching) = matchings["abc"].clone();
        assert!(!mutable);
        assert_eq!(matching, name);

        Ok(())
    }

    #[test]
    fn test_match_name_err_iden_is_tuple() {
        let iden = Identifier::from((false, "abc"));
        let iden2 = Identifier::from((false, "abc2"));
        let iden3 = Identifier::from(&vec![iden, iden2]);

        let name = Name::from("MyType3");
        let err = match_name(&iden3, &name, Position::invisible());

        assert!(err.is_err())
    }

    #[test]
    fn test_match_name_type_is_tuple() -> TypeResult<()> {
        let iden = Identifier::from((false, "abc"));
        let name = Name::tuple(&[Name::from("A2"), Name::from("A1")]);
        let matchings = match_name(&iden, &name, Position::invisible())?;

        assert_eq!(matchings.len(), 1);
        let (mutable, matching) = matchings["abc"].clone();
        assert!(!mutable);
        assert_eq!(matching, name);

        Ok(())
    }

    #[test]
    fn test_match_name_iden_and_type_is_tuple() -> TypeResult<()> {
        let iden = Identifier::from((false, "abc"));
        let iden2 = Identifier::from((false, "abc2"));
        let iden3 = Identifier::from(&vec![iden, iden2]);

        let name = Name::tuple(&[Name::from("A2"), Name::from("A1")]);

        let matchings = match_name(&iden3, &name, Position::invisible())?;

        assert_eq!(matchings.len(), 2);
        let (mutable1, matching1) = matchings["abc"].clone();
        let (mutable2, matching2) = matchings["abc2"].clone();

        assert!(!mutable1);
        assert!(!mutable2);
        assert_eq!(matching1, Name::from("A2"));
        assert_eq!(matching2, Name::from("A1"));

        Ok(())
    }

    #[test]
    fn test_match_name_iden_wrong_size_and_type() {
        let iden = Identifier::from((false, "abc"));
        let iden2 = Identifier::from((false, "abc2"));
        let iden3 = Identifier::from((false, "abc2"));
        let iden4 = Identifier::from(&vec![iden, iden2, iden3]);

        let name = Name::tuple(&[Name::from("A2"), Name::from("A1")]);

        let matchings = match_name(&iden4, &name, Position::invisible());
        assert!(matchings.is_err());
    }

    #[test]
    fn test_match_name_iden_and_type_wrong_size() {
        let iden = Identifier::from((false, "abc"));
        let iden2 = Identifier::from((false, "abc2"));
        let iden3 = Identifier::from(&vec![iden, iden2]);

        let name = Name::tuple(&[
            Name::from("A2"),
            Name::from("A1"),
            Name::from("A0"),
        ]);

        let matchings = match_name(&iden3, &name, Position::invisible());
        assert!(matchings.is_err());
    }

    #[test]
    fn range_has_collection_int_as_parent() -> TypeResult<()> {
        let range_name = Name::from(RANGE);
        let int_name = Name::from(INT);

        let ctx = Context::default().into_with_primitives().unwrap();
        let collection_ty = range_name.col_type(&ctx, Position::invisible())?;
        assert_eq!(collection_ty, Some(int_name));
        Ok(())
    }

    #[test]
    fn float_parent_of_int() -> TypeResult<()> {
        let float_name = Name::from(FLOAT);
        let int_name = StringName::from(INT);
        let ctx = Context::default().into_with_primitives().unwrap();

        let clss = ctx.class(&int_name, Position::invisible())?;
        assert!(clss.has_parent(&float_name, &ctx, Position::invisible())?);
        Ok(())
    }

    #[test]
    fn int_not_parent_of_float() -> TypeResult<()> {
        let float_name = StringName::from(FLOAT);
        let int_name = Name::from(INT);
        let ctx = Context::default().into_with_primitives().unwrap();

        let clss = ctx.class(&float_name, Position::invisible())?;
        assert!(!clss.has_parent(&int_name, &ctx, Position::invisible())?);
        Ok(())
    }

    #[test]
    fn slice_not_collection_int_as_parent() {
        let range_name = Name::from(clss::SLICE);

        let ctx = Context::default().into_with_primitives().unwrap();
        let collection_ty = range_name.col_type(&ctx, Position::invisible());
        assert!(collection_ty.is_err());
    }

    #[test]
    fn name_fold() {
        let int_name = Name::from(INT);
        let float_name = Name::from(FLOAT);

        let name1 = Name::from(&HashSet::from([int_name.clone(), float_name.clone()]));
        let name2 = [int_name, float_name].iter().fold(Name::empty(), |name, n| name.union(n));
        assert_eq!(name1, name2);
    }
}