diff --git a/builtins.go b/builtins.go index d7267989..3930604e 100644 --- a/builtins.go +++ b/builtins.go @@ -704,6 +704,18 @@ func builtinNegation(i *interpreter, x value) (value, error) { return makeValueBoolean(!b.value), nil } +func builtinXnor(i *interpreter, xv, yv value) (value, error) { + p, err := i.getBoolean(xv) + if err != nil { + return nil, err + } + q, err := i.getBoolean(yv) + if err != nil { + return nil, err + } + return makeValueBoolean(p.value == q.value), nil +} + func builtinXor(i *interpreter, xv, yv value) (value, error) { p, err := i.getBoolean(xv) if err != nil { @@ -2198,6 +2210,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{ &binaryBuiltin{name: "pow", function: builtinPow, params: ast.Identifiers{"x", "n"}}, &binaryBuiltin{name: "modulo", function: builtinModulo, params: ast.Identifiers{"x", "y"}}, &unaryBuiltin{name: "md5", function: builtinMd5, params: ast.Identifiers{"s"}}, + &binaryBuiltin{name: "xnor", function: builtinXnor, params: ast.Identifiers{"x", "y"}}, &binaryBuiltin{name: "xor", function: builtinXor, params: ast.Identifiers{"x", "y"}}, &binaryBuiltin{name: "lstripChars", function: builtinLstripChars, params: ast.Identifiers{"str", "chars"}}, &binaryBuiltin{name: "rstripChars", function: builtinRstripChars, params: ast.Identifiers{"str", "chars"}}, diff --git a/internal/testutils/test_utils.go b/internal/testutils/test_utils.go index af67294c..cfa558f1 100644 --- a/internal/testutils/test_utils.go +++ b/internal/testutils/test_utils.go @@ -5,12 +5,27 @@ import ( "bytes" "io/ioutil" "os" - + "bufio" + "fmt" "github.com/sergi/go-diff/diffmatchpatch" ) +func check(err error) { + if err != nil { + panic(err) + } +} // Diff produces a pretty diff of two files func Diff(a, b string) string { + f, err := os.Create("/Users/salonijuneja/yourfile11.txt") + check(err) + defer f.Close() + + w := bufio.NewWriter(f) + + _, err = fmt.Fprintf(w, "%v\n", a) + check(err) + w.Flush() dmp := diffmatchpatch.New() diffs := dmp.DiffMain(a, b, false) return dmp.DiffPrettyText(diffs) diff --git a/linter/internal/types/stdlib.go b/linter/internal/types/stdlib.go index 5083fa9a..5984b43f 100644 --- a/linter/internal/types/stdlib.go +++ b/linter/internal/types/stdlib.go @@ -175,6 +175,7 @@ func prepareStdlib(g *typeGraph) { // Boolean "xor": g.newSimpleFuncType(boolType, "x", "y"), + "xnor": g.newSimpleFuncType(boolType, "x", "y"), } fieldContains := map[string][]placeholderID{} diff --git a/testdata/builtinXnor.golden b/testdata/builtinXnor.golden new file mode 100644 index 00000000..27ba77dd --- /dev/null +++ b/testdata/builtinXnor.golden @@ -0,0 +1 @@ +true diff --git a/testdata/builtinXnor.jsonnet b/testdata/builtinXnor.jsonnet new file mode 100644 index 00000000..69784e04 --- /dev/null +++ b/testdata/builtinXnor.jsonnet @@ -0,0 +1 @@ +std.xnor(false, false) diff --git a/testdata/builtinXnor.linter.golden b/testdata/builtinXnor.linter.golden new file mode 100644 index 00000000..e69de29b diff --git a/testdata/builtinXnor1.golden b/testdata/builtinXnor1.golden new file mode 100644 index 00000000..c508d536 --- /dev/null +++ b/testdata/builtinXnor1.golden @@ -0,0 +1 @@ +false diff --git a/testdata/builtinXnor1.jsonnet b/testdata/builtinXnor1.jsonnet new file mode 100644 index 00000000..2fa8ff3d --- /dev/null +++ b/testdata/builtinXnor1.jsonnet @@ -0,0 +1 @@ +std.xnor(true, false) diff --git a/testdata/builtinXnor1.linter.golden b/testdata/builtinXnor1.linter.golden new file mode 100644 index 00000000..e69de29b diff --git a/testdata/builtinXnor2.golden b/testdata/builtinXnor2.golden new file mode 100644 index 00000000..4480ac18 --- /dev/null +++ b/testdata/builtinXnor2.golden @@ -0,0 +1,10 @@ +RUNTIME ERROR: Unexpected type string, expected boolean +------------------------------------------------- + testdata/builtinXnor2:1:1-24 $ + +std.xnor("true", false) + +------------------------------------------------- + During evaluation + + diff --git a/testdata/builtinXnor2.jsonnet b/testdata/builtinXnor2.jsonnet new file mode 100644 index 00000000..bb4abf52 --- /dev/null +++ b/testdata/builtinXnor2.jsonnet @@ -0,0 +1 @@ +std.xnor("true", false) diff --git a/testdata/builtinXnor2.linter.golden b/testdata/builtinXnor2.linter.golden new file mode 100644 index 00000000..e69de29b