Beispiel #1
0
/*
	Test qsep which should return null tokens when multiple separaters exist (e.g. foo,,,bar)
*/
func TestToken_qsep(t *testing.T) {
	str := `hello "world this is" a test where token 2 was quoted`
	expect := "world this is"

	ntokens, tokens := token.Tokenise_qsep(str, " ")
	if ntokens != 9 {
		fmt.Fprintf(os.Stderr, "FAIL: bad number of tokens, expected 9 got %d from (%s)\n", ntokens, str)
		t.Fail()
	}

	if strings.Index(tokens[1], `"`) >= 0 {
		fmt.Fprintf(os.Stderr, "FAIL: token 2 contained quotes (%s) in %s\n", tokens[1], str)
		t.Fail()
	}

	if tokens[1] != expect {
		fmt.Fprintf(os.Stderr, "FAIL: token 2 expected to be 'world this is' but was '%s'\n", tokens[1])
		t.Fail()
	}
	fmt.Fprintf(os.Stderr, "expected: '%s' found: '%s'   [OK]\n", expect, tokens[1])

	// ------------------------------------------------------------------------------------------------------
	str = `hello "world"` // specific test on 2014/01/13 bug fix
	_, tokens = token.Tokenise_qsep(str, " ")
	if strings.Index(tokens[1], `"`) >= 0 {
		fmt.Fprintf(os.Stderr, "FAIL: qsep test token 2 contained quotes (%s) in (%s)\n", tokens[1], str)
		t.Fail()
	} else {
		fmt.Fprintf(os.Stderr, "expected 'world' found '%s'   [OK]\n", tokens[1])
	}

}
Beispiel #2
0
/*
	Helper function for next real test
*/
func qsep_t2(t *testing.T, str string, expect string, sep string, check_quotes bool) {
	fmt.Fprintf(os.Stderr, "\nqsep testing with: (%s)\n", str)
	ntokens, tokens := token.Tokenise_qsep(str, ",")

	if ntokens != 4 {
		fmt.Fprintf(os.Stderr, "FAIL: qsep test expected 4 tokens, two nil, received %d tokens\n", ntokens)
		t.Fail()
	} else {
		fmt.Fprintf(os.Stderr, "expected 4 tokens, received %d   [OK]\n", ntokens)
	}
	for i := range tokens {
		fmt.Fprintf(os.Stderr, "\ttoken[%d] (%s)\n", i, tokens[i])
	}

	if ntokens >= 4 {
		if tokens[1] != "" {
			fmt.Fprintf(os.Stderr, "FAIL: qsep test expected token 2 to be empty: (%s)\n", tokens[1])
			t.Fail()
		}
		fmt.Fprintf(os.Stderr, "token 2 was nil/empty as expected [OK]\n")

		if tokens[2] != "" {
			fmt.Fprintf(os.Stderr, "FAIL: qsep test expected token 3 to be empty: (%s)\n", tokens[2])
			t.Fail()
		}
		fmt.Fprintf(os.Stderr, "token 3 was nil/empty as expected [OK]\n")

		if check_quotes {
			if strings.Index(tokens[3], `"`) >= 0 {
				fmt.Fprintf(os.Stderr, "FAIL: qsep test token 4 contained quotes (%s) in (%s)\n", tokens[3], str)
				t.Fail()
			} else {
				if tokens[3] == expect {
					fmt.Fprintf(os.Stderr, "expected '%s' found '%s'   [OK]\n", expect, tokens[3])
				} else {
					fmt.Fprintf(os.Stderr, "FAIL: expected '%s' found '%s'   [OK]\n", expect, tokens[3])
				}
			}
		}
	}
}