File: raw_test.go

package info (click to toggle)
golang-github-cloudflare-redoctober 0.0~git20161017.0.78e9720-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 584 kB
  • ctags: 560
  • sloc: sh: 65; makefile: 7
file content (82 lines) | stat: -rw-r--r-- 1,824 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package msp

import (
	"testing"
)

func TestRaw(t *testing.T) {
	alice := Condition(Name{"Alice", 0})
	bob := Condition(Name{"Bob", 0})
	carl := Condition(Name{"Carl", 0})

	query1 := Raw{
		NodeType: NodeAnd,
		Left:     alice,
		Right:    bob,
	}

	aliceOrBob := Condition(Raw{
		NodeType: NodeOr,
		Left:     alice,
		Right:    bob,
	})

	query2 := Raw{
		NodeType: NodeAnd,
		Left:     aliceOrBob,
		Right:    carl,
	}

	db := &Database{
		"Alice": [][]byte{[]byte("blah")},
		"Carl":  [][]byte{[]byte("herp")},
	}

	if query1.Ok(db) != false {
		t.Fatalf("Query #1 was wrong.")
	}

	if query2.Ok(db) != true {
		t.Fatalf("Query #2 was wrong.")
	}

	query1String := "Alice & Bob"
	query2String := "(Alice | Bob) & Carl"

	if query1.String() != query1String {
		t.Fatalf("Query #1 String was wrong; %v", query1.String())
	}

	if query2.String() != query2String {
		t.Fatalf("Query #2 String was wrong; %v", query2.String())
	}

	decQuery1, err := StringToRaw(query1String)
	if err != nil || decQuery1.String() != query1String {
		t.Fatalf("Query #1 decoded wrong: %v %v", decQuery1.String(), err)
	}

	decQuery2, err := StringToRaw(query2String)
	if err != nil || decQuery2.String() != query2String {
		t.Fatalf("Query #2 decoded wrong: %v %v", decQuery2.String(), err)
	}

	formattedQuery1String := "(2, Alice, Bob)"
	formattedQuery2String := "(2, (1, Alice, Bob), Carl)"

	if query1.Formatted().String() != formattedQuery1String {
		t.Fatalf("Query #1 formatted wrong: %v", query1.Formatted().String())
	}

	if query2.Formatted().String() != formattedQuery2String {
		t.Fatalf("Query #2 formatted wrong: %v", query2.Formatted().String())
	}
}

func TestOneCondition(t *testing.T) {
	_, err := StringToRaw("(Alice or Bob)")

	if err == nil {
		t.Fatalf("A predicate with only one condition should fail to parse!")
	}
}