ChaiScript/unittests/nested_namespaces.chai
leftibot 8f463078f3 Address review: C++-style namespace scoping and block declarations
Add :: scope resolution operator for member access (ns::func works
like ns.func). Add block namespace declarations:
  namespace x::y { def func() { ... } }
Functions and variables declared inside a namespace block are added
as members of the namespace, accessible via :: or dot notation.
Namespaces can be reopened to add more members, matching C++ behavior.

Requested by @lefticus in PR #675 review.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 17:16:55 -06:00

56 lines
1.1 KiB
ChaiScript

// Test C++-style block namespace declarations
namespace constants::si {
def mu_B() { return 1.0 }
}
namespace constants::mm {
def mu_B() { return 2.0 }
}
assert_equal(1.0, constants::si::mu_B())
assert_equal(2.0, constants::mm::mu_B())
// Test deeper nesting with block syntax
namespace a::b::c {
def val() { return 42 }
}
assert_equal(42, a::b::c::val())
// Test reopening a namespace to add more members
namespace math {
def square(x) { x * x }
}
namespace math::trig {
def double_angle(x) { 2.0 * x }
}
assert_equal(16, math::square(4))
assert_equal(6.0, math::trig::double_angle(3.0))
// Test reopening a namespace (C++ allows this)
namespace math {
def cube(x) { x * x * x }
}
assert_equal(27, math::cube(3))
// Test that :: scope resolution works the same as . for access
assert_equal(16, math.square(4))
assert_equal(6.0, math.trig.double_angle(3.0))
// Test namespace with var declarations
namespace config {
var pi = 3.14159
var name = "test"
}
assert_equal(3.14159, config::pi)
assert_equal("test", config::name)
// Test function-call style still works
namespace("compat")
compat.legacy = 99
assert_equal(99, compat::legacy)