Skip to content

Commit

Permalink
Adds start of custom rules code
Browse files Browse the repository at this point in the history
  • Loading branch information
tomnomnom committed Sep 16, 2018
1 parent 50e8856 commit a62c6c9
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
44 changes: 44 additions & 0 deletions rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import "strings"

type evaluator interface {
Eval(content string) bool
}
type sexp struct {
op string
args []evaluator
}

func (s sexp) Eval(content string) bool {
switch s.op {
case "not":
if len(s.args) == 0 {
return true
}
return !s.args[0].Eval(content)

case "and":
for _, a := range s.args {
if !a.Eval(content) {
return false
}
}
return true

case "or":
for _, a := range s.args {
if a.Eval(content) {
return true
}
}
return false
}
return false
}

type matcher string

func (m matcher) Eval(content string) bool {
return strings.Contains(content, string(m))
}
38 changes: 38 additions & 0 deletions rules_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import "testing"

func TestSexp(t *testing.T) {

sexp := sexp{
op: "and",
args: []evaluator{
sexp{op: "or", args: []evaluator{matcher("too"), matcher("boo")}},
sexp{op: "not", args: []evaluator{matcher("crumble")}},
},
}

if sexp.Eval("footle boolte") != true {
t.Errorf("expected true, have false")
}

if sexp.Eval("boo crumble") != false {
t.Errorf("expected false, have true")
}
}

func TestSexpNot(t *testing.T) {

sexp := sexp{
op: "not",
args: []evaluator{matcher("footle")},
}

if sexp.Eval("boolte tootle") != true {
t.Errorf("expected true, have false")
}

if sexp.Eval("footle mcdootle") != false {
t.Errorf("expected false, have true")
}
}

0 comments on commit a62c6c9

Please sign in to comment.