aboutsummaryrefslogtreecommitdiff
path: root/stdlib/test/test/lux/data/name.lux
blob: 16ab67f5966352c138994183aeb706186c68de01 (plain)
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
(.module:
  [lux #*
   [control
    [monad (#+ do Monad)]
    pipe]
   [data
    ["&" name]
    ["." text ("text/." Equivalence<Text>)
     format]]
   [math
    ["r" random]]]
  lux/test)

(def: (gen-part size)
  (-> Nat (r.Random Text))
  (|> (r.unicode size) (r.filter (|>> (text.contains? ".") not))))

(context: "Names"
  (<| (times |100)
      (do @
        [## First Name
         sizeM1 (|> r.nat (:: @ map (n/% |100)))
         sizeN1 (|> r.nat (:: @ map (|>> (n/% |100) (n/max |1))))
         module1 (gen-part sizeM1)
         short1 (gen-part sizeN1)
         #let [name1 [module1 short1]]
         ## Second Name
         sizeM2 (|> r.nat (:: @ map (n/% |100)))
         sizeN2 (|> r.nat (:: @ map (|>> (n/% |100) (n/max |1))))
         module2 (gen-part sizeM2)
         short2 (gen-part sizeN2)
         #let [name2 [module2 short2]]
         #let [(^open "&/.") &.Equivalence<Name>
               (^open "&/.") &.Codec<Text,Name>]]
        ($_ seq
            (test "Can get the module & short parts of an name."
                  (and (is? module1 (&.module name1))
                       (is? short1 (&.short name1))))

            (test "Can compare names for equivalence."
                  (and (&/= name1 name1)
                       (if (&/= name1 name2)
                         (and (text/= module1 module2)
                              (text/= short1 short2))
                         (or (not (text/= module1 module2))
                             (not (text/= short1 short2))))))

            (test "Can encode names as text."
                  (|> name1
                      &/encode &/decode
                      (case> (#.Right dec-name) (&/= name1 dec-name)
                             _ #0)))

            (test "Encoding an name without a module component results in text equal to the short of the name."
                  (if (text.empty? module1)
                    (text/= short1 (&/encode name1))
                    #1))
            ))))

(context: "Name-related macros."
  (let [(^open "&/.") &.Equivalence<Name>]
    ($_ seq
        (test "Can obtain Name from identifier."
              (and (&/= ["lux" "yolo"] (name-of .yolo))
                   (&/= ["test/lux/data/name" "yolo"] (name-of ..yolo))
                   (&/= ["" "yolo"] (name-of yolo))
                   (&/= ["lux/test" "yolo"] (name-of lux/test.yolo))))
        
        (test "Can obtain Name from tag."
              (and (&/= ["lux" "yolo"] (name-of #.yolo))
                   (&/= ["test/lux/data/name" "yolo"] (name-of #..yolo))
                   (&/= ["" "yolo"] (name-of #yolo))
                   (&/= ["lux/test" "yolo"] (name-of #lux/test.yolo)))))))