@@ -52,3 +52,61 @@ func TestEvalJsonnetWithExpression(t *testing.T) {
52
52
})
53
53
}
54
54
}
55
+
56
+ // An EvalScript with a top-level function containing only optional arguments
57
+ // should be evaluated as a function even if no TLAs are provided.
58
+ func TestEvalWithOptionalTlas (t * testing.T ) {
59
+ opts := jsonnet.Opts {
60
+ EvalScript : "main.metadata.name" ,
61
+ }
62
+ json , err := evalJsonnet ("testdata/cases/with-optional-tlas/main.jsonnet" , jsonnetImpl , opts )
63
+ assert .NoError (t , err )
64
+ assert .Equal (t , `"bar-baz"` , strings .TrimSpace (json ))
65
+ }
66
+
67
+ // An EvalScript with a top-level function containing should allow passing only
68
+ // a subset of the TLAs.
69
+ func TestEvalWithOptionalTlasSpecifiedArg2 (t * testing.T ) {
70
+ opts := jsonnet.Opts {
71
+ EvalScript : "main.metadata.name" ,
72
+ TLACode : jsonnet.InjectedCode {"baz" : "'changed'" },
73
+ }
74
+ json , err := evalJsonnet ("testdata/cases/with-optional-tlas/main.jsonnet" , jsonnetImpl , opts )
75
+ assert .NoError (t , err )
76
+ assert .Equal (t , `"bar-changed"` , strings .TrimSpace (json ))
77
+ }
78
+
79
+ // An EvalScript with a top-level function having no arguments should be
80
+ // evaluated as a function even if no TLAs are provided.
81
+ func TestEvalFunctionWithNoTlas (t * testing.T ) {
82
+ opts := jsonnet.Opts {
83
+ EvalScript : "main.metadata.name" ,
84
+ }
85
+ json , err := evalJsonnet ("testdata/cases/function-with-zero-params/main.jsonnet" , jsonnetImpl , opts )
86
+ assert .NoError (t , err )
87
+ assert .Equal (t , `"inline"` , strings .TrimSpace (json ))
88
+ }
89
+
90
+ // An EvalScript with a top-level function should return an understandable
91
+ // error message if an incorrect TLA is provided.
92
+ func TestInvalidTlaArg (t * testing.T ) {
93
+ opts := jsonnet.Opts {
94
+ EvalScript : "main" ,
95
+ TLACode : jsonnet.InjectedCode {"foo" : "'bar'" },
96
+ }
97
+ json , err := evalJsonnet ("testdata/cases/function-with-zero-params/main.jsonnet" , jsonnetImpl , opts )
98
+ assert .Contains (t , err .Error (), "function has no parameter foo" )
99
+ assert .Equal (t , "" , json )
100
+ }
101
+
102
+ // Providing a TLA to an EvalScript with a non-function top level mainfile
103
+ // should not return an error.
104
+ func TestTlaWithNonFunction (t * testing.T ) {
105
+ opts := jsonnet.Opts {
106
+ EvalScript : "main" ,
107
+ TLACode : jsonnet.InjectedCode {"foo" : "'bar'" },
108
+ }
109
+ json , err := evalJsonnet ("testdata/cases/withenv/main.jsonnet" , jsonnetImpl , opts )
110
+ assert .NoError (t , err )
111
+ assert .NotEmpty (t , json )
112
+ }
0 commit comments