testctx

package
v0.14.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBool

func GetBool(v eval.Value) bool

GetBool extracts a Go bool from an AILANG BoolValue

Parameters:

  • v: AILANG value (must be BoolValue)

Returns:

  • Go bool value
  • Panics if v is not a BoolValue

Example:

result, _ := myBuiltin(ctx, arg)
assert.True(t, testctx.GetBool(result))

func GetFloat

func GetFloat(v eval.Value) float64

GetFloat extracts a Go float64 from an AILANG FloatValue

Parameters:

  • v: AILANG value (must be FloatValue)

Returns:

  • Go float64 value
  • Panics if v is not a FloatValue

Example:

result, _ := myBuiltin(ctx, arg)
assert.InDelta(t, 3.14, testctx.GetFloat(result), 0.01)

func GetInt

func GetInt(v eval.Value) int

GetInt extracts a Go int from an AILANG IntValue

Parameters:

  • v: AILANG value (must be IntValue)

Returns:

  • Go int value
  • Panics if v is not an IntValue

Example:

result, _ := myBuiltin(ctx, arg)
assert.Equal(t, 42, testctx.GetInt(result))

func GetList

func GetList(v eval.Value) []eval.Value

GetList extracts a slice of values from an AILANG ListValue

Parameters:

  • v: AILANG value (must be ListValue)

Returns:

  • Slice of AILANG values
  • Panics if v is not a ListValue

Example:

result, _ := myBuiltin(ctx, arg)
items := testctx.GetList(result)
assert.Len(t, items, 3)

func GetRecord

func GetRecord(v eval.Value) map[string]eval.Value

GetRecord extracts a map from an AILANG RecordValue

Parameters:

  • v: AILANG value (must be RecordValue)

Returns:

  • Map of field names to AILANG values
  • Panics if v is not a RecordValue

Example:

result, _ := myBuiltin(ctx, arg)
fields := testctx.GetRecord(result)
assert.Equal(t, "Alice", testctx.GetString(fields["name"]))

func GetString

func GetString(v eval.Value) string

GetString extracts a Go string from an AILANG StringValue

Parameters:

  • v: AILANG value (must be StringValue)

Returns:

  • Go string value
  • Panics if v is not a StringValue

Example:

result, _ := myBuiltin(ctx, arg)
assert.Equal(t, "expected", testctx.GetString(result))

func IsUnit

func IsUnit(v eval.Value) bool

IsUnit checks if a value is a unit value

Parameters:

  • v: AILANG value

Returns:

  • true if v is a UnitValue

Example:

result, _ := myBuiltin(ctx, arg)
assert.True(t, testctx.IsUnit(result))

func MakeBool

func MakeBool(b bool) eval.Value

MakeBool creates an AILANG BoolValue from a Go bool

Parameters:

  • b: Go bool value

Returns:

  • AILANG BoolValue

Example:

verbose := testctx.MakeBool(true)

func MakeFloat

func MakeFloat(f float64) eval.Value

MakeFloat creates an AILANG FloatValue from a Go float64

Parameters:

  • f: Go float64 value

Returns:

  • AILANG FloatValue

Example:

pi := testctx.MakeFloat(3.14159)

func MakeInt

func MakeInt(n int) eval.Value

MakeInt creates an AILANG IntValue from a Go int

Parameters:

  • n: Go int value

Returns:

  • AILANG IntValue

Example:

timeout := testctx.MakeInt(5000)

func MakeList

func MakeList(items []eval.Value) eval.Value

MakeList creates an AILANG ListValue from a slice of values

Parameters:

  • items: Slice of AILANG values

Returns:

  • AILANG ListValue

Example:

headers := testctx.MakeList([]eval.Value{
    testctx.MakeRecord(map[string]eval.Value{
        "name":  testctx.MakeString("Content-Type"),
        "value": testctx.MakeString("application/json"),
    }),
})

func MakeRecord

func MakeRecord(fields map[string]eval.Value) eval.Value

MakeRecord creates an AILANG RecordValue from a map

Parameters:

  • fields: Map of field names to AILANG values

