Test case that generates expressions

This commit is contained in:
2021-08-04 16:48:31 -06:00
parent 960ab35314
commit e6b42dbaf2
4 changed files with 79 additions and 0 deletions

View File

@@ -76,6 +76,7 @@ class Kiss {
"count" => Symbol("Lambda.count"),
"enumerate" => Symbol("Prelude.enumerate"),
"assertProcess" => Symbol("Prelude.assertProcess"),
"random" => Symbol("Std.random"),
// These work with (apply) because they are added as "opAliases" in Macros.kiss:
"min" => Symbol("Prelude.min"),
"max" => Symbol("Prelude.max"),
@@ -93,6 +94,8 @@ class Kiss {
"<" => Symbol("Prelude.lessThan"),
"<=" => Symbol("Prelude.lesserEqual"),
"=" => Symbol("Prelude.areEqual"),
// These ones *probably* won't conflict with variables and might be passed as functions
"chooseRandom" => Symbol("Prelude.chooseRandom"),
// These ones *probably* won't conflict with variables and might commonly be used with (apply) because they are variadic
"concat" => Symbol("Prelude.concat"),
"zipKeep" => Symbol("Prelude.zipKeep"),

View File

@@ -301,6 +301,10 @@ class Prelude {
}
}
public static function chooseRandom<T>(l:kiss.List<T>) {
return l[Std.random(l.length)];
}
// Based on: http://old.haxe.org/doc/snip/memoize
public static function memoize(func:Function, ?caller:Dynamic):Function {
var argMap = new Map<String, Dynamic>();

View File

@@ -0,0 +1,17 @@
package test.cases;
import kiss.Prelude;
import kiss.List;
import utest.Test;
import utest.Assert;
@:build(kiss.Kiss.build())
class GenerativeTestCase extends Test {
function testTruthy() {
_testTruthy();
}
function testFalsy() {
_testFalsy();
}
}

View File

@@ -0,0 +1,55 @@
(defMacroVar maxInt 1000000000)
(defMacroVar maxStringLength 20)
(defMacroVar maxExps 4)
(defMacroFunction _macroList [func length]
~(for _ (range length) (func)))
(defMacro macroRepeat [exp length]
`(begin ,@(_macroList ->{exp} (eval length))))
(defMacroFunction _randomLengthMacroList [func]
(_macroList func (+ 2 (random (- maxExps 2)))))
(defMacroFunction _randomLetterString []
(ReaderExp.StrExp (apply + (for _ (range (random maxStringLength)) (chooseRandom (.split "abcdefghijklmnopqrstuvwxyz" ""))))))
(defMacroFunction _randomFalsyExp []
((chooseRandom
[
->{`null}
->{`false}
->{`""}
->{`[]}
->{`(or ,@(_randomLengthMacroList _randomFalsyExp))}
->{`(and
,@(_randomLengthMacroList _randomUncertainExp)
(_randomFalsyExp)
,@(_randomLengthMacroList _randomUncertainExp))}
])))
(defMacro randomFalsyExp []
~(_randomFalsyExp))
(defMacroFunction _randomTruthyExp []
((chooseRandom
[
->{`true}
->{(_randomLetterString)}
])))
(defMacro randomTruthyExp []
~(_randomTruthyExp))
(defMacroFunction _randomUncertainExp []
((chooseRandom
[
->(_randomFalsyExp)
->(_randomTruthyExp)
])))
(defMacro randomUncertainExp []
~(_randomUncertainExp))
(function _testTruthy []
(macroRepeat (Assert.isTrue ?(randomTruthyExp)) maxExps))
(function _testFalsy []
(macroRepeat (Assert.isFalse ?(randomFalsyExp)) maxExps))