Exemplo n.º 1
0
// Parse parser operands.
func (operands *Number) Parse(source string) (string, bool) {
	maybeDecimal := operand.NewDecimal()
	remains, ok := maybeDecimal.Parse(source)
	if ok {
		operands.Operands = []operand.Operand{maybeDecimal}
		return remains, true
	}

	maybeHex := operand.NewHex()
	remains, ok = maybeHex.Parse(remains)
	if ok {
		operands.Operands = []operand.Operand{maybeHex}
		return remains, true
	}
	return source, false
}
Exemplo n.º 2
0
func TestOperandHexParse(t *testing.T) {
	testCaseList := []testCaseForTestOperandHexParse{
		// This is a comment line.
		{
			data:                       "; This is a comment",
			expectedRemains:            "; This is a comment",
			expectedOK:                 false,
			expectedOperandSingleValue: "",
			expectedHasValue:           false,
		},
		// This is a OperandHex and a OperandHex.
		{
			data: "MAIN			START",
			expectedRemains: "MAIN			START",
			expectedOK:                 false,
			expectedOperandSingleValue: "",
			expectedHasValue:           false,
		},
		// A OperandHex must be starts without any sapce.
		{
			data:                       "         OUT    BUF,LEN ",
			expectedRemains:            "         OUT    BUF,LEN ",
			expectedOK:                 false,
			expectedOperandSingleValue: "",
			expectedHasValue:           false,
		},
		{
			data:                       "#ABCD, DATA1",
			expectedRemains:            ", DATA1",
			expectedOK:                 true,
			expectedOperandSingleValue: "#ABCD",
			expectedHasValue:           true,
		},
		{
			data: "		#10FE",
			expectedRemains:            "",
			expectedOK:                 true,
			expectedOperandSingleValue: "#10FE",
			expectedHasValue:           true,
		},
		{
			data:                       "#999 ; this is comment",
			expectedRemains:            "; this is comment",
			expectedOK:                 true,
			expectedOperandSingleValue: "#999",
			expectedHasValue:           true,
		},
	}
	for testIndex, testCase := range testCaseList {
		target := operand.NewHex()
		testData := testCase.data
		expectedRemains := testCase.expectedRemains
		expectedOK := testCase.expectedOK
		expectedOperandSingleValue := testCase.expectedOperandSingleValue
		expectedHasValue := testCase.expectedHasValue

		actualRemains, actualOK := target.Parse(testData)
		if actualRemains != expectedRemains {
			t.Errorf("testCase[%d]=%#v, expected=%#v, actual=%#v\n", testIndex, testData, expectedRemains, actualRemains)
		}
		if actualOK != expectedOK {
			t.Errorf("testCase[%d]=%#v, expected=%#v, actual=%#v\n", testIndex, testData, expectedOK, actualOK)
		}
		actualOperandSingleValue := target.SingleValue
		if actualOperandSingleValue != expectedOperandSingleValue {
			t.Errorf("testCase[%d]=%#v, expected=%#v, actual=%#v\n", testIndex, testData, expectedOperandSingleValue, actualOperandSingleValue)
		}
		actualHasValue := target.HasValue
		if actualHasValue != expectedHasValue {
			t.Errorf("testCase[%d]=%#v, expected=%#v, actual=%#v\n", testIndex, testData, expectedHasValue, actualHasValue)
		}
	}

}