summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/03_stdlib/40_proto
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-01-29 23:31:16 +0100
committerJo-Philipp Wich <jo@mein.io>2022-02-03 17:22:43 +0100
commit7edad5cefa0f065aa83dffd2d7830aeaf9f38662 (patch)
tree86b727f434302ffb28cb59278243517f9765e170 /tests/custom/03_stdlib/40_proto
parentd5003fde57eab19588da7bfdbaefe93d47435eb6 (diff)
tests: add functional tests for builtin functions
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/custom/03_stdlib/40_proto')
-rw-r--r--tests/custom/03_stdlib/40_proto89
1 files changed, 89 insertions, 0 deletions
diff --git a/tests/custom/03_stdlib/40_proto b/tests/custom/03_stdlib/40_proto
new file mode 100644
index 0000000..017a821
--- /dev/null
+++ b/tests/custom/03_stdlib/40_proto
@@ -0,0 +1,89 @@
+The `proto()` function retrievs or sets the prototype of the given object
+or resource value.
+
+Throws an exception if given value does not support setting prototypes.
+
+When invoked with one argument, returns the prototype of the given value
+(if any).
+
+When invoked with two arguments, returns the given value.
+
+-- Testcase --
+{%
+ let fs = require("fs");
+
+ // create a "class instance" by attaching a function dictionary to
+ // a plain object.
+ let obj = proto({}, {
+ greeting: function(name) {
+ printf("Hello, %s!\n", name);
+ }
+ });
+
+ // accessing a property on `obj` will look up the prototype chain
+ // if the object itself does not have it
+ obj.greeting("World");
+
+ printf("%.J\n", [
+ // retrieve prototype of `fs.file` resource
+ proto(fs.stdout),
+
+ // retrieve prototype of `obj`
+ proto(obj)
+ ]);
+%}
+-- End --
+
+-- Expect stdout --
+Hello, World!
+[
+ {
+ "error": "function error(...) { [native code] }",
+ "fileno": "function fileno(...) { [native code] }",
+ "close": "function close(...) { [native code] }",
+ "tell": "function tell(...) { [native code] }",
+ "seek": "function seek(...) { [native code] }",
+ "write": "function write(...) { [native code] }",
+ "read": "function read(...) { [native code] }"
+ },
+ {
+ "greeting": "function(name) { ... }"
+ }
+]
+-- End --
+
+
+
+Passing an invalid value throws an exception.
+
+-- Testcase --
+{%
+ proto("inval", {});
+%}
+-- End --
+
+-- Expect stderr --
+Type error: Passed value is neither a prototype, resource or object
+In line 2, byte 19:
+
+ ` proto("inval", {});`
+ Near here -----------^
+
+
+-- End --
+
+-- Testcase --
+{%
+ proto({}, "inval");
+%}
+-- End --
+
+-- Expect stderr --
+Type error: Passed value is neither a prototype, resource or object
+In line 2, byte 19:
+
+ ` proto({}, "inval");`
+ Near here -----------^
+
+
+-- End --