Returns:

  • AILANG RecordValue

Example:

user := testctx.MakeRecord(map[string]eval.Value{
    "id":    testctx.MakeInt(123),
    "name":  testctx.MakeString("Alice"),
    "admin": testctx.MakeBool(true),
})

func MakeString

func MakeString(s string) eval.Value

MakeString creates an AILANG StringValue from a Go string

Parameters:

  • s: Go string value

Returns:

  • AILANG StringValue

Example:

url := testctx.MakeString("https://example.com")

func MakeUnit

func MakeUnit() eval.Value

MakeUnit creates an AILANG unit value

Returns:

  • AILANG unit value

Example:

unit := testctx.MakeUnit()

Types

type MockEffContext

type MockEffContext struct {
	*effects.EffContext
	HTTPClient *http.Client // Mock HTTP client for testing
}

MockEffContext provides a test-friendly effect context with mocking capabilities

The mock context extends the standard EffContext with test-specific features:

  • Mock HTTP clients for hermetic network tests
  • Pre-configured capability grants
  • Simplified network security settings for testing
  • Value constructor/extractor helpers

Example usage:

ctx := testctx.NewMockEffContext()
ctx.GrantAll("IO", "Net")
ctx.SetHTTPClient(mockClient)

// Test a builtin
result, err := myBuiltin(ctx, arg1, arg2)
assert.NoError(t, err)
assert.Equal(t, expected, testctx.GetString(result))

func NewMockEffContext

func NewMockEffContext() *MockEffContext

NewMockEffContext creates a new mock effect context for testing

The mock context is pre-configured for hermetic testing:

  • No capabilities granted by default (must call Grant or GrantAll)
  • Deterministic seed (AILANG_SEED=42)
  • Short timeouts (5s)
  • Localhost allowed for testing
  • HTTP allowed for testing

Returns:

  • A new MockEffContext ready for testing

func (*MockEffContext) GetHTTPClient

func (m *MockEffContext) GetHTTPClient() *http.Client

GetHTTPClient returns the HTTP client to use for requests

Returns the mock client if set, otherwise returns http.DefaultClient.

Returns:

  • HTTP client for network operations

func (*MockEffContext) GrantAll

func (m *MockEffContext) GrantAll(caps ...string)

GrantAll grants multiple capabilities at once

This is a convenience method for tests that need multiple capabilities.

Parameters:

  • caps: Capability names to grant (e.g., "IO", "Net", "FS")

Example:

ctx.GrantAll("IO", "Net", "FS")

func (*MockEffContext) SetAllowedHosts

func (m *MockEffContext) SetAllowedHosts(hosts []string)

SetAllowedHosts restricts network access to specific hosts

This is useful for testing network security features or ensuring tests only access specific mock servers.

Parameters:

  • hosts: List of allowed hostnames (e.g., ["example.com", "api.test.com"])

Example:

ctx.SetAllowedHosts([]string{"api.example.com"})

func (*MockEffContext) SetBudget

func (m *MockEffContext) SetBudget(limits map[string]*int)

SetBudget sets effect budget limits for testing budget enforcement

Parameters:

  • limits: Map of effect name to budget limit (nil value = unlimited)

Example:

// Allow only 2 IO operations
ctx.SetBudget(map[string]*int{"IO": intPtr(2)})

func (*MockEffContext) SetHTTPClient

func (m *MockEffContext) SetHTTPClient(client *http.Client)

SetHTTPClient sets a mock HTTP client for network tests

The mock client will be used by Net effect operations instead of the default http.DefaultClient. This enables hermetic testing without real network requests.

Parameters:

  • client: A mock http.Client (can use httptest.NewServer)

Example:

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(200)
    w.Write([]byte(`{"status": "ok"}`))
}))
defer server.Close()

ctx.SetHTTPClient(server.Client())

func (*MockEffContext) SetNetTimeout

func (m *MockEffContext) SetNetTimeout(timeout time.Duration)

SetNetTimeout sets the network request timeout

Parameters:

  • timeout: Request timeout duration

Example:

ctx.SetNetTimeout(1 * time.Second)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL