Skip to content

Commit

Permalink
add tests for json module
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidesu committed Oct 27, 2023
1 parent 74b0cec commit 496193e
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions library/modules/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package modules

import (
"testing"

"ghostlang.org/x/ghost/object"
"ghostlang.org/x/ghost/token"
"github.com/shopspring/decimal"
)

func TestJsonDecode(t *testing.T) {
input := `{"name": "John", "age": 30, "city": "New York"}`

expected := &object.Map{Pairs: map[object.MapKey]object.MapPair{
(&object.String{Value: "name"}).MapKey(): {Key: &object.String{Value: "name"}, Value: &object.String{Value: "Kai"}},
(&object.String{Value: "age"}).MapKey(): {Key: &object.String{Value: "age"}, Value: &object.Number{Value: decimal.NewFromInt(34)}},
}}

result := jsonDecode(nil, token.Token{}, &object.String{Value: input})

if result.Type() != expected.Type() {
t.Errorf("wrong result type. got=%s, expected=%s", result.Type(), expected.Type())
}
}

func TestJsonEncode(t *testing.T) {
input := &object.Map{Pairs: map[object.MapKey]object.MapPair{
(&object.String{Value: "name"}).MapKey(): {Key: &object.String{Value: "name"}, Value: &object.String{Value: "Kai"}},
(&object.String{Value: "age"}).MapKey(): {Key: &object.String{Value: "age"}, Value: &object.Number{Value: decimal.NewFromInt(34)}},
}}

expected := `{"age":34,"name":"Kai"}`

result := jsonEncode(nil, token.Token{}, input)

if result.String() != expected {
t.Errorf("wrong result. got=%s, expected=%s", result.String(), expected)
}
}

0 comments on commit 496193e

Please sign in to comment.