|
1 | 1 | package ferret |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "errors" |
| 6 | + "slices" |
5 | 7 | "strings" |
6 | 8 | "testing" |
7 | 9 |
|
8 | 10 | "github.com/MontFerret/ferret/v2/pkg/compiler" |
9 | 11 | ferretnet "github.com/MontFerret/ferret/v2/pkg/net" |
10 | 12 | ferrethttp "github.com/MontFerret/ferret/v2/pkg/net/http" |
11 | 13 | "github.com/MontFerret/ferret/v2/pkg/runtime" |
| 14 | + "github.com/MontFerret/ferret/v2/pkg/source" |
12 | 15 | "github.com/MontFerret/ferret/v2/pkg/stdlib" |
| 16 | + "github.com/MontFerret/ferret/v2/pkg/vm" |
13 | 17 | ) |
14 | 18 |
|
15 | 19 | func mustNewOptionsForTest(t *testing.T, setters ...Option) *options { |
@@ -226,6 +230,109 @@ func TestWithStdlibSafeRegistersSelectedGroups(t *testing.T) { |
226 | 230 | } |
227 | 231 | } |
228 | 232 |
|
| 233 | +func TestWithFunctionsRegistrarPreservesQualifiedHostFunctionNames(t *testing.T) { |
| 234 | + t.Parallel() |
| 235 | + |
| 236 | + eng := mustNewEngine(t, |
| 237 | + WithoutStdlib(), |
| 238 | + WithFunctionsRegistrar(func(ns runtime.Namespace) { |
| 239 | + ns.Namespace("Tools").Namespace("Risk").Function().A0().Add("Calculate_Risk", func(context.Context) (runtime.Value, error) { |
| 240 | + return runtime.NewString("ok"), nil |
| 241 | + }) |
| 242 | + }), |
| 243 | + ) |
| 244 | + defer func() { _ = eng.Close() }() |
| 245 | + |
| 246 | + if got := eng.host.functions.List(); !slices.Equal(got, []string{"Tools::Risk::Calculate_Risk"}) { |
| 247 | + t.Fatalf("expected declared host metadata, got %v", got) |
| 248 | + } |
| 249 | + |
| 250 | + for _, query := range []string{ |
| 251 | + "return tools::risk::calculate_risk()", |
| 252 | + "return TOOLS::RISK::CALCULATE_RISK()", |
| 253 | + "return Tools::Risk::Calculate_Risk()", |
| 254 | + } { |
| 255 | + output, err := eng.Run(t.Context(), source.NewAnonymous(query)) |
| 256 | + if err != nil { |
| 257 | + t.Fatalf("run %q: %v", query, err) |
| 258 | + } |
| 259 | + |
| 260 | + if got := string(output.Content); got != `"ok"` { |
| 261 | + t.Fatalf("run %q = %s, want %q", query, got, `"ok"`) |
| 262 | + } |
| 263 | + } |
| 264 | +} |
| 265 | + |
| 266 | +func TestWithFunctionsRegistrarRejectsCaseOnlyDuplicate(t *testing.T) { |
| 267 | + t.Parallel() |
| 268 | + |
| 269 | + eng, err := New( |
| 270 | + WithoutStdlib(), |
| 271 | + WithFunctionsRegistrar(func(ns runtime.Namespace) { |
| 272 | + fn := func(context.Context) (runtime.Value, error) { return runtime.None, nil } |
| 273 | + ns.Function().A0().Add("foo", fn).Add("FOO", fn) |
| 274 | + }), |
| 275 | + ) |
| 276 | + if eng != nil { |
| 277 | + _ = eng.Close() |
| 278 | + } |
| 279 | + |
| 280 | + if err == nil { |
| 281 | + t.Fatal("expected case-only duplicate registration to fail") |
| 282 | + } |
| 283 | +} |
| 284 | + |
| 285 | +func TestUnknownHostFunctionDiagnosticPreservesSourceSpelling(t *testing.T) { |
| 286 | + t.Parallel() |
| 287 | + |
| 288 | + eng := mustNewEngine(t, WithoutStdlib()) |
| 289 | + defer func() { _ = eng.Close() }() |
| 290 | + |
| 291 | + const name = "TOOLS::MiSsInG" |
| 292 | + _, err := eng.Run(t.Context(), source.NewAnonymous("return "+name+"()")) |
| 293 | + if err == nil { |
| 294 | + t.Fatal("expected unknown host function to fail") |
| 295 | + } |
| 296 | + |
| 297 | + var runtimeErr *vm.RuntimeError |
| 298 | + if !errors.As(err, &runtimeErr) { |
| 299 | + t.Fatalf("expected RuntimeError, got %T: %v", err, err) |
| 300 | + } |
| 301 | + |
| 302 | + want := "function '" + name + "' is not registered" |
| 303 | + if got := runtimeErr.Spans[0].Label; got != want { |
| 304 | + t.Fatalf("diagnostic label = %q, want %q", got, want) |
| 305 | + } |
| 306 | +} |
| 307 | + |
| 308 | +func TestResolvedHostFunctionDiagnosticUsesRegisteredQualifiedName(t *testing.T) { |
| 309 | + t.Parallel() |
| 310 | + |
| 311 | + eng := mustNewEngine(t, |
| 312 | + WithoutStdlib(), |
| 313 | + WithFunctionsRegistrar(func(ns runtime.Namespace) { |
| 314 | + ns.Namespace("DB").Namespace("POSTGRES").Function().A1().Add("QUERY", func(context.Context, runtime.Value) (runtime.Value, error) { |
| 315 | + return runtime.None, nil |
| 316 | + }) |
| 317 | + }), |
| 318 | + ) |
| 319 | + defer func() { _ = eng.Close() }() |
| 320 | + |
| 321 | + _, err := eng.Run(t.Context(), source.NewAnonymous("return Db::Postgres::Query()")) |
| 322 | + if err == nil { |
| 323 | + t.Fatal("expected invalid host function arity to fail") |
| 324 | + } |
| 325 | + |
| 326 | + var runtimeErr *vm.RuntimeError |
| 327 | + if !errors.As(err, &runtimeErr) { |
| 328 | + t.Fatalf("expected RuntimeError, got %T: %v", err, err) |
| 329 | + } |
| 330 | + |
| 331 | + if got, want := runtimeErr.Note, "DB::POSTGRES::QUERY expects 1 argument, but got 0"; got != want { |
| 332 | + t.Fatalf("diagnostic note = %q, want %q", got, want) |
| 333 | + } |
| 334 | +} |
| 335 | + |
229 | 336 | func TestWithStdlibEmptyMatchesWithoutStdlib(t *testing.T) { |
230 | 337 | t.Parallel() |
231 | 338 |
|
|
0 commit comments