Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pretty print to object mod #732

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/docs/standard-lib/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,27 @@ This method will return a string of the object's hash value.
```cs
Object.hash("Dictu");
```

### Object.prettyPrint(Value, indent -> Optional) -> Result\<String>
briandowns marked this conversation as resolved.
Show resolved Hide resolved

This method will return a string representation of the given value.

**NOTE** Strings, dicts, lists, numbers, and nil are valid values for pretty printing at this time.
briandowns marked this conversation as resolved.
Show resolved Hide resolved

```cs
Object.prettyPrint([1, 2, 3]).unwrap();

// Output
'[1, 2, 3]'
```

```cs
Object.prettyPrint({"a": 1}, 4).unwrap()
briandowns marked this conversation as resolved.
Show resolved Hide resolved

// Output
'
{
"a": 1
}
'
```
2 changes: 1 addition & 1 deletion src/optionals/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ json_value* stringifyJson(DictuVM *vm, Value value) {
return NULL;
}

static Value stringify(DictuVM *vm, int argCount, Value *args) {
Value stringify(DictuVM *vm, int argCount, Value *args) {
if (argCount != 1 && argCount != 2) {
runtimeError(vm, "stringify() takes 1 or 2 arguments (%d given).", argCount);
return EMPTY_VAL;
Expand Down
1 change: 1 addition & 0 deletions src/optionals/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
#include "../vm/vm.h"

Value createJSONModule(DictuVM *vm);
Value stringify(DictuVM *vm, int argCount, Value *args);

#endif //dictu_json_h
16 changes: 16 additions & 0 deletions src/optionals/object/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,21 @@ static Value objectHash(DictuVM *vm, int argCount, Value *args) {
return OBJ_VAL(copyString(vm, (char *)str, 21));
}

static Value objectPrettyPrint(DictuVM *vm, int argCount, Value *args) {
if (argCount != 1 && argCount != 2) {
runtimeError(vm, "prettyPrint() takes 1 or arguments (%d given)", argCount);
return EMPTY_VAL;
}

return OBJ_VAL(stringify(vm, argCount, args));
}

Value createObjectModule(DictuVM *vm) {
ObjString *name = copyString(vm, "Object", 6);
push(vm, OBJ_VAL(name));
ObjModule *module = newModule(vm, name);
push(vm, OBJ_VAL(module));

briandowns marked this conversation as resolved.
Show resolved Hide resolved
ObjClosure *closure = compileModuleToClosure(vm, "Object", DICTU_OBJECT_SOURCE);

if (closure == NULL) {
Expand All @@ -75,6 +89,8 @@ Value createObjectModule(DictuVM *vm) {
defineNative(vm, &closure->function->module->values, "getClassRef", objectGetClassRef);
defineNative(vm, &closure->function->module->values, "hash", objectHash);

defineNative(vm, &module->values, "prettyPrint", objectPrettyPrint);
briandowns marked this conversation as resolved.
Show resolved Hide resolved

pop(vm);

return OBJ_VAL(closure);
Expand Down
1 change: 1 addition & 0 deletions src/optionals/object/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "../optionals.h"
#include "../../vm/vm.h"
#include "../json.h"

Value createObjectModule(DictuVM *vm);

Expand Down
1 change: 1 addition & 0 deletions tests/object/import.du
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

import "createFrom.du";
import "hash.du";
import "prettyPrint.du";
30 changes: 30 additions & 0 deletions tests/object/prettyPrint.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* prettyPrint.du
*
* Testing the Object.prettyPrint method
*/
from UnitTest import UnitTest;

import Object;

class Test {}

class TestObjectPrettyPrint < UnitTest {
testObjectPrettyPrint() {
this.assertEquals(Object.prettyPrint(1).unwrap(), "1");
this.assertEquals(Object.prettyPrint("1").unwrap(), '"1"');
this.assertEquals(Object.prettyPrint([1, 2, 3]).unwrap(), '[1, 2, 3]');
this.assertEquals(Object.prettyPrint({"a": 1}).unwrap(), '{"a": 1}');
this.assertEquals(Object.prettyPrint(nil).unwrap(), 'null');
this.assertEquals(Object.prettyPrint(true).unwrap(), 'true');
this.assertEquals(Object.prettyPrint(false).unwrap(), 'false');
this.assertError(Object.prettyPrint(Test()));
}

testObjectPrettyPrintWithIndent() {
this.assertEquals(Object.prettyPrint([1, 2, 3], 2).unwrap(), '[\n 1,\n 2,\n 3\n]');
this.assertEquals(Object.prettyPrint({"a": 1}, 2).unwrap(), '{\n "a": 1\n}');
}
}

TestObjectPrettyPrint().run();
Loading