Test case that generates expressions
This commit is contained in:
@@ -76,6 +76,7 @@ class Kiss {
|
|||||||
"count" => Symbol("Lambda.count"),
|
"count" => Symbol("Lambda.count"),
|
||||||
"enumerate" => Symbol("Prelude.enumerate"),
|
"enumerate" => Symbol("Prelude.enumerate"),
|
||||||
"assertProcess" => Symbol("Prelude.assertProcess"),
|
"assertProcess" => Symbol("Prelude.assertProcess"),
|
||||||
|
"random" => Symbol("Std.random"),
|
||||||
// These work with (apply) because they are added as "opAliases" in Macros.kiss:
|
// These work with (apply) because they are added as "opAliases" in Macros.kiss:
|
||||||
"min" => Symbol("Prelude.min"),
|
"min" => Symbol("Prelude.min"),
|
||||||
"max" => Symbol("Prelude.max"),
|
"max" => Symbol("Prelude.max"),
|
||||||
@@ -93,6 +94,8 @@ class Kiss {
|
|||||||
"<" => Symbol("Prelude.lessThan"),
|
"<" => Symbol("Prelude.lessThan"),
|
||||||
"<=" => Symbol("Prelude.lesserEqual"),
|
"<=" => Symbol("Prelude.lesserEqual"),
|
||||||
"=" => Symbol("Prelude.areEqual"),
|
"=" => 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
|
// These ones *probably* won't conflict with variables and might commonly be used with (apply) because they are variadic
|
||||||
"concat" => Symbol("Prelude.concat"),
|
"concat" => Symbol("Prelude.concat"),
|
||||||
"zipKeep" => Symbol("Prelude.zipKeep"),
|
"zipKeep" => Symbol("Prelude.zipKeep"),
|
||||||
|
|||||||
@@ -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
|
// Based on: http://old.haxe.org/doc/snip/memoize
|
||||||
public static function memoize(func:Function, ?caller:Dynamic):Function {
|
public static function memoize(func:Function, ?caller:Dynamic):Function {
|
||||||
var argMap = new Map<String, Dynamic>();
|
var argMap = new Map<String, Dynamic>();
|
||||||
|
|||||||
17
kiss/src/test/cases/GenerativeTestCase.hx
Normal file
17
kiss/src/test/cases/GenerativeTestCase.hx
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
55
kiss/src/test/cases/GenerativeTestCase.kiss
Normal file
55
kiss/src/test/cases/GenerativeTestCase.kiss
Normal 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))
|
||||||
Reference in New Issue
Block a user