Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly propagate object assertions during inheritance #172

Merged
merged 2 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sjsonnet/src/sjsonnet/Val.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ object Val{

def addSuper(pos: Position, lhs: Val.Obj): Val.Obj = {
`super` match{
case null => new Val.Obj(pos, getValue0, false, null, lhs)
case x => new Val.Obj(pos, getValue0, false, null, x.addSuper(pos, lhs))
case null => new Val.Obj(pos, getValue0, false, triggerAsserts, lhs)
case x => new Val.Obj(pos, getValue0, false, triggerAsserts, x.addSuper(pos, lhs))
}
}

Expand Down
29 changes: 15 additions & 14 deletions sjsonnet/test/src/sjsonnet/EvaluatorTests.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package sjsonnet

import utest._
import TestUtils.eval
import TestUtils.{eval, evalErr}
object EvaluatorTests extends TestSuite{

def evalErr(s: String, strict: Boolean = false) = {
try {
val x = eval(s, strict = strict)
throw new Exception (s"Expected exception, got result: $x")
}catch{case e: Exception =>
e.getMessage.split('\n').map(_.trim).mkString("\n") // normalize inconsistent indenation on JVM vs JS
}
}

def tests = Tests{
test("arithmetic") {
eval("1 + 2 + 3") ==> ujson.Num(6)
Expand Down Expand Up @@ -315,13 +308,13 @@ object EvaluatorTests extends TestSuite{
eval("'%#-0- + 5.5f' % -123.456") ==> ujson.Str("-123.45600")
}
test("strict") {
eval("({ a: 1 } { b: 2 }).a", false) ==> ujson.Num(1)
evalErr("({ a: 1 } { b: 2 }).a", true) ==>
eval("({ a: 1 } { b: 2 }).a", strict = false) ==> ujson.Num(1)
evalErr("({ a: 1 } { b: 2 }).a", strict = true) ==>
"""sjsonnet.StaticError: Adjacent object literals not allowed in strict mode - Use '+' to concatenate objects
|at [ObjExtend].(:1:11)""".stripMargin
eval("local x = { c: 3 }; (x { a: 1 } { b: 2 }).a", false) ==> ujson.Num(1)
eval("local x = { c: 3 }; (x { a: 1 }).a", true) ==> ujson.Num(1)
evalErr("local x = { c: 3 }; ({ a: 1 } { b: 2 }).a", true) ==>
eval("local x = { c: 3 }; (x { a: 1 } { b: 2 }).a", strict = false) ==> ujson.Num(1)
eval("local x = { c: 3 }; (x { a: 1 }).a", strict = true) ==> ujson.Num(1)
evalErr("local x = { c: 3 }; ({ a: 1 } { b: 2 }).a", strict = true) ==>
"""sjsonnet.StaticError: Adjacent object literals not allowed in strict mode - Use '+' to concatenate objects
|at [ObjExtend].(:1:31)""".stripMargin
}
Expand Down Expand Up @@ -368,5 +361,13 @@ object EvaluatorTests extends TestSuite{
assert(evalErr("""error {a: "b"}""").contains("""{"a": "b"}"""))
assert(evalErr("""assert 1 == 2 : { a: "b"}; 1""").contains("""{"a": "b"}"""))
}

test("assertInheritance"){
test - assert(evalErr("""{ } + {assert false}""").contains("sjsonnet.Error: Assertion failed"))
test - assert(evalErr("""{assert false} + {}""").contains("sjsonnet.Error: Assertion failed"))
test - assert(evalErr("""{assert false} + {} + {}""").contains("sjsonnet.Error: Assertion failed"))
test - assert(evalErr("""{} + {assert false} + {}""").contains("sjsonnet.Error: Assertion failed"))
test - assert(evalErr("""{} + {} + {assert false}""").contains("sjsonnet.Error: Assertion failed"))
}
}
}
18 changes: 8 additions & 10 deletions sjsonnet/test/src/sjsonnet/PreserveOrderTests.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sjsonnet

import utest._
import TestUtils.eval
import TestUtils.{eval, evalErr}
object PreserveOrderTests extends TestSuite {

def tests = Tests {
Expand Down Expand Up @@ -289,19 +289,17 @@ object PreserveOrderTests extends TestSuite {
}

test("preserveOrderError") {
try {
eval(
assert(
evalErr(
"""local x = { b: 1, a: 2, c: self.a + self.b };
error x""", true)
assert(false)
} catch {
case e: Exception =>
assert(e.getMessage().startsWith("""sjsonnet.Error: {"b": 1, "a": 2, "c": 3}"""))
}
error x""", preserveOrder = true
)
.startsWith("""sjsonnet.Error: {"b": 1, "a": 2, "c": 3}""")
)
}

test("preserveOrderPreservesEquality") {
eval("""{a: 1, b: 2} == {b: 2, a: 1}""", true).toString ==> "true"
eval("""{a: 1, b: 2} == {b: 2, a: 1}""", preserveOrder = true).toString ==> "true"
}

test("preserveOrderSet") {
Expand Down
14 changes: 6 additions & 8 deletions sjsonnet/test/src/sjsonnet/StdFlatMapTests.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sjsonnet

import utest._
import TestUtils.eval
import TestUtils.{eval, evalErr}
object StdFlatMapTests extends TestSuite {

def tests = Tests {
Expand All @@ -14,13 +14,11 @@ object StdFlatMapTests extends TestSuite {

eval("std.flatMap(function (x) if x == \" \" then null else x, \"a b c d e\")") ==> ujson.Str("abcde")

try {
eval("std.flatMap(function(x) 123, 'Hello')")
assert(false)
} catch {
case e: Exception =>
assert(e.getMessage().startsWith("""sjsonnet.Error: flatMap func must return string, got number"""))
}
assert(
evalErr("std.flatMap(function(x) 123, 'Hello')")
.startsWith("sjsonnet.Error: flatMap func must return string, got number")
)

}
}
}
14 changes: 12 additions & 2 deletions sjsonnet/test/src/sjsonnet/TestUtils.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package sjsonnet

object TestUtils {
def eval(s: String, preserveOrder: Boolean = false, strict: Boolean = false) = {
def eval0(s: String, preserveOrder: Boolean = false, strict: Boolean = false) = {
new Interpreter(
Map(),
Map(),
DummyPath(),
Importer.empty,
parseCache = new DefaultParseCache,
new Settings(preserveOrder = preserveOrder, strict = strict)
).interpret(s, DummyPath("(memory)")) match {
).interpret(s, DummyPath("(memory)"))
}

def eval(s: String, preserveOrder: Boolean = false, strict: Boolean = false) = {
eval0(s, preserveOrder, strict) match {
case Right(x) => x
case Left(e) => throw new Exception(e)
}
}

def evalErr(s: String, preserveOrder: Boolean = false, strict: Boolean = false) = {
eval0(s, preserveOrder, strict) match{
case Left(err) => err.split('\n').map(_.trim).mkString("\n") // normalize inconsistent indenation on JVM vs JS
case Right(r) => throw new Exception(s"Expected exception, got result: $r")
}
}
}