Skip to content

Commit

Permalink
feat(logic): uri_encoded/3 detect component used
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed May 5, 2023
1 parent 1ab81d9 commit 276bf77
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
51 changes: 51 additions & 0 deletions x/logic/predicate/uri.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package predicate

import (
"context"
"fmt"

"github.com/ichiban/prolog/engine"
)

type Component string

const (
QueryComponent Component = "query"
FragmentComponent Component = "fragment"
PathComponent Component = "path"
SegmentComponent Component = "segment"
)

func NewComponent(v string) (Component, error) {
switch v {
case string(QueryComponent):
return QueryComponent, nil
case string(FragmentComponent):
return FragmentComponent, nil
case string(PathComponent):
return PathComponent, nil
case string(SegmentComponent):
return SegmentComponent, nil
default:
return "", fmt.Errorf("invalid component name %s, expected `query`, `fragment`, `path` or `segment`", v)
}
}

func URIEncoded(vm *engine.VM, component, decoded, encoded engine.Term, cont engine.Cont, env *engine.Env) *engine.Promise {
return engine.Delay(func(ctx context.Context) *engine.Promise {
var comp Component
switch c := env.Resolve(component).(type) {
case engine.Atom:
_, err := NewComponent(c.String())
if err != nil {
return engine.Error(fmt.Errorf("uri_encoded/3: %w", err))
}
default:
return engine.Error(fmt.Errorf("uri_encoded/3: invalid component type: %T, should be Atom", component))
}

fmt.Printf("%s", comp)

return engine.Bool(true)
})
}
90 changes: 90 additions & 0 deletions x/logic/predicate/uri_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package predicate

import (
"fmt"
"testing"

tmdb "github.com/cometbft/cometbft-db"
"github.com/cometbft/cometbft/libs/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ichiban/prolog/engine"
"github.com/okp4/okp4d/x/logic/testutil"
"github.com/okp4/okp4d/x/logic/types"

. "github.com/smartystreets/goconvey/convey"
)

func TestURIEncoded(t *testing.T) {
Convey("Given a test cases", t, func() {
cases := []struct {
program string
query string
wantResult []types.TermResults
wantError error
wantSuccess bool
}{
{
query: `uri_encoded(hey, foo, Decoded).`,
wantSuccess: false,
wantError: fmt.Errorf("uri_encoded/3: invalid component name hey, expected `query`, `fragment`, `path` or `segment`"),
},
}
for nc, tc := range cases {
Convey(fmt.Sprintf("Given the query #%d: %s", nc, tc.query), func() {
Convey("and a context", func() {
db := tmdb.NewMemDB()
stateStore := store.NewCommitMultiStore(db)
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())

Convey("and a vm", func() {
interpreter := testutil.NewLightInterpreterMust(ctx)
interpreter.Register3(engine.NewAtom("uri_encoded"), URIEncoded)

err := interpreter.Compile(ctx, tc.program)
So(err, ShouldBeNil)

Convey("When the predicate is called", func() {
sols, err := interpreter.QueryContext(ctx, tc.query)

Convey("Then the error should be nil", func() {
So(err, ShouldBeNil)
So(sols, ShouldNotBeNil)

Convey("and the bindings should be as expected", func() {
var got []types.TermResults
for sols.Next() {
m := types.TermResults{}
err := sols.Scan(m)
So(err, ShouldBeNil)

got = append(got, m)
}
if tc.wantError != nil {
So(sols.Err(), ShouldNotBeNil)
So(sols.Err().Error(), ShouldEqual, tc.wantError.Error())
} else {
So(sols.Err(), ShouldBeNil)

if tc.wantSuccess {
So(len(got), ShouldBeGreaterThan, 0)
So(len(got), ShouldEqual, len(tc.wantResult))
for iGot, resultGot := range got {
for varGot, termGot := range resultGot {
So(testutil.ReindexUnknownVariables(termGot), ShouldEqual, tc.wantResult[iGot][varGot])
}
}
} else {
So(len(got), ShouldEqual, 0)
}
}
})
})
})
})
})
})
}
})
}

0 comments on commit 276bf77

Please sign in to comment.