Initial commit

This commit is contained in:
Peter Niederwieser
2016-01-19 14:51:19 +01:00
committed by Dan Chao
commit ecad035dca
2972 changed files with 211653 additions and 0 deletions
@@ -0,0 +1 @@
result = IntSeq(1, 10).fold(0, (result, next) -> result + next)
@@ -0,0 +1,72 @@
/// module doc comment
@MyAnn { text = "module annotation" }
open module api.reflect.BaseModule
/// module property doc comment
@MyAnn { text = "module property annotation" }
int = 42
float = 12.3
string = "hello"
boolean = true
duration = 42.h
dataSize = 12.3.gb
pair = Pair(42, "hello")
list = List(1, 2, 3)
set = Set(1, 2, 3)
map = Map(1, "one", 2, "two", 3, "three")
listing = new Listing { 1; 2; 3 }
mapping = new Mapping { [1] = "one"; [2] = "two"; [3] = "three" }
dynamic = new Dynamic { name = "Pigeon"; age = 42 }
typed = new Person { age = 42 }
/// module method doc comment
@MyAnn { text = "module method annotation" }
function sing(person, songs) = "\(person.name) sings \(songs[0])"
int2: Int = 42
float2: Float = 12.3
string2: String = "hello"
boolean2: Boolean = true
duration2: Duration = 42.h
dataSize2: DataSize = 12.3.gb
pair2: Pair<Int, String> = Pair(42, "hello")
list2: List<Int> = List(1, 2, 3)
set2: Set<Int> = Set(1, 2, 3)
map2: Map<Int, String> = Map(1, "one", 2, "two", 3, "three")
listing2: Listing<Int> = new Listing { 1; 2; 3 }
mapping2: Mapping<Int, String> = new Mapping { [1] = "one"; [2] = "two"; [3] = "three" }
dynamic2: Dynamic = new Dynamic { name = "Pigeon"; age = 42 }
typed2: Person = new Person { age = 42 }
function sing2(person: Person, songs: List<String>): String = "\(person.name) sings \(songs[0])"
any: Any
noth: nothing
unkn: unknown
union: Int|List<String>|Person
nullable: Person?
stringLiteral: "yes"
constrained: String(length.isBetween(3, 10))
aliased: MyMap<Person>
/// class doc comment
@MyAnn { text = "class annotation" }
open class Person {
name: String = "Pigeon"
/// class property doc comment
@MyAnn { text = "class property annotation"}
age: Int = 42
/// class method doc comment
@MyAnn { text = "class method annotation" }
function sing(songs: List<String>): String = "\(name) sings \(songs[0])"
}
class MyAnn extends Annotation {
text: String
}
/// type alias doc comment
@MyAnn { text = "type alias annotation" }
typealias MyMap<V> = Map<String, V>(isEmpty)
@@ -0,0 +1,10 @@
/// extending module doc comment
@MyAnn { text = "extending module annotation" }
extends "BaseModule.pkl"
property: Person2
function method(): Person = new {}
class Person2 extends Person
@@ -0,0 +1,5 @@
/// amending module doc comment
@MyAnn { text = "amending module annotation" }
amends "BaseModule.pkl"
int = 22
@@ -0,0 +1,30 @@
import "pkl:reflect"
import "BaseModule.pkl"
referenceModule = "BaseModule.pkl"
function MyAnn(message: String): List<BaseModule.MyAnn> = List(new BaseModule.MyAnn {
text = message
})
function method(m: reflect.Method, paramTypes: List<reflect.Type>, returnType: reflect.Type): Dynamic = new {
hasExpectedLocation = m.location.displayUri.contains(referenceModule)
docComment = m.docComment
annotations = m.annotations
modifiers = m.modifiers
name = m.name
typeParameters = m.typeParameters
parameterNames = m.parameters.keys
hasExpectedParameterTypes = m.parameters.values.map((it) -> it.type) == paramTypes
hasExpectedReturnType = m.returnType == returnType
}
function property(p: reflect.Property, expectedType: reflect.Type): Dynamic = new {
hasExpectedLocation = p.location.displayUri.contains(referenceModule)
docComment = p.docComment
annotations = p.annotations
modifiers = p.modifiers
name = p.name
defaultValue = p.defaultValue
hasExpectedType = p.type == expectedType
}
@@ -0,0 +1 @@
name = "child/moduleC"
@@ -0,0 +1 @@
child/
@@ -0,0 +1 @@
name = "moduleA"
@@ -0,0 +1 @@
name = "moduleB"
@@ -0,0 +1,70 @@
/// doc comment
/// doc comment
@Ann
@Ann2 { x = "foo" }
@Ann3 {
x = "foo"
y = 1
}
module some.mod
/// doc comment
/// doc comment
@Ann
@Ann2 { x = "foo" }
@Ann3 {
x = "foo"
y = 1
}
class SomeClass {
/// doc comment
/// doc comment
@Ann
@Ann2 { x = "foo" }
@Ann3 {
x = "foo"
y = 1
}
function someMethod() = "hi"
/// doc comment
/// doc comment
@Ann
@Ann2 { x = "foo" }
@Ann3 {
x = "foo"
y = 1
}
someProperty: String = "hi"
}
/// doc comment
/// doc comment
@Ann
@Ann2 { x = "foo" }
@Ann3 {
x = "foo"
y = 1
}
function someMethod() = "hi"
/// doc comment
/// doc comment
@Ann
@Ann2 { x = "foo" }
@Ann3 {
x = "foo"
y = 1
}
someProperty: String = "hi"
class Ann extends Annotation
class Ann2 extends Annotation {
x: String
}
class Ann3 extends Annotation {
x: String
y: Int
}
@@ -0,0 +1,4 @@
class NoAnn
@NoAnn
invalidAnnotationOnTypeCheckedProperty = "hi"
@@ -0,0 +1,4 @@
class NoAnn
@NoAnn
invalidAnnotationOnTypedProperty: String = "hi"
@@ -0,0 +1,5 @@
@Mapping {
a = "hi"
b = "hi"
}
someProperty: String = "hi"
@@ -0,0 +1,3 @@
integer: Int = 1
@integer someProperty: String = "hi"
@@ -0,0 +1 @@
@Int someProperty: String = "hi"
@@ -0,0 +1,6 @@
class MyAnn extends Annotation
local myAnn = MyAnn
@myAnn
foo = 1
@@ -0,0 +1,6 @@
class Ann1 extends Annotation
class Ann2 extends Annotation
@Ann1|Ann2
foo = 1
@@ -0,0 +1,43 @@
open module deprecated1
function f1(n) = n * 1000 + 1
@Deprecated
function f2(n) = n * 1000 + 2
class A {
@Deprecated
function f(n) = n * 1000 + 101
}
open class B {
function f1(n) = n * 1000 + 201
@Deprecated
function f2(n) = n * 1000 + 202
}
class C extends B {
@Deprecated
function f1(n) = n * 1000 + 301
}
res1 = f1(100)
res1b = List(1, 2, 3).map((n) -> f1(n))
res2 = f2(200)
res2b = List(4, 5, 6).map((n) -> f2(n))
res3 = new A {}.f(300)
res3b = List(7, 8, 9).map((n) -> new A {}.f(n))
res4 = new B {}.f1(400)
res4b = List(10, 11, 12).map((n) -> new B {}.f1(n))
res5 = new B {}.f2(500)
res5b = List(13, 14, 15).map((n) -> new B {}.f2(n))
res6 = new C {}.f1(600)
res6b = List(16, 17, 18).map((n) -> new C {}.f1(n))
res7 = new C {}.f2(700)
res7b = List(19, 20, 21).map((n) -> new C {}.f2(n))
@@ -0,0 +1,12 @@
module deprecated2
extends "deprecated1.pkl"
@Deprecated
function f1(n) = -1 * (n * 1000 + 1)
res1 = f1(800)
res1b = List(22, 23, 24).map((n) -> f1(n))
res2 = f2(900)
res2b = List(25, 26, 27).map((n) -> f2(n))
@@ -0,0 +1,16 @@
module deprecated3
import "deprecated1.pkl"
import "deprecated2.pkl"
res1 = deprecated1.f1(1000)
res1b = List(28, 29, 30).map((n) -> deprecated1.f1(n))
res2 = deprecated1.f2(1100)
res2b = List(31, 32, 33).map((n) -> deprecated1.f2(n))
res3 = deprecated2.f1(1200)
res3b = List(34, 35, 36).map((n) -> deprecated2.f1(n))
res4 = deprecated2.f2(1300)
res4b = List(37, 38, 39).map((n) -> deprecated2.f2(n))
@@ -0,0 +1,10 @@
@Deprecated
function foo() = 0
function bar() = foo()
shouldWarnForFirstCallToFooFromBar = bar()
shouldNotWarnForRepeatedCallToFooFromBar1 = bar()
shouldNotWarnForRepeatedCallToFooFromBar2 = bar()
shouldWarnForDirectCallToFoo = foo()
shouldWarnForRepeatedDirectCallToFoo = foo()
@@ -0,0 +1,43 @@
open module deprecatedWithMessage1
function f1(n) = n * 1000 + 1
@Deprecated { message = "use f1 instead"}
function f2(n) = n * 1000 + 2
class A {
@Deprecated { message = "use B.f1 instead" }
function f(n) = n * 1000 + 101
}
open class B {
function f1(n) = n * 1000 + 201
@Deprecated { message = "use this.f1 instead"}
function f2(n) = n * 1000 + 202
}
class C extends B {
@Deprecated { message = "avoid using C.f1" }
function f1(n) = n * 1000 + 301
}
res1 = f1(100)
res1b = List(1, 2, 3).map((n) -> f1(n))
res2 = f2(200)
res2b = List(4, 5, 6).map((n) -> f2(n))
res3 = new A {}.f(300)
res3b = List(7, 8, 9).map((n) -> new A {}.f(n))
res4 = new B {}.f1(400)
res4b = List(10, 11, 12).map((n) -> new B {}.f1(n))
res5 = new B {}.f2(500)
res5b = List(13, 14, 15).map((n) -> new B {}.f2(n))
res6 = new C {}.f1(600)
res6b = List(16, 17, 18).map((n) -> new C {}.f1(n))
res7 = new C {}.f2(700)
res7b = List(19, 20, 21).map((n) -> new C {}.f2(n))
@@ -0,0 +1,12 @@
module deprecatedWithMessage2
extends "deprecatedWithMessage1.pkl"
@Deprecated { message = "avoid using f1" }
function f1(n) = -1 * (n * 1000 + 1)
res1 = f1(800)
res1b = List(22, 23, 24).map((n) -> f1(n))
res2 = f2(900)
res2b = List(25, 26, 27).map((n) -> f2(n))
@@ -0,0 +1,16 @@
module deprecatedWithMessage3
import "deprecatedWithMessage1.pkl"
import "deprecatedWithMessage2.pkl"
res1 = deprecatedWithMessage1.f1(1000)
res1b = List(28, 29, 30).map((n) -> deprecatedWithMessage1.f1(n))
res2 = deprecatedWithMessage1.f2(1100)
res2b = List(31, 32, 33).map((n) -> deprecatedWithMessage1.f2(n))
res3 = deprecatedWithMessage2.f1(1200)
res3b = List(34, 35, 36).map((n) -> deprecatedWithMessage2.f1(n))
res4 = deprecatedWithMessage2.f2(1300)
res4b = List(37, 38, 39).map((n) -> deprecatedWithMessage2.f2(n))
@@ -0,0 +1,22 @@
open class Foo {
@Deprecated
function bar() = 40
@Deprecated { message="Use only Foo#qux" }
function baz() = "baz"
function qux() = 0
}
class Bar extends Foo {
function bar() = super.bar() + 2
function baz() = super.baz() + " was called"
function qux() = super.qux() - 1
}
barCall = new Bar {}.bar()
bazCall = new Bar {}.baz()
quxCall = new Bar {}.qux()
barCall2ShouldNotWarn = new Bar {}.bar()
bazCall2ShouldNotWarn = new Bar {}.baz()
@@ -0,0 +1,18 @@
examples {
["text"] {
""
"The quick brown fox jumps over the lazy dog."
}
["md5"] {
"d41d8cd98f00b204e9800998ecf8427e"
"e4d909c290d0fb1ca068ffaddf22cbd0"
}
["sha1"] {
"da39a3ee5e6b4b0d3255bfef95601890afd80709"
"408d94384216f890ff7a0c3528e8bed1e0b01621"
}
["sha256"] {
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
"ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c"
}
}
@@ -0,0 +1,31 @@
amends "../snippetTest.pkl"
examples {
local empty = read("empty.txt")
local fox = read("fox.txt")
["text"] {
empty.text
fox.text
}
["md5"] {
empty.md5
fox.md5
}
["sha1"] {
empty.sha1
fox.sha1
}
["sha256"] {
empty.sha256
fox.sha256
}
["sha256Int"] {
empty.sha256Int
fox.sha256Int
}
}
@@ -0,0 +1,62 @@
amends "../snippetTest.pkl"
examples {
["getClass()"] {
1.getClass().simpleName
1.2.getClass().simpleName
true.getClass().simpleName
"hello".getClass().simpleName
5.min.getClass().simpleName
5.mb.getClass().simpleName
List(1, 2, 3).getClass().simpleName
Map("one", 1, "two", 2).getClass().simpleName
new Listing { 1; 2; 3 }.getClass().simpleName
new Mapping { ["one"] = 1; ["two"] = 2 }.getClass().simpleName
new Dynamic { x = 1; y = 2 }.getClass().simpleName
new Person { name = "Pigeon" }.getClass().simpleName
Person.getClass().simpleName
null.getClass().simpleName
Pair(1, 2).getClass().simpleName
import("pkl:base").getClass().simpleName
}
["ifNonNull"] {
local s = 10
s.ifNonNull((x) -> x)
s.ifNonNull((x) -> "foo")
s.ifNonNull((x) -> null)
null.ifNonNull((x) -> x)
null.ifNonNull((x) -> "foo")
null.ifNonNull((x) -> null)
}
["toString()"] {
"Pigeon".toString()
42.toString()
1.23.toString()
true.toString()
1.23.ns.toString()
1.23.mb.toString()
Pair(1, 2).toString()
List(1, 2, 3).toString()
Set(1, 2, 3).toString()
Map(1, 2, 3, 4).toString()
new Listing { 1 2 3 }.toString()
new Mapping { ["Pigeon"] = 42; ["Barn Owl"] = 21 }.toString()
new Dynamic { name = "Pigeon"; age = 42 }.toString()
new Person2 { name = "Pigeon"; age = 42 }.toString()
null.toString()
Null(new Dynamic { name = "Pigeon" }).toString()
}
}
local class Person {
name: String
}
local class Person2 {
name: String
age: Int
}
@@ -0,0 +1,33 @@
class User {
name: String
}
class Dog {
age: Int
}
class Env {
type: "test"|"prod"
}
foo = new Listing {
new User {
name = "Pigeon"
}
new Dog {
age = 5
}
new Env {
type = "prod"
}
"42"
}
output {
renderer = new PcfRenderer {
converters {
[Any] = (o) -> if (o is module) o else "Unconverted class: \(o.getClass())"
[Dog] = (d) -> "Dog(\(d.age))"
}
}
}
@@ -0,0 +1,38 @@
amends "../snippetTest.pkl"
examples {
["TODO()"] {
module.catch(() -> TODO())
}
["Undefined()"] {
module.catch(() -> Undefined())
module.catch(() -> obj.foo.bar.baz)
local obj = new {
foo {
bar {
baz = 3 + Undefined()
}
}
}
}
["Regex"] {
Regex("")
Regex(#"[ab]\s+"#)
module.catch(() -> Regex("("))
}
["Pair()"] {
Pair(1, "two")
}
["NaN"] {
NaN
}
["Infinity"] {
Infinity
-Infinity
}
}
@@ -0,0 +1,90 @@
amends ".../snippetTest.pkl"
import "pkl:Benchmark"
facts {
["run microbenchmark"] {
local benchmark = new Benchmark.Microbenchmark {
expression = IntSeq(1, 10).fold(0, (result, next) -> result + next)
iterations = 3
iterationTime = 1.ms
isVerbose = false
}
local result = benchmark.run()
result.iterations == 3
result.repetitions >= 1
result.samples == null
result.min > 0.s
result.max >= result.min
result.mean >= result.min
result.mean <= result.max
}
["run output benchmark (in verbose mode)"] {
local benchmark = new Benchmark.OutputBenchmark {
sourceModule = import("../../input-helper/api/benchmark/moduleToBenchmark.pkl")
iterations = 3
iterationTime = 1.ms
isVerbose = true
}
local result = benchmark.run()
result.iterations == 3
result.repetitions >= 1
result.samples != null
result.samples.length == 3
result.min == result.samples.min
result.max == result.samples.max
result.mean >= result.min
result.mean <= result.max
}
["run parser benchmark"] {
local benchmark = new Benchmark.ParserBenchmark {
sourceText = "result = IntSeq(1, 10).fold(0, (result, next) -> result + next)"
iterations = 3
iterationTime = 1.ms
isVerbose = false
}
local result = benchmark.run()
result.iterations == 3
result.repetitions >= 1
result.samples == null
result.min > 0.s
result.max >= result.min
result.mean >= result.min
result.mean <= result.max
}
["evaluate entire benchmark module"] {
local benchmark = new Benchmark {
iterations = 3
iterationTime = 1.ms
microbenchmarks {
["micro"] {
expression = IntSeq(1, 10).fold(0, (result, next) -> result + next)
}
}
outputBenchmarks {
["output"] {
sourceModule = import("../../input-helper/api/benchmark/moduleToBenchmark.pkl")
}
}
parserBenchmarks {
["parser"] {
sourceText = "result = IntSeq(1, 10).fold(0, (result, next) -> result + next)"
}
}
}
local text = benchmark.output.text
text.contains("[\"micro\"]")
text.contains("[\"output\"]")
text.contains("[\"parser\"]")
}
}
@@ -0,0 +1,160 @@
amends "../snippetTest.pkl"
facts {
["isPositive"] {
0.mb.isPositive
0.0.mb.isPositive
1.mb.isPositive
0.1.mb.isPositive
(-0).mb.isPositive
(-0.0).mb.isPositive
!(-1).mb.isPositive
!(-0.1).mb.isPositive
}
["isBinaryUnit"] {
1.pib.isBinaryUnit
2.tib.isBinaryUnit
3.gib.isBinaryUnit
4.mib.isBinaryUnit
5.kib.isBinaryUnit
6.b.isBinaryUnit
!1.pb.isBinaryUnit
!2.tb.isBinaryUnit
!3.gb.isBinaryUnit
!4.mb.isBinaryUnit
!5.kb.isBinaryUnit
}
["isDecimalUnit"] {
1.pb.isDecimalUnit
2.tb.isDecimalUnit
3.gb.isDecimalUnit
4.mb.isDecimalUnit
5.kb.isDecimalUnit
6.b.isDecimalUnit
!1.pib.isDecimalUnit
!2.tib.isDecimalUnit
!3.gib.isDecimalUnit
!4.mib.isDecimalUnit
!5.kib.isDecimalUnit
}
["isBetween"] {
3.kb.isBetween(2.kb, 4.kb)
3.kb.isBetween(3.kb, 4.kb)
3.kb.isBetween(2.kb, 3.kb)
3.kb.isBetween(3.kb, 3.kb)
3.kb.isBetween(2000.b, 3000.b)
!3.kb.isBetween(1.kb, 2.kb)
!3.kb.isBetween(4.kb, 2.kb)
3.3.kb.isBetween(2.2.kb, 4.4.kb)
3.3.kb.isBetween(3.3.kb, 4.4.kb)
3.3.kb.isBetween(2.2.kb, 3.3.kb)
3.3.kb.isBetween(3.3.kb, 3.3.kb)
3.3.kb.isBetween(2000.b, 3300.b)
!3.3.kb.isBetween(1.1.kb, 2.2.kb)
!3.3.kb.isBetween(4.4.kb, 2.2.kb)
}
}
examples {
["value"] {
1.b.value
2.2.kb.value
3.kib.value
4.4.mb.value
5.mib.value
6.6.gb.value
7.gib.value
8.8.tb.value
9.tib.value
10.1.pb.value
11.pib.value
List(1, 2.2, 3).map((d) -> d.mb.value)
}
["unit"] {
1.b.unit
2.2.kb.unit
3.kib.unit
4.4.mb.unit
5.mib.unit
6.6.gb.unit
7.gib.unit
8.8.tb.unit
9.tib.unit
10.1.pb.unit
11.pib.unit
}
["toUnit()"] {
1.pb.toUnit("pb")
1.pb.toUnit("tb")
1.pb.toUnit("gb")
1.pb.toUnit("mb")
1.pb.toUnit("kb")
1.pb.toUnit("b")
local b = 1.pb.toUnit("b")
b.toUnit("pb")
b.toUnit("tb")
b.toUnit("gb")
b.toUnit("mb")
b.toUnit("kb")
b.toUnit("b")
1.pib.toUnit("pib")
1.pib.toUnit("tib")
1.pib.toUnit("gib")
1.pib.toUnit("mib")
1.pib.toUnit("kib")
1.pib.toUnit("b")
local b2 = 1.pib.toUnit("b")
b2.toUnit("pib")
b2.toUnit("tib")
b2.toUnit("gib")
b2.toUnit("mib")
b2.toUnit("kib")
b2.toUnit("b")
module.catch(() -> 1.pb.toUnit("foo"))
0.5.gb.toUnit("kb")
0.5.gb.toUnit("gib")
}
["toBinaryUnit()"] {
1.024.pb.toBinaryUnit()
1.024.tb.toBinaryUnit()
1.024.gb.toBinaryUnit()
1.024.mb.toBinaryUnit()
1.024.kb.toBinaryUnit()
1.024.b.toBinaryUnit()
1.024.pib.toBinaryUnit()
1.024.tib.toBinaryUnit()
1.024.gib.toBinaryUnit()
1.024.mib.toBinaryUnit()
1.024.kib.toBinaryUnit()
}
["toDecimalUnit()"] {
1.pb.toDecimalUnit()
1.tb.toDecimalUnit()
1.gb.toDecimalUnit()
1.mb.toDecimalUnit()
1.kb.toDecimalUnit()
1.b.toDecimalUnit()
1.pib.toDecimalUnit()
1.tib.toDecimalUnit()
1.gib.toDecimalUnit()
1.mib.toDecimalUnit()
1.kib.toDecimalUnit()
}
}
@@ -0,0 +1,3 @@
import "pkl:test"
res1 = test.catch(() -> module.relativePathTo(import("../../module.pkl")))
@@ -0,0 +1,106 @@
amends "../snippetTest.pkl"
import "pkl:math"
facts {
["isPositive"] {
0.min.isPositive
0.0.min.isPositive
1.min.isPositive
0.1.min.isPositive
(-0).min.isPositive
(-0.0).min.isPositive
!(-1).min.isPositive
!(-0.1).min.isPositive
}
["isBetween"] {
3.min.isBetween(2.min, 4.min)
3.min.isBetween(3.min, 4.min)
3.min.isBetween(2.min, 3.min)
3.min.isBetween(3.min, 3.min)
3.min.isBetween(120.s, 180.s)
!3.min.isBetween(1.min, 2.min)
!3.min.isBetween(4.min, 2.min)
3.3.min.isBetween(2.2.min, 4.4.min)
3.3.min.isBetween(3.3.min, 4.4.min)
3.3.min.isBetween(2.2.min, 3.3.min)
3.3.min.isBetween(3.3.min, 3.3.min)
3.3.min.isBetween(120.s, 210.s)
!3.3.min.isBetween(1.1.min, 2.2.min)
!3.3.min.isBetween(4.4.min, 2.2.min)
}
}
examples {
local cases = new Listing {
1.ns
2.2.us
3.ms
4.4.s
5.min
6.6.h
7.d
}
["value"] {
for (c in cases) {
c.value
}
List(1, 2.2, 3).map((d) -> d.mb.value)
}
["unit"] {
for (c in cases) {
c.unit
}
}
["isoString"] {
for (c in cases) {
c.isoString
}
2000.5.ms.isoString
0.h.isoString
0.0.h.isoString
(-0.0).h.isoString
0.s.isoString
0.0.s.isoString
(-0.0).s.isoString
0.ms.isoString
0.0.ms.isoString
(-0.0).ms.isoString
100.d.isoString
(-10.001).s.isoString
(-3.1.h).isoString
math.maxFiniteFloat.s.isoString
module.catch(() -> (0/0).s.isoString)
module.catch(() -> (1/0).s.isoString)
module.catch(() -> (-1/0).s.isoString)
module.catch(() -> (math.maxFiniteFloat * 1.00000000001).s.isoString)
}
["toUnit()"] {
1.d.toUnit("d")
1.d.toUnit("h")
1.d.toUnit("min")
1.d.toUnit("s")
1.d.toUnit("ms")
1.d.toUnit("us")
1.d.toUnit("ns")
local ns = 1.d.toUnit("ns")
ns.toUnit("d")
ns.toUnit("h")
ns.toUnit("min")
ns.toUnit("s")
ns.toUnit("us")
ns.toUnit("ns")
0.5.h.toUnit("s")
1800.s.toUnit("h")
module.catch(() -> 1.d.toUnit("foo"))
}
}
@@ -0,0 +1,91 @@
amends "../snippetTest.pkl"
facts {
["length()"] {
obj.length() == 2
((obj) { "three"; "four" } { x = 1 } { "five" } {}).length() == 5
obj2.length() == 2
new Dynamic {}.length() == 0
new Dynamic { name = "Pigeon" }.length() == 0
new Dynamic { ["name"] = "Pigeon" }.length() == 0
}
}
examples {
["getProperty()"] {
pigeon.getProperty("name")
pigeon.getProperty("na" + "me")
pigeon.getProperty("age")
module.catch(() -> pigeon.getProperty("other"))
}
["getPropertyOrNull()"] {
pigeon.getPropertyOrNull("name")
pigeon.getPropertyOrNull("na" + "me")
pigeon.getPropertyOrNull("age")
pigeon.getPropertyOrNull("other")
}
["hasProperty()"] {
pigeon.hasProperty("name")
pigeon.hasProperty("na" + "me")
pigeon.hasProperty("age")
pigeon.hasProperty("other")
futurePigeon.hasProperty("name")
pigeon.hasProperty("nostalgia")
futurePigeon.hasProperty("nostalgia")
}
["toMap()"] {
pigeon.toMap()
obj.toMap()
obj2.toMap()
}
["toList()"] {
pigeon.toList()
obj.toList()
obj2.toList()
}
["toTyped()"] {
pigeon.toTyped(Person)
futurePigeon.toTyped(Person)
new Dynamic { age = 42 }.toTyped(Person)
new Dynamic { name = "Pigeon" }.toTyped(Person).name
module.catch(() -> new Dynamic { name = "Pigeon" }.toTyped(Person).age)
module.catch(() -> obj.toTyped(Pair)) // Pair is not a Typed
module.catch(() -> obj.toTyped(ValueRenderer)) // ValueRenderer is abstract
}
}
local obj = new Dynamic {
prop1 = "prop1"
prop2 = "prop2"
["name"] = "Pigeon"
["age"] = 42
"one"
"two"
}
local obj2 = (obj) {
[0] = "one one"
[1] = "two two"
}
local pigeon = new Dynamic {
local n = "Pigeon"
name = n
age = 42
}
local futurePigeon = (pigeon) {
age = 43
nostalgia = true
}
local class Person {
name: String = "Default"
age: UInt
}
@@ -0,0 +1,220 @@
amends "../snippetTest.pkl"
facts {
["isNaN"] {
NaN.isNaN
!Infinity.isNaN
!(-Infinity).isNaN
!42.123.isNaN
!(-42.123).isNaN
}
["isFinite"] {
42.123.isFinite
(-42.123).isFinite
!NaN.isFinite
!Infinity.isFinite
!(-Infinity).isFinite
}
["isInfinite"] {
Infinity.isInfinite
(-Infinity).isInfinite
!42.123.isInfinite
!(-42.123).isInfinite
!NaN.isInfinite
}
["isPositive"] {
42.123.isPositive
0.0.isPositive
Infinity.isPositive
(-0.0).isPositive
!(-42.123).isPositive
!NaN.isPositive
!(-Infinity).isPositive
}
["isBetween()"] {
3.3.isBetween(2, 4)
3.3.isBetween(2.2, 4.4)
3.3.isBetween(2.2, 4)
3.3.isBetween(2, 4.4)
3.3.isBetween(2, 3.3)
3.3.isBetween(3.3, 4.0)
3.3.isBetween(3.3, 3.3)
!3.3.isBetween(1.1, 2.2)
!3.3.isBetween(4.4, 2.2)
}
["isNonZero"] {
3.3.isNonZero
(-3.3).isNonZero
NaN.isNonZero
!0.isNonZero
!0.0.isNonZero
!(-0.0).isNonZero
}
["toDuration()"] {
3.21.toDuration("min") == 3.21.min
-99.999.toDuration("ms") == -99.999.ms
}
["toDataSize()"] {
3.21.toDataSize("kb") == 3.21.kb
-99.999.toDataSize("gib") == -99.999.gib
}
}
examples {
["sign"] {
2.34.sign
(-2.34).sign
0.0.sign
(-0.0).sign
NaN.sign
Infinity.sign
(-Infinity).sign
}
["abs"] {
2.34.abs
(-2.34).abs
0.0.abs
(-0.0).abs
NaN.abs
Infinity.abs
(-Infinity).abs
}
["ceil"] {
2.34.ceil
2.9.ceil
(-2.34).ceil
(-2.9).ceil
0.0.ceil
(-0.0).ceil
NaN.ceil
Infinity.ceil
(-Infinity).ceil
}
["floor"] {
2.34.floor
2.9.floor
(-2.34).floor
(-2.9).floor
0.0.floor
(-0.0).floor
NaN.floor
Infinity.floor
(-Infinity).floor
}
["round()"] {
2.34.round()
2.9.round()
(-2.34).round()
(-2.9).round()
0.0.round()
(-0.0).round()
NaN.round()
Infinity.round()
(-Infinity).round()
}
["truncate()"] {
2.34.truncate()
2.9.truncate()
(-2.34).truncate()
(-2.9).truncate()
0.0.truncate()
(-0.0).truncate()
NaN.truncate()
Infinity.truncate()
(-Infinity).truncate()
}
["toInt()"] {
2.34.toInt()
2.9.toInt()
(-2.34).toInt()
(-2.9).toInt()
0.0.toInt()
(-0.0).toInt()
module.catch(() -> Infinity.toInt())
module.catch(() -> (-Infinity).toInt())
module.catch(() -> NaN.toInt())
module.catch(() -> 9223372036854775808.1.toInt())
}
["toFloat()"] {
2.34.toFloat()
2.9.toFloat()
(-2.34).toFloat()
(-2.9).toFloat()
0.0.toFloat()
(-0.0).toFloat()
NaN.toFloat()
Infinity.toFloat()
(-Infinity).toFloat()
}
["toString()"] {
2.34.toString()
2.9.toString()
(-2.34).toString()
(-2.9).toString()
0.0.toString()
(-0.0).toString()
NaN.toString()
Infinity.toString()
(-Infinity).toString()
}
["toFixed()"] {
2.34.toFixed(0)
2.9.toFixed(0)
(-2.34).toFixed(0)
(-2.9).toFixed(0)
0.0.toFixed(0)
(-0.0).toFixed(0)
NaN.toFixed(0)
Infinity.toFixed(0)
(-Infinity).toFixed(0)
2.34.toFixed(1)
2.9.toFixed(2)
(-2.34).toFixed(3)
(-2.9).toFixed(4)
0.0.toFixed(5)
(-0.0).toFixed(6)
NaN.toFixed(7)
Infinity.toFixed(8)
(-Infinity).toFixed(9)
1.23456789.toFixed(1)
(-1.23456789).toFixed(2)
1.23456789.toFixed(3)
(-1.23456789).toFixed(4)
1.23456789.toFixed(5)
(-1.23456789).toFixed(6)
1.23456789.toFixed(7)
(-1.23456789).toFixed(8)
1.23456789.toFixed(9)
123456789.123456789.toFixed(1)
123456789.123456789.toFixed(2)
123456789.123456789.toFixed(3)
123456789.123456789.toFixed(4)
123456789.123456789.toFixed(5)
123456789.123456789.toFixed(6)
123456789.123456789.toFixed(7)
123456789.123456789.toFixed(8)
123456789.123456789.toFixed(9)
module.catch(() -> 1.23.toFixed(-1))
module.catch(() -> 1.23.toFixed(21))
}
}
@@ -0,0 +1 @@
The quick brown fox jumps over the lazy dog.
@@ -0,0 +1,216 @@
amends "../snippetTest.pkl"
facts {
["isEven"] {
(-124).isEven
0.isEven
!123.isEven
}
["isOdd"] {
123.isOdd
!(-124).isOdd
!0.isOdd
}
["isNaN"] {
!42.isNaN
!(-42).isNaN
}
["isFinite"] {
42.isFinite
(-42).isFinite
}
["isInfinite"] {
!42.isInfinite
!(-42).isInfinite
}
["isPositive"] {
42.isPositive
0.isPositive
!(-42).isPositive
}
["isBetween()"] {
3.isBetween(2, 4)
3.isBetween(2.2, 4.4)
3.isBetween(2.2, 4)
3.isBetween(2, 4.4)
3.isBetween(2, 3)
3.isBetween(3, 4)
3.isBetween(3, 3)
3.isBetween(3.0, 3.0)
!3.isBetween(1, 2)
!3.isBetween(4, 2)
}
["isNonZero"] {
3.isNonZero
(-3).isNonZero
!0.isNonZero
}
["toDuration()"] {
3.toDuration("min") == 3.min
-99.toDuration("ms") == -99.ms
}
["toDataSize()"] {
3.toDataSize("kb") == 3.kb
-99.toDataSize("gib") == -99.gib
}
}
examples {
["sign"] {
123.sign
(-123).sign
0.sign
(-0).sign
}
["abs"] {
123.abs
(-123).abs
0.abs
(-0).abs
}
["ceil"] {
123.ceil
(-123).ceil
0.ceil
(-0).ceil
}
["floor"] {
123.floor
(-123).floor
0.floor
(-0).floor
}
["toRadixString()"] {
123.toRadixString(16)
123.toRadixString(2)
123.toRadixString(33)
(-123).toRadixString(16)
(-123).toRadixString(2)
(-123).toRadixString(33)
0.toRadixString(16)
0.toRadixString(2)
0.toRadixString(33)
(-0).toRadixString(16)
(-0).toRadixString(2)
(-0).toRadixString(33)
module.catch(() -> (-123).toRadixString(-1))
module.catch(() -> (-123).toRadixString(64))
}
["round()"] {
123.round()
(-123).round()
0.round()
(-0).round()
}
["truncate()"] {
123.truncate()
(-123).truncate()
0.truncate()
(-0).truncate()
}
["toInt()"] {
123.toInt()
(-123).toInt()
0.toInt()
(-0).toInt()
}
["toFloat()"] {
123.toFloat()
(-123).toFloat()
0.toFloat()
(-0).toFloat()
}
["toString()"] {
123.toString()
(-123).toString()
0.toString()
(-0).toString()
}
["toFixed()"] {
123.toFixed(0)
(-123).toFixed(0)
0.toFixed(0)
(-0).toFixed(0)
123.toFixed(1)
(-123).toFixed(2)
0.toFixed(3)
(-0).toFixed(4)
123456789.toFixed(1)
123456789.toFixed(2)
123456789.toFixed(3)
123456789.toFixed(4)
module.catch(() -> 123.toFixed(21))
module.catch(() -> (-123).toFixed(-1))
}
["shl()"] {
123.shl(2)
(-123).shl(2)
0.shl(2)
}
["shr()"] {
123.shr(2)
(-123).shr(2)
0.shr(2)
}
["ushr()"] {
123.ushr(2)
(-123).ushr(2)
0.ushr(2)
}
["and()"] {
123.and(456)
(-123).and(456)
}
["or()"] {
123.or(456)
(-123).or(456)
}
["xor()"] {
123.xor(456)
(-123).xor(456)
}
["inv"] {
123.inv
(-123).inv
}
["toChar()"] {
"a".codePoints.single.toChar()
"😊".codePoints.single.toChar()
module.catch(() -> (-1).toChar())
module.catch(() -> (0x110000).toChar())
}
}
@@ -0,0 +1,94 @@
amends "../snippetTest.pkl"
import "pkl:math"
local id = (it) -> it
examples {
["construction"] {
IntSeq(-3, 2)
IntSeq(5, 5)
IntSeq(5, 0)
IntSeq(0, 0)
IntSeq(0, 10)
IntSeq(-10, 0)
}
["construction with step"] {
IntSeq(-3, 2).step(2)
IntSeq(2, -3).step(-2)
IntSeq(2, -3).step(-5)
IntSeq(2, -3).step(-7)
IntSeq(0, 1).step(math.minInt)
IntSeq(math.maxInt, math.minInt).step(math.minInt)
IntSeq(math.minInt, math.maxInt).step(math.maxInt)
module.catch(() -> IntSeq(-3, 2).step(0))
}
["start"] {
IntSeq(-3, 2).start
IntSeq(2, -3).start
}
["end"] {
IntSeq(-3, 2).end
IntSeq(2, -3).end
}
["step"] {
IntSeq(-3, 2).step
IntSeq(-3, 2).step(2).step
IntSeq(2, -3).step
IntSeq(2, -3).step(-2).step
}
["fold"] {
IntSeq(-3, 2).fold(List(), (l, e) -> l.add(e))
IntSeq(5, 5).fold(List(), (l, e) -> l.add(e))
IntSeq(5, 0).fold(List(), (l, e) -> l.add(e))
IntSeq(0, 0).fold(List(), (l, e) -> l.add(e))
IntSeq(0, 10).fold(List(), (l, e) -> l.add(e))
IntSeq(-10, 0).fold(List(), (l, e) -> l.add(e))
IntSeq(-3, 2).step(2).fold(List(), (l, e) -> l.add(e))
IntSeq(2, -3).step(-2).fold(List(), (l, e) -> l.add(e))
IntSeq(2, -3).step(-5).fold(List(), (l, e) -> l.add(e))
IntSeq(2, -3).step(-7).fold(List(), (l, e) -> l.add(e))
}
["map"] {
IntSeq(-3, 2).map(id)
IntSeq(5, 5).map(id)
IntSeq(5, 0).map(id)
IntSeq(0, 0).map(id)
IntSeq(0, 10).map(id)
IntSeq(-10, 0).map(id)
IntSeq(-3, 2).step(2).map(id)
IntSeq(2, -3).step(-2).map(id)
IntSeq(2, -3).step(-5).map(id)
IntSeq(2, -3).step(-7).map(id)
IntSeq(0, 1).step(math.minInt).map(id)
IntSeq(math.maxInt, math.minInt).step(math.minInt).map(id)
IntSeq(math.minInt, math.maxInt).step(math.maxInt).map(id)
}
["cannot instantiate"] {
module.catch(() -> new IntSeq {})
}
["toList"] {
IntSeq(0, 3).toList() == List(0, 1, 2, 3)
IntSeq(-3, 2).step(2).toList() == List(-3, -1, 1)
IntSeq(5, 1).step(-2).toList() == List(5, 3, 1)
}
["toListing"] {
IntSeq(0, 3).toListing() == new Listing { 0; 1; 2; 3 }
IntSeq(-3, 2).step(2).toListing() == new Listing { -3; -1; 1 }
IntSeq(5, 1).step(-2).toListing() == new Listing { 5; 3; 1 }
}
}
@@ -0,0 +1,68 @@
import "pkl:json"
import "pkl:test"
local parser = new json.Parser {}
res1 = parser.parse("null")
res2 = parser.parse("true")
res3 = parser.parse("false")
res4 = parser.parse("123")
// json doesn't support leading zero
res5 = test.catch(() -> parser.parse("0123"))
// json doesn't support binary literals
res6 = test.catch(() -> parser.parse("b1001"))
// json doesn't support hex literals
res7 = test.catch(() -> parser.parse("0x1A3"))
res8 = parser.parse("123.456")
res9 = parser.parse("123.456e2")
// empty document does not seem to be valid json
res10 = test.catch(() -> parser.parse(""))
res11 = test.catch(() -> parser.parse(" "))
res12 = parser.parse(#""hello""#)
res13 = parser.parse(#""hello\nworld""#)
res14 = parser.parse("""
["one", "two", "three"]
""")
res15 = parser.parse("""
{ "name": "Pigeon", "age": 42 }
""")
// from: https://en.wikipedia.org/wiki/JSON#JSON_sample
res16 = parser.parse("""
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
],
"gender": {
"type": "male"
}
}
""")
// invalid syntax
res17 = test.catch(() -> parser.parse("!@#$%"))
@@ -0,0 +1,65 @@
import "pkl:json"
import "pkl:test"
local parser = new json.Parser {
converters {
[Boolean] = (it) -> !it
[String] = (it) -> it + "x"
[Int] = (it) -> it + 1
[Float] = (it) -> it + 1.23
[Dynamic] = (it) -> (it) { other = "other" }
[Listing] = (it) -> (it) { "other" }
}
}
res1 = parser.parse("null")
res2 = parser.parse("true")
res3 = parser.parse("false")
res4 = parser.parse("123")
res8 = parser.parse("123.456")
res9 = parser.parse("123.456e2")
// empty document
res10 = test.catch(() -> parser.parse(""))
res11 = test.catch(() -> parser.parse(" "))
res12 = parser.parse(#""hello""#)
res13 = parser.parse(#""hello\nworld""#)
res14 = parser.parse("""
["one", "two", "three"]
""")
res15 = parser.parse("""
{ "name": "Pigeon", "age": 42 }
""")
// from: https://en.wikipedia.org/wiki/JSON#JSON_sample
res16 = parser.parse("""
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
],
"gender": {
"type": "male"
}
}
""")
@@ -0,0 +1,42 @@
import "pkl:json"
local parser = new json.Parser {
converters {
["^firstName"] = (_) -> "changed"
["^city"] = (_) -> "no match"
["age"] = (_) -> 42
["address.city"] = (it) -> it.reverse()
["state"] = (_) -> "CA"
["phoneNumber[*].type"] = (_) -> "mobile"
["phoneNumber[*]"] = (it) -> it.type
["gender.*"] = (_) -> "female"
}
}
// from: https://en.wikipedia.org/wiki/JSON#JSON_sample
res1 = parser.parse("""
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
],
"gender": {
"type": "male"
}
}
""")
@@ -0,0 +1,43 @@
import "pkl:json"
local parser = new json.Parser {
useMapping = true
}
res1 = parser.parse("""
["one", "two", "three"]
""")
res2 = parser.parse("""
{ "name": "Pigeon", "age": 42 }
""")
res3 = parser.parse("""
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
],
"gender": {
"type": "male"
}
}
""")
res4 = res3 is Mapping
res5 = res3["address"] is Mapping
@@ -0,0 +1,47 @@
import "pkl:json"
local parser = new json.Parser {
useMapping = true
converters {
["^firstName"] = (_) -> "changed"
["^city"] = (_) -> "no match"
["age"] = (_) -> 42
["address.city"] = (it) -> it.reverse()
["state"] = (_) -> "CA"
["phoneNumber[*].type"] = (_) -> "mobile"
["phoneNumber[*]"] = (it) -> it["type"]
["gender.*"] = (_) -> "female"
}
}
// from: https://en.wikipedia.org/wiki/JSON#JSON_sample
res1 = parser.parse("""
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
],
"gender": {
"type": "male"
}
}
""")
res2 = res1 is Mapping
res3 = res1["address"] is Mapping
@@ -0,0 +1,9 @@
pigeon {
name = "pigeon"
age = 30
}
output {
value = pigeon
renderer = new JsonRenderer {}
}
@@ -0,0 +1,45 @@
class Person {
name: String
age: Int
}
res1 = "string"
res2 = true
res3 = 42
res4 = 1.23
res5 = 3.s
res6 = 4.mb
res8 = List("string", true, 42)
res9 = Set("string", true, 42)
res10 = Map("one", true, "two", 1.23)
res11 = new Listing { "string"; true; 42 }
res12 = new Mapping { ["name"] = "pigeon"; ["age"] = 30 }
res13 = new Dynamic { name = "pigeon"; age = 30 }
res14 = new Person { name = "pigeon" }
res15 = null
res16 = Pair(1, 2)
res17 = IntSeq(1, 4)
output {
renderer = new JsonRenderer {
indent = " "
converters {
[String] = (it) -> it.reverse()
[Boolean] = (it) -> !it
[Int] = (it) -> it + 1
[Float] = (it) -> it + 1.1
[Duration] = (it) -> "\(it.value) \(it.unit)"
[DataSize] = (it) -> "\(it.value) \(it.unit)"
[List] = (it) -> it.reverse()
[Set] = (it) -> it + List(4)
[Map] = (it) -> it + Map("three", 3.s)
[Listing] = (it) -> (it) { 4 }
[Mapping] = (it) -> (it) { ["three"] = 3.s }
[Dynamic] = (it) -> (it) { other = "other" }
[Person] = (it) -> (it) { age = 40 } // fill in missing property
[Null] = (_) -> "String"
[Pair] = (it) -> List(it.first, it.second)
[IntSeq] = (it) -> List(it.start, it.end, it.step)
}
}
}
@@ -0,0 +1,50 @@
local renderer = new JsonRenderer {
converters {
["^apple"] = (it) -> it.toUpperCase()
["^[*].apple"] = (it) -> it.toUpperCase()
["^[apple]"] = (it) -> it.toUpperCase()
}
}
local properties = renderer.renderDocument(
new Dynamic {
apple = "yes"
banana {
apple = "no"
}
})
res1 = properties.contains("YES")
res2 = properties.contains("no")
res3 = !properties.contains("yes")
res4 = !properties.contains("NO")
local elements = renderer.renderDocument(
new Dynamic {
new {
apple = "yes"
}
new {
new {
apple = "no"
}
}
})
res5 = elements.contains("YES")
res6 = elements.contains("no")
res7 = !elements.contains("yes")
res8 = !elements.contains("NO")
local entries = renderer.renderValue(
new Dynamic {
["apple"] = "yes"
["banana"] {
["apple"] = "no"
}
})
res9 = entries.contains("YES")
res10 = entries.contains("no")
res11 = !entries.contains("yes")
res12 = !entries.contains("NO")
@@ -0,0 +1,44 @@
class Person {
name: String
age: Int
}
res1 = "string"
res2 = true
res3 = 42
res4 = 1.23
res5 = 3.s
res6 = 4.mb
res8 = List("string", true, 42)
res9 = Set("string", true, 42)
res10 = Map("string", true, 42, 1.23)
res11 = new Listing { "string"; true; 42 }
res12 = new Mapping { ["name"] = "pigeon"; ["age"] = 30 }
res13 = new Dynamic { name = "pigeon"; age = 30 }
res14 = new Person { name = "pigeon"; age = 30 }
res15 = null
res16 = Pair(1, 2)
res17 = IntSeq(1, 4)
output {
renderer = new JsonRenderer {
converters {
[String] = (_) -> "converted"
[Boolean] = (_) -> "converted"
[Int] = (_) -> "converted"
[Float] = (_) -> "converted"
[Duration] = (_) -> "converted"
[DataSize] = (_) -> "converted"
[List] = (_) -> "converted"
[Set] = (_) -> "converted"
[Map] = (_) ->"converted"
[Listing] = (_) ->"converted"
[Mapping] = (_) ->"converted"
[Dynamic] = (_) ->"converted"
[Person] = (_) -> "converted"
[Null] = (_) -> "converted"
[Pair] = (_) -> "converted"
[IntSeq] = (_) -> "converted"
}
}
}
@@ -0,0 +1,32 @@
import "pkl:test"
class Person {
name: String
age: Int
}
typealias Email = String
local renderer = new JsonRenderer {}
res1 = renderer.renderValue(123)
res2 = renderer.renderValue(1.23)
res3 = renderer.renderValue(false)
res4 = renderer.renderValue("pigeon")
res6 = renderer.renderValue(List("pigeon", "parrot"))
res7 = renderer.renderValue(Set("pigeon", "parrot"))
res8 = renderer.renderValue(Map("name", "pigeon", "age", 42))
res9 = renderer.renderValue(new Listing { "pigeon"; "parrot" })
res10 = renderer.renderValue(new Mapping { ["name"] = "pigeon"; ["age"] = 42 })
res11 = renderer.renderValue(new Dynamic { name = "pigeon"; age = 42 })
res12 = renderer.renderValue(new Person { name = "pigeon"; age = 42 })
res13 = renderer.renderValue(null)
res14 = test.catch(() -> renderer.renderValue(1.min))
res15 = test.catch(() -> renderer.renderValue(Pair(1, 2)))
res16 = test.catch(() -> renderer.renderValue(1.mb))
res17 = test.catch(() -> renderer.renderValue(Person))
res18 = test.catch(() -> renderer.renderValue(Email))
res19 = test.catch(() -> renderer.renderValue((x) -> x))
res20 = test.catch(() -> new JsonRenderer { converters { [Int] = (_) -> throw("ouch") } }.renderValue(42))
res21 = test.catch(() -> renderer.renderValue(IntSeq(1, 4)))
@@ -0,0 +1,32 @@
import "pkl:test"
class Person {
name: String
age: Int
}
typealias Email = String
local renderer = new JsonRenderer {}
res1 = renderer.renderDocument(123)
res2 = renderer.renderDocument(1.23)
res3 = renderer.renderDocument(false)
res4 = renderer.renderDocument("pigeon")
res6 = renderer.renderDocument(List("pigeon", "parrot"))
res7 = renderer.renderDocument(Set("pigeon", "parrot"))
res8 = renderer.renderDocument(Map("name", "pigeon", "age", 42))
res9 = renderer.renderDocument(new Listing { "pigeon"; "parrot" })
res10 = renderer.renderDocument(new Mapping { ["name"] = "pigeon"; ["age"] = 42 })
res11 = renderer.renderDocument(new Dynamic { name = "pigeon"; age = 42 })
res12 = renderer.renderDocument(new Person { name = "pigeon"; age = 42 })
res13 = renderer.renderDocument(null)
res14 = test.catch(() -> renderer.renderDocument(1.min))
res15 = test.catch(() -> renderer.renderDocument(Pair(1, 2)))
res16 = test.catch(() -> renderer.renderDocument(1.mb))
res17 = test.catch(() -> renderer.renderDocument(Person))
res18 = test.catch(() -> renderer.renderDocument(Email))
res19 = test.catch(() -> renderer.renderDocument((x) -> x))
res20 = test.catch(() -> new JsonRenderer { converters { [Int] = (_) -> throw("ouch") } }.renderDocument(42))
res21 = test.catch(() -> renderer.renderDocument(IntSeq(1, 4)))
@@ -0,0 +1,31 @@
name = "pigeon"
age = 42
friends = new Listing {
"barn owl"
"parrot"
}
hobbies {
["surfing"] {
skill = "low"
}
["firemaking"] {
skill = "high"
}
}
address {
street = "Norton St."
zip = 12345
}
output {
renderer = new JsonRenderer {
converters {
["name"] = (it) -> it.reverse()
["friends[*]"] = (it) -> it + "x"
["hobbies[*]"] = (it) -> it.skill
["address.street"] = (it) -> "Other St."
["address.*"] = (it) -> "changed"
["unmatched"] = (it) -> "unmatched"
}
}
}
@@ -0,0 +1,13 @@
res1 = null
res2 = 1
res3 = "Hello"
res4 {
foo = 1
bar = null
}
output {
renderer = new JsonRenderer {
omitNullProperties = true
}
}
@@ -0,0 +1,6 @@
// Test that unrenderable types do have an informative location in the error message.
m = new Mixin {}
output {
renderer = new JsonRenderer {}
}
@@ -0,0 +1,21 @@
a1 = List()
a2 = Set()
a3 = new Listing {}
b1 = Map()
b2 = new Mapping {}
b3 = new Dynamic {}
nested {
a1 = List()
a2 = Set()
a3 = new Listing {}
b1 = Map()
b2 = new Mapping {}
b3 = new Dynamic {}
}
output {
renderer = new JsonRenderer {}
}
@@ -0,0 +1,66 @@
import "pkl:jsonnet"
int = 123
float = 1.23
bool = true
string = "Pigeon"
unicodeString = "abc😀abc😎abc"
multiLineString = """
have a
great
day
"""
class Person {
name: String
age: Int
address: Address
}
class Address {
street: String
}
typedObject = new Person {
name = "Bob"
age = 42
address {
street = "Abby Rd."
}
}
dynamicObject {
name = "Pigeon"
age = 30
address {
street = "Folsom St."
}
friend = null
}
annoyingNames {
["5hello"] = 123
["local"] = "remote"
["foo.bar"] = "baz"
["single'quote"] = "double\"quote"
}
list = new {
1
2
3
null
}
someExternalVariable = jsonnet.ExtVar("MY_VARIABLE")
someImportStr = jsonnet.ImportStr("my/private/key.pem")
output {
renderer = new jsonnet.Renderer {}
}
@@ -0,0 +1,17 @@
import "pkl:jsonnet"
hello {
world {
1
"foo\nbar"
}
}
emptyArray = List()
emptyObject {}
output {
renderer = new jsonnet.Renderer {
indent = ""
}
}
@@ -0,0 +1,46 @@
import "pkl:jsonnet"
class Person {
name: String
age: Int
}
res1 = "string"
res2 = true
res3 = 42
res4 = 1.23
res5 = 3.s
res6 = 4.mb
res8 = List("string", true, 42)
res9 = Set("string", true, 42)
res10 = Map("string", true, 42, 1.23)
res11 = new Listing { "string"; true; 42 }
res12 = new Mapping { ["name"] = "pigeon"; ["age"] = 30 }
res13 = new Dynamic { name = "pigeon"; age = 30 }
res14 = new Person { name = "pigeon"; age = 30 }
res15 = null
res16 = Pair(1, 2)
res17 = IntSeq(1, 4)
output {
renderer = new jsonnet.Renderer {
converters {
[String] = (it) -> "converted"
[Boolean] = (it) -> "converted"
[Int] = (it) -> "converted"
[Float] = (it) -> "converted"
[Duration] = (it) -> "converted"
[DataSize] = (it) -> "converted"
[List] = (it) -> "converted"
[Set] = (it) -> "converted"
[Map] = (it) ->"converted"
[Listing] = (it) ->"converted"
[Mapping] = (it) ->"converted"
[Dynamic] = (it) ->"converted"
[Person] = (it) -> "converted"
[Null] = (it) -> "converted"
[Pair] = (it) -> "converted"
[IntSeq] = (it) -> "converted"
}
}
}
@@ -0,0 +1,33 @@
import "pkl:test"
import "pkl:jsonnet"
class Person {
name: String
age: Int
}
typealias Email = String
local renderer = new jsonnet.Renderer {}
res1 = renderer.renderValue(123)
res2 = renderer.renderValue(1.23)
res3 = renderer.renderValue(false)
res4 = renderer.renderValue("pigeon")
res6 = renderer.renderValue(List("pigeon", "parrot"))
res7 = renderer.renderValue(Set("pigeon", "parrot"))
res8 = renderer.renderValue(Map("name", "pigeon", "age", 42))
res9 = renderer.renderValue(new Listing { "pigeon"; "parrot" })
res10 = renderer.renderValue(new Mapping { ["name"] = "pigeon"; ["age"] = 42 })
res11 = renderer.renderValue(new Dynamic { name = "pigeon"; age = 42 })
res12 = renderer.renderValue(new Person { name = "pigeon"; age = 42 })
res13 = renderer.renderValue(null)
res14 = test.catch(() -> renderer.renderValue(Pair(1, 2)))
res15 = test.catch(() -> renderer.renderValue(1.min))
res16 = test.catch(() -> renderer.renderValue(1.mb))
res17 = test.catch(() -> renderer.renderValue(Person))
res18 = test.catch(() -> renderer.renderValue(Email))
res19 = test.catch(() -> renderer.renderValue((x) -> x))
res20 = test.catch(() -> new JsonRenderer { converters { [Int] = (_) -> throw("ouch") } }.renderValue(42))
res21 = test.catch(() -> renderer.renderValue(IntSeq(1, 4)))
@@ -0,0 +1,33 @@
import "pkl:test"
import "pkl:jsonnet"
class Person {
name: String
age: Int
}
typealias Email = String
local renderer = new jsonnet.Renderer {}
res1 = renderer.renderDocument(123)
res2 = renderer.renderDocument(1.23)
res3 = renderer.renderDocument(false)
res4 = renderer.renderDocument("pigeon")
res6 = renderer.renderDocument(List("pigeon", "parrot"))
res7 = renderer.renderDocument(Set("pigeon", "parrot"))
res8 = renderer.renderDocument(Map("name", "pigeon", "age", 42))
res9 = renderer.renderDocument(new Listing { "pigeon"; "parrot" })
res10 = renderer.renderDocument(new Mapping { ["name"] = "pigeon"; ["age"] = 42 })
res11 = renderer.renderDocument(new Dynamic { name = "pigeon"; age = 42 })
res12 = renderer.renderDocument(new Person { name = "pigeon"; age = 42 })
res13 = renderer.renderDocument(null)
res14 = test.catch(() -> renderer.renderDocument(Pair(1, 2)))
res15 = test.catch(() -> renderer.renderDocument(1.min))
res16 = test.catch(() -> renderer.renderDocument(1.mb))
res17 = test.catch(() -> renderer.renderDocument(Person))
res18 = test.catch(() -> renderer.renderDocument(Email))
res19 = test.catch(() -> renderer.renderDocument((x) -> x))
res20 = test.catch(() -> new JsonRenderer { converters { [Int] = (_) -> throw("ouch") } }.renderDocument(42))
res21 = test.catch(() -> renderer.renderDocument(IntSeq(1, 4)))
@@ -0,0 +1,33 @@
import "pkl:jsonnet"
name = "pigeon"
age = 42
friends = new Listing {
"barn owl"
"parrot"
}
hobbies {
["surfing"] {
skill = "low"
}
["firemaking"] {
skill = "high"
}
}
address {
street = "Norton St."
zip = 12345
}
output {
renderer = new jsonnet.Renderer {
converters {
["name"] = (it) -> it.reverse()
["friends[*]"] = (it) -> it + "x"
["hobbies[*]"] = (it) -> it.skill
["address.street"] = (it) -> "Other St."
["address.*"] = (it) -> "changed"
["unmatched"] = (it) -> "unmatched"
}
}
}
@@ -0,0 +1,8 @@
// Test that unrenderable types do have an informative location in the error message.
import "pkl:jsonnet"
m = new Mixin {}
output {
renderer = new jsonnet.Renderer {}
}
@@ -0,0 +1,491 @@
amends "../snippetTest.pkl"
local list1 = List(1, 2, 3)
local list2 = List(1, 2, 3, 4, 5)
local list3 = List(1, 2, 3, 2, 1)
local comparator = (x, y) -> x < y
facts {
["isEmpty"] {
List().isEmpty
!list1.isEmpty
}
["every()"] {
list1.every((x) -> x <= 3)
!list1.every((x) -> x > 2)
}
["any()"] {
list1.any((x) -> x > 2)
!list1.any((x) -> x > 3)
}
["contains()"] {
list1.contains(2)
!list1.contains(4)
}
["startsWith()"] {
list1.startsWith(List())
list1.startsWith(List(1, 2))
!list1.startsWith(List(1, 3))
}
["endsWith()"] {
list1.endsWith(List())
list1.endsWith(List(2, 3))
!list1.endsWith(List(1, 3))
}
["isDistinct"] {
list1.isDistinct
List(1, "1").isDistinct
List().isDistinct
!List(1, 2, 1).isDistinct
!List("Pigeon", "Barn Owl", "Pigeon", "Parrot").isDistinct
}
["isDistinctBy()"] {
list1.isDistinctBy((it) -> it)
List("Pigeon", "Barn Owl", "Parrot").isDistinctBy((it) -> it.reverse())
List().isDistinctBy((it) -> 42)
!list1.isDistinctBy((it) -> it.isOdd)
!list1.isDistinctBy((it) -> 42)
}
}
examples {
["every"] {
module.catch(() -> list1.every((x) -> "wrong type"))
}
["any"] {
module.catch(() -> list1.any((x) -> "wrong type"))
}
["length"] {
list1.length
}
["filter()"] {
list1.filter((x) -> x > 1)
list1.filter((x) -> x > 3)
module.catch(() -> list1. filter((x) -> "wrong type"))
}
["map()"] {
list1.map((x) -> x * 2)
list1.map((x) -> List(x, x))
}
["flatMap()"] {
list1.flatMap((x) -> List(x, x + 1))
module.catch(() -> list1.flatMap((x) -> "wrong type"))
}
["flatten()"] {
List(List(1, 2), List(2, 3), List(4, 5)).flatten()
List(Set(1, 2), Set(2, 3), Set(4, 5)).flatten()
List(new Listing { 1; 2 }, new Listing { 2; 3 }, new Listing { 4; 5 }).flatten()
List(List(1, 2), Set(2, 3), new Listing { 4; 5 }).flatten()
List(List(), Set(), new Listing {}).flatten()
}
["first"] {
list1.first
}
["rest"] {
list1.rest
}
["last"] {
list1.last
}
["single"] {
List(1).single
}
["count()"] {
list1.count((x) -> x >= 2)
list1.count((x) -> true)
list1.count((x) -> false)
module.catch(() -> list1.count((x) -> "wrong type"))
}
["find()"] {
list1.find((x) -> x >= 2)
list1.find((x) -> true)
module.catch(() -> list1.find((x) -> "wrong type"))
}
["findLast()"] {
list1.findLast((x) -> x >= 2)
list1.findLast((x) -> true)
module.catch(() -> list1.findLast((x) -> "wrong type"))
}
["indexOf()"] {
list1.indexOf(2)
List(1,2,3,2,1).indexOf(2)
}
["lastIndexOf()"] {
list1.lastIndexOf(2)
List(1,2,3,2,1).lastIndexOf(2)
}
["findIndex()"] {
list1.findIndex((x) -> x >= 2)
List(1,2,3,2,1).findIndex((x) -> x >= 2)
module.catch(() -> list1.findIndex((x) -> "wrong type"))
}
["findLastIndex()"] {
list1.findLastIndex((x) -> x >= 2)
List(1,2,3,2,1).findLastIndex((x) -> x >= 2)
module.catch(() -> list1.findLastIndex((x) -> "wrong type"))
}
["take()"] {
list1.take(0)
list1.take(2)
list1.take(4)
}
["takeWhile()"] {
list1.takeWhile((x) -> true)
list1.takeWhile((x) -> false)
list1.takeWhile((x) -> x < 3)
module.catch(() -> list1.takeWhile((x) -> "wrong type"))
}
["takeLast()"] {
list1.takeLast(0)
list1.takeLast(2)
list1.takeLast(4)
}
["takeLastWhile()"] {
list1.takeLastWhile((x) -> true)
list1.takeLastWhile((x) -> false)
list1.takeLastWhile((x) -> x > 1)
module.catch(() -> list1.takeLastWhile((x) -> "wrong type"))
}
["drop()"] {
list1.drop(0)
list1.drop(2)
list1.drop(4)
}
["dropWhile()"] {
list1.dropWhile((x) -> true)
list1.dropWhile((x) -> false)
list1.dropWhile((x) -> x < 3)
module.catch(() -> list1.dropWhile((x) -> "wrong type"))
}
["dropLast()"] {
list1.dropLast(0)
list1.dropLast(2)
list1.dropLast(4)
}
["dropLastWhile()"] {
list1.dropLastWhile((x) -> true)
list1.dropLastWhile((x) -> false)
list1.dropLastWhile((x) -> x > 1)
module.catch(() -> list1.dropLastWhile((x) -> "wrong type"))
}
["fold()"] {
list1.fold(0, (acc, x) -> acc + x)
list1.fold(List(), (acc, x) -> acc.add(x))
List(1).fold(0, (acc, x) -> acc + x)
List().fold(0, (acc, x) -> acc + x)
}
["foldBack()"] {
list1.foldBack(0, (x, acc) -> x + acc)
list1.foldBack(List(), (x, acc) -> acc.add(x))
List(1).foldBack(0, (x, acc) -> x + acc)
List().foldBack(0, (x, acc) -> x + acc)
}
["reduce()"] {
list1.reduce((x, y) -> x + y)
List(1).reduce((x, y) -> x + y)
module.catch(() -> List().reduce((x, y) -> x + y))
}
["groupBy()"] {
list2.groupBy((x) -> x)
list3.groupBy((x) -> x)
list2.groupBy((x) -> x.isOdd)
list3.groupBy((x) -> true)
List().groupBy((x) -> x)
}
["repeat()"] {
list1.repeat(0)
list1.repeat(1)
list1.repeat(5)
List().repeat(0)
List().repeat(1)
List().repeat(5)
module.catch(() -> list1.repeat(-1))
}
["sortWith()"] {
List().sortWith(comparator)
List(3, 1, 1, 2, 1).sortWith(comparator)
List(3, 1, 2, 5, 4).sortWith(comparator)
}
["replaceRange()"] {
list2.replaceRange(1, 4, List(9, 8))
list2.replaceRange(1, 4, List(9))
list2.replaceRange(1, 4, List())
list2.replaceRange(1, 1, List(9, 8))
list2.replaceRange(1, 1, List(9))
list2.replaceRange(1, 1, List())
module.catch(() -> list2.replaceRange(-1, 2, List(1)))
module.catch(() -> list2.replaceRange(3, 6, List(1)))
}
["toList()"] {
list1.toList()
list2.toList()
list3.toList()
}
["toSet()"] {
list1.toSet()
list2.toSet()
list3.toSet()
List(
new Dynamic { a = 1 },
new Dynamic { b = 2 },
new Dynamic { a = 1 }
).toSet()
}
["toMap()"] {
list1.toMap((x) -> x, (x) -> 2 * x)
local persons = List(
new Dynamic { name = "Pigeon"; age = 40 },
new Dynamic { name = "Parrot"; age = 30 }
)
persons.toMap((p) -> p.name, (p) -> p)
}
["lastIndex"] {
list1.lastIndex
List().lastIndex
}
["add()"] {
List().add(42)
list1.add(42)
}
["replace()"] {
list1.replace(0, 42)
list1.replace(1, 42)
list1.replace(2, 42)
module.catch(() -> list1.replace(-1, 42))
module.catch(() -> list1.replace(3, 42))
module.catch(() -> List().replace(0, 42))
}
["minWith()"] {
list1.minWith(comparator)
List(1).minWith(comparator)
module.catch(() -> List().minWith(comparator))
}
["maxWith()"] {
list1.maxWith(comparator)
List(1).maxWith(comparator)
module.catch(() -> List().maxWith(comparator))
}
["zip()"] {
List(1, 2, 3).zip(List(4, 5, 6))
List(1, 2, 3).zip(List(4, 5, 6, 7, 8))
}
["filterIndexed()"] {
List(1, 2, 3).filterIndexed((i, n) -> i.isOdd)
List(1, 2, 3).filterIndexed((i, n) -> i.isEven)
List(1, 2, 3).filterIndexed((i, n) -> i.isOdd && n > 2)
List(1, 2, 3).filterIndexed((i, n) -> i.isEven && n > 2)
}
["mapIndexed()"] {
List(1, 2, 3).mapIndexed((i, n) -> n * i)
List(1, 2, 3).mapIndexed((i, n) -> i.isOdd && n.isEven)
}
["flatMapIndexed()"] {
List(1, 2, 3).flatMapIndexed((i, n) -> List(n * i))
List(1, 2, 3).flatMapIndexed((i, n) -> List(i.isOdd && n.isEven))
}
["foldIndexed()"] {
list1.foldIndexed(0, (idx, x, y) -> idx + x + y)
List(1).foldIndexed(0, (idx, x, y) -> idx + x + y)
List().foldIndexed(0, (idx, x, y) -> idx + x + y)
}
["toListing()"] {
list1.toListing()
List().toListing()
}
["toDynamic()"] {
list1.toDynamic()
List().toDynamic()
}
["filterNonNull()"] {
list1.filterNonNull()
List().filterNonNull()
List(1, null, 2, null, 3).filterNonNull()
List(null, null, null).filterNonNull()
}
["mapNonNull()"] {
list1.mapNonNull((it) -> it)
list1.mapNonNull((it) -> null)
List().mapNonNull((it) -> it)
List(1, null, 2, null, 3).mapNonNull((it) -> it)
}
["filterIsInstance()"] {
list1.filterIsInstance(Any)
list1.filterIsInstance(Int)
list1.filterIsInstance(Number)
list1.filterIsInstance(Float)
list1.filterIsInstance(String)
List(1, "2", 3.0, "4").filterIsInstance(String)
List().filterIsInstance(String)
}
["distinct"] {
list1.distinct
List(1, 2, 1).distinct
List("Pigeon", "Barn Owl", "Pigeon", "Parrot").distinct
List(1, "1").distinct
List().distinct
}
["distinctBy()"] {
list1.distinctBy((it) -> it)
list1.distinctBy((it) -> it.isOdd)
List("Pigeon", "Barn Owl", "Parrot").distinctBy((it) -> it.reverse())
list1.distinctBy((it) -> 42)
List().distinctBy((it) -> 42)
}
["split()"] {
list1.split(0)
list1.split(1)
list1.split(2)
list1.split(3)
List().split(0)
module.catch(() -> list1.split(-1))
module.catch(() -> list1.split(4))
}
["partition()"] {
list1.partition((it) -> it.isEven)
list1.partition((it) -> it.isOdd)
list1.partition((it) -> true)
list1.partition((it) -> false)
List().partition((it) -> throw("unreachable"))
}
["min"] {
list1.min
List("Pigeon", "Barn Owl", "Parrot").min
List(3.9, -8.4, 42, -99999).min
List(42, 42, 42).min
List(42, 42.0).min
List(42.0, 42).min
List(11.gb, 100.mb, 12.tb).min
List(11.s, 100.ms, 3.d).min
module.catch(() -> List().min)
module.catch(() -> List(1, "Pigeon", 3.d).min)
}
["max"] {
list1.max
List("Pigeon", "Barn Owl", "Parrot").max
List(3.9, -8.4, 42, -99999).max
List(42, 42, 42).max
List(42, 42.0).max
List(42.0, 42).max
List(11.gb, 100.mb, 12.tb).max
List(11.s, 100.ms, 3.d).max
module.catch(() -> List().max)
module.catch(() -> List(1, "Pigeon", 3.d).max)
}
["minBy()"] {
list1.minBy((it) -> it)
list1.minBy((it) -> -it)
List("Pigeon", "Barn Owl", "Parrot").minBy((it) -> it.length)
List("Pigeon", "Barn Owl", "Parrot").minBy((it) -> it.reverse())
List(11.gb, 100.mb, 12.tb).minBy((it) -> it.value)
List(11.gb, 100.mb, 12.tb).minBy((it) -> it)
List(11.s, 100.ms, 12.min).minBy((it) -> it.value)
List(11.s, 100.ms, 12.min).minBy((it) -> it)
module.catch(() -> List().minBy((it) -> it))
module.catch(() -> List(1, "Pigeon", 3.d).minBy((it) -> it))
}
["maxBy()"] {
list1.maxBy((it) -> it)
list1.maxBy((it) -> -it)
List("Pigeon", "Barn Owl", "Parrot").maxBy((it) -> it.length)
List("Pigeon", "Barn Owl", "Parrot").maxBy((it) -> it.reverse())
List(11.gb, 100.mb, 12.tb).maxBy((it) -> it.value)
List(11.gb, 100.mb, 12.tb).maxBy((it) -> it)
List(11.s, 100.ms, 12.min).maxBy((it) -> it.value)
List(11.s, 100.ms, 12.min).maxBy((it) -> it)
module.catch(() -> List().maxBy((it) -> it))
module.catch(() -> List(1, "Pigeon", 3.d).maxBy((it) -> it))
}
["sort()"] {
list1.sort()
List(3.9, -8.4, 42, -99999).sort()
List("Pigeon", "Barn Owl", "Parrot").sort()
List(11.gb, 100.mb, 12.tb).sort()
List(11.s, 100.ms, 3.d).sort()
List().sort()
module.catch(() -> List(1, "Pigeon", 3.d).sort())
}
["sortBy()"] {
list1.sortBy((it) -> it)
list1.sortBy((it) -> -it)
List("Pigeon", "Barn Owl", "Parrot").sortBy((it) -> it.length)
List("Pigeon", "Barn Owl", "Parrot").sortBy((it) -> it.reverse())
List(11.gb, 100.mb, 12.tb).sortBy((it) -> it.value)
List(11.gb, 100.mb, 12.tb).sortBy((it) -> it)
List().sortBy((it) -> throw("unreachable"))
List(0, -1, 2, -3, 4, -5, 6, -7, 8, -9, 10).sortBy((it) -> 42)
}
["reverse()"] {
list1.reverse()
list1.reverse().reverse()
List(1, "Pigeon", 3.d).reverse()
List().reverse()
}
}
@@ -0,0 +1,173 @@
amends "../snippetTest.pkl"
local list1 = List(1, 2, 3)
examples {
["getOrNull()"] {
list1.getOrNull(1)
list1.getOrNull(3)
list1.getOrNull(-1)
}
["sublistOrNull()"] {
list1.sublistOrNull(1, 3)
list1.sublistOrNull(2, 4)
}
["firstOrNull"] {
list1.firstOrNull
List().firstOrNull
}
["restOrNull"] {
list1.restOrNull
List().restOrNull
}
["lastOrNull"] {
list1.lastOrNull
List().lastOrNull
}
["singleOrNull"] {
List(1).singleOrNull
list1.singleOrNull
List().singleOrNull
}
["findOrNull()"] {
list1.findOrNull((x) -> x >= 2)
list1.findOrNull((x) -> false)
module.catch(() -> list1.findOrNull((x) -> "wrong type"))
}
["findLastOrNull()"] {
list1.findLastOrNull((x) -> x >= 2)
list1.findLastOrNull((x) -> false)
module.catch(() -> list1.findLastOrNull((x) -> "wrong type"))
}
["indexOfOrNull()"] {
list1.indexOfOrNull(2)
list1.indexOfOrNull(4)
}
["lastIndexOfOrNull()"] {
list1.lastIndexOfOrNull(2)
list1.lastIndexOfOrNull(4)
}
["findIndexOrNull()"] {
list1.findIndexOrNull((x) -> x >= 2)
list1.findIndexOrNull((x) -> false)
module.catch(() -> list1.findIndexOrNull((x) -> "wrong type"))
}
["findLastIndexOrNull()"] {
list1.findLastIndexOrNull((x) -> x >= 2)
list1.findLastIndexOrNull((x) -> false)
module.catch(() -> list1.findLastIndexOrNull((x) -> "wrong type"))
}
["reduceOrNull()"] {
list1.reduceOrNull((x, y) -> x + y)
List(1).reduceOrNull((x, y) -> x + y)
List().reduceOrNull((x, y) -> x + y)
}
["replaceRangeOrNull()"] {
local list2 = List(1, 2, 3, 4, 5)
list2.replaceRangeOrNull(1, 4, List(9, 8))
list2.replaceRangeOrNull(1, 4, List(9))
list2.replaceRangeOrNull(1, 4, List())
list2.replaceRangeOrNull(1, 1, List(9, 8))
list2.replaceRangeOrNull(1, 1, List(9))
list2.replaceRangeOrNull(1, 1, List())
list2.replaceRangeOrNull(-1, 2, List(1))
list2.replaceRangeOrNull(3, 6, List(1))
}
["replaceOrNull()"] {
list1.replaceOrNull(0, 42)
list1.replaceOrNull(1, 42)
list1.replaceOrNull(2, 42)
list1.replaceOrNull(-1, 42)
list1.replaceOrNull(3, 42)
List().replaceOrNull(0, 42)
}
["minWithOrNull()"] {
local comparator = (x, y) -> x < y
list1.minWithOrNull(comparator)
List(1).minWithOrNull(comparator)
List().minWithOrNull(comparator)
list1.maxWithOrNull(comparator)
List(1).maxWithOrNull(comparator)
List().maxWithOrNull(comparator)
}
["splitOrNull()"] {
list1.splitOrNull(0)
list1.splitOrNull(1)
list1.splitOrNull(2)
list1.splitOrNull(3)
List().split(0)
list1.splitOrNull(-1)
list1.splitOrNull(4)
}
["minOrNull"] {
list1.minOrNull
List("Pigeon", "Barn Owl", "Parrot").minOrNull
List(3.9, -8.4, 42, -99999).minOrNull
List(42, 42, 42).minOrNull
List(42, 42.0).minOrNull
List(42.0, 42).minOrNull
List(11.gb, 100.mb, 12.tb).minOrNull
List(11.s, 100.ms, 3.d).minOrNull
List().minOrNull
module.catch(() -> List(1, "Pigeon", 3.d).minOrNull)
}
["maxOrNull"] {
list1.maxOrNull
List("Pigeon", "Barn Owl", "Parrot").maxOrNull
List(3.9, -8.4, 42, -99999).maxOrNull
List(42, 42, 42).maxOrNull
List(42, 42.0).maxOrNull
List(42.0, 42).maxOrNull
List(11.gb, 100.mb, 12.tb).maxOrNull
List(11.s, 100.ms, 3.d).maxOrNull
List().maxOrNull
module.catch(() -> List(1, "Pigeon", 3.d).maxOrNull)
}
["minByOrNull()"] {
list1.minByOrNull((it) -> it)
list1.minByOrNull((it) -> -it)
List("Pigeon", "Barn Owl", "Parrot").minByOrNull((it) -> it.length)
List("Pigeon", "Barn Owl", "Parrot").minByOrNull((it) -> it.reverse())
List(11.gb, 100.mb, 12.tb).minByOrNull((it) -> it.value)
List(11.gb, 100.mb, 12.tb).minByOrNull((it) -> it)
List(11.s, 100.ms, 12.min).minByOrNull((it) -> it.value)
List(11.s, 100.ms, 12.min).minByOrNull((it) -> it)
List().minByOrNull((it) -> it)
module.catch(() -> List(1, "Pigeon", 3.d).minByOrNull((it) -> it))
}
["maxByOrNull()"] {
list1.maxByOrNull((it) -> it)
list1.maxByOrNull((it) -> -it)
List("Pigeon", "Barn Owl", "Parrot").maxByOrNull((it) -> it.length)
List("Pigeon", "Barn Owl", "Parrot").maxByOrNull((it) -> it.reverse())
List(11.gb, 100.mb, 12.tb).maxByOrNull((it) -> it.value)
List(11.gb, 100.mb, 12.tb).maxByOrNull((it) -> it)
List(11.s, 100.ms, 12.min).maxByOrNull((it) -> it.value)
List(11.s, 100.ms, 12.min).maxByOrNull((it) -> it)
List().maxByOrNull((it) -> it)
module.catch(() -> List(1, "Pigeon", 3.d).maxByOrNull((it) -> it))
}
}
@@ -0,0 +1,141 @@
amends "../snippetTest.pkl"
local class Person { name: String }
local empty = new Listing {}
local empty2 = (empty) {}
local base: Listing<Person> = new {
new { name = "Pigeon" }
new { name = "Barn Owl" }
new { name = "Parrot" }
}
local derived: Listing<Person> = (base) {
new { name = "Albatross" }
new { name = "Elf Owl" }
}
local duplicate: Listing<Person> = (base) {
new { name = "Albatross" }
new { name = "Parrot" }
new { name = "Elf Owl" }
}
facts {
["isEmpty"] {
empty.isEmpty
empty2.isEmpty
!base.isEmpty
!derived.isEmpty
}
["isDistinct"] {
empty.isDistinct
empty2.isDistinct
base.isDistinct
derived.isDistinct
!duplicate.isDistinct
}
["isDistinctBy()"] {
empty.isDistinctBy((it) -> it)
empty2.isDistinctBy((it) -> it)
base.isDistinctBy((it) -> it)
derived.isDistinctBy((it) -> it)
!duplicate.isDistinctBy((it) -> it)
empty.isDistinctBy((it) -> it.name)
empty2.isDistinctBy((it) -> it.name)
base.isDistinctBy((it) -> it.name)
derived.isDistinctBy((it) -> it.name)
!duplicate.isDistinctBy((it) -> it.name)
empty.isDistinctBy((it) -> it.getClass())
empty2.isDistinctBy((it) -> it.getClass())
!base.isDistinctBy((it) -> it.getClass())
!derived.isDistinctBy((it) -> it.getClass())
!duplicate.isDistinctBy((it) -> it.getClass())
}
}
examples {
["length"] {
empty.length
empty2.length
base.length
derived.length
}
["toList()"] {
empty.toList()
empty2.toList()
base.toList()
derived.toList()
duplicate.toList()
}
["toSet()"] {
empty.toSet()
empty2.toSet()
base.toSet()
derived.toSet()
duplicate.toSet()
}
["distinct"] {
empty.distinct
empty2.distinct
base.distinct
derived.distinct
duplicate.distinct
}
["distinctBy()"] {
empty.distinctBy((it) -> it)
empty2.distinctBy((it) -> it)
base.distinctBy((it) -> it)
derived.distinctBy((it) -> it)
duplicate.distinctBy((it) -> it)
empty.distinctBy((it) -> it.name)
empty2.distinctBy((it) -> it.name)
base.distinctBy((it) -> it.name)
derived.distinctBy((it) -> it.name)
duplicate.distinctBy((it) -> it.name)
empty.distinctBy((it) -> it.getClass())
empty2.distinctBy((it) -> it.getClass())
base.distinctBy((it) -> it.getClass())
derived.distinctBy((it) -> it.getClass())
duplicate.distinctBy((it) -> it.getClass())
}
["fold"] {
empty.fold(List(), (l, e) -> l.add(e))
base.fold(List(), (l, e) -> l.add(e))
derived.fold(List(), (l, e) -> l.add(e))
}
["foldIndexed"] {
empty.foldIndexed(List(), (i, l, e) -> l.add(Pair(i, e)))
base.foldIndexed(List(), (i, l, e) -> l.add(Pair(i, e)))
derived.foldIndexed(List(), (i, l, e) -> l.add(Pair(i, e)))
}
local baseNum = new Listing { 1; 2; 3 }
local baseString = new Listing { "Pigeon"; "Barn Owl"; "Parrot" }
local derivedString = (baseString) { "Albatross"; "Elf Owl" }
["join"] {
empty.join("")
baseNum.join("")
baseNum.join(", ")
baseString.join("")
baseString.join("---")
derivedString.join("")
derivedString.join("\n")
}
}
@@ -0,0 +1,139 @@
amends "../snippetTest.pkl"
local map1: Map<String, Int> = Map("one", 1, "two", 2, "three", 3)
facts {
["isEmpty"] {
Map().isEmpty
!map1.isEmpty
}
["containsKey()"] {
map1.containsKey("two")
!Map().containsKey("two")
!map1.containsKey("other")
}
["containsValue()"] {
map1.containsValue(3)
!Map().containsValue(3)
!map1.containsValue(4)
}
["every()"] {
Map().every((k, v) -> throw("unreachable code"))
map1.every((k, v) -> v < 10)
!map1.every((k, v) -> k.contains("o"))
}
["any()"] {
map1.any((k, v) -> k.contains("o"))
!Map().any((k, v) -> throw("unreachable code"))
!map1.any((k, v) -> false)
}
}
examples {
["length"] {
Map().length
map1.length
}
["getOrNull()"] {
map1.getOrNull("one")
map1.getOrNull("five")
}
["keys"] {
Map().keys
map1.keys
(Map("one", 1, "two", 2) + Map("three", 3, "four", 4)).keys
}
["values"] {
Map().values
map1.values
(Map("one", 1, "two", 2) + Map("three", 3, "four", 4)).values
}
["remove()"] {
Map().remove("two")
map1.remove("two")
map1.remove(2)
}
["filter()"] {
Map().filter((k, v) -> throw("unreachable"))
map1.filter((k, v) -> k.contains("o"))
map1.filter((k, v) -> v.isEven)
}
["mapKeys()"] {
Map().mapKeys((k, v) -> throw("unreachable"))
map1.mapKeys((k, v) -> k.toUpperCase() + v.toString())
map1.mapKeys((k, v) -> k)
}
["mapValues()"] {
Map().mapValues((k, v) -> throw("unreachable"))
map1.mapValues((k, v) -> k.length + v * 3)
map1.mapValues((k, v) -> v)
}
["map()"] {
Map().map((k, v) -> throw("unreachable"))
map1.map((k, v) -> Pair(k, v * 3))
map1.map((k, v) -> Pair(v, k))
module.catch(() -> map1.map((k, v) -> 42))
}
["flatMap()"] {
Map().flatMap((k, v) -> throw("unreachable"))
map1.flatMap((k, v) -> Map(v, v * 3))
map1.flatMap((k, v) -> Map(k, k.reverse()))
module.catch(() -> map1.flatMap((k, v) -> 42))
}
["entries"] {
Map().entries
map1.entries
(Map("one", 1, "two", 2) + Map("three", 3, "four", 4)).entries
}
["toMap()"] {
Map().toMap()
map1.toMap()
}
["toDynamic()"] {
Map().toDynamic()
map1.toDynamic()
}
["toMapping()"] {
Map().toMapping()
map1.toMapping()
}
["put()"] {
Map().put("one", 1)
map1.put("one", 1)
map1.put("four", 4)
map1.put("one", 4)
}
["toTyped()"] {
Map("name", "Pigeon", "age", 42).toTyped(Person)
Map("age", 42, "name", "Pigeon").toTyped(Person)
Map("age", 42).toTyped(Person).name
Map("name", "Pigeon").toTyped(Person).hasProperty("name")
Map("name", "Pigeon").toTyped(Person).hasProperty("age")
Map("name", "Pigeon", "age", 42, "hobby", "surfing").toTyped(Person)
module.catch(() -> Map("name", "Pigeon").toTyped(Person).age)
module.catch(() -> Map().toTyped(Int))
module.catch(() -> Map().toTyped(Abstract))
}
}
local class Person { name: String = "Default"; age: Int }
local abstract class Abstract
@@ -0,0 +1,116 @@
amends "../snippetTest.pkl"
local base = new Mapping {
default {
name = Undefined()
age = 42
}
["Pigeon"] {
name = "Pigeon"
}
["Parrot"] {
name = "Parrot"
age = 24
}
}
local derived = (base) {
["Pigeon"] {
name = "Piggy"
}
["Barn Owl"] {
name = "Barn Owl"
age = 84
}
}
local empty = new Mapping {
default = (_) -> 1
}
local empty2 = (empty) {
default = (name) -> name.length + 2
local `_` = 42
local function `_`() = 42
}
facts {
["isEmpty"] {
!base.isEmpty
!derived.isEmpty
empty.isEmpty
empty2.isEmpty
}
["containsKey()"] {
base.containsKey("Pigeon")
base.containsKey("Parrot")
!base.containsKey("Barn Owl")
!base.containsKey("Other")
derived.containsKey("Pigeon")
derived.containsKey("Parrot")
derived.containsKey("Barn Owl")
!derived.containsKey("Other")
!empty.containsKey("Pigeon")
!empty.containsKey("default")
!empty2.containsKey("Pigeon")
}
["length"] {
empty.length == 0
base.length == 2
derived.length == 3
}
["keys (of type string)"] {
empty.keys == Set()
derived.keys == Set("Pigeon", "Parrot", "Barn Owl")
base.keys == Set("Pigeon", "Parrot")
}
["keys (of type object)"] {
local base2 = new Mapping {
[empty] = "one"
[base] = "two"
}
local derived2 = (base2) {
[derived] = "three"
}
base2.keys == Set(empty, base)
derived2.keys == Set(empty, base, derived)
}
}
examples {
["getOrNull()"] {
base.getOrNull("Pigeon")
base.getOrNull("Parrot")
base.getOrNull("Barn Owl")
base.getOrNull("Other")
derived.getOrNull("Pigeon")
derived.getOrNull("Parrot")
derived.getOrNull("Barn Owl")
derived.getOrNull("Other")
empty.getOrNull("Pigeon")
empty2.getOrNull("Pigeon")
}
["fold()"] {
base.fold(List(), (l, k, v) -> l.add(Pair(k, v)))
derived.fold(List(), (l, k, v) -> l.add(Pair(k, v)))
empty.fold(List(), (l, k, v) -> l.add(Pair(k, v)))
empty2.fold(List(), (l, k, v) -> l.add(Pair(k, v)))
}
["toMap()"] {
base.toMap()
derived.toMap()
empty.toMap()
empty2.toMap()
}
}
@@ -0,0 +1,191 @@
amends "../snippetTest.pkl"
import "pkl:math"
examples {
["minInt"] {
math.minInt
math.minInt8
math.minInt16
math.minInt32
}
["maxInt"] {
math.maxInt
math.maxInt8
math.maxInt16
math.maxInt32
}
["maxUInt"] {
math.maxUInt
math.maxUInt8
math.maxUInt16
math.maxUInt32
}
["e"] {
math.e
}
["pi"] {
math.pi
}
["exp"] {
math.exp(2)
math.exp(2.34)
math.exp(-2)
math.exp(-2.34)
}
["sqrt"] {
math.sqrt(9)
math.sqrt(2)
math.sqrt(2.34)
math.sqrt(-2)
math.sqrt(-2.34)
}
["cbrt"] {
math.cbrt(27)
math.cbrt(2)
math.cbrt(2.34)
math.cbrt(-2)
math.cbrt(-2.34)
}
["log"] {
math.log(2)
math.log(2.34)
math.log(-2)
math.log(-2.34)
}
["log2"] {
math.log2(2)
math.log2(2.34)
math.log2(-2)
math.log2(-2.34)
}
["log10"] {
math.log10(2)
math.log10(2.34)
math.log10(-2)
math.log10(-2.34)
}
["sin"] {
math.sin(0)
math.sin(math.pi)
math.sin(2)
math.sin(2.34)
math.sin(-2)
math.sin(-2.34)
}
["cos"] {
math.cos(0)
math.cos(math.pi)
math.cos(2)
math.cos(2.34)
math.cos(-2)
math.cos(-2.34)
}
["tan"] {
math.tan(0)
math.tan(math.pi)
math.tan(2)
math.tan(2.34)
math.tan(-2)
math.tan(-2.34)
}
["asin"] {
math.asin(math.sin(0))
math.asin(math.sin(math.pi))
math.asin(math.sin(2))
math.asin(math.sin(2.34))
math.asin(math.sin(-2))
math.asin(math.sin(-2.34))
}
["acos"] {
math.acos(math.cos(0))
math.acos(math.cos(math.pi))
math.acos(math.cos(2))
math.acos(math.cos(2.34))
math.acos(math.cos(-2))
math.acos(math.cos(-2.34))
}
["atan"] {
math.atan(math.tan(0))
math.atan(math.tan(math.pi))
math.atan(math.tan(2))
math.atan(math.tan(2.34))
math.atan(math.tan(-2))
math.atan(math.tan(-2.34))
}
["gcd"] {
math.gcd(0, 0)
math.gcd(4, 6)
module.catch(() -> math.gcd(-4, 6))
module.catch(() -> math.gcd(4, -6))
math.gcd(9999999999999999, 888888888888888888)
module.catch(() -> math.gcd(4, 6.1))
}
["lcm"] {
math.lcm(0, 0)
math.lcm(4, 6)
module.catch(() -> math.lcm(-4, 6))
module.catch(() -> math.lcm(4, -6))
module.catch(() -> math.lcm(9999999999999999, 88888888888))
module.catch(() -> math.lcm(4, 6.1))
}
["isPowerOfTwo"] {
math.isPowerOfTwo(0)
math.isPowerOfTwo(0.0)
math.isPowerOfTwo(1)
math.isPowerOfTwo(1.0)
math.isPowerOfTwo(2)
math.isPowerOfTwo(2.0)
math.isPowerOfTwo(4096)
math.isPowerOfTwo(4096.0)
math.isPowerOfTwo(4097)
math.isPowerOfTwo(4096.01)
math.isPowerOfTwo(-8)
math.isPowerOfTwo(-8.0)
}
["max"] {
math.max(0, 0)
math.max(0, 0.0)
math.max(0, -0.0)
math.max(2, 4)
math.max(4, -2)
math.max(123, 123.456)
math.max(123.455, 123.456)
math.max(NaN, NaN)
math.max(NaN, 2)
math.max(Infinity, -Infinity)
}
["min"] {
math.min(0, 0)
math.min(0, 0.0)
math.min(0, -0.0)
math.min(2, 4)
math.min(4, -2)
math.min(123, 123.456)
math.min(123.455, 123.456)
math.min(NaN, NaN)
math.min(NaN, 2)
math.min(Infinity, -Infinity)
}
}
@@ -0,0 +1,12 @@
amends "../snippetTest.pkl"
examples {
["relativePathTo()"] {
module.relativePathTo(import("dir1/dir2/relativePathTo.pkl"))
module.relativePathTo(import("list.pkl"))
import("list.pkl").relativePathTo(import("set.pkl"))
module.relativePathTo(module)
module.catch(() -> import("dir1/dir2/relativePathTo.pkl").relativePathTo(module))
}
}
@@ -0,0 +1,5 @@
amends "../snippetTest.pkl"
output {
text = "some string"
}
@@ -0,0 +1,6 @@
output {
renderer = new JsonRenderer {}
}
// defining properties after output {} used to cause NPE
res1 = 42
@@ -0,0 +1,9 @@
pigeon {
name = "pigeon"
age = 30
}
output {
value = pigeon
renderer = new PListRenderer {}
}
@@ -0,0 +1,44 @@
class Person {
name: String
age: Int
}
res1 = "string"
res2 = true
res3 = 42
res4 = 1.23
res5 = 3.s
res6 = 4.mb
res8 = List("string", true, 42)
res9 = Set("string", true, 42)
res10 = Map("one", true, "two", 1.23)
res11 = new Listing { "string"; true; 42 }
res12 = new Mapping { ["name"] = "pigeon"; ["age"] = 30 }
res13 = new Dynamic { name = "pigeon"; age = 30 }
res14 = new Person { name = "pigeon" }
res15 = null
res16 = IntSeq(1, 4)
output {
renderer = new PListRenderer {
indent = " "
converters {
[String] = (it) -> it.reverse()
[Boolean] = (it) -> !it
[Int] = (it) -> it + 1
[Float] = (it) -> it + 1.1
[Duration] = (it) -> "\(it.value) \(it.unit)"
[DataSize] = (it) -> "\(it.value) \(it.unit)"
[List] = (it) -> it.reverse()
[Set] = (it) -> it + List(4)
[Map] = (it) -> it + Map("three", 3.s)
[Listing] = (it) -> (it) { 4 }
[Mapping] = (it) -> (it) { ["three"] = 3.s }
[Dynamic] = (it) -> (it) { other = "other" }
[Person] = (it) -> (it) { age = 40 } // fill in missing property
[Null] = (it) -> "String"
[Pair] = (it) -> Pair(it.second, it.first)
[IntSeq] = (it) -> List(it.start, it.end, it.step)
}
}
}
@@ -0,0 +1,44 @@
class Person {
name: String
age: Int
}
res1 = "string"
res2 = true
res3 = 42
res4 = 1.23
res5 = 3.s
res6 = 4.mb
res7 = List("string", true, 42)
res8 = Set("string", true, 42)
res9 = Map("string", true, 42, 1.23)
res10 = new Listing { "string"; true; 42 }
res11 = new Mapping { ["string"] = true; [42] = 1.23 }
res12 = new Dynamic { name = "pigeon"; age = 30 }
res13 = new Person { name = "pigeon"; age = 30 }
res14 = null
res15 = Pair(1, 2)
res16 = IntSeq(1, 4)
output {
renderer = new PListRenderer {
converters {
[String] = (it) -> "converted"
[Boolean] = (it) -> "converted"
[Int] = (it) -> "converted"
[Float] = (it) -> "converted"
[Duration] = (it) -> "converted"
[DataSize] = (it) -> "converted"
[List] = (it) -> "converted"
[Set] = (it) -> "converted"
[Map] = (it) -> "converted"
[Listing] = (it) -> "converted"
[Mapping] = (it) -> "converted"
[Dynamic] = (it) -> "converted"
[Person] = (it) -> "converted"
[Null] = (it) -> "converted"
[Pair] = (it) -> "converted"
[IntSeq] = (it) -> "converted"
}
}
}
@@ -0,0 +1,31 @@
import "pkl:test"
class Person {
name: String
age: Int
}
typealias Email = String
local renderer = new PListRenderer {}
res1 = renderer.renderValue(123)
res2 = renderer.renderValue(1.23)
res3 = renderer.renderValue(false)
res4 = renderer.renderValue("pigeon")
res6 = renderer.renderValue(List("pigeon", "parrot"))
res7 = renderer.renderValue(Set("pigeon", "parrot"))
res8 = renderer.renderValue(Map("name", "pigeon", "age", 42))
res9 = renderer.renderValue(new Listing { "pigeon"; "parrot" })
res10 = renderer.renderValue(new Mapping { ["name"] = "pigeon"; ["age"] = 42 })
res11 = renderer.renderValue(new Dynamic { name = "pigeon"; age = 42 })
res12 = renderer.renderValue(new Person { name = "pigeon"; age = 42 })
res13 = test.catch(() -> renderer.renderValue(null))
res14 = test.catch(() -> renderer.renderValue(1.min))
res15 = test.catch(() -> renderer.renderValue(1.mb))
res16 = test.catch(() -> renderer.renderValue(Person))
res17 = test.catch(() -> renderer.renderValue(Email))
res18 = test.catch(() -> renderer.renderValue((x) -> x))
res19 = test.catch(() -> new PListRenderer { converters { [Int] = (_) -> throw("ouch") } }.renderValue(42))
res20 = test.catch(() -> renderer.renderValue(IntSeq(1, 4)))
@@ -0,0 +1,33 @@
import "pkl:test"
class Person {
name: String
age: Int
}
typealias Email = String
local renderer = new PListRenderer {}
res1 = test.catch(() -> renderer.renderDocument(123))
res2 = test.catch(() -> renderer.renderDocument(1.23))
res3 = test.catch(() -> renderer.renderDocument(false))
res4 = test.catch(() -> renderer.renderDocument("pigeon"))
res6 = renderer.renderDocument(List("pigeon", "parrot"))
res7 = renderer.renderDocument(Set("pigeon", "parrot"))
res8 = renderer.renderDocument(Map("name", "pigeon", "age", 42))
res9 = renderer.renderDocument(new Listing { "pigeon"; "parrot" })
res10 = renderer.renderDocument(new Mapping { ["name"] = "pigeon"; ["age"] = 42 })
res11 = renderer.renderDocument(new Dynamic { name = "pigeon"; age = 42 })
res12 = renderer.renderDocument(new Person { name = "pigeon"; age = 42 })
res13 = test.catch(() -> renderer.renderDocument(1.min))
res14 = test.catch(() -> renderer.renderDocument(1.mb))
res15 = test.catch(() -> renderer.renderDocument(Person))
res16 = test.catch(() -> renderer.renderDocument(Email))
res17 = test.catch(() -> renderer.renderDocument((x) -> x))
res18 = test.catch(() -> renderer.renderDocument(null))
res19 = test.catch(() -> renderer.renderDocument(Pair(1, 2)))
res20 = test.catch(() -> new PListRenderer { converters { [Int] = (_) -> throw("ouch") } }.renderDocument(42))
res21 = test.catch(() -> renderer.renderDocument(IntSeq(1, 4)))
@@ -0,0 +1,31 @@
name = "pigeon"
age = 42
friends = new Listing {
"barn owl"
"parrot"
}
hobbies {
["surfing"] {
skill = "low"
}
["firemaking"] {
skill = "high"
}
}
address {
street = "Norton St."
zip = 12345
}
output {
renderer = new PListRenderer {
converters {
["name"] = (it) -> it.reverse()
["friends[*]"] = (it) -> it + "x"
["hobbies[*]"] = (it) -> it.skill
["address.street"] = (it) -> "Other St."
["address.*"] = (it) -> "changed"
["unmatched"] = (it) -> "unmatched"
}
}
}
@@ -0,0 +1,6 @@
// Test that unrenderable types do have an informative location in the error message.
m = new Mixin {}
output {
renderer = new PListRenderer {}
}
@@ -0,0 +1,31 @@
amends "../snippetTest.pkl"
local pair = Pair("Pigeon", 42)
local pair2 = Pair(new Dynamic { name = "Pigeon" }, List(1, 2, 3))
local pair3 = Pair(null, Pair(Regex("Pigeon"), 42.ms))
examples {
["first"] {
pair.first
pair2.first
pair3.first
}
["second"] {
pair.second
pair2.second
pair3.second
}
["key"] {
pair.key
pair2.key
pair3.key
}
["value"] {
pair.value
pair2.value
pair3.value
}
}
@@ -0,0 +1,38 @@
pigeon {
name = "pigeon"
age = 30
}
// dynamic object with properties and elements
barnOwl {
name = "barn owl"
"surfing"
age = 42
"fire making"
}
parrot {
new {
// multiline string that is a property within an element
lastName = """
Flinstone
Flinstone
"""
"""
Flinstone
Flinstone
"""
}
// multiline string that is an element
"""
Flistone
Flistone
"""
}
output {
renderer = new PcfRenderer {}
}
@@ -0,0 +1,45 @@
class Person {
name: String
age: Int
}
res1 = "string"
res2 = true
res3 = 42
res4 = 1.23
res5 = 3.s
res6 = 4.mb
res8 = List("string", true, 42)
res9 = Set("string", true, 42)
res10 = Map("one", true, "two", 1.23)
res11 = new Listing { "string"; true; 42 }
res12 = new Mapping { ["name"] = "pigeon"; ["age"] = 30 }
res13 = new Dynamic { name = "pigeon"; age = 30 }
res14 = new Person { name = "pigeon" }
res15 = null
res16 = Pair(1, 2)
res17 = IntSeq(1, 4)
output {
renderer = new PcfRenderer {
indent = " "
converters {
[String] = (it) -> it.reverse()
[Boolean] = (it) -> !it
[Int] = (it) -> it + 1
[Float] = (it) -> it + 1.1
[Duration] = (it) -> it + 2.s
[DataSize] = (it) -> it + 2.mb
[List] = (it) -> it.reverse()
[Set] = (it) -> it + List(4)
[Map] = (it) -> it + Map("three", 3.s)
[Listing] = (it) -> (it) { 4 }
[Mapping] = (it) -> (it) { ["three"] = 3.s }
[Dynamic] = (it) -> (it) { other = "other" }
[Person] = (it) -> (it) { age = 40 } // fill in missing property
[Null] = (_) -> "String"
[Pair] = (it) -> Pair(it.second, it.first)
[IntSeq] = (it) -> List(it.start, it.end, it.step)
}
}
}
@@ -0,0 +1,51 @@
local renderer = new PcfRenderer {
converters {
["^apple"] = (it) -> it.toUpperCase()
["^[*].apple"] = (it) -> it.toUpperCase()
["^[apple]"] = (it) -> it.toUpperCase()
}
}
local properties = renderer.renderDocument(
new Dynamic {
apple = "yes"
banana {
apple = "no"
}
})
res1 = properties.contains("YES")
res2 = properties.contains("no")
res3 = !properties.contains("yes")
res4 = !properties.contains("NO")
local elements = renderer.renderDocument(
new Dynamic {
new {
apple = "yes"
}
new {
new {
apple = "no"
}
}
})
res5 = elements.contains("YES")
res6 = elements.contains("no")
res7 = !elements.contains("yes")
res8 = !elements.contains("NO")
local entries = renderer.renderValue(
new Dynamic {
["apple"] = "yes"
["banana"] {
["apple"] = "no"
}
})
res9 = entries.contains("YES")
res10 = entries.contains("no")
res11 = !entries.contains("yes")
res12 = !entries.contains("NO")
@@ -0,0 +1,44 @@
class Person {
name: String
age: Int
}
res1 = "string"
res2 = true
res3 = 42
res4 = 1.23
res5 = 3.s
res6 = 4.mb
res8 = List("string", true, 42)
res9 = Set("string", true, 42)
res10 = Map("string", true, 42, 1.23)
res11 = new Listing { "string"; true; 42 }
res12 = new Mapping { ["name"] = "pigeon"; ["age"] = 30 }
res13 = new Dynamic { name = "pigeon"; age = 30 }
res14 = new Person { name = "pigeon"; age = 30 }
res15 = null
res16 = Pair(1, 2)
res17 = IntSeq(1, 4)
output {
renderer = new PcfRenderer {
converters {
[String] = (it) -> "converted"
[Boolean] = (it) -> "converted"
[Int] = (it) -> "converted"
[Float] = (it) -> "converted"
[Duration] = (it) -> "converted"
[DataSize] = (it) -> "converted"
[List] = (it) -> "converted"
[Set] = (it) -> "converted"
[Map] = (it) -> "converted"
[Listing] = (it) -> "converted"
[Mapping] = (it) -> "converted"
[Dynamic] = (it) -> "converted"
[Person] = (it) -> "converted"
[Null] = (it) -> "converted"
[Pair] = (it) -> "converted"
[IntSeq] = (it) -> "converted"
}
}
}
@@ -0,0 +1,34 @@
import "pkl:test"
class Person {
name: String
age: Int
}
typealias Email = String
local renderer = new PcfRenderer {}
res1 = renderer.renderValue(123)
res2 = renderer.renderValue(1.23)
res3 = renderer.renderValue(false)
res4 = renderer.renderValue("pigeon")
res5 = renderer.renderValue(List("pigeon", "parrot"))
res6 = renderer.renderValue(Set("pigeon", "parrot"))
res7 = renderer.renderValue(Map("name", "pigeon", "age", 42))
res8 = renderer.renderValue(new Listing { "pigeon"; "parrot" })
res9 = renderer.renderValue(new Mapping { ["name"] = "pigeon"; ["age"] = 42 })
res10 = renderer.renderValue(new Dynamic { name = "pigeon"; age = 42 })
res11 = renderer.renderValue(new Person { name = "pigeon"; age = 42 })
res12 = renderer.renderValue(1.min)
res13 = renderer.renderValue(1.mb)
res14 = renderer.renderValue(null)
res15 = renderer.renderValue(Pair(1, 2))
res16 = renderer.renderValue(IntSeq(1, 4))
res17 = renderer.renderValue(IntSeq(1, 4).step(-2))
res18 = renderer.renderValue(Regex("a.*b"))
res19 = test.catch(() -> renderer.renderValue(Person))
res20 = test.catch(() -> renderer.renderValue(Email))
res21 = test.catch(() -> renderer.renderValue((x) -> x))
res22 = test.catch(() -> new PcfRenderer { converters { [Int] = (_) -> throw("ouch") } }.renderValue(42))
@@ -0,0 +1,34 @@
import "pkl:test"
class Person {
name: String
age: Int
}
typealias Email = String
local renderer = new PcfRenderer {}
res1 = test.catch(() -> renderer.renderDocument(123))
res2 = test.catch(() -> renderer.renderDocument(1.23))
res3 = test.catch(() -> renderer.renderDocument(false))
res4 = test.catch(() -> renderer.renderDocument("pigeon"))
res5 = test.catch(() -> renderer.renderDocument(List("pigeon", "parrot")))
res6 = test.catch(() -> renderer.renderDocument(Set("pigeon", "parrot")))
res7 = test.catch(() -> renderer.renderDocument(Map("name", "pigeon", "age", 42)))
res8 = test.catch(() -> renderer.renderDocument(new Listing { "pigeon"; "parrot" }))
res9 = test.catch(() -> renderer.renderDocument(new Mapping { ["name"] = "pigeon"; ["age"] = 42 }))
res10 = renderer.renderDocument(new Dynamic { name = "pigeon"; age = 42 })
res11 = renderer.renderDocument(new Person { name = "pigeon"; age = 42 })
res12 = test.catch(() -> renderer.renderDocument(1.min))
res13 = test.catch(() -> renderer.renderDocument(1.mb))
res14 = test.catch(() -> renderer.renderDocument(null))
res15 = test.catch(() -> renderer.renderDocument(Pair(1, 2)))
res16 = test.catch(() -> renderer.renderDocument(Person))
res17 = test.catch(() -> renderer.renderDocument(Email))
res18 = test.catch(() -> renderer.renderDocument((x) -> x))
res19 = test.catch(() -> new PcfRenderer { converters { [Int] = (_) -> throw("ouch") } }.renderDocument(42))
res20 = test.catch(() -> renderer.renderDocument(IntSeq(1, 4)))
@@ -0,0 +1,26 @@
foo = null
bar = null
baz {
qux = 42
quux = null
corge = new Listing {
null
1337
null
"Hello World"
}
grault = new Mapping {
["garply"] = null
["waldo"] = 42
[new Listing {
"pigeon"
}] = null
}
thud = Pair(new Dynamic {}, new Listing {})
}
output {
renderer = new PcfRenderer {
omitNullProperties = true
}
}
@@ -0,0 +1,19 @@
rawString = import("../basic/rawString.pkl")
res39 = """
""\"#####
delimiters for multiline string
"""
res40 = """
"#####
no delimiters for multiline string
"""
res41 = "\\#"
output {
renderer = new PcfRenderer {
useCustomStringDelimiters = true
}
}
@@ -0,0 +1,2 @@
// Test that unrenderable types do have an informative location in the error message.
m = new Mixin {}
@@ -0,0 +1,30 @@
amends "../snippetTest.pkl"
import "pkl:platform"
local current = platform.current
facts {
["language"] {
current.language.version != ""
}
["runtime"] {
current.runtime.name != ""
current.runtime.version != ""
}
["virtualMachine"] {
current.virtualMachine.name != ""
current.virtualMachine.version != ""
}
["operatingSystem"] {
current.operatingSystem.name != ""
current.operatingSystem.version != ""
}
["processor"] {
current.processor.architecture != ""
}
}
@@ -0,0 +1,50 @@
local renderer = new PListRenderer {
converters {
["^apple"] = (it) -> it.toUpperCase()
["^[*].apple"] = (it) -> it.toUpperCase()
["^[apple]"] = (it) -> it.toUpperCase()
}
}
local properties = renderer.renderDocument(
new Dynamic {
apple = "yes"
banana {
apple = "no"
}
})
res1 = properties.contains("YES")
res2 = properties.contains("no")
res3 = !properties.contains("yes")
res4 = !properties.contains("NO")
local elements = renderer.renderDocument(
new Dynamic {
new {
apple = "yes"
}
new {
new {
apple = "no"
}
}
})
res5 = elements.contains("YES")
res6 = elements.contains("no")
res7 = !elements.contains("yes")
res8 = !elements.contains("NO")
local entries = renderer.renderValue(
new Dynamic {
["apple"] = "yes"
["banana"] {
["apple"] = "no"
}
})
res9 = entries.contains("YES")
res10 = entries.contains("no")
res11 = !entries.contains("yes")
res12 = !entries.contains("NO")
@@ -0,0 +1,9 @@
pigeon {
name = "pigeon"
age = 30
}
output {
value = pigeon
renderer = new PropertiesRenderer {}
}
@@ -0,0 +1 @@
foo = new PropertiesRenderer {}.renderValue(List("bar"))
@@ -0,0 +1,6 @@
// Test that unrenderable types do have an informative location in the error message.
m = new Mixin {}
output {
renderer = new PropertiesRenderer {}
}
@@ -0,0 +1,48 @@
class Person {
name: String
age: Int
}
res1 = "string"
res2 = true
res3 = 42
res4 = 1.23
res5 = 3.s
res6 = 4.mb
res8 = List("string", true, 42)
res9 = Set("string", true, 42)
res10 = Map("one", true, "two", 1.23)
res11 = new Listing { "string"; true; 42 }
res12 = new Mapping { ["name"] = "pigeon"; ["age"] = 30 }
res13 = new Dynamic { name = "pigeon"; age = 30 }
res14 = new Person { name = "pigeon" }
res15 = null
res16 = Pair(1, 2)
output {
renderer = new PropertiesRenderer {
omitNullProperties = false
converters = (jsonValueRenderer.converters) {
[List] = (it) -> jsonValueRenderer.renderValue(it.reverse())
[Set] = (it) -> jsonValueRenderer.renderValue(it + List(4))
[Map] = (it) -> jsonValueRenderer.renderValue(it + Map("three", 3.s))
[Listing] = (it) -> jsonValueRenderer.renderValue((it) { 4 })
[Mapping] = (it) -> jsonValueRenderer.renderValue((it) { ["three"] = 3.s })
[Dynamic] = (it) -> jsonValueRenderer.renderValue((it) { other = "other" })
[Person] = (it) -> jsonValueRenderer.renderValue((it) { age = 40 } /* fill in missing property */)
[Pair] = (it) -> jsonValueRenderer.renderValue(List(it.second, it.first))
[IntSeq] = (it) -> jsonValueRenderer.renderValue(List(it.start, it.end, it.step))
}
}
local jsonValueRenderer = new JsonRenderer {
converters {
[String] = (it) -> it.reverse()
[Boolean] = (it) -> !it
[Int] = (it) -> it + 1
[Float] = (it) -> it + 1.1
[Duration] = (it) -> "\(it.value) \(it.unit)"
[DataSize] = (it) -> "\(it.value) \(it.unit)"
[Null] = (it) -> "String"
}
}
}
@@ -0,0 +1,50 @@
local renderer = new PropertiesRenderer {
converters {
["^apple"] = (it) -> it.toUpperCase()
["^[*].apple"] = (it) -> it.toUpperCase()
["^[apple]"] = (it) -> it.toUpperCase()
}
}
local properties = renderer.renderDocument(
new Dynamic {
apple = "yes"
banana {
apple = "no"
}
})
res1 = properties.contains("YES")
res2 = properties.contains("no")
res3 = !properties.contains("yes")
res4 = !properties.contains("NO")
local elements = renderer.renderDocument(
new Dynamic {
["foo"] {
apple = "yes"
}
["bar"] {
["baz"] {
apple = "no"
}
}
})
res5 = elements.contains("YES")
res6 = elements.contains("no")
res7 = !elements.contains("yes")
res8 = !elements.contains("NO")
local entries = renderer.renderDocument(
new Dynamic {
["apple"] = "yes"
["banana"] {
["apple"] = "no"
}
})
res9 = entries.contains("YES")
res10 = entries.contains("no")
res11 = !entries.contains("yes")
res12 = !entries.contains("NO")
@@ -0,0 +1,51 @@
class Person {
name: String
age: Int
}
class NullablePerson {
name: String
age: Int
}
res1 = "string"
res2 = true
res3 = 42
res4 = 1.23
res5 = 3.s
res6 = 4.mb
res8 = List("string", true, 42)
res9 = Set("string", true, 42)
res10 = Map("string", true, 42, 1.23)
res11 = new Listing { "string"; true; 42 }
res12 = new Mapping { ["name"] = "pigeon"; ["age"] = 30 }
res13 = new Dynamic { name = "pigeon"; age = 30 }
res14 = new Person { name = "pigeon"; age = 30 }
res15 = null
res16 = Pair(1, 2)
res17 = new NullablePerson { name = "pigeon"; age = 30 } // not rendered
res18 = IntSeq(1, 4)
output {
renderer = new PropertiesRenderer {
converters {
[String] = (it) -> "converted"
[Boolean] = (it) -> "converted"
[Int] = (it) -> "converted"
[Float] = (it) -> "converted"
[Duration] = (it) -> "converted"
[DataSize] = (it) -> "converted"
[List] = (it) -> "converted"
[Set] = (it) -> "converted"
[Map] = (it) ->"converted"
[Listing] = (it) ->"converted"
[Mapping] = (it) ->"converted"
[Dynamic] = (it) ->"converted"
[Person] = (it) -> "converted"
[NullablePerson] = (it) -> null
[Null] = (it) -> "converted"
[Pair] = (it) -> "converted"
[IntSeq] = (it) -> "converted"
}
}
}
@@ -0,0 +1,30 @@
import "pkl:test"
class Person {
name: String
age: Int
}
typealias Email = String
res1 = new PropertiesRenderer {}.renderValue(123)
res2 = new PropertiesRenderer {}.renderValue(1.23)
res3 = new PropertiesRenderer {}.renderValue(false)
res4 = new PropertiesRenderer {}.renderValue("pigeon")
res5 = new PropertiesRenderer {}.renderValue(null)
res6 = test.catch(() -> new PropertiesRenderer {}.renderValue(1.min))
res7 = test.catch(() -> new PropertiesRenderer {}.renderValue(1.mb))
res8 = test.catch(() -> new PropertiesRenderer {}.renderValue(List("pigeon", "parrot")))
res9 = test.catch(() -> new PropertiesRenderer {}.renderValue(Set("pigeon", "parrot")))
res10 = test.catch(() -> new PropertiesRenderer {}.renderValue(Map("name", "pigeon", "age", 42)))
res11 = test.catch(() -> new PropertiesRenderer {}.renderValue(new Listing { "pigeon"; "parrot" }))
res12 = test.catch(() -> new PropertiesRenderer {}.renderValue(new Mapping { ["name"] = "pigeon"; ["age"] = 42 }))
res13 = test.catch(() -> new PropertiesRenderer {}.renderValue(new Dynamic { name = "pigeon"; age = 42 }))
res14 = test.catch(() -> new PropertiesRenderer {}.renderValue(new Person { name = "pigeon"; age = 42 }))
res15 = test.catch(() -> new PropertiesRenderer {}.renderValue(Pair(1, 2)))
res16 = test.catch(() -> new PropertiesRenderer {}.renderValue(Person))
res17 = test.catch(() -> new PropertiesRenderer {}.renderValue(Email))
res18 = test.catch(() -> new PropertiesRenderer {}.renderValue((x) -> x))
res19 = test.catch(() -> new PropertiesRenderer { converters { [Int] = (_) -> throw("ouch") } }.renderValue(42))
res20 = test.catch(() -> new PropertiesRenderer {}.renderValue(IntSeq(1, 4)))
@@ -0,0 +1,30 @@
import "pkl:test"
class Person {
name: String
age: Int
}
typealias Email = String
res1 = new PropertiesRenderer {}.renderDocument(Map("name", "pigeon", "age", 42))
res2 = new PropertiesRenderer {}.renderDocument(new Mapping { ["name"] = "pigeon"; ["age"] = 42 })
res3 = new PropertiesRenderer {}.renderDocument(new Dynamic { name = "pigeon"; age = 42 })
res4 = new PropertiesRenderer {}.renderDocument(new Person { name = "pigeon"; age = 42 })
res5 = test.catch(() -> new PropertiesRenderer {}.renderDocument(Pair(1, 2)))
res6 = test.catch(() -> new PropertiesRenderer {}.renderDocument(null))
res7 = test.catch(() -> new PropertiesRenderer {}.renderDocument(123))
res8 = test.catch(() -> new PropertiesRenderer {}.renderDocument(1.23))
res9 = test.catch(() -> new PropertiesRenderer {}.renderDocument(false))
res10 = test.catch(() -> new PropertiesRenderer {}.renderDocument("pigeon"))
res11 = test.catch(() -> new PropertiesRenderer {}.renderDocument(List("pigeon", "parrot")))
res12 = test.catch(() -> new PropertiesRenderer {}.renderDocument(Set("pigeon", "parrot")))
res13 = test.catch(() -> new PropertiesRenderer {}.renderDocument(new Listing { "pigeon"; "parrot" }))
res14 = test.catch(() -> new PropertiesRenderer {}.renderDocument(1.min))
res15 = test.catch(() -> new PropertiesRenderer {}.renderDocument(1.mb))
res16 = test.catch(() -> new PropertiesRenderer {}.renderDocument(Person))
res17 = test.catch(() -> new PropertiesRenderer {}.renderDocument(Email))
res18 = test.catch(() -> new PropertiesRenderer {}.renderDocument((x) -> x))
res19 = test.catch(() -> new PropertiesRenderer { converters { [Int] = (_) -> throw("ouch") } }.renderDocument(42))
res20 = test.catch(() -> new PropertiesRenderer {}.renderDocument(IntSeq(1, 4)))

Some files were not shown because too many files have changed in this diff Show More