package parse

import (
	"testing"
)

func TestAccept(t *testing.T) {
	// Accept(0) will always match :)
	p0 := Accept(0)
	if r, s := p0(""); !r.Matched || r.Content != "" || r.Nodes != nil || s != "" {
		t.Error("Invalid result for Accept's negative test", r)
	}
	// Test with a non-zero amount of bytes
	p := Accept(1)
	if r, s := p(""); r.Matched || r.Content != "" || r.Nodes != nil || s != "" {
		t.Error("Invalid result for Accept's negative test", r)
	}
	if r, s := p("a"); !r.Matched || r.Content != "a" || r.Nodes != nil || s != "" {
		t.Error("Invalid result for Accept's positive test", r)
	}
}

func TestK(t *testing.T) {
	p := Accept(1)
	k := K(p)
	if r, s := k("aaa"); !r.Matched || r.Content != "aaa" || r.Nodes == nil || s != "" {
		t.Error("Invalid result for K* match test", r)
	}
	if r, s := k(""); !r.Matched || r.Content != "" || r.Nodes != nil || s != "" {
		t.Error("Invalid result for K* no-match test", r)
	}
}

func TestSeq(t *testing.T) {
	p1, p2 := Accept(1), Accept(1)
	s := Seq(p1, p2)
	if r, s := s("aa"); !r.Matched || r.Content != "aa" || r.Nodes == nil || s != "" {
		t.Error("Invalid result for Seq positive test", r)
	}
	if r, s := s("a"); r.Matched || r.Content != "a" || r.Nodes == nil || s != "" {
		t.Error("Invalid result for Seq partial match test", r)
	}
	if r, s := s(""); r.Matched || r.Content != "" || r.Nodes == nil || s != "" {
		t.Error("Invalid result for Seq no-match test", r)
	}
}

func TestAny(t *testing.T) {
	p, p0 := Accept(1), Accept(0)
	a := Any(p, p0)
	if r, s := a(""); !r.Matched || r.Content != "" || r.Nodes != nil || s != "" {
		t.Error("Invalid result for Any match test", r)
	}
	if r, s := a("aa"); !r.Matched || r.Content != "a" || r.Nodes != nil || s != "a" {
		t.Error("Invalid result for Any match test", r)
	}
}
