generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logic): add util func to extract json object attribute
- Loading branch information
Showing
2 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package predicate | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/ichiban/prolog/engine" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestExtractJsonTerm(t *testing.T) { | ||
Convey("Given a test cases", t, func() { | ||
cases := []struct { | ||
compound engine.Compound | ||
result map[string]engine.Term | ||
wantSuccess bool | ||
wantError error | ||
}{ | ||
{ | ||
compound: engine.NewAtom("foo").Apply(engine.NewAtom("bar")).(engine.Compound), | ||
wantSuccess: false, | ||
wantError: fmt.Errorf("invalid functor foo. Expected json"), | ||
}, | ||
{ | ||
compound: engine.NewAtom("json").Apply(engine.NewAtom("bar"), engine.NewAtom("foobar")).(engine.Compound), | ||
wantSuccess: false, | ||
wantError: fmt.Errorf("invalid compound arity : 2 but expected 1"), | ||
}, | ||
{ | ||
compound: engine.NewAtom("json").Apply(engine.NewAtom("bar")).(engine.Compound), | ||
wantSuccess: false, | ||
wantError: fmt.Errorf("json compound should contains one list, give engine.Atom"), | ||
}, | ||
{ | ||
compound: AtomJSON.Apply(engine.List(AtomPair.Apply(engine.NewAtom("foo"), engine.NewAtom("bar")))).(engine.Compound), | ||
result: map[string]engine.Term{ | ||
"foo": engine.NewAtom("bar"), | ||
}, | ||
wantSuccess: true, | ||
}, | ||
{ | ||
compound: AtomJSON.Apply(engine.List(engine.NewAtom("foo"), engine.NewAtom("bar"))).(engine.Compound), | ||
wantSuccess: false, | ||
wantError: fmt.Errorf("json attributes should be a pair"), | ||
}, | ||
{ | ||
compound: AtomJSON.Apply(engine.List(AtomPair.Apply(engine.Integer(10), engine.NewAtom("bar")))).(engine.Compound), | ||
wantSuccess: false, | ||
wantError: fmt.Errorf("first pair arg should be an atom"), | ||
}, | ||
} | ||
for nc, tc := range cases { | ||
Convey(fmt.Sprintf("Given the term compound #%d: %s", nc, tc.compound), func() { | ||
Convey("when extract json term", func() { | ||
env := engine.Env{} | ||
result, err := ExtractJsonTerm(tc.compound, &env) | ||
|
||
if tc.wantSuccess { | ||
Convey("then no error should be thrown", func() { | ||
So(err, ShouldBeNil) | ||
So(result, ShouldNotBeNil) | ||
|
||
Convey("and result should be as expected", func() { | ||
So(result, ShouldResemble, tc.result) | ||
}) | ||
}) | ||
} else { | ||
Convey("then error should occurs", func() { | ||
So(err, ShouldNotBeNil) | ||
|
||
Convey("and should be as expected", func() { | ||
So(err, ShouldResemble, tc.wantError) | ||
}) | ||
}) | ||
} | ||
}) | ||
}) | ||
} | ||
}) | ||
} |