mirror of
https://github.com/google/go-jsonnet.git
synced 2026-05-05 12:06:11 +02:00
Add desugaring of standalone assertions
This commit is contained in:
parent
0d716ae56f
commit
a4456d8ecf
17
desugarer.go
17
desugarer.go
@ -228,6 +228,13 @@ func desugarObjectComp(astComp *ast.ObjectComp, objLevel int) (ast.Node, error)
|
||||
// TODO(sbarzowski) this
|
||||
}
|
||||
|
||||
func buildLiteralString(value string) ast.Node {
|
||||
return &ast.LiteralString{
|
||||
Kind: ast.StringDouble,
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func buildSimpleIndex(obj ast.Node, member ast.Identifier) ast.Node {
|
||||
return &ast.Index{
|
||||
Target: obj,
|
||||
@ -301,8 +308,14 @@ func desugar(astPtr *ast.Node, objLevel int) (err error) {
|
||||
*astPtr = comp
|
||||
|
||||
case *ast.Assert:
|
||||
// TODO(sbarzowski) this
|
||||
*astPtr = &ast.LiteralNull{}
|
||||
if node.Message == nil {
|
||||
node.Message = buildLiteralString("Assertion failed")
|
||||
}
|
||||
*astPtr = &ast.Conditional{
|
||||
Cond: node.Cond,
|
||||
BranchTrue: node.Rest,
|
||||
BranchFalse: &ast.Error{Expr: node.Message},
|
||||
}
|
||||
|
||||
case *ast.Binary:
|
||||
// some operators get replaced by stdlib functions
|
||||
|
||||
1
testdata/assert.golden
vendored
Normal file
1
testdata/assert.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
true
|
||||
1
testdata/assert.input
vendored
Normal file
1
testdata/assert.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
assert true; true
|
||||
1
testdata/assert_failed.golden
vendored
Normal file
1
testdata/assert_failed.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
RUNTIME ERROR: Assertion failed
|
||||
1
testdata/assert_failed.input
vendored
Normal file
1
testdata/assert_failed.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
assert false; true
|
||||
1
testdata/assert_failed_custom.golden
vendored
Normal file
1
testdata/assert_failed_custom.golden
vendored
Normal file
@ -0,0 +1 @@
|
||||
RUNTIME ERROR: Custom Message
|
||||
1
testdata/assert_failed_custom.input
vendored
Normal file
1
testdata/assert_failed_custom.input
vendored
Normal file
@ -0,0 +1 @@
|
||||
assert false : "Custom Message"; true
|
||||
Loading…
x
Reference in New Issue
Block a user