Re: GET_LONG_ANSWER error return
- Posted by slowmaker Jul 27, 2013
- 1368 views
DerekParnell said...
We need to put in a LOT more test cases for this function.
Here's what I managed so far, should anyone care to make use of it. I won't be able to continue on it myself; I've been forced to admit I've just got too many other irons in the fire to make any more progress on the get()/value() related stuff I had started.
--t_get.e include std/get.e include std/unittest.e include std/filesys.e procedure dual_test_value(sequence name, object expected, object input, integer offset=1) -- Reduce a little of the pain of extensive test case typing by running -- both GET_LONG_ANSWER and GET_SHORT_ANSWER versions from a single -- call. -- so...you have to always specify the long answer in 'expected'. -- -- Automatically appends ' [LONG ANSWER]' to test names when appropriate. -- Prepends "value() " string to test name. -- Also allows offset specification, defaulted to the 'no offset' case. -- -- ASSUMPTION: the expected return for the short answer case will -- always match the first two elements of the expected return -- for the long answer case. -- -- The name parameter is the same as for test_equal(), bearing in mind -- that it will be modified for the long answer run. -- test_equal("value() " & name, expected[1..2], value(input,offset,GET_SHORT_ANSWER)) test_equal("value() " & name & " [LONG ANSWER]", expected, value(input,offset,GET_LONG_ANSWER)) end procedure dual_test_value("no data supplied", {GET_EOF, 0, 0, 0}, "") dual_test_value("integer", {GET_SUCCESS, 10, 2, 0}, "10") dual_test_value("integer terminated by non-integer, whitespace", {GET_SUCCESS, 10, 3, 1}, " 10john") dual_test_value("bad integer", {GET_FAIL, 0, 0, 0}, "John") dual_test_value("integer, from offset", {GET_SUCCESS, 10, 2, 0}, "Data: 10", 7) dual_test_value("integer, underscores", {GET_SUCCESS, 44444, 8, 0}, "4__444_4") dual_test_value("integer, from offset, leading whitespace", {GET_SUCCESS, 10, 3, 1}, "Data: 10", 6) dual_test_value("hex integer, # notation", {GET_SUCCESS, 241, 3, 0}, "#F1") dual_test_value("hex integer, # notation, lower case", {GET_SUCCESS, 241, 3, 0}, "#f1") dual_test_value("hex integer, 0x notation", {GET_SUCCESS, 241, 4, 0}, "0xF1") dual_test_value("hex integer, 0x notation, mixed case", {GET_SUCCESS, 254, 4, 0}, "0xFe") dual_test_value("hex integer, 0X notation (capital X)", {GET_SUCCESS, 254, 4, 0}, "0Xfe") dual_test_value("binary integer, 0b notation", {GET_SUCCESS, 15, 6, 0}, "0b1111") dual_test_value("binary integer, 0B notation (capital B)", {GET_SUCCESS, 15, 6, 0}, "0B1111") dual_test_value("float", {GET_SUCCESS, 10.5, 4, 0}, "10.5") dual_test_value("float, whitespace", {GET_SUCCESS, 10.5, 5, 1}, " 10.5 ") dual_test_value("sequence", {GET_SUCCESS, {1,2}, 5, 0}, "{1,2}") dual_test_value("sequence terminator", {GET_SUCCESS, {4}, 6, 0}, "{4, $}") dual_test_value("float, underscores", {GET_SUCCESS, 6.6, 5, 0}, "6_._6") dual_test_value("scientific notation #1", {GET_SUCCESS, 3.14e2, 6, 0}, "3.14e2") dual_test_value("scientific notation #2", {GET_SUCCESS, 3.14e-12, 8, 0}, "3.14E-12") dual_test_value("scientific notation #3", {GET_SUCCESS, 3.14e+12, 9, 0}, "+3.14e+12") dual_test_value("scientific notation #4 no exponent", {GET_FAIL, 0, 6, 0}, "+3.14e") dual_test_value("no hex digits", {GET_FAIL, 0, 1, 0}, "#") dual_test_value("decimal point only", {GET_FAIL, 0, 1, 0}, ".") dual_test_value("mixed object", {GET_SUCCESS, { 1, 2, 3, "foo", {-9},{}}, 26, 0}, "{ 1, 2, 3, \"foo\", {-9},{}}") dual_test_value("quoted text", {GET_SUCCESS, "some text", 11, 0},"\"some text\"") dual_test_value("single-quoted character", {GET_SUCCESS, 'f', 3, 0}, "'f'") dual_test_value("escaped binary", {GET_SUCCESS, 170, 10, 0}, "\"\b10101010\"") dual_test_value("escaped binary, terminated by non-binary", {GET_SUCCESS, 240, 10, 0}, "\"\b11110000abcdefg\"") test_equal("defaulted_value() #1", 0, defaulted_value("abc", 0)) test_equal("defaulted_value() #2", 10, defaulted_value("10", 0)) test_equal("defaulted_value() #3", 10.5, defaulted_value("10.5", 0)) test_equal("defaulted_value() #4", 0, defaulted_value(10.5, 0)) test_equal("defaulted_value() #5", {1,2,3}, defaulted_value("123={1,2,3}", 0, 5)) test_equal("defaulted_value() #6", 0, defaulted_value("123={1,2,3}", 0, 4)) constant OBJECT = { 1, 2, 3, "foo", {-9},{}}, TEXT = "some text", CHAR = 'f' integer fn = open( "get.txt", "w" ) print(fn, OBJECT ) printf(fn, "\n\"%s\"\n'%s'\n", { TEXT, CHAR } ) close(fn) fn = open( "get.txt", "r" ) test_equal( "print / get", { GET_SUCCESS, OBJECT}, get( fn ) ) test_equal( "print / get", { GET_SUCCESS, TEXT}, get( fn ) ) test_equal( "print / get", { GET_SUCCESS, CHAR}, get( fn ) ) test_equal( "print / get", { GET_EOF, 0}, get( fn ) ) close(fn) delete_file( "get.txt" ) test_report()