Example #1
0
func (m *machine) Consult(text interface{}) Machine {
	terms := read.TermAll_(text)
	m1 := m.clone()
	for _, t := range terms {
		if IsDirective(t) {
			// ignore all directives, for now
			continue
		}
		m1.db = m1.db.Assertz(t)
	}
	return m1
}
Example #2
0
func TestPureProlog(t *testing.T) {
	// find all t/*.pl files
	file, err := os.Open("t")
	MaybePanic(err)
	names, err := file.Readdirnames(-1)

	useModule := read.Term_(`:- use_module(library(tap)).`)
	env := term.NewBindings()

	// run tests found in each file
	for _, name := range names {
		if name[0] == '.' {
			continue // skip hidden files
		}
		//t.Logf("-------------- %s", name)
		openTest := func() *os.File {
			f, err := os.Open("t/" + name)
			MaybePanic(err)
			return f
		}

		// which tests does the file have?
		pastUseModule := false
		tests := make([]term.Term, 0)
		terms := read.TermAll_(openTest())
		for _, s := range terms {
			x := s.(term.Callable)
			if pastUseModule {
				if x.Arity() == 2 && x.Name() == ":-" {
					tests = append(tests, x.Arguments()[0])
				} else {
					tests = append(tests, x)
				}
			} else {
				// look for use_module(library(tap)) declaration
				_, err := s.Unify(env, useModule)
				if err == nil {
					pastUseModule = true
					//t.Logf("found use_module directive")
				}
			}
		}

		// run each test in this file
		m := NewMachine().Consult(openTest())
		for _, test := range tests {
			x := test.(term.Callable)
			//t.Logf("proving: %s", test)
			canProve := m.CanProve(test)
			if x.Arity() > 0 && x.Arguments()[0].String() == "fail" {
				if canProve {
					t.Errorf("%s: %s should fail", name, test)
				}
			} else {
				if !canProve {
					t.Errorf("%s: %s failed", name, test)
				}
			}
		}
	}
}