From faa0caae084363f6809341ddb63f75d6c682b175 Mon Sep 17 00:00:00 2001 From: herrBez Date: Sat, 15 Sep 2018 13:03:47 +0200 Subject: [PATCH] Add syntax tests to augment the test coverage of libsolidity/parsing/Scanner.cpp and libsolidity/parsing/Scanner.h Fix #4627 and PR #5003. - Add multiline comment test - Add upper case hex literal test - Add test for unicode escapes - Add test for strings with escaped newlines - Add test for string escapes - Add test for strings that do not terminate before end of file - Add test for unterminated blocks --- .../syntaxTests/multiline_comments.sol | 13 ++++++++ .../syntaxTests/string/string_escapes.sol | 7 +++++ .../syntaxTests/string/string_new_line.sol | 9 ++++++ .../string/string_terminated_by_backslash.sol | 8 +++++ .../string/string_unterminated.sol | 7 +++++ .../string_unterminated_no_new_line.sol | 4 +++ .../syntaxTests/unicode_escape_literals.sol | 31 +++++++++++++++++++ .../unterminatedBlocks/one_dot.sol | 4 +++ .../unterminatedBlocks/one_dot_x.sol | 5 +++ .../unterminatedBlocks/zero_dot.sol | 4 +++ .../unterminatedBlocks/zero_dot_x.sol | 5 +++ .../syntaxTests/upper_case_hex_literals.sol | 9 ++++++ 12 files changed, 106 insertions(+) create mode 100644 test/libsolidity/syntaxTests/multiline_comments.sol create mode 100644 test/libsolidity/syntaxTests/string/string_escapes.sol create mode 100644 test/libsolidity/syntaxTests/string/string_new_line.sol create mode 100644 test/libsolidity/syntaxTests/string/string_terminated_by_backslash.sol create mode 100644 test/libsolidity/syntaxTests/string/string_unterminated.sol create mode 100644 test/libsolidity/syntaxTests/string/string_unterminated_no_new_line.sol create mode 100644 test/libsolidity/syntaxTests/unicode_escape_literals.sol create mode 100644 test/libsolidity/syntaxTests/unterminatedBlocks/one_dot.sol create mode 100644 test/libsolidity/syntaxTests/unterminatedBlocks/one_dot_x.sol create mode 100644 test/libsolidity/syntaxTests/unterminatedBlocks/zero_dot.sol create mode 100644 test/libsolidity/syntaxTests/unterminatedBlocks/zero_dot_x.sol create mode 100644 test/libsolidity/syntaxTests/upper_case_hex_literals.sol diff --git a/test/libsolidity/syntaxTests/multiline_comments.sol b/test/libsolidity/syntaxTests/multiline_comments.sol new file mode 100644 index 000000000..480fde6c3 --- /dev/null +++ b/test/libsolidity/syntaxTests/multiline_comments.sol @@ -0,0 +1,13 @@ +/* + * This is a multi-line comment + * it should create no problems + * +*/ + +contract test { + /* + * this is another multi-line comment + * + */ +} +// ---- diff --git a/test/libsolidity/syntaxTests/string/string_escapes.sol b/test/libsolidity/syntaxTests/string/string_escapes.sol new file mode 100644 index 000000000..51b90d735 --- /dev/null +++ b/test/libsolidity/syntaxTests/string/string_escapes.sol @@ -0,0 +1,7 @@ +contract test { + function f() public pure returns (bytes32) { + bytes32 escapeCharacters = "\t\b\n\r\f\'\"\\\b"; + return escapeCharacters; + } +} +// ---- diff --git a/test/libsolidity/syntaxTests/string/string_new_line.sol b/test/libsolidity/syntaxTests/string/string_new_line.sol new file mode 100644 index 000000000..da2240f78 --- /dev/null +++ b/test/libsolidity/syntaxTests/string/string_new_line.sol @@ -0,0 +1,9 @@ +contract test { + function f() public pure returns (bytes32) { + bytes32 escapeCharacters = "This a test + "; + return escapeCharacters; + } +} +// ---- +// ParserError: (100-112): Expected primary expression. diff --git a/test/libsolidity/syntaxTests/string/string_terminated_by_backslash.sol b/test/libsolidity/syntaxTests/string/string_terminated_by_backslash.sol new file mode 100644 index 000000000..3eaba6af0 --- /dev/null +++ b/test/libsolidity/syntaxTests/string/string_terminated_by_backslash.sol @@ -0,0 +1,8 @@ +contract test { + function f() public pure returns (bytes32) { + bytes32 escapeCharacters = "text \"; + return escapeCharacters; + } +} +// ---- +// ParserError: (100-109): Expected primary expression. \ No newline at end of file diff --git a/test/libsolidity/syntaxTests/string/string_unterminated.sol b/test/libsolidity/syntaxTests/string/string_unterminated.sol new file mode 100644 index 000000000..3291781e0 --- /dev/null +++ b/test/libsolidity/syntaxTests/string/string_unterminated.sol @@ -0,0 +1,7 @@ +contract test { + function f() public pure returns (bytes32) { + bytes32 escapeCharacters = "This a test + } +} +// ---- +// ParserError: (100-112): Expected primary expression. \ No newline at end of file diff --git a/test/libsolidity/syntaxTests/string/string_unterminated_no_new_line.sol b/test/libsolidity/syntaxTests/string/string_unterminated_no_new_line.sol new file mode 100644 index 000000000..e7be50d23 --- /dev/null +++ b/test/libsolidity/syntaxTests/string/string_unterminated_no_new_line.sol @@ -0,0 +1,4 @@ +contract test { + function f() pure public { "abc\ +// ---- +// ParserError: (47-53): Expected primary expression. \ No newline at end of file diff --git a/test/libsolidity/syntaxTests/unicode_escape_literals.sol b/test/libsolidity/syntaxTests/unicode_escape_literals.sol new file mode 100644 index 000000000..a340487b1 --- /dev/null +++ b/test/libsolidity/syntaxTests/unicode_escape_literals.sol @@ -0,0 +1,31 @@ +contract test { + + function oneByteUTF8() public pure returns (bytes32) { + bytes32 usdollar = "aaa\u0024aaa"; + return usdollar; + } + + function twoBytesUTF8() public pure returns (bytes32) { + bytes32 cent = "aaa\u00A2aaa"; + return cent; + } + + function threeBytesUTF8() public pure returns (bytes32) { + bytes32 eur = "aaa\u20ACaaa"; + return eur; + } + + function together() public pure returns (bytes32) { + bytes32 res = "\u0024\u00A2\u20AC"; + return res; + } + + // this function returns an invalid unicode character + function invalidLiteral() public pure returns(bytes32) { + bytes32 invalid = "\u00xx"; + return invalid; + } + +} +// ---- +// ParserError: (678-681): Expected primary expression. diff --git a/test/libsolidity/syntaxTests/unterminatedBlocks/one_dot.sol b/test/libsolidity/syntaxTests/unterminatedBlocks/one_dot.sol new file mode 100644 index 000000000..a678f0042 --- /dev/null +++ b/test/libsolidity/syntaxTests/unterminatedBlocks/one_dot.sol @@ -0,0 +1,4 @@ +contract c { + function f() pure public { 1. +// ---- +// ParserError: (47-47): Expected identifier but got end of source \ No newline at end of file diff --git a/test/libsolidity/syntaxTests/unterminatedBlocks/one_dot_x.sol b/test/libsolidity/syntaxTests/unterminatedBlocks/one_dot_x.sol new file mode 100644 index 000000000..3cc59374c --- /dev/null +++ b/test/libsolidity/syntaxTests/unterminatedBlocks/one_dot_x.sol @@ -0,0 +1,5 @@ +contract test { + function f() pure public { 1.x; } +} +// ---- +// TypeError: (47-50): Member "x" not found or not visible after argument-dependent lookup in int_const 1. \ No newline at end of file diff --git a/test/libsolidity/syntaxTests/unterminatedBlocks/zero_dot.sol b/test/libsolidity/syntaxTests/unterminatedBlocks/zero_dot.sol new file mode 100644 index 000000000..6ba2b4c22 --- /dev/null +++ b/test/libsolidity/syntaxTests/unterminatedBlocks/zero_dot.sol @@ -0,0 +1,4 @@ +contract c { + function f() pure public { 0. +// ---- +// ParserError: (47-47): Expected identifier but got end of source \ No newline at end of file diff --git a/test/libsolidity/syntaxTests/unterminatedBlocks/zero_dot_x.sol b/test/libsolidity/syntaxTests/unterminatedBlocks/zero_dot_x.sol new file mode 100644 index 000000000..8648bce24 --- /dev/null +++ b/test/libsolidity/syntaxTests/unterminatedBlocks/zero_dot_x.sol @@ -0,0 +1,5 @@ +contract test { + function f() pure public { 0.x; } +} +// ---- +// TypeError: (47-50): Member "x" not found or not visible after argument-dependent lookup in int_const 0. \ No newline at end of file diff --git a/test/libsolidity/syntaxTests/upper_case_hex_literals.sol b/test/libsolidity/syntaxTests/upper_case_hex_literals.sol new file mode 100644 index 000000000..0842c2ec1 --- /dev/null +++ b/test/libsolidity/syntaxTests/upper_case_hex_literals.sol @@ -0,0 +1,9 @@ +contract test { + + function f() public pure returns (uint256) { + uint256 a = 0x1234aAbcC; + uint256 b = 0x1234ABCDEF; + return a + b; + } +} +// ----