Added tests for std::trait

parent 1a577430
......@@ -73,6 +73,7 @@
#! particular a mirror has to be allocated every time you need one. As such we
#! recommend against using reflection in performance critical code.
import std::fs::path::Path
import std::trait::IMPLEMENTED_TRAITS_ATTRIBUTE
## The default name to use for objects that don't have an explicit name.
let DEFAULT_OBJECT_NAME = 'Object'
......@@ -80,9 +81,6 @@ let DEFAULT_OBJECT_NAME = 'Object'
## The name of the attribute that contains the name of an object.
let OBJECT_NAME_ATTRIBUTE = '@_object_name'
## The name of the attribute that stores all the implemented traits.
let IMPLEMENTED_TRAITS_ATTRIBUTE = '@_implemented_traits'
## A generic mirror for a regular object.
object ObjectMirror {
def init(subject: Object) {
......@@ -314,6 +312,32 @@ object ObjectMirror {
DEFAULT_OBJECT_NAME
}
}
## Returns the traits implemented by the subject.
##
## # Examples
##
## Obtaining the implemented traits of an object:
##
## import std::conversion::ToString
## import std::mirror
##
## object Person impl ToString {
## def to_string -> String {
## 'Alice'
## }
## }
##
## mirror.reflect_object(Person).implemented_traits # => [ToString]
def implemented_traits -> Array!(Trait) {
let impl_set = get_attribute(IMPLEMENTED_TRAITS_ATTRIBUTE) as Object
impl_set.if true: {
_INKOC.get_attribute_names(impl_set) as Dynamic as Array!(Trait)
}, false: {
[]
}
}
}
## A mirror for a `Block`.
......
#! Methods for `Trait` that should be available before defining other types.
## The name of the attribute that stores all the implemented traits.
let IMPLEMENTED_TRAITS_ATTRIBUTE = '@_implemented_traits'
## Implements a `Trait` for an `Object`.
##
## Implementing a trait will result in all the trait's methods being copied over
......@@ -32,7 +35,7 @@ def implement(implement_for: Object, to_implement: Trait) -> Trait {
# This will look up `@_implemented_traits` if it exists, or create it (then
# return it) if it does not.
let mut traits =
_INKOC.set_attribute_to_object(implement_for, '@_implemented_traits')
_INKOC.set_attribute_to_object(implement_for, IMPLEMENTED_TRAITS_ATTRIBUTE)
_INKOC.copy_blocks(implement_for, to_implement)
......
......@@ -32,5 +32,6 @@ import test::std::test_process
import test::std::test_range
import test::std::test_string
import test::std::test_string_buffer
import test::std::test_trait
test.run
......@@ -190,6 +190,20 @@ test.group('std::mirror::ObjectMirror.name') do (g) {
}
}
test.group('std::mirror::ObjectMirror.implemented_traits') do (g) {
g.test('Obtaining the traits implemented by an object') {
let traits = ObjectMirror.new(Dummy).implemented_traits
assert.equal(traits, [Trait1])
}
g.test('Obtaining the traits implemented by an object instance') {
let traits = ObjectMirror.new(Dummy.new).implemented_traits
assert.equal(traits, [Trait1])
}
}
test.group('std::mirror::BlockMirror.name') do (g) {
g.test('Obtaining the name of a closure') {
let mirror = BlockMirror.new({})
......
import std::mirror
import std::test
import std::test::assert
import std::trait::(self as trait_mod)
trait Example {
def pass(number: Integer) -> Integer {
number
}
}
test.group('std::trait.implement') do (g) {
g.test('Copying the default methods of a Trait to the Object') {
let obj = Object.new
let obj_mirror = mirror.reflect_object(obj)
trait_mod.implement(implement_for: obj, to_implement: Example)
let method_impl = obj_mirror.get_attribute('pass') as Block
let method_exp =
mirror.reflect_object(Example).get_attribute('pass') as Block
assert.equal(method_impl, method_exp)
}
g.test('Tracking the implemented trait') {
let obj = Object.new
let obj_mirror = mirror.reflect_object(obj)
trait_mod.implement(implement_for: obj, to_implement: Example)
assert.equal(obj_mirror.implemented_traits, [Example])
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment