aboutsummaryrefslogtreecommitdiff
path: root/stdlib/test
diff options
context:
space:
mode:
authorEduardo Julian2018-05-15 19:52:04 -0400
committerEduardo Julian2018-05-15 19:52:04 -0400
commit4242e4d3b18eb532ae18e8b38e85ad1ee1988e02 (patch)
tree96f25b4ed5e428eea5c8bb4532a228b84d1f1b7b /stdlib/test
parentbb2ec42843ba0f13adafe1f2f4a7b2820fbcaafa (diff)
- Migrated primitive analysis to stdlib.
Diffstat (limited to 'stdlib/test')
-rw-r--r--stdlib/test/test/lux/lang/analysis/primitive.lux63
1 files changed, 63 insertions, 0 deletions
diff --git a/stdlib/test/test/lux/lang/analysis/primitive.lux b/stdlib/test/test/lux/lang/analysis/primitive.lux
new file mode 100644
index 000000000..2e7c2057a
--- /dev/null
+++ b/stdlib/test/test/lux/lang/analysis/primitive.lux
@@ -0,0 +1,63 @@
+(.module:
+ lux
+ (lux [io]
+ (control [monad #+ do]
+ pipe
+ ["ex" exception #+ exception:])
+ (data (text format)
+ ["e" error])
+ ["r" math/random]
+ [macro]
+ (macro [code])
+ (lang [".L" type "type/" Eq<Type>]
+ [".L" init]
+ [analysis #+ Analysis]
+ (analysis [".A" type]
+ ["/" primitive]))
+ test))
+
+(exception: (wrong-inference {expected Type} {inferred Type})
+ (format "Expected: " (%type expected) "\n"
+ "Inferred: " (%type inferred) "\n"))
+
+(def: (infer-primitive expected-type analysis)
+ (-> Type (Meta Analysis) (e.Error Analysis))
+ (|> (typeA.with-inference
+ analysis)
+ (macro.run (initL.compiler []))
+ (case> (#e.Success [inferred-type output])
+ (if (is? expected-type inferred-type)
+ (#e.Success output)
+ (ex.throw wrong-inference [expected-type inferred-type]))
+
+ (#e.Error error)
+ (#e.Error error))))
+
+(context: "Primitives"
+ (<| (times +100)
+ (`` ($_ seq
+ (test "Can analyse unit."
+ (|> (infer-primitive Top /.unit)
+ (case> (^ (#e.Success (#analysis.Primitive (#analysis.Unit output))))
+ (is? [] output)
+
+ _
+ false)))
+ (~~ (do-template [<desc> <type> <tag> <random> <analyser>]
+ [(do @
+ [sample <random>]
+ (test (format "Can analyse " <desc> ".")
+ (|> (infer-primitive <type> (<analyser> sample))
+ (case> (#e.Success (#analysis.Primitive (<tag> output)))
+ (is? sample output)
+
+ _
+ false))))]
+
+ ["bool" Bool #analysis.Bool r.bool /.bool]
+ ["nat" Nat #analysis.Nat r.nat /.nat]
+ ["int" Int #analysis.Int r.int /.int]
+ ["deg" Deg #analysis.Deg r.deg /.deg]
+ ["frac" Frac #analysis.Frac r.frac /.frac]
+ ["text" Text #analysis.Text (r.unicode +5) /.text]
+ ))))))