aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/aedifex/repository.lux
blob: 6b4f0213004ec464e6a5008bc151720218d09b7c (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
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
(.require
 [library
  [lux (.except)
   [abstract
    [equivalence (.only Equivalence)]
    [hash (.only Hash)]
    ["[0]" monad (.only do)]]
   [control
    ["[0]" io]
    ["[0]" try (.only Try)]
    ["[0]" exception (.only Exception)]
    [concurrency
     ["[0]" async (.only Async)]]]
   [data
    ["[0]" product]
    ["[0]" binary (.only Binary)
     ["_[1]" \\test]]
    ["[0]" text (.only)
     ["%" \\format (.only format)]]
    [collection
     ["[0]" dictionary (.only Dictionary)]]]
   [math
    ["[0]" random (.only Random)]]
   [world
    [net
     ["[0]" uri (.only URI)]]]
   [test
    ["_" property (.only Test)]
    ["[0]" unit]]]]
 ["[0]" /
  ["[1][0]" identity]
  ["[1][0]" origin]
  ["[1][0]" local]
  ["[1][0]" remote]
  [//
   ["@[0]" artifact]]]
 [\\program
  ["[0]" / (.only)
   ["[0]" remote]
   ["/[1]" //
    ["[1][0]" artifact (.only Version Artifact)
     ["[1]/[0]" extension (.only Extension)]]]]])

(def .public (spec valid_artifact invalid_artifact subject)
  (-> Artifact Artifact (/.Repository Async)
      Test)
  (do random.monad
    [expected (_binary.random 100)]
    (in (all unit.and
             (do async.monad
               [.let [good_uri (remote.uri (the //artifact.#version valid_artifact) valid_artifact //artifact/extension.lux_library)]
                good_upload! (of subject upload good_uri expected)
                good_download! (of subject download good_uri)

                .let [bad_uri (remote.uri (the //artifact.#version invalid_artifact) invalid_artifact //artifact/extension.lux_library)]
                bad_upload! (of subject upload bad_uri expected)
                bad_download! (of subject download bad_uri)]
               (unit.coverage [/.Repository]
                 (let [successfull_flow!
                       (when [good_upload! good_download!]
                         [{try.#Success _} {try.#Success actual}]
                         (of binary.equivalence = expected actual)

                         _
                         false)

                       failed_flow!
                       (when [bad_upload! bad_download!]
                         [{try.#Failure _} {try.#Failure _}]
                         true

                         _
                         false)]
                   (and successfull_flow!
                        failed_flow!))))
             ))))

(def artifact
  (-> Version Artifact)
  (|>> ["com.github.luxlang" "test-artifact"]))

(with_template [<name>]
  [(exception.def (<name> uri)
     (Exception URI)
     (exception.report
      (list ["URI" (%.text uri)])))]

  [not_found]
  [cannot_upload]
  )

(type Store
  (Dictionary URI Binary))

(def .public empty
  Store
  (dictionary.empty text.hash))

(def valid_version
  Version
  "1.2.3-YES")

(def invalid_version
  Version
  "4.5.6-NO")

(def .public mock
  (/.Mock Store)
  (implementation
   (def the_description
     "@")
   (def (on_download uri state)
     (when (dictionary.value uri state)
       {.#Some content}
       (when (binary.size content)
         0 (exception.except ..not_found [uri])
         _ {try.#Success [state content]})
       
       {.#None}
       (exception.except ..not_found [uri])))
   (def (on_upload uri content state)
     (if (dictionary.key? state uri)
       (exception.except ..cannot_upload [uri])
       {try.#Success (dictionary.has uri content state)}))))

(def .public test
  Test
  (<| (_.covering /._)
      (all _.and
           (_.for [/.mock /.Mock]
                  (do random.monad
                    [_ (in [])]
                    (..spec (..artifact ..valid_version)
                            (..artifact ..invalid_version)
                            (/.mock ..mock
                                    (|> ..empty
                                        (dictionary.has (remote.uri ..invalid_version
                                                                    (..artifact ..invalid_version)
                                                                    //artifact/extension.lux_library)
                                                        (binary.empty 0)))))))

           /identity.test
           /origin.test
           /local.test
           /remote.test
           )